diff --git a/docs/graphics-kext/design.md b/docs/graphics-kext/design.md
index e6d2e2832..a3b1615b0 100644
--- a/docs/graphics-kext/design.md
+++ b/docs/graphics-kext/design.md
@@ -580,13 +580,20 @@ Stages:
or paged out since, counted so, and holds bytes the cache's zeros
would lose. The kext counts only a surface whose memory is a buffer
the kernel allocated (`IOBufferMemoryDescriptor`, anonymous memory,
- never a client's own, which may be a file's), and only on a kernel
- whose `IOMemoryDescriptor::getPageCounts` counts the pages compressed
- or paged out, referenced weakly: of the release kernels checked, those
- of 26.6.1 and 26.7.2 export it, those of 11.7.10 to 15.8.2 do not, nor
- any routine that tells a page compressed or paged out. A surface not
- counted is never fresh, so there every block's pages are named at its
- ask (`guest/kext/src/RgfxAcceleratorLocks.cpp`). Found fresh, its `SURFACE_PAGES`
+ never a client's own, which may be a file's). It counts them on every
+ kernel the same way: it maps the memory read-only into the kernel's
+ own map for the count, and `mach_vm_region`'s
+ `VM_REGION_EXTENDED_INFO` walks the mapping's regions, which count
+ each page resident or compressed or paged out (`pages_swapped_out`;
+ **measured** on 11.7.11 against each page's disposition, with the
+ pages in the compressor and on swap). `mach_vm_region` and
+ `kernel_map` are strong imports from `com.apple.kpi.unsupported`,
+ which every release kernel checked exports (x86-64 11.7.10 to 26.7.2,
+ arm64 13 to 26). The regions must be the mapping exactly, none a
+ pager's: a region past it, a gap, or one with nothing to count
+ refuses the count. A surface not counted is never fresh, so its
+ block's pages are named at its ask
+ (`guest/kext/src/RgfxAcceleratorLocks.cpp`). Found fresh, its `SURFACE_PAGES`
carries `DEFERRED` and no runs, the block keeping room for one run a
page of the whole allocation, every plane's; the host makes the cache
as zeros. Not fresh, or refused, the kext names the pages at once, as
diff --git a/guest/kext/src/Info-arm64e.plist b/guest/kext/src/Info-arm64e.plist
index 33a6ceedf..9b5a0913a 100644
--- a/guest/kext/src/Info-arm64e.plist
+++ b/guest/kext/src/Info-arm64e.plist
@@ -101,6 +101,8 @@
20.0.0
com.apple.kpi.mach
20.0.0
+ com.apple.kpi.unsupported
+ 20.0.0
OSBundleRequired
Root
diff --git a/guest/kext/src/Info.plist b/guest/kext/src/Info.plist
index 8e7af0281..57bba0216 100644
--- a/guest/kext/src/Info.plist
+++ b/guest/kext/src/Info.plist
@@ -118,6 +118,8 @@
20.0.0
com.apple.kpi.mach
20.0.0
+ com.apple.kpi.unsupported
+ 20.0.0
OSBundleRequired
Root
diff --git a/guest/kext/src/RgfxAccelerator.hpp b/guest/kext/src/RgfxAccelerator.hpp
index 31b4ba97d..e57d9ef7c 100644
--- a/guest/kext/src/RgfxAccelerator.hpp
+++ b/guest/kext/src/RgfxAccelerator.hpp
@@ -215,9 +215,10 @@ bool rgfxSurfaceLocksStartsAt(OSObject *surface, const RgfxPageRun *runs, UInt32
UInt64 length);
struct RgfxSurfacePageCounts;
// What the kernel counts of `surface`'s pages, resident and compressed or paged out
-// (RgfxSurfaceCaches.hpp): counted only where its memory is a buffer the kernel allocated and
-// the kernel counts the pages compressed or paged out. Takes no lock, so a caller may hold the
-// surface's read-only lock across it.
+// (RgfxSurfaceCaches.hpp), from the regions of a mapping of its memory in the kernel's map:
+// counted only where its memory is a buffer the kernel allocated and those regions are exactly
+// the mapping (rgfxRegionPageCounts). Takes no lock, so a caller may hold the surface's read-only
+// lock across it.
RgfxSurfacePageCounts rgfxSurfaceLocksPageCounts(OSObject *surface);
// Whether `surface` reads fresh, to look at: its seed 0, its pages counted, none of them
// compressed or paged out, none resident or as many as `listed`, the pages the ask lists of them
diff --git a/guest/kext/src/RgfxAcceleratorLocks.cpp b/guest/kext/src/RgfxAcceleratorLocks.cpp
index ec289c48b..fc19725cc 100644
--- a/guest/kext/src/RgfxAcceleratorLocks.cpp
+++ b/guest/kext/src/RgfxAcceleratorLocks.cpp
@@ -63,9 +63,9 @@
#include
#include
#include
-#include
#include
-#include
+#include
+#include
#include
namespace {
@@ -582,58 +582,60 @@ bool rgfxSurfaceLocksStartsAt(OSObject *surface, const RgfxPageRun *runs, UInt32
return rgfxSurfaceRunsSame(walk);
}
-// IOMemoryDescriptor::getPageCounts(resident, dirty, swapped), which counts the pages compressed
-// or paged out too (`swapped`), referenced weakly: of the release kernels checked, the symbol
-// sets of 26.6.1 (25G76) and 26.7.2 (25G332) export it in com.apple.kpi.iokit, and those of
-// 11.7.10, 11.7.11, 12.7.4, 12.7.6, 13.7.8, 14.8.9, 15.7.9 and 15.8.2 do not, nor any routine of
-// any KPI set that counts or queries a page's being compressed or paged out (each kernel's
-// __LINKINFO,__symbolsets, read 2026-11-08). The two-count form counts the resident pages alone,
-// so a page a writer that takes no lock dirtied, then compressed or paged out, would read as
-// fresh, and the host's zeros would take its place. A binder that cannot resolve the symbol
-// binds it to gOSKextUnresolved, as the kernel's linker does, or to 0: either reads as absent,
-// and then no surface is counted, so none is deferred, and every block's pages are named at its
-// ask. A non-virtual member: `memory` is its `this`.
-extern "C" IOReturn rgfxMemoryPageCounts(IOMemoryDescriptor *memory, IOByteCount *resident,
- IOByteCount *dirty, IOByteCount *swapped)
- __asm("__ZN18IOMemoryDescriptor13getPageCountsEPyS0_S0_") __attribute__((weak_import));
+// The kernel's map, exported in com.apple.kpi.unsupported, which no SDK header declares.
+extern "C" vm_map_t kernel_map;
-// The function is reached only through this word of data, its address bound there whole, never
-// from the code: an address relative to an instruction cannot reach 0, and OpenCore's linker
-// refuses the whole kext where it binds such a reference to 0 ("RIP-relative overflow ...
-// target=0x0", then the kext not injected, measured with its TestKextInject on the Boot KCs of
-// 11.7.11, 13.7.8 and 15.8.2). Volatile, so the compiler reads the word rather than call the
-// function directly.
-using RgfxMemoryPageCountsFn = IOReturn (*)(IOMemoryDescriptor *, IOByteCount *, IOByteCount *,
- IOByteCount *);
-static RgfxMemoryPageCountsFn volatile memoryPageCounts = &rgfxMemoryPageCounts;
+namespace {
+// The most regions of a surface's mapping a count walks: more, and it is not counted.
+constexpr uint32_t kMaxCountRegions = 8;
+// Surfaces of this many bytes or more have each count's time logged, the first few of them.
+constexpr uint64_t kCountLoggedBytes = 16ull << 20;
+constexpr uint32_t kCountsLogged = 16;
+} // namespace
-// Whether the weak function at `symbol` was bound to a definition. A pointer to it is signed
-// where calls are authenticated (arm64e), so it is stripped before it is compared with the
-// pointer gOSKextUnresolved holds.
-static bool weakFunctionResolved(const void *symbol)
+// What a count of a surface's pages took, logged where it is the longest yet by a quarter or
+// more, or where the surface is large, for the first kCountsLogged of those.
+static void noteCountTime(uint64_t bytes, uint64_t ns, const RgfxSurfacePageCounts &counts)
{
-#if __has_feature(ptrauth_calls)
- symbol = ptrauth_strip(symbol, ptrauth_key_function_pointer);
-#endif
- return symbol != nullptr && symbol != gOSKextUnresolved;
+ static uint64_t longest = 0;
+ static uint32_t largeLogged = 0;
+ uint64_t was = __atomic_load_n(&longest, __ATOMIC_RELAXED);
+ bool longer = false;
+ while (ns > was + was / 4 && !longer)
+ longer = __atomic_compare_exchange_n(&longest, &was, ns, false, __ATOMIC_RELAXED,
+ __ATOMIC_RELAXED);
+ const bool large = bytes >= kCountLoggedBytes &&
+ __atomic_fetch_add(&largeLogged, 1, __ATOMIC_RELAXED) < kCountsLogged;
+ if (longer || large)
+ IOLog("RgfxAccelerator: counting an IOSurface's %llu KiB of pages took %llu us%s: %s, "
+ "%llu resident, %llu compressed or paged out\n",
+ bytes >> 10, ns / 1000, longer ? ", the longest yet" : "",
+ counts.counted ? "counted" : "not counted", counts.counted ? counts.resident : 0,
+ counts.counted ? counts.swapped : 0);
}
+// The counts come from the regions of a mapping of the surface's memory in the kernel's own map,
+// made for the count (read-only, unique, faulting nothing in) and gone after it: mach_vm_region's
+// VM_REGION_EXTENDED_INFO walks the memory's object page by page and counts each page resident,
+// or compressed or paged out (pages_swapped_out), measured on 11.7.11 (20G1443) for an IOSurface
+// whose pages a child's allocations pushed into the compressor and on to swap: its counts equal
+// each page's disposition, resident, compressed or mixed. mach_vm_region and kernel_map are in
+// com.apple.kpi.unsupported, and kernel_task in com.apple.kpi.mach, in the symbol sets of every
+// release kernel checked (x86_64 11.7.10 to 26.7.2, arm64 13 to 26; each kernel's
+// __LINKINFO,__symbolsets): a strong import, bound by every linker, OpenCore's included. The
+// walk costs about a microsecond per 1-16 pages (measured from user space on 11.7.11: 0.13 ms
+// for 16384 resident pages, 1.3 ms for the same compressed), plus the mapping's making and
+// removal.
RgfxSurfacePageCounts rgfxSurfaceLocksPageCounts(OSObject *surface)
{
RgfxSurfacePageCounts counts = {false, ~0ull, ~0ull};
- static bool countsNoted = false, memoryNoted = false;
- const RgfxMemoryPageCountsFn pageCounts = memoryPageCounts;
- if (!weakFunctionResolved(reinterpret_cast(pageCounts))) {
- if (!__atomic_exchange_n(&countsNoted, true, __ATOMIC_RELAXED))
- IOLog("RgfxAccelerator: this kernel counts no compressed or paged-out pages of a "
- "memory descriptor: no IOSurface's block is deferred\n");
- return counts;
- }
+ static bool memoryNoted = false, mapNoted = false;
// An IOBufferMemoryDescriptor allocates its memory itself, pageable or wired, from the
// kernel's anonymous allocators, never over a file (xnu's IOBufferMemoryDescriptor.cpp; not
// re-read for this change): a page of it neither resident nor compressed nor paged out was
// never written. A descriptor over other memory, a client's own, may be a file's, whose
- // pages not resident hold the file's bytes, which no count shows: it is not counted.
+ // pages not resident hold the file's bytes, which no count shows: it is not counted. The
+ // walk says so too (external_pager), and a region of a pager's is not counted either.
IOMemoryDescriptor *memory = OSDynamicCast(IOMemoryDescriptor, rgfxIOSurfaceMemory(surface));
if (!memory)
return counts;
@@ -644,10 +646,42 @@ RgfxSurfacePageCounts rgfxSurfaceLocksPageCounts(OSObject *surface)
memory->getMetaClass()->getClassName());
return counts;
}
- IOByteCount resident = 0, dirty = 0, swapped = 0;
- if (pageCounts(memory, &resident, &dirty, &swapped) != kIOReturnSuccess)
+ const uint64_t began = mach_absolute_time();
+ IOMemoryMap *map =
+ memory->createMappingInTask(kernel_task, 0, kIOMapAnywhere | kIOMapReadOnly | kIOMapUnique);
+ if (!map) {
+ if (!__atomic_exchange_n(&mapNoted, true, __ATOMIC_RELAXED))
+ IOLog("RgfxAccelerator: an IOSurface's memory could not be mapped to count its "
+ "pages: its block, and any such, is not deferred\n");
return counts;
- counts = {true, resident, swapped};
+ }
+ const uint64_t start = map->getAddress(), length = map->getLength();
+ RgfxPageRegion regions[kMaxCountRegions] = {};
+ uint32_t found = 0;
+ mach_vm_address_t at = start;
+ while (found < kMaxCountRegions && at < start + length) {
+ mach_vm_address_t address = at;
+ mach_vm_size_t size = 0;
+ vm_region_extended_info_data_t info = {};
+ mach_msg_type_number_t infoCount = VM_REGION_EXTENDED_INFO_COUNT;
+ mach_port_t object = MACH_PORT_NULL;
+ if (mach_vm_region(kernel_map, &address, &size, VM_REGION_EXTENDED_INFO,
+ reinterpret_cast(&info), &infoCount,
+ &object) != KERN_SUCCESS)
+ break;
+ regions[found++] = {address, size, info.pages_resident, info.pages_swapped_out,
+ info.external_pager != 0, info.share_mode == SM_EMPTY};
+ if (address + size <= address)
+ break;
+ at = address + size;
+ }
+ // A walk that ended short (a failed call, or more regions than kMaxCountRegions) leaves the
+ // regions short of the mapping's end, which rgfxRegionPageCounts refuses.
+ counts = rgfxRegionPageCounts(regions, found, start, length);
+ map->release();
+ uint64_t ns = 0;
+ absolutetime_to_nanoseconds(mach_absolute_time() - began, &ns);
+ noteCountTime(length, ns, counts);
return counts;
}
diff --git a/guest/kext/src/RgfxDeferCarry.hpp b/guest/kext/src/RgfxDeferCarry.hpp
index e083aeb0e..ea184bc90 100644
--- a/guest/kext/src/RgfxDeferCarry.hpp
+++ b/guest/kext/src/RgfxDeferCarry.hpp
@@ -9,7 +9,7 @@
// kRgfxCarryPagesMost of them, and the kext proves the list under its read of the surface
// (rgfxSurfaceLocksDefer): the surface's memory holds exactly as many resident pages as listed and
// none compressed or paged out (rgfxSurfaceHoldsOnlyResident, the fresh test's own verdict, over
-// the kernel's counts: where it counts no compressed or paged-out pages, nothing is deferred), the
+// the kernel's counts: where they cannot be taken, nothing is deferred), the
// listed runs wire with no fault, and its counts are the same after the wire. The deferral then
// names those pages, every other run of the surface's bytes RGFX_PAGE_RUN_ZEROS, and the host
// fills its cache from them as a new cache's first fill. The kext holds them wired until the
@@ -138,8 +138,8 @@ enum RgfxCarryOutcome : uint32_t {
kRgfxCarryNone,
// Not deferred: the surface's seed moved, or the read-only lock failed.
kRgfxCarryTouched,
- // Not deferred: the surface's pages were not counted (rgfxSurfaceLocksPageCounts): the kernel
- // counts no compressed or paged-out pages, or its memory is not a buffer the kernel allocated.
+ // Not deferred: the surface's pages were not counted (rgfxSurfaceLocksPageCounts): its memory
+ // is not a buffer the kernel allocated, or the regions of its mapping are not exactly it.
kRgfxCarryUncounted,
// Not deferred: more pages resident than listed, or fewer, or a page compressed or paged out.
kRgfxCarryCounted,
diff --git a/guest/kext/src/RgfxSurfaceCaches.hpp b/guest/kext/src/RgfxSurfaceCaches.hpp
index 82d8e2357..ffb17057d 100644
--- a/guest/kext/src/RgfxSurfaceCaches.hpp
+++ b/guest/kext/src/RgfxSurfaceCaches.hpp
@@ -534,14 +534,55 @@ inline uint64_t rgfxCacheAttached(RgfxCacheTable &t, uint64_t key, RgfxPagesNami
// What the kernel counted of a surface's pages (rgfxSurfaceLocksPageCounts). `counted` only
// where the counts cover every place its bytes can be: memory the kernel allocated anonymous,
-// so a page neither resident nor compressed nor paged out holds nothing, and a kernel that
-// counts the compressed and paged-out pages (`swapped`), not only the resident ones.
+// so a page neither resident nor compressed nor paged out holds nothing, and counts of the
+// compressed and paged-out pages (`swapped`) as well as the resident ones, over its whole
+// mapping and nothing past it (rgfxRegionPageCounts).
struct RgfxSurfacePageCounts {
bool counted;
uint64_t resident;
uint64_t swapped;
};
+// One region of the kernel's map, as VM_REGION_EXTENDED_INFO describes it: where it starts, its
+// size, its pages resident and its pages compressed or paged out (pages_swapped_out, which
+// counts both: measured on 11.7.11 against each page's disposition), whether its memory is a
+// pager's (`external`, a file's), and whether it has no memory to count (`empty`, SM_EMPTY: no
+// object, a submap, or physically contiguous memory).
+struct RgfxPageRegion {
+ uint64_t start;
+ uint64_t size;
+ uint64_t resident;
+ uint64_t swapped;
+ bool external;
+ bool empty;
+};
+
+// The counts of a surface mapped at [start, start + length), from the `count` regions a walk from
+// `start` found, in order. Counted only where the regions tile the mapping exactly: the first
+// starting at `start`, each the next where the previous ends, the last ending where the mapping
+// does, none a pager's or empty. A region reaching past the mapping would count pages that are
+// not the surface's, and a gap or a missing region pages that are not counted, so either refuses
+// the count, which only ever refuses a deferral, never grants one.
+inline RgfxSurfacePageCounts rgfxRegionPageCounts(const RgfxPageRegion *regions, uint32_t count,
+ uint64_t start, uint64_t length)
+{
+ const RgfxSurfacePageCounts uncounted = {false, ~0ull, ~0ull};
+ if (count == 0 || length == 0 || start + length < start)
+ return uncounted;
+ RgfxSurfacePageCounts counts = {true, 0, 0};
+ uint64_t at = start;
+ for (uint32_t i = 0; i < count; i++) {
+ const RgfxPageRegion &r = regions[i];
+ if (r.start != at || r.size == 0 || r.size > start + length - at || r.external ||
+ r.empty)
+ return uncounted;
+ counts.resident += r.resident;
+ counts.swapped += r.swapped;
+ at += r.size;
+ }
+ return at == start + length ? counts : uncounted;
+}
+
// Whether a surface's bytes can only be in its `resident` resident pages, as the read that
// found `seed` and `counts` saw it: under its read-only lock (`locked`), its seed 0 (no lock for
// writing yet), its pages counted, none compressed or paged out, and `resident` of them in
diff --git a/guest/kext/tests/defer_carry_test.cpp b/guest/kext/tests/defer_carry_test.cpp
index d62600c62..e16cb4d9e 100644
--- a/guest/kext/tests/defer_carry_test.cpp
+++ b/guest/kext/tests/defer_carry_test.cpp
@@ -105,8 +105,8 @@ RgfxSurfacePageCounts at(uint64_t resident, uint64_t swapped = 0)
return {true, resident, swapped};
}
-// Counts not taken: the kernel counts no compressed or paged-out pages, or the surface's memory is
-// not a buffer the kernel allocated.
+// Counts not taken: the surface's memory is not a buffer the kernel allocated, or the regions of
+// its mapping are not exactly it.
constexpr RgfxSurfacePageCounts kUncounted = {false, ~0ull, ~0ull};
void reads()
diff --git a/guest/kext/tests/surface_caches_test.cpp b/guest/kext/tests/surface_caches_test.cpp
index 6717ad7ca..3e9c281d7 100644
--- a/guest/kext/tests/surface_caches_test.cpp
+++ b/guest/kext/tests/surface_caches_test.cpp
@@ -318,6 +318,56 @@ void heldBytes()
check(!rgfxSurfaceHoldsOnlyResident(true, 0, {true, 2, 0}, 3), "two of three: no");
}
+// A surface's counts come from the regions of its mapping in the kernel's map, and count only
+// where those regions are the mapping exactly: a region past it would count pages not the
+// surface's, and a gap pages not counted, so either refuses the count (and with it a deferral),
+// never grants one.
+void regionCounts()
+{
+ constexpr uint64_t start = 0x10000000, page = 0x1000, length = 16 * page;
+ const RgfxPageRegion whole[] = {{start, length, 0, 0, false, false}};
+ RgfxSurfacePageCounts c = rgfxRegionPageCounts(whole, 1, start, length);
+ check(c.counted && c.resident == 0 && c.swapped == 0,
+ "one region that is the mapping, nothing in it: counted, none resident or swapped");
+ check(rgfxSurfaceHoldsOnlyResident(true, 0, c, 0), "and that surface reads fresh");
+ const RgfxPageRegion split[] = {{start, 4 * page, 2, 1, false, false},
+ {start + 4 * page, 12 * page, 3, 4, false, false}};
+ c = rgfxRegionPageCounts(split, 2, start, length);
+ check(c.counted && c.resident == 5 && c.swapped == 5,
+ "two regions that tile the mapping: their counts summed");
+ check(!rgfxSurfaceHoldsOnlyResident(true, 0, c, 0), "with pages swapped: not fresh");
+ const RgfxPageRegion before[] = {{start - page, length + page, 0, 0, false, false}};
+ check(!rgfxRegionPageCounts(before, 1, start, length).counted,
+ "a region starting before the mapping: refused");
+ const RgfxPageRegion past[] = {{start, length + page, 0, 0, false, false}};
+ check(!rgfxRegionPageCounts(past, 1, start, length).counted,
+ "a region ending past the mapping: refused, though its counts are all zero");
+ const RgfxPageRegion pastLast[] = {{start, 4 * page, 0, 0, false, false},
+ {start + 4 * page, 13 * page, 0, 0, false, false}};
+ check(!rgfxRegionPageCounts(pastLast, 2, start, length).counted,
+ "the last of two regions ending past the mapping: refused");
+ const RgfxPageRegion gap[] = {{start, 4 * page, 0, 0, false, false},
+ {start + 5 * page, 11 * page, 0, 0, false, false}};
+ check(!rgfxRegionPageCounts(gap, 2, start, length).counted,
+ "a gap between two regions: refused");
+ const RgfxPageRegion part[] = {{start, 4 * page, 0, 0, false, false}};
+ check(!rgfxRegionPageCounts(part, 1, start, length).counted,
+ "regions covering part of the mapping: refused");
+ const RgfxPageRegion pager[] = {{start, length, 0, 0, true, false}};
+ check(!rgfxRegionPageCounts(pager, 1, start, length).counted,
+ "a pager's memory (a file's), whose pages not resident hold its bytes: refused");
+ const RgfxPageRegion empty[] = {{start, length, 0, 0, false, true}};
+ check(!rgfxRegionPageCounts(empty, 1, start, length).counted,
+ "a region with nothing the walk can count (SM_EMPTY): refused");
+ check(!rgfxRegionPageCounts(whole, 0, start, length).counted, "no region: refused");
+ check(!rgfxRegionPageCounts(whole, 1, start, 0).counted, "an empty mapping: refused");
+ const RgfxPageRegion zero[] = {{start, 0, 0, 0, false, false}};
+ check(!rgfxRegionPageCounts(zero, 1, start, length).counted, "a region of no size: refused");
+ const RgfxPageRegion top[] = {{~0ull - page + 1, page, 0, 0, false, false}};
+ check(!rgfxRegionPageCounts(top, 1, ~0ull - page + 1, 2 * page).counted,
+ "a mapping that would wrap the address space: refused");
+}
+
// On a host that answers which surfaces need a cache, a deferral refused, or a surface read not
// fresh, has the block ask with its pages; answered in place, its entry goes.
void ungated()
@@ -1102,6 +1152,7 @@ int main()
room();
deferral();
heldBytes();
+ regionCounts();
carriedWires();
ungated();
deferredRelease();