🐛 Map whole 16 KiB pages of an arm64 process's surface blocks

On arm64 a window maps whole 16 KiB pages, and refused every span of a
surface's block or the scanout that was whole 4 KiB guest pages alone: a
27.0.1 WindowServer's 1920x1080 RGBA16Float plane (0xfd3000 bytes) and others
were "not mapped in: 0xe00002c2", so it never reached their blocks. Every
caller of mapSpan and unmapSpan now passes the span of whole window pages
(rgfxWindowSpanLength): a surface block's runs start past that span
(rgfxSurfaceRunsFrom), the scanout's grant is rounded to it, and a block's
write-combined split in the block's rounding leaves the block write-back
(rgfxWindowWriteBackPart). Every block is whole window pages, so the pages a
span adds are its block's own.

On x86-64 a window page is 4 KiB and every length was a 4 KiB multiple, so
each span is as before; the kext's code differs, not what it maps. The arm64
region test checks the layouts on 16 KiB pages, with the refused sizes, and
that every mapSpan and unmapSpan call passes a span.
This commit is contained in:
Anees Iqbal committed 2026-11-09 11:09:07 -08:00
1 parent 2a7078b2bd
commit 560e96b2cc
7 files changed
+168 -28

No files matched your search

+4
View File
@@ -1987,6 +1987,10 @@ before a block's free marks it freed, and when a hold ends, a scanout grant
goes or a mode change moves the scanout. At the close the whole window goes,
every span in it, before the context's blocks are freed. Each span is whole
pages past the region's head, so no mapped page holds a byte of anything else.
A page here is the process's: 4 KiB on x86-64 and 16 KiB on arm64, where the
kext maps the whole 16 KiB pages of a block's part a process uses (a plane's
bytes, whole 4 KiB guest pages) and of the scanout's rows. Every block is whole
such pages, and a surface block's runs start past the mapped pages.
The device info shares its page with the control completion ring's tail, so
the kext reads it for the process (selector 0x10b), and the region's own map
(`kRgfxAccelMemoryRegion`) is refused. One difference from Apple's: a process
+35 -27
View File
@@ -1069,6 +1069,9 @@ void RgfxAccelerator::keepIdChunks(const void *owner)
// MARK: - Blocks
// A block is whole window pages, so the whole of it is a span a window maps.
static_assert(kRgfxBlockPage == kRgfxWindowPage, "a block is whole window pages");
IOReturn RgfxAccelerator::allocBlock(RgfxAcceleratorUserClient *owner, UInt32 context,
UInt64 size, UInt64 align, UInt64 writeCombinedFrom,
UInt64 *offset)
@@ -1097,7 +1100,7 @@ IOReturn RgfxAccelerator::allocBlock(RgfxAcceleratorUserClient *owner, UInt32 co
// which are no RAM of the guest's, are mapped uncached for any other ask (xnu's pmap
// sets PCD on every page it does not manage). The process fences its stores before it
// publishes them.
const UInt64 combined = writeCombinedFrom < bytes ? writeCombinedFrom : bytes;
const UInt64 combined = rgfxWindowWriteBackPart(size, bytes, writeCombinedFrom);
IOReturn posted = combined ? owner->mapSpan(pin.offset, combined) : kIOReturnSuccess;
if (posted == kIOReturnSuccess && combined < bytes)
posted = owner->mapSpan(pin.offset + combined, bytes - combined, true);
@@ -1143,7 +1146,7 @@ IOReturn RgfxAccelerator::freeBlock(RgfxAcceleratorUserClient *owner, UInt64 off
// the window.
const int64_t at = rgfxBlockIndex(blockTable, offset);
if (at < 0 || !blockTable.blocks[at].wired)
owner->unmapSpan(offset, size);
owner->unmapSpan(offset, rgfxWindowSpanLength(size));
r = rgfxBlockFree(blockTable, owner, offset, mach_absolute_time());
}
IOLockUnlock(contextLock);
@@ -1817,12 +1820,13 @@ IOReturn RgfxAccelerator::acquireSurfaceBlock(RgfxAcceleratorUserClient *client,
writeBackLate = true;
releaseStats.writeBackLate++;
}
// A new block in place holds the runs past the part the processes map, whole pages
// of them; a deferred one room for one run a page of the plane.
// A new block in place holds the runs past the span the processes map
// (rgfxSurfaceRunsFrom), whole pages of them; a deferred one room for one run a
// page of the plane.
const UInt64 runBytes = sizeof(RgfxPageRun) * static_cast<UInt64>(count);
total = !inPlace ? size
: count ? size + alignUp(runBytes, kPage)
: size + rgfxSurfaceRunRoom(length);
: count ? rgfxSurfaceRunsFrom(size) + alignUp(runBytes, kPage)
: rgfxSurfaceRunsFrom(size) + rgfxSurfaceRunRoom(length);
// A new block: a dormant one gives its entry where the table is full, and the
// region's room where it has none, its release waited for a while, once.
slot = rgfxSurfaceFreeEntry(table);
@@ -1885,14 +1889,14 @@ IOReturn RgfxAccelerator::acquireSurfaceBlock(RgfxAcceleratorUserClient *client,
IOReturn mapped = kIOReturnSuccess;
bool keptWire = false;
if (held == kRgfxSurfaceHoldNew) {
mapped = client->mapSpan(e.offset, e.size);
mapped = client->mapSpan(e.offset, rgfxWindowSpanLength(e.size));
if (mapped != kIOReturnSuccess) {
// No hold the device was not told of, nor one not in the window; a block held
// again goes, holding none.
void *mine = nullptr;
UInt32 c = 0;
rgfxSurfaceLetGo(e, client, true, &mine, &c);
client->unmapSpan(e.offset, e.size);
client->unmapSpan(e.offset, rgfxWindowSpanLength(e.size));
held = kRgfxSurfaceHoldFull;
if (!e.holdCount && letGoBlock(e, context, false, mine, &keptWire))
thread_call_enter(releaseCall);
@@ -1931,7 +1935,7 @@ IOReturn RgfxAccelerator::acquireSurfaceBlock(RgfxAcceleratorUserClient *client,
true, &mine, &c)
: kRgfxSurfaceLetGoNotHeld;
if (gone != kRgfxSurfaceLetGoNotHeld)
client->unmapSpan(block->offset, block->size);
client->unmapSpan(block->offset, rgfxWindowSpanLength(block->size));
if (gone == kRgfxSurfaceLetGoLast)
release = letGoBlock(table.entries[again], c, false, mine, &shareKeptWire);
}
@@ -1979,10 +1983,10 @@ IOReturn RgfxAccelerator::acquireSurfaceBlock(RgfxAcceleratorUserClient *client,
// In the client's window, under the lock, so no let-go of the hold comes between: the part
// the processes map, never the runs. A block that is not goes back as if never made: the
// device was not told of it.
IOReturn mapped = client->mapSpan(offset, size);
IOReturn mapped = client->mapSpan(offset, rgfxWindowSpanLength(size));
if (mapped != kIOReturnSuccess) {
OSObject *deferredSurface = deferring ? static_cast<OSObject *>(e.surface) : nullptr;
client->unmapSpan(offset, size);
client->unmapSpan(offset, rgfxWindowSpanLength(size));
rgfxBlockDrop(blockTable, pin);
rgfxSurfaceClear(table, e);
if (inPlace)
@@ -2025,8 +2029,9 @@ IOReturn RgfxAccelerator::acquireSurfaceBlock(RgfxAcceleratorUserClient *client,
cached, ask ? layout : nullptr, &status)) {
if (ask && status == RGFX_STATUS_CACHE) {
cached = cacheSurface(e, task, surfaceId, offset);
if (postPages(context, offset, total, offset + size, length, count,
cached ? RGFX_SURFACE_PAGES_CACHED : 0) != kIOReturnSuccess)
if (postPages(context, offset, total, offset + rgfxSurfaceRunsFrom(size),
length, count, cached ? RGFX_SURFACE_PAGES_CACHED : 0) !=
kIOReturnSuccess)
posted = kIOReturnUnsupported;
} else {
posted = kIOReturnUnsupported;
@@ -2051,7 +2056,7 @@ IOReturn RgfxAccelerator::acquireSurfaceBlock(RgfxAcceleratorUserClient *client,
UInt32 c = 0;
RgfxSurfaceLetGo gone = rgfxSurfaceLetGo(e, client, true, &mine, &c);
if (gone != kRgfxSurfaceLetGoNotHeld)
client->unmapSpan(offset, size);
client->unmapSpan(offset, rgfxWindowSpanLength(size));
if (gone == kRgfxSurfaceLetGoLast)
release = letGoBlock(e, c, false, mine, &keptWire);
}
@@ -2214,16 +2219,17 @@ bool RgfxAccelerator::deferSurfaceBlock(RgfxSurfaceEntry &e, UInt32 context, uin
// A deferral that carries pages names them in its runs, past the part the processes map, in
// the room the block keeps for one run a page: one whose runs could not be written names its
// pages now, as a refused one does, nothing read of the carried pages.
const bool runsWritten =
!carriedCount || writeRuns(region, offset + size, carried, carriedCount);
const bool runsWritten = !carriedCount ||
writeRuns(region, offset + rgfxSurfaceRunsFrom(size), carried,
carriedCount);
if (answer == kRgfxDeferFresh && !runsWritten)
rgfxSurfaceLocksRefusedCarry(cache);
if (answer == kRgfxDeferFresh && runsWritten) {
UInt32 status = RGFX_STATUS_OK;
const UInt32 flags = RGFX_SURFACE_PAGES_CACHED | RGFX_SURFACE_PAGES_DEFERRED |
(asking ? RGFX_SURFACE_PAGES_ASK_CACHE : 0);
const IOReturn r = postPages(context, offset, total, offset + size, length, carriedCount,
flags, &status, asking);
const IOReturn r = postPages(context, offset, total, offset + rgfxSurfaceRunsFrom(size),
length, carriedCount, flags, &status, asking);
if (r == kIOReturnSuccess || r != kIOReturnUnsupported || status != RGFX_STATUS_REFUSED) {
// Deferred, or not answered: a block whose answer did not come goes through its
// release, its region bytes given out again only at an OK answer. No guest page was
@@ -2319,27 +2325,29 @@ RgfxPagesNaming RgfxAccelerator::attachSurfacePages(UInt32 context, UInt64 offse
return kRgfxPagesNotNamed;
UInt32 count = 0;
RgfxPageRun *runs = walkRuns(memory, 0, length, &count);
// Its runs past the span its holders map (rgfxSurfaceRunsFrom).
const UInt64 runsAt = offset + rgfxSurfaceRunsFrom(size);
RgfxPagesNaming naming = kRgfxPagesNotNamed;
const char *why = nullptr;
if (!runs) {
why = "its runs do not fit";
} else if (!writeRuns(region, offset + size, runs, count)) {
} else if (!writeRuns(region, runsAt, runs, count)) {
why = "its runs could not be written";
} else {
const UInt64 total = size + rgfxSurfaceRunRoom(length);
const UInt64 total = rgfxSurfaceRunsFrom(size) + rgfxSurfaceRunRoom(length);
IOReturn r = kIOReturnSuccess;
UInt32 status = RGFX_STATUS_OK;
if (layout) {
// The ask: in place where answered OK; named again with `flags` where the host
// answered that the surface needs a device cache, the runs written already.
r = postPages(context, offset, total, offset + size, length, count,
r = postPages(context, offset, total, runsAt, length, count,
RGFX_SURFACE_PAGES_ASK_CACHE, &status, layout);
if (r == kIOReturnSuccess && inPlace)
*inPlace = true;
else if (r == kIOReturnUnsupported && status == RGFX_STATUS_CACHE)
r = postPages(context, offset, total, offset + size, length, count, flags);
r = postPages(context, offset, total, runsAt, length, count, flags);
} else {
r = postPages(context, offset, total, offset + size, length, count, flags);
r = postPages(context, offset, total, runsAt, length, count, flags);
}
naming = rgfxPagesNaming(r == kIOReturnSuccess || r == kIOReturnUnsupported,
r == kIOReturnSuccess);
@@ -2386,7 +2394,7 @@ bool RgfxAccelerator::postSurfacePages(UInt32 context, UInt64 offset, UInt64 tot
bool cached, const RgfxSurfacePagesLayout *layout,
UInt32 *status)
{
if (!writeRuns(region, offset + size, runs, count)) {
if (!writeRuns(region, offset + rgfxSurfaceRunsFrom(size), runs, count)) {
IOLog("RgfxAccelerator: the block 0x%llx's runs could not be written: its surface is "
"copied\n",
offset);
@@ -2395,8 +2403,8 @@ bool RgfxAccelerator::postSurfacePages(UInt32 context, UInt64 offset, UInt64 tot
const UInt32 flags = cached ? RGFX_SURFACE_PAGES_CACHED
: layout ? RGFX_SURFACE_PAGES_ASK_CACHE
: 0;
IOReturn r = postPages(context, offset, total, offset + size, length, count, flags, status,
layout);
IOReturn r = postPages(context, offset, total, offset + rgfxSurfaceRunsFrom(size), length,
count, flags, status, layout);
if (r != kIOReturnSuccess && r != kIOReturnUnsupported)
IOLog("RgfxAccelerator: the block 0x%llx's pages were not answered (0x%x): its surface "
"is copied\n",
@@ -2576,7 +2584,7 @@ IOReturn RgfxAccelerator::releaseSurfaceBlock(RgfxAcceleratorUserClient *client,
UInt64 serial = at < 0 ? 0 : table.entries[at].serial;
// Out of the client's window once its hold ends, before the block can be anyone else's.
if (gone == kRgfxSurfaceLetGoHold || gone == kRgfxSurfaceLetGoLast)
client->unmapSpan(offset, size);
client->unmapSpan(offset, rgfxWindowSpanLength(size));
bool release = false, slept = false, keptWire = false;
OSObject *sleeper = nullptr;
if (gone == kRgfxSurfaceLetGoLast) {
+4 -1
View File
@@ -30,6 +30,7 @@
#include "RgfxSurfaceUserClient.hpp"
#include "ReimsGraphics.hpp"
#include "RgfxScanoutOwner.hpp"
#include "RgfxWindowSpans.hpp"
#include "RgfxTrace.hpp"
#include <IOKit/IOLib.h>
@@ -168,7 +169,9 @@ bool RgfxAccelerator::currentScanout(Scanout *scanout)
fb->release();
if (!ok)
return false;
scanout->length = round_page_64(static_cast<UInt64>(scanout->rowBytes) * scanout->height);
// The span a window maps of it, whole window pages: 16 KiB on arm64 (RgfxWindowSpans.hpp).
scanout->length =
rgfxWindowSpanLength(static_cast<UInt64>(scanout->rowBytes) * scanout->height);
return true;
}
+9
View File
@@ -50,6 +50,8 @@
// RgfxPageRun, a run of physically contiguous bytes of a plane's pages, is the wire's: an
// in-place block hands its runs to the device as they are.
#include "reims_graphics_wire.h"
// The span a holder's window maps of a block (rgfxWindowSpanLength).
#include "RgfxWindowSpans.hpp"
// The most clients that hold one plane's block.
constexpr uint32_t kRgfxSurfaceHolders = 16;
@@ -483,6 +485,13 @@ inline bool rgfxSurfaceDeferFits(const RgfxSurfaceTable &t, uint64_t length, uin
return budget != 0 && length <= budget && t.deferredBytes <= budget - length;
}
// Where a block's runs lie from its start, past the `size` bytes its holders use: past the span
// their windows map of those (rgfxWindowSpanLength), so no page a holder maps holds a run.
inline uint64_t rgfxSurfaceRunsFrom(uint64_t size)
{
return rgfxWindowSpanLength(size);
}
// The bytes an in-place block of a plane of `length` bytes keeps for its runs past the part its
// holders map: one run a page at most, in whole pages. A deferred block keeps that much, its
// runs unknown until they are named.
+19
View File
@@ -42,4 +42,23 @@ inline bool rgfxWindowSpan(uint64_t windowSize, uint64_t offset, uint64_t length
length <= windowSize - offset;
}
// The span a window maps for the `length` bytes a process uses from the start of a block or the
// scanout: whole window pages, 16 KiB on arm64, where the bytes the process uses are whole guest
// pages of 4 KiB (a 1920x1080 RGBA16Float plane's 0xfd2000). Every block is whole window pages
// at a page's offset (RgfxBlockTable.hpp), so the span's pages past `length` are the block's
// own; a block whose part only the kext and the device read (a surface block's runs) places it
// past the span.
inline uint64_t rgfxWindowSpanLength(uint64_t length)
{
return (length + kRgfxWindowPage - 1) & ~(kRgfxWindowPage - 1);
}
// The part of a block of `bytes`, asked as `size` bytes, a window maps write-back, the rest
// write-combined: up to `writeCombinedFrom` where that lies in the asked bytes, a page's offset,
// and the whole block where it does not, the bytes past `size` being the block's rounding.
inline uint64_t rgfxWindowWriteBackPart(uint64_t size, uint64_t bytes, uint64_t writeCombinedFrom)
{
return writeCombinedFrom < size ? writeCombinedFrom : bytes;
}
#endif // RGFX_WINDOW_SPANS_HPP_
+84
View File
@@ -2,10 +2,14 @@
// Makefile's rule for this test): a process's page is 16 KiB, so a window's span and a block are
// whole 16 KiB pages, and no write-combined part is offered, so every span is mapped write-back.
#include "RgfxBlockTable.hpp"
#include "RgfxSurfaceBlocks.hpp"
#include "RgfxWindowSpans.hpp"
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iterator>
#include <string>
#if !RGFX_GUEST_ARM64
#error "arm64_region_test is built with RGFX_GUEST_ARM64=1, as the arm64e kext is"
@@ -26,6 +30,48 @@ void check(bool ok, const char *what)
const char kA = 0, kB = 0;
const void *const a = &kA;
const void *const b = &kB;
std::string readFile(const char *path)
{
std::ifstream in(path);
check(in.good(), path);
return std::string((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
}
// The length each mapSpan and unmapSpan call of `path` passes, its text from the call's first
// comma to its closing parenthesis, is a span's: rounded by rgfxWindowSpanLength, or one of the
// lengths that are whole window pages already, a block's as allocated (`bytes`, `bytes -
// combined` and `combined`, split by rgfxWindowWriteBackPart) and a grant's of the scanout
// (`scanout->length`, `g.length`), which currentScanout rounds.
void spansRounded(const char *path)
{
const std::string source = readFile(path);
const char *const whole[] = {"bytes", "bytes - combined", "combined", "scanout->length",
"g.length"};
for (const char *call : {"mapSpan(", "unmapSpan("}) {
for (size_t at = source.find(call); at != std::string::npos;
at = source.find(call, at + 1)) {
// Not a declaration or a definition: those name the length's type.
const size_t open = at + std::string(call).size();
const size_t comma = source.find(',', open);
const size_t close = source.find(')', open);
if (comma == std::string::npos || close == std::string::npos || comma > close)
continue;
std::string length = source.substr(comma + 1, close - comma - 1);
if (length.find("UInt64") != std::string::npos)
continue;
const size_t first = length.find_first_not_of(" \n\t");
length.erase(0, first == std::string::npos ? length.size() : first);
bool ok = length.rfind("rgfxWindowSpanLength(", 0) == 0;
for (const char *w : whole)
ok = ok || length == w || length == std::string(w) + ", true";
if (!ok)
std::fprintf(stderr, "arm64_region_test: %s: a span of \"%s\"\n", path,
length.c_str());
check(ok, "every span a window maps or takes back is whole window pages");
}
}
}
} // namespace
int main()
@@ -64,6 +110,44 @@ int main()
check(rgfxWindowSpan(start + 0x1000000, second, t.blocks[1].size),
"a block is a span a window maps");
// The spans of a surface's block, as the kext lays it out: its holders use `size` bytes, whole
// 4 KiB guest pages, and their windows map those bytes' 16 KiB pages, the runs past them. The
// sizes are two of the blocks a 27.0.1 guest's WindowServer asked for, which the windows
// refused to map as they were (0xe00002c2): a 1920x1080 RGBA16Float plane's and a smaller one.
for (const uint64_t size : {0xfd3000ull, 0x71000ull}) {
const uint64_t length = size;
const uint64_t runs = 4 * sizeof(RgfxPageRun);
const uint64_t total = rgfxSurfaceRunsFrom(size) + rgfxSurfaceRunRoom(length);
RgfxBlock surfaceBlocks[2] = {};
RgfxBlockTable st = {surfaceBlocks, 0, 2, start, start + 0x4000000, 0, 0, 0, 0, 0, 0, 0};
uint64_t at = 0;
check(rgfxBlockAlloc(st, a, 1, total, 0x1000, &at) == kRgfxBlockOk, "the block is made");
const uint64_t span = rgfxWindowSpanLength(size);
check(!rgfxWindowSpan(start + 0x4000000, at, size),
"the bytes the holders use are no span on 16 KiB pages");
check(span % 0x4000 == 0 && span >= size && span - size < 0x4000 &&
rgfxWindowSpan(start + 0x4000000, at, span),
"the span the windows map is those bytes' 16 KiB pages");
check(rgfxSurfaceRunsFrom(size) >= span && rgfxSurfaceRunsFrom(size) + runs <= total &&
total <= st.blocks[0].size,
"the runs lie past the span, in the block");
}
// The scanout's grant: its rows of a mode, whole 16 KiB pages from the region's head, inside
// the scanout's span.
const uint64_t scanout = rgfxWindowSpanLength(7680ull * 1080);
check(scanout == 0x7ec000 && rgfxWindowSpan(0x10000000, RGFX_ALLOCATIONS_OFFSET, scanout) &&
rgfxWindowSpanLength(RGFX_SCANOUT_SPAN) == RGFX_SCANOUT_SPAN,
"a 1920x1080 scanout's grant is a span, as is the largest");
// A block's write-combined split, offered on x86-64 alone, falls on a block page or leaves
// the block whole.
check(rgfxWindowWriteBackPart(0x5000, 0x8000, 0x6000) == 0x8000 &&
rgfxWindowWriteBackPart(0x9000, 0xc000, 0x4000) == 0x4000,
"the write-back part of a block is a span");
// And every caller of mapSpan and unmapSpan passes a span's length.
spansRounded("src/RgfxAcceleratorContexts.cpp");
spansRounded("src/RgfxAcceleratorSurfaces.cpp");
std::printf("arm64_region_test: %d checks passed\n", checks);
return 0;
}
+13
View File
@@ -37,6 +37,19 @@ int main()
check(!rgfxWindowSpan(window, window, 0x1000), "nor one after it");
check(!rgfxWindowSpan(window, 0xfffffffffffff000ull, 0x2000), "nor one that wraps");
check(!rgfxWindowSpan(0, head, 0x1000), "a closed window maps nothing");
// On x86-64 a window page is a guest page, 4 KiB: the span of whole guest pages is those
// pages, and a part of one maps the page it is in.
check(rgfxWindowSpanLength(0x1000) == 0x1000 && rgfxWindowSpanLength(0xfd2000) == 0xfd2000,
"whole 4 KiB pages are their own span");
check(rgfxWindowSpanLength(0x1800) == 0x2000 && rgfxWindowSpanLength(1) == 0x1000,
"a part of a page maps the page");
// The write-combined split of a block: from its byte where that lies in the asked bytes, a
// page's offset; none, the whole block write-back, where it lies in the block's rounding or
// past it.
check(rgfxWindowWriteBackPart(0x3000, 0x3000, 0x1000) == 0x1000, "write-back to the split");
check(rgfxWindowWriteBackPart(0x1800, 0x2000, 0x1900) == 0x2000,
"a split in the rounding leaves the block write-back, a span");
check(rgfxWindowWriteBackPart(0x2000, 0x2000, UINT64_MAX) == 0x2000, "no split, no part");
// xnu charges a mapping of pageable memory that is not purgeable at its whole size, the
// region's, to the process's footprint as the context opens.
check((kRgfxWindowMemoryOptions & kRgfxWindowPageable) != 0, "the window's memory is pageable");