Repository navigation
Expand file tree
/
Copy pathPVGKernelCompat.cpp
More file actions
208 lines (176 loc) · 7.51 KB
/
Copy pathPVGKernelCompat.cpp
File metadata and controls
208 lines (176 loc) · 7.51 KB
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
#include <IOKit/IOLib.h>
#include <IOKit/IOMemoryDescriptor.h>
#include <IOKit/IORegistryEntry.h>
#include <libkern/c++/OSMetaClass.h>
#include <libkern/c++/OSObject.h>
#include <mach/vm_param.h>
#include <stdint.h>
#if PVG_HIGH_SIERRA
#include "PVGHighSierraKernel.inc"
#endif
#if PVG_MOJAVE
#include "PVGMojaveKernel.inc"
#endif
// This kext supplies the small set of kernel interfaces imported by the
// macOS 11.7 IOSurface/IOGraphics/PVG binaries but absent from Catalina
// 19H15. Every adapter below either delegates to Catalina's predecessor API
// or implements the corresponding xnu-7195 behavior using Catalina's public
// IOKit ABI. It intentionally has no dependency on IOSurface, which lets the
// donor IOSurface depend on this bundle without a circular dependency.
namespace {
constexpr IOOptionBits kIOMemorySourceSegment = 0x00002000;
} // namespace
// AppleParavirtGPU imports this class-specific allocator. Big Sur resolves it
// as the ordinary OSObject allocator; Catalina has no exported alias.
class IORangeAllocator {
public:
static void *operator new(size_t size);
};
void *IORangeAllocator::operator new(size_t size) {
return OSObject::operator new(size);
}
// IOGraphicsFamily 585.2 imports the IOService-specific spelling introduced
// after Catalina. Catalina's OSObject allocator is the correct paired
// allocator for this binary (its delete import is OSObject::operator delete).
extern "C" __attribute__((used, noinline, visibility("default")))
void *IOServiceOperatorNew(size_t size)
__asm__("__ZN9IOServicenwEm");
extern "C" __attribute__((used, noinline, visibility("default")))
void *IOServiceOperatorNew(size_t size) {
return OSObject::operator new(size);
}
// Big Sur's zoned metaclass constructor delegates to the pre-zone constructor
// before optionally creating a typed zone. Catalina has the delegated
// constructor but not typed IOKit heaps. Leave the caller's zone null: the
// generated Big Sur operator new/delete paths then deliberately use the
// Catalina OSObject allocator pair.
extern "C" void CatalinaOSMetaClassConstructor(
OSMetaClass *self,
const char *className,
const OSMetaClass *superClass,
unsigned int classSize)
__asm__("__ZN11OSMetaClassC2EPKcPKS_j");
extern "C" __attribute__((used, noinline, visibility("default")))
void BigSurOSMetaClassConstructor(
OSMetaClass *self,
const char *className,
const OSMetaClass *superClass,
unsigned int classSize,
void **classZone,
const char *zoneName,
uint64_t zoneFlags)
__asm__("__ZN11OSMetaClassC2EPKcPKS_jPP4zoneS1_19zone_create_flags_t");
extern "C" __attribute__((used, noinline, visibility("default")))
void BigSurOSMetaClassConstructor(
OSMetaClass *self,
const char *className,
const OSMetaClass *superClass,
unsigned int classSize,
void **classZone,
const char *zoneName,
uint64_t zoneFlags) {
(void)zoneName;
(void)zoneFlags;
if (classZone) {
*classZone = nullptr;
}
CatalinaOSMetaClassConstructor(self, className, superClass, classSize);
}
// Catalina predates the convenience method but exposes the same virtual
// physical-segment API. This is the xnu-7195 fallback implementation for
// descriptors without the later internal memory-reference representation.
extern "C" __attribute__((used, noinline, visibility("default")))
uint64_t BigSurGetDMAMapLength(
IOMemoryDescriptor *descriptor,
uint64_t *firstPageOffset)
__asm__("__ZN18IOMemoryDescriptor15getDMAMapLengthEPy");
extern "C" __attribute__((used, noinline, visibility("default")))
uint64_t BigSurGetDMAMapLength(
IOMemoryDescriptor *descriptor,
uint64_t *firstPageOffset) {
uint64_t totalLength = 0;
IOByteCount iterate = 0;
if (firstPageOffset) {
*firstPageOffset = 0;
}
while (iterate < descriptor->getLength()) {
IOByteCount segmentLength = 0;
const addr64_t sourceAddress = descriptor->getPhysicalSegment(
iterate, &segmentLength, kIOMemorySourceSegment);
if (!sourceAddress || !segmentLength) {
break;
}
if (firstPageOffset && iterate == 0) {
*firstPageOffset = sourceAddress & PAGE_MASK_64;
}
const uint64_t pageStart = sourceAddress & ~PAGE_MASK_64;
const uint64_t pageEnd =
(sourceAddress + segmentLength + PAGE_MASK_64) & ~PAGE_MASK_64;
totalLength += pageEnd - pageStart;
iterate += segmentLength;
}
return totalLength;
}
// In xnu-7195 descriptor IDs exist only to correlate kernel trace records.
// Catalina has no reserved field in which to cache the new counter, so a
// stable non-zero per-object value preserves the functional contract without
// touching the incompatible private object layout.
extern "C" __attribute__((used, noinline, visibility("default")))
uint64_t BigSurGetDescriptorID(IOMemoryDescriptor *descriptor)
__asm__("__ZN18IOMemoryDescriptor15getDescriptorIDEv");
extern "C" __attribute__((used, noinline, visibility("default")))
uint64_t BigSurGetDescriptorID(IOMemoryDescriptor *descriptor) {
return static_cast<uint64_t>(reinterpret_cast<uintptr_t>(descriptor));
}
// This API emits optional kdebug diagnostics only. Returning success preserves
// behavior when tracing is disabled, which is the first branch in xnu-7195.
extern "C" __attribute__((used, noinline, visibility("default")))
int32_t BigSurKtraceEmitPhysicalSegments(IOMemoryDescriptor *descriptor)
__asm__("__ZN18IOMemoryDescriptor26ktraceEmitPhysicalSegmentsEv");
extern "C" __attribute__((used, noinline, visibility("default")))
int32_t BigSurKtraceEmitPhysicalSegments(IOMemoryDescriptor *descriptor) {
(void)descriptor;
return kIOReturnSuccess;
}
// xnu-7195 implements this method as a one-line getProperty predicate.
extern "C" __attribute__((used, noinline, visibility("default")))
bool BigSurPropertyExists(
const IORegistryEntry *entry,
const char *key,
const IORegistryPlane *plane,
uint32_t options)
__asm__("__ZNK15IORegistryEntry14propertyExistsEPKcPK15IORegistryPlanej");
extern "C" __attribute__((used, noinline, visibility("default")))
bool BigSurPropertyExists(
const IORegistryEntry *entry,
const char *key,
const IORegistryPlane *plane,
uint32_t options) {
return entry->getProperty(key, plane, options) != nullptr;
}
// These two allocator entry points remain imports in the donor binary's
// generated zoned operator methods. The metaclass bridge above guarantees its
// zone pointer remains null, so normal execution cannot reach either wrapper.
// Returning null for an impossible allocation and leaking rather than freeing
// through an unknown zone are the only safe fallbacks without private KPIs.
extern "C" __attribute__((used, noinline, visibility("default")))
void *BigSurZallocFlags(void *zone, uint32_t flags)
__asm__("_zalloc_flags");
extern "C" __attribute__((used, noinline, visibility("default")))
void *BigSurZallocFlags(void *zone, uint32_t flags) {
// No allocation size is present in this ABI, so there is no honest
// Catalina heap fallback. The only donor call site is guarded by the zone
// pointer that BigSurOSMetaClassConstructor always leaves null.
(void)zone;
(void)flags;
return nullptr;
}
extern "C" __attribute__((used, noinline, visibility("default")))
void BigSurKernOSZfree(void *zone, void *address, uint64_t size)
__asm__("_kern_os_zfree");
extern "C" __attribute__((used, noinline, visibility("default")))
void BigSurKernOSZfree(void *zone, void *address, uint64_t size) {
(void)zone;
(void)address;
(void)size;
}