Files
reims-vgpu/guest/kext/tests/pages_block_test.cpp
T
Anees Iqbal 6dbe51cf93 🆕 Wire a process's own pages into a pages block in the kext
The bundle shadows a no-copy buffer over app memory with a region block and
syncs it every commit. With the host mapping a process's pages in place
(PAGES_BUFFERS), the kext now makes the block that names them: PagesBlock,
selector 0x10e, the interface fix-shared-mem's guest-RAM slabs take too.

It takes the memory's address, its bytes and whether the process may write
it, and answers the block's offset and size. The kext wires the pages that
hold the range, rounded out to whole pages, in the caller's task, which
resolves copy-on-write first. It counts their runs in one pass over the
physical segments and writes them in a second straight into a block of the
client's own through a kernel mapping, so no scratch copy is made however
large the range, and the process never maps the block or sees a physical
address. It posts SURFACE_PAGES marked RGFX_SURFACE_PAGES_PRIVATE, waits for
the answer, then grants the block.

The block holds its wired pages: they are unwired once the device answers
its release OK, or once the client's close is answered (freeBlocks takes
each first, so no late release answer unwires it again), and stay wired for
good where neither is answered. A refusal drops the block and unwires at
once; an unanswered SURFACE_PAGES or grant releases the block, so the
device's view goes before the pages do. Read-only memory answers
Unsupported until such pages have a user, as GPU writes would land in pages
the process cannot write.

The framebuffer now takes FEATURES_LO bit 7 where offered, the kext sets its
own PagesBlock bit, a BUFFER_OVER_PAGES submit pins the client's block it
names, and Free skips the window for a block that was never in it.
2026-11-05 21:59:26 -08:00

87 lines
3.6 KiB
C++

// The native test of RgfxPagesBlock.hpp: a pages block's range rounded out to whole pages with
// the call's offset in the first, its runs counted and then written as one per stretch of
// contiguous segments, and its size in whole pages.
#include "RgfxPagesBlock.hpp"
#include <cstdio>
#include <cstdlib>
#include <vector>
namespace {
int checks = 0;
void check(bool ok, const char *what)
{
checks++;
if (!ok) {
std::fprintf(stderr, "pages_block_test: FAILED: %s\n", what);
std::exit(1);
}
}
constexpr uint64_t kPage = 0x1000;
// A wired range's segments: `pages` physical pages in order, each answered as a segment of its
// own from the byte asked to its page's end, as getPhysicalSegment answers across pages that are
// not contiguous.
struct Pages {
std::vector<uint64_t> pages;
uint64_t operator()(uint64_t at, uint64_t *bytes) const
{
const uint64_t i = at / kPage;
if (i >= pages.size() || pages[i] == 0) {
*bytes = 0;
return 0;
}
*bytes = kPage - at % kPage;
return pages[i] + at % kPage;
}
};
} // namespace
int main()
{
RgfxPagesRange r = {};
check(rgfxPagesRange(0x7000'0123, 0x2000, kPage, &r), "a range across three pages");
check(r.base == 0x7000'0000 && r.length == 0x3000 && r.offset == 0x123,
"rounded out, the first byte's offset kept");
check(rgfxPagesRange(0x7000'0000, 0x1000, kPage, &r) && r.base == 0x7000'0000 &&
r.length == 0x1000 && r.offset == 0,
"a page is itself");
check(rgfxPagesRange(0x7000'0fff, 1, kPage, &r) && r.length == 0x1000 && r.offset == 0xfff,
"a page's last byte");
check(!rgfxPagesRange(0x7000'0000, 0, kPage, &r), "an empty range");
check(!rgfxPagesRange(UINT64_MAX - 0x10, 0x20, kPage, &r), "a range that wraps");
check(!rgfxPagesRange(UINT64_MAX - 0x1800, 0x1000, kPage, &r), "one whose last page wraps");
check(!rgfxPagesRange(0x1000, kRgfxPagesBlockMaxBytes + 1, kPage, &r), "one past the most");
check(rgfxPagesRange(0, kRgfxPagesBlockMaxBytes, kPage, &r), "the most");
check(!rgfxPagesRange(0x1000, 0x1000, 0x1800, &r), "a page that is no power of two");
// Pages 0x10 and 0x11 meet, and 0x40, 0x41 and 0x42.
Pages pages = {{0x10'000, 0x11'000, 0x40'000, 0x41'000, 0x42'000}};
const uint64_t length = 5 * kPage;
check(rgfxPagesRuns(length, pages, nullptr, 0) == 2, "counted: two stretches");
pages.pages = {0x10'000, 0x11'000, 0x40'000, 0x30'000, 0x31'000};
check(rgfxPagesRuns(length, pages, nullptr, 0) == 3, "three where the third page meets none");
RgfxPageRun runs[3] = {};
check(rgfxPagesRuns(length, pages, runs, 3) == 3, "written: as many as counted");
check(runs[0].address == 0x10'000 && runs[0].length == 0x2000, "the first stretch");
check(runs[1].address == 0x40'000 && runs[1].length == 0x1000, "the lone page");
check(runs[2].address == 0x30'000 && runs[2].length == 0x2000, "the last stretch");
check(rgfxPagesRuns(length, pages, runs, 2) == 0, "more runs than room: none");
// A range that ends inside a segment takes its bytes alone.
check(rgfxPagesRuns(kPage + 0x10, pages, runs, 3) == 1 && runs[0].length == 0x1010,
"a range ending inside a page");
pages.pages = {0x10'000, 0};
check(rgfxPagesRuns(2 * kPage, pages, nullptr, 0) == 0, "a page with no segment: none");
check(rgfxPagesRuns(0, pages, nullptr, 0) == 0, "no bytes, no runs");
check(rgfxPagesBlockSize(1, kPage) == kPage, "one run takes a page");
check(rgfxPagesBlockSize(256, kPage) == kPage, "256 runs fill one");
check(rgfxPagesBlockSize(257, kPage) == 2 * kPage, "257 take two");
check(rgfxPagesBlockSize(1u << 24, kPage) == (1ull << 28), "a 64 GiB range of lone pages");
std::printf("pages_block_test: OK (%d checks)\n", checks);
return 0;
}