diff --git a/docs/graphics-kext/design.md b/docs/graphics-kext/design.md index b2b9e96b6..00ff16880 100644 --- a/docs/graphics-kext/design.md +++ b/docs/graphics-kext/design.md @@ -122,23 +122,19 @@ Stages: - The BAR is one host memory region that the device reserves at start and registers with KVM once. Blocks are ranges inside it, so making or freeing a block never changes the VM's memory map. It is ordinary - cached memory on the host, but the guest's kernel maps it uncached - whatever the kext asks: xnu's pmap sets PCD on every page it does not - manage (its PAT's UC-), and the region is no RAM of the guest's. An AMD - host applies the guest's memory type, where an Intel host's EPT ignores - it: measured on the Radeon 780M host, the guest's stores there ran at - 13 MB/s and its loads at 8 to 180 MB/s, against 5.6 to 123 GB/s in its - own RAM. Uncached loads cost 79-138 ns against 2-4 ns cached. So what - the CPU touches most lies in the process's own pages instead (its Shared - and Managed buffers' slabs and its upload chunks, "Managed storage"), - and what the process only writes takes a write-combined part of its - block, which the kext maps so (a submission ring's data, command - buffers' bodies and a library's staging chunks; the bundle's - `RgfxRegion.hpp`). The kext maps those parts from a descriptor of the - region of their own: a mapping's cache mode is set on its descriptor's - one VM object, for every page of it, so asked of the region's own - descriptor it came out uncached like the rest (measured: no page of the - process's was write-combined; `RgfxAccelerator::copyWriteCombinedRegion`). + cached memory on the host, but every mapping of it in a guest process + is uncached, whatever cache mode the kext asks: xnu's fault path enters + a page with no cache flags, and its pmap gives a page it does not + manage PCD (the guest PAT's UC-; read from the public xnu source, + `pmap_get_cache_attributes` and `vm_fault`'s `pmap_enter` wrappers, and + measured: every page of a process's over BAR 2 was PCD alone, asked + write-back or write-combined). An AMD host applies the guest's memory + type, where an Intel host's EPT ignores it: measured on the Radeon 780M + host, the guest's stores there ran at 13 MB/s and its loads at 8 to + 180 MB/s, against 5.6 to 123 GB/s in its own RAM. Uncached loads cost + 79-138 ns against 2-4 ns cached. So what the CPU touches most lies in + the process's own pages instead: its Shared and Managed buffers' slabs + and its upload chunks ("Managed storage"). - The region is memory of QEMU's process, and the host side runs in that process. On Linux it is a shared mapping of a memfd of the library's (`reims-vgpu-vulkan`'s `RegionMemory`): RADV refuses a pointer to a @@ -3026,7 +3022,7 @@ the kext and the bundle include. | doorbell 0 | `Doorbell` | | writes a ring's id to `DOORBELL` | | doorbell 1 | `Consumed` | 1 << 3 `ConsumedDoorbell` | reserved: a completion ring's room doorbell (`CONSUMED`) | | 0x100 | `OpenContext` | 1 << 5 `ContextSpan` (its sixth output) | the context and its window; five outputs, or six with its GPU addresses' span | -| 0x101 | `Alloc` | 1 << 7 `WriteCombined` (its third input) | a region block; 2 inputs, or 3 with the byte its write-combined part starts at | +| 0x101 | `Alloc` | | a region block | | 0x102 | `Free` | | frees a block | | 0x103 | `QueueCreate` | | a command queue; 6 inputs, or 7 with the flags wanted | | 0x104 | `QueueDestroy` | | closes a queue | @@ -3052,7 +3048,7 @@ the accelerator, which the bundle opens for each event it hands out: 0x113 and 0x114 are its own selectors, numbered past the accelerator's own so that a call on a connection of another kind finds none of that number there. -The kext sets 1 << 0 `PagesBlock`, 1 << 4 `DeferredAnswers`, 1 << 5 `ContextSpan`, 1 << 6 `SharedEvents` and 1 << 7 `WriteCombined`; no other bit yet. +The kext sets 1 << 0 `PagesBlock`, 1 << 4 `DeferredAnswers`, 1 << 5 `ContextSpan` and 1 << 6 `SharedEvents`; no other bit yet. - **Boot-gated — base class / kernel family (notes UNKNOWN #1).** The scaffold subclasses `IOAccelerator`, the base the macOS SDK and KDK expose (the IOGPU diff --git a/guest/bundle/src/RgfxLibraryStream.cpp b/guest/bundle/src/RgfxLibraryStream.cpp index a023cc358..a8d4aa929 100644 --- a/guest/bundle/src/RgfxLibraryStream.cpp +++ b/guest/bundle/src/RgfxLibraryStream.cpp @@ -69,10 +69,7 @@ bool makeWindow(RgfxRegion ®ion, uint64_t length, Window *w) if (s.bytes == lastBytes) continue; // a library smaller than the chunk: the same window again lastBytes = s.bytes; - // The chunks, which the process only writes, past the acks' page, which it reads: - // write-combined where the kext maps parts so. - RgfxRegion::Block b = - region.allocate(s.bytes, RgfxRegion::kDefaultBlockAlign, kRgfxStagingPage); + RgfxRegion::Block b = region.allocate(s.bytes); if (!b.ok) continue; w->offset = b.offset; diff --git a/guest/bundle/src/RgfxMetalDevice+CommandBuffer.mm b/guest/bundle/src/RgfxMetalDevice+CommandBuffer.mm index 25af49196..be8ba1f29 100644 --- a/guest/bundle/src/RgfxMetalDevice+CommandBuffer.mm +++ b/guest/bundle/src/RgfxMetalDevice+CommandBuffer.mm @@ -60,8 +60,8 @@ constexpr uint32_t kQueueCompleteDataSize = 32768; return nil; // the region had no room for the ring pair } auto releaseRings = [region, rings] { - region->release(rings.submit_block); - region->release(rings.complete_block); + region->release(rings.submit_offset); + region->release(rings.complete_offset); }; RgfxRingWriter *submit = [self addQueueSubmitRingAtOffset:rings.submit_offset dataSize:kQueueSubmitDataSize]; diff --git a/guest/bundle/src/RgfxMetalDevice.mm b/guest/bundle/src/RgfxMetalDevice.mm index 7a866b716..c01a158b2 100644 --- a/guest/bundle/src/RgfxMetalDevice.mm +++ b/guest/bundle/src/RgfxMetalDevice.mm @@ -213,19 +213,6 @@ bool rgfxKextAlloc(void *context, uint64_t size, uint64_t align, uint64_t *offse KERN_SUCCESS; } -// The region's write-combined source (RgfxRegion::setWriteCombinedSource): the kext's Alloc with -// its third input, the byte the block's write-combined part starts at -// (kRgfxKextFeatureWriteCombined). -bool rgfxKextAllocWriteCombined(void *context, uint64_t size, uint64_t align, - uint64_t writeCombinedFrom, uint64_t *offset) -{ - io_connect_t conn = static_cast(reinterpret_cast(context)); - uint64_t in[3] = {size, align, writeCombinedFrom}; - uint32_t outCount = 1; - return IOConnectCallScalarMethod(conn, kRgfxAccelMethodAlloc, in, 3, offset, &outCount) == - KERN_SUCCESS; -} - // What the process keeps for reuse, as RgfxRegion's reclaim; `context` is the RgfxSlabs: the // slabs' empty and cached blocks, and the IOSurface views the keep holds, a cache that costs no // refusal (a texture over one of their surfaces then asks the kext for its block, as with no @@ -712,10 +699,6 @@ RGFX_LOG_UNRECOGNIZED : RGFX_RESOURCE_ID_CHUNK); _region = new RgfxRegion(base, mapSize); _region->setBlockSource(&rgfxKextAlloc, &rgfxKextRelease, connContext); - // The bytes the process only writes in write-combined parts of their blocks, where the kext - // maps them so (RgfxRegion.hpp, WRITE-COMBINED PARTS). - if ((_caps.kextFeatures() & kRgfxKextFeatureWriteCombined) != 0) - _region->setWriteCombinedSource(&rgfxKextAllocWriteCombined); _region->setSurfaceSource(&rgfxKextSurfaceBlock, &rgfxKextSurfaceRelease, connContext); _region->setSurfacePagesSource(&rgfxKextSurfacePages, _caps.linearTextureAlignment(), connContext); diff --git a/guest/bundle/src/RgfxRegion.cpp b/guest/bundle/src/RgfxRegion.cpp index e8fa9d22c..4c352fdd8 100644 --- a/guest/bundle/src/RgfxRegion.cpp +++ b/guest/bundle/src/RgfxRegion.cpp @@ -42,12 +42,6 @@ void RgfxRegion::setBlockSource(AllocFn alloc, ReleaseFn release, void *context) m_sourceContext = context; } -void RgfxRegion::setWriteCombinedSource(WriteCombinedAllocFn alloc) -{ - std::lock_guard guard(m_lock); - m_writeCombinedAlloc = alloc; -} - void RgfxRegion::setReclaim(ReclaimFn reclaim, void *context) { std::lock_guard guard(m_lock); @@ -189,11 +183,6 @@ void RgfxRegion::release(uint64_t offset) } RgfxRegion::Block RgfxRegion::allocate(uint64_t size, uint64_t align) -{ - return allocate(size, align, UINT64_MAX); -} - -RgfxRegion::Block RgfxRegion::allocate(uint64_t size, uint64_t align, uint64_t writeCombinedFrom) { Block out = {}; if (!isPowerOfTwo(align)) { @@ -202,13 +191,8 @@ RgfxRegion::Block RgfxRegion::allocate(uint64_t size, uint64_t align, uint64_t w if (m_alloc != nullptr) { uint64_t offset = 0; - const bool combined = m_writeCombinedAlloc != nullptr && writeCombinedFrom < size; - auto ask = [&] { - return combined ? m_writeCombinedAlloc(m_sourceContext, size, align, - writeCombinedFrom, &offset) - : m_alloc(m_sourceContext, size, align, &offset); - }; - const bool placed = ask() || (reclaimed() && ask()); + const bool placed = m_alloc(m_sourceContext, size, align, &offset) || + (reclaimed() && m_alloc(m_sourceContext, size, align, &offset)); if (!placed || offset < m_floor || offset > m_size || size > m_size - offset) { return out; // no room; ok == false } @@ -249,15 +233,7 @@ RgfxRegion::RingPair RgfxRegion::allocateRingPair(uint32_t submit_data_size, return out; // ok == false } - // Where the kext maps part of a block write-combined, the submission ring's data takes it, - // from its block's second page, and the ring's header, which the host writes too and the - // writer reads, ends the first page, write-back (WRITE-COMBINED PARTS). - const bool combined = writeCombines(); - const uint64_t head = combined ? kDefaultBlockAlign - RGFX_RING_HEADER_SIZE : 0; - Block submit = combined - ? allocate(kDefaultBlockAlign + submit_data_size, kDefaultBlockAlign, - kDefaultBlockAlign) - : allocate(RGFX_RING_HEADER_SIZE + submit_data_size, kRingAlign); + Block submit = allocate(RGFX_RING_HEADER_SIZE + submit_data_size, kRingAlign); if (!submit.ok) { return out; } @@ -265,23 +241,20 @@ RgfxRegion::RingPair RgfxRegion::allocateRingPair(uint32_t submit_data_size, if (complete_data_size != 0) { complete = allocate(RGFX_RING_HEADER_SIZE + complete_data_size, kRingAlign); if (!complete.ok) { - release(submit.offset); return out; } } // Zero each ring's header + byte ring so a fresh RgfxRingWriter (producer 0, // consumer 0, consumer_idle 0, armed 0) starts clean over BAR memory whose - // contents are otherwise undefined. - std::memset(const_cast(submit.ptr + head), 0, submit.size - head); + // contents are otherwise undefined. The region is plain write-back memory. + std::memset(const_cast(submit.ptr), 0, submit.size); if (complete_data_size != 0) { std::memset(const_cast(complete.ptr), 0, complete.size); } - out.submit_offset = submit.offset + head; + out.submit_offset = submit.offset; out.complete_offset = complete_data_size != 0 ? complete.offset : 0; - out.submit_block = submit.offset; - out.complete_block = out.complete_offset; out.ok = true; return out; } diff --git a/guest/bundle/src/RgfxRegion.hpp b/guest/bundle/src/RgfxRegion.hpp index 88a3ca36d..1c100ba64 100644 --- a/guest/bundle/src/RgfxRegion.hpp +++ b/guest/bundle/src/RgfxRegion.hpp @@ -26,18 +26,6 @@ * aligned to a cache line so the ring header's 64-byte lines never share one; resource * blocks default to a page so a block can later back a mapping. * - * WRITE-COMBINED PARTS. The region is no RAM of the guest's, so the kernel maps it uncached - * whatever the kext asks for write-back (xnu's pmap sets PCD on every page it does not manage), - * and an AMD host applies the guest's memory type: plain stores there cost about 0.3 us per - * 4 bytes. Where the kext maps part of a block write-combined (setWriteCombinedSource, - * kRgfxKextFeatureWriteCombined), the bytes the process only writes take that part: a submission - * ring's data (allocateRingPair, its header alone in the page before it), command buffers' bodies - * and a library's staging chunks (allocate's `writeCombinedFrom`). Write-combined stores are - * weakly ordered: the ring writer fences them (sfence) before it publishes a message that names - * them, and a lock or a fence orders them for another thread, as for any store. The process never - * reads them back, nor takes an atomic there: loads there are uncached, and a locked operation on - * write-combined memory is not atomic. - * * Our own code; depends only on the committed wire header. In a unit test the region * is a plain buffer; in the bundle it is the context's window. */ @@ -73,10 +61,6 @@ public: uint32_t submit_data_size; uint32_t complete_data_size; bool ok; - /// The blocks the rings lie in, which release() takes: a ring's own offset, but for a - /// submission ring whose data is write-combined, which starts its block's second page. - uint64_t submit_block; - uint64_t complete_block; }; /// The allocator over the region whose CPU base is `base`, `size` bytes long. @@ -104,11 +88,6 @@ public: /// `ok` is false when `align` is not a power of two or the region has no room. Block allocate(uint64_t size, uint64_t align = kDefaultBlockAlign); - /// As allocate, with the block's bytes from `writeCombinedFrom` (a multiple of - /// kDefaultBlockAlign) mapped write-combined where a write-combined source is set - /// (WRITE-COMBINED PARTS); the whole block write-back otherwise. - Block allocate(uint64_t size, uint64_t align, uint64_t writeCombinedFrom); - /// Hands back the block at region offset `offset` (one allocate() returned) to the block /// source; nothing without one. void release(uint64_t offset); @@ -120,16 +99,6 @@ public: typedef void (*ReleaseFn)(void *context, uint64_t offset); void setBlockSource(AllocFn alloc, ReleaseFn release, void *context); - /// Where blocks part of which is write-combined come from (WRITE-COMBINED PARTS): as the - /// block source's `alloc`, with the bytes from `writeCombinedFrom` mapped write-combined. - /// Called with the block source's context; its blocks go back through its `release`. - typedef bool (*WriteCombinedAllocFn)(void *context, uint64_t size, uint64_t align, - uint64_t writeCombinedFrom, uint64_t *offset); - void setWriteCombinedSource(WriteCombinedAllocFn alloc); - - /// Whether a write-combined source is set. - bool writeCombines() const { return m_writeCombinedAlloc != nullptr; } - /// What the process keeps for reuse and can give back where the kext has no room: `reclaim` /// lets go of such blocks (the buffers' empty and cached slabs, RgfxSlabs.hpp, and the /// IOSurface views the keep holds, RgfxIOSurface.h) and answers whether it let any go, and @@ -275,7 +244,6 @@ private: uint64_t m_floor; // RGFX_ALLOCATIONS_OFFSET uint64_t m_cursor; // the next free offset, at or above the floor AllocFn m_alloc = nullptr; // the block source, where set (the kext) - WriteCombinedAllocFn m_writeCombinedAlloc = nullptr; // the write-combined source (the kext) ReleaseFn m_release = nullptr; void *m_sourceContext = nullptr; ReclaimFn m_reclaim = nullptr; // the process's blocks kept for reuse, where set (the slabs) diff --git a/guest/bundle/src/RgfxRingWriter.cpp b/guest/bundle/src/RgfxRingWriter.cpp index eb6b3302b..f25ead948 100644 --- a/guest/bundle/src/RgfxRingWriter.cpp +++ b/guest/bundle/src/RgfxRingWriter.cpp @@ -57,22 +57,9 @@ void fullFence() __atomic_thread_fence(__ATOMIC_SEQ_CST); } -// Orders every store before it, write-combined ones among them, before every store after it: -// the ring's data and the bodies and chunks a message names may lie in write-combined parts of -// their blocks (RgfxRegion.hpp, WRITE-COMBINED PARTS), whose stores x86 does not keep in order -// with others, as a release store does for write-back memory. -void storeFence() -{ -#if defined(__x86_64__) || defined(__i386__) - __builtin_ia32_sfence(); -#else - __atomic_thread_fence(__ATOMIC_RELEASE); -#endif -} - // Copies `len` bytes to the ring at byte offset `at` (already within the data -// area, no wrap). A plain copy: storeFence orders it before the release store of -// `producer`, so the host reads it whole. +// area, no wrap). The region is write-back memory; a plain copy is right, and +// the release store of `producer` orders it before the host reads it. void writeBytes(volatile uint8_t *data, uint32_t at, const void *src, uint32_t len) { std::memcpy(const_cast(data + at), src, len); @@ -131,7 +118,6 @@ bool RgfxRingWriter::tryPostKnown(uint32_t type, const void *message, uint32_t s std::memcpy(buf.get(), message, size); RgfxMsgHeader header = { type, size, 0 }; std::memcpy(buf.get(), &header, sizeof(header)); - storeFence(); uint64_t seqno = 0; if (!m_deferredPost(m_deferredContext, buf.get(), size, &seqno, out_deferred)) return false; @@ -235,8 +221,6 @@ bool RgfxRingWriter::tryPost(uint32_t type, const void *message, uint32_t size, std::memcpy(buf, message, size); RgfxMsgHeader header = { type, size, 0 }; std::memcpy(buf, &header, sizeof(header)); - // What the message names (a library's staging chunk) is written before the kext posts it. - storeFence(); uint64_t seqno = 0; if (!m_forward(m_forwardContext, buf, size, &seqno, out_status)) return false; @@ -276,7 +260,6 @@ bool RgfxRingWriter::tryPost(uint32_t type, const void *message, uint32_t size, if (beforePublish) beforePublish(seqno); - storeFence(); store64(m_header + kProducer, m_produced); fullFence(); if (m_doorbell && load32(m_header + kConsumerIdle) == 1) diff --git a/guest/bundle/src/RgfxUploads.mm b/guest/bundle/src/RgfxUploads.mm index 24f028e46..db2723981 100644 --- a/guest/bundle/src/RgfxUploads.mm +++ b/guest/bundle/src/RgfxUploads.mm @@ -150,8 +150,7 @@ bool RgfxBodyPool::take(uint64_t size, RgfxBodyBlock *block) if (m_region == nullptr) { return false; } - // Written only, never read back here: write-combined where the kext maps parts so. - RgfxRegion::Block made = m_region->allocate(bytes, RgfxRegion::kDefaultBlockAlign, 0); + RgfxRegion::Block made = m_region->allocate(bytes); if (!made.ok) { return false; } diff --git a/guest/bundle/tests/serializer_tests.cpp b/guest/bundle/tests/serializer_tests.cpp index 1eba6db0d..0156169b6 100644 --- a/guest/bundle/tests/serializer_tests.cpp +++ b/guest/bundle/tests/serializer_tests.cpp @@ -2844,86 +2844,11 @@ void test_region_pages_blocks() CHECK(src.released.size() == 1 && src.released[0] == mem.size() - 0x800); } -// Where the kext maps parts of blocks write-combined, a block asked with a write-combined part -// takes it from the write-combined source, and one asked without from the block source; a queue's -// submission ring's data starts its block's second page, write-combined, with the ring's header -// ending the first, and the pair names the blocks to release. Without the source every block is -// the block source's, the rings at their blocks' starts. -void test_region_write_combined_parts() -{ - std::vector mem(RGFX_ALLOCATIONS_OFFSET + 0x100000, 0xee); - struct Source { - uint64_t next = RGFX_ALLOCATIONS_OFFSET; - std::vector combinedFrom; // UINT64_MAX for a plain block - std::vector released; - bool place(uint64_t size, uint64_t align, uint64_t from, uint64_t *offset) - { - next = (next + align - 1) & ~(align - 1); - *offset = next; - next += size; - combinedFrom.push_back(from); - return true; - } - static bool alloc(void *c, uint64_t size, uint64_t align, uint64_t *offset) - { - return static_cast(c)->place(size, align, UINT64_MAX, offset); - } - static bool combined(void *c, uint64_t size, uint64_t align, uint64_t from, - uint64_t *offset) - { - return static_cast(c)->place(size, align, from, offset); - } - static void release(void *c, uint64_t offset) - { - static_cast(c)->released.push_back(offset); - } - }; - { - RgfxRegion region(mem.data(), mem.size()); - Source src; - region.setBlockSource(&Source::alloc, &Source::release, &src); - CHECK(!region.writeCombines()); - RgfxRegion::Block b = region.allocate(0x2000, 0x1000, 0); - CHECK(b.ok && src.combinedFrom == std::vector({UINT64_MAX})); - RgfxRegion::RingPair rings = region.allocateRingPair(0x8000, 0x8000); - CHECK(rings.ok && rings.submit_offset == rings.submit_block); - CHECK(rings.complete_offset == rings.complete_block && rings.complete_offset != 0); - } - std::fill(mem.begin(), mem.end(), 0xee); // the rings above zeroed theirs - RgfxRegion region(mem.data(), mem.size()); - Source src; - region.setBlockSource(&Source::alloc, &Source::release, &src); - region.setWriteCombinedSource(&Source::combined); - CHECK(region.writeCombines()); - RgfxRegion::Block plain = region.allocate(0x2000); - RgfxRegion::Block whole = region.allocate(0x2000, 0x1000, 0); - RgfxRegion::Block tail = region.allocate(0x3000, 0x1000, 0x1000); - RgfxRegion::Block past = region.allocate(0x1000, 0x1000, 0x1000); - CHECK(plain.ok && whole.ok && tail.ok && past.ok); - CHECK(src.combinedFrom == - std::vector({UINT64_MAX, 0, 0x1000, UINT64_MAX})); - src.combinedFrom.clear(); - RgfxRegion::RingPair rings = region.allocateRingPair(0x8000, 0x8000); - CHECK(rings.ok && rings.submit_data_size == 0x8000); - CHECK(src.combinedFrom == std::vector({0x1000, UINT64_MAX})); - CHECK(rings.submit_block % 0x1000 == 0); - CHECK(rings.submit_offset == rings.submit_block + 0x1000 - RGFX_RING_HEADER_SIZE); - CHECK((rings.submit_offset + RGFX_RING_HEADER_SIZE) % 0x1000 == 0); - CHECK(rings.submit_offset % RgfxRegion::kRingAlign == 0); - CHECK(rings.complete_block == rings.complete_offset); - // The ring's header and data are zeroed; the head of the block before them is not touched. - bool zeroed = true; - for (uint64_t i = 0; i < RGFX_RING_HEADER_SIZE + 0x8000; i++) - zeroed = zeroed && mem[rings.submit_offset + i] == 0; - CHECK(zeroed && mem[rings.submit_block] == 0xee); -} - int main() { test_id_space(); test_obj_ids_are_given_once(); test_region_pages_blocks(); - test_region_write_combined_parts(); test_address_picks(); test_resource_id_chunks(); test_small_resource_id_chunks(); diff --git a/guest/kext/src/RgfxAccelerator.hpp b/guest/kext/src/RgfxAccelerator.hpp index 221a0bad1..d5bac4ae4 100644 --- a/guest/kext/src/RgfxAccelerator.hpp +++ b/guest/kext/src/RgfxAccelerator.hpp @@ -85,13 +85,12 @@ enum { // The kext's own features (kRgfxKextFeature*, RgfxUserClientABI.hpp) this kext carries out, the // second word kRgfxAccelMethodFeatures answers: PagesBlock, Submit's deferred flag and -// TakeAnswers, OpenContext's span, shared events' ports, and Alloc's write-combined part. Each -// bit is set by the change that lands its call. +// TakeAnswers, OpenContext's span, and shared events' ports. Each bit is set by the change that +// lands its call. constexpr uint64_t kRgfxKextFeatureWord = kRgfxKextFeaturePagesBlock | kRgfxKextFeatureDeferredAnswers | kRgfxKextFeatureContextSpan | - kRgfxKextFeatureSharedEvents | - kRgfxKextFeatureWriteCombined; + kRgfxKextFeatureSharedEvents; class RgfxAccelerator; class RgfxAcceleratorUserClient; @@ -289,16 +288,6 @@ public: // client's task (retained for the caller), and the doorbell write that wakes the // host's consumer of ring `ringId`. IODeviceMemory *copyRegion() const; - // The region again, as a descriptor of its own that only write-combined spans are mapped - // from (retained for the caller; null before the contexts' start, or where it could not be - // made). A mapping's cache mode is set on its descriptor's one VM object for every page of - // it (xnu's IOKit hands the mode to mach_make_memory_entry with MAP_MEM_ONLY, which changes - // the object's WIMG bits; read from the public xnu source), so a write-combined span mapped - // from the region's own descriptor would leave each region page faulted after it - // write-combined, and the next write-back span would take them all back: measured on the - // Radeon 780M host, no page of the process's came out write-combined. Defined with the - // contexts' state (RgfxAcceleratorContexts.cpp). - IODeviceMemory *copyWriteCombinedRegion() const; // The region's first `size` bytes from RGFX_DEVICE_INFO_OFFSET (the device info, and the // video caps after it), mapped read-only into the kernel: the map, which the caller // releases, with *info its first byte; null where it cannot be mapped. @@ -374,11 +363,10 @@ public: // is kIOReturnBusy while a create naming it is in flight (pinRange). freeBlocks frees the // owner's blocks after its close is answered; keepBlocks keeps them from use for good after // a close that was not. - // allocBlock maps the block into the client's window before it returns, its bytes from - // `writeCombinedFrom` on write-combined (none where that is past the block); freeBlock takes - // it back from the window before the block is freed. + // allocBlock maps the block into the client's window before it returns; freeBlock takes it + // back from the window before the block is freed. IOReturn allocBlock(RgfxAcceleratorUserClient *owner, UInt32 context, UInt64 size, - UInt64 align, UInt64 writeCombinedFrom, UInt64 *offset); + UInt64 align, UInt64 *offset); IOReturn freeBlock(RgfxAcceleratorUserClient *owner, UInt64 offset); // A pages block (kRgfxAccelMethodPagesBlock) of `owner` of context `context` over the // `length` bytes at `address` in `task`, `writable` there: the block's offset and size. The diff --git a/guest/kext/src/RgfxAcceleratorContexts.cpp b/guest/kext/src/RgfxAcceleratorContexts.cpp index 486ac2f54..baad5b83d 100644 --- a/guest/kext/src/RgfxAcceleratorContexts.cpp +++ b/guest/kext/src/RgfxAcceleratorContexts.cpp @@ -137,9 +137,6 @@ struct ContextState { bool hooked; // The thread call that posts freed blocks' releases (releaseFreedBlocks). thread_call_t releaseCall; - // The region again, a descriptor of its own that only write-combined spans are mapped from - // (copyWriteCombinedRegion): null where it could not be made. - IODeviceMemory *combinedRegion; }; ContextState state = {nullptr, kRgfxContextSlotsEmpty, @@ -151,7 +148,6 @@ ContextState state = {nullptr, nullptr, 0, false, - nullptr, nullptr}; IOLock *&contextLock = state.contextLock; RgfxContextSlots &slots = state.slots; @@ -685,12 +681,6 @@ bool RgfxAccelerator::startContexts() RgfxReleaseEntry *releases = static_cast(IOMalloc(sizeof(RgfxReleaseEntry) * kMaxReleases)); thread_call_t timer = thread_call_allocate(releaseTimerFired, nullptr); - // The region's bytes again for the write-combined spans (copyWriteCombinedRegion), with no - // effect on the region's own descriptor; a context goes on without it. - IODeviceMemory *combined = - region ? IODeviceMemory::withRange(region->getPhysicalSegment(0, nullptr, kIOMemoryMapperNone), - region->getLength()) - : nullptr; RgfxBlock *expected = nullptr; if (lock && wlock && table && rings && call && naming && rlock && releases && timer && __atomic_compare_exchange_n(&blocks, &expected, table, false, __ATOMIC_ACQ_REL, @@ -704,7 +694,6 @@ bool RgfxAccelerator::startContexts() namingCall = naming; releaseTable = {releases, kMaxReleases, 0}; releaseTimer = timer; - state.combinedRegion = combined; __atomic_store_n(&releaseLock, rlock, __ATOMIC_RELEASE); const UInt32 features = readRegister(RGFX_REG_FEATURES_LO); surfacePages = (features & RGFX_FEATURE_SURFACE_PAGES) != 0; @@ -775,23 +764,12 @@ bool RgfxAccelerator::startContexts() IOFree(releases, sizeof(RgfxReleaseEntry) * kMaxReleases); if (timer) thread_call_free(timer); - OSSafeReleaseNULL(combined); // Another opener is making it: wait for it to publish. while (expected && !__atomic_load_n(&contextLock, __ATOMIC_ACQUIRE)) IOSleep(1); return __atomic_load_n(&contextLock, __ATOMIC_ACQUIRE) != nullptr; } -IODeviceMemory *RgfxAccelerator::copyWriteCombinedRegion() const -{ - // Set before contextLock is published, and kept for the kernel's life. - IODeviceMemory *combined = - __atomic_load_n(&contextLock, __ATOMIC_ACQUIRE) ? state.combinedRegion : nullptr; - if (combined) - combined->retain(); - return combined; -} - // MARK: - Contexts IOReturn RgfxAccelerator::openContext(UInt32 *context, bool oldSpan, bool windowServer, @@ -977,13 +955,10 @@ void RgfxAccelerator::keepIdChunks(const void *owner) // MARK: - Blocks IOReturn RgfxAccelerator::allocBlock(RgfxAcceleratorUserClient *owner, UInt32 context, - UInt64 size, UInt64 align, UInt64 writeCombinedFrom, - UInt64 *offset) + UInt64 size, UInt64 align, UInt64 *offset) { if (!contextLock) return kIOReturnBadArgument; - if (writeCombinedFrom < size && writeCombinedFrom % kRgfxBlockPage != 0) - return kIOReturnBadArgument; const bool mayReserve = owner->isWindowServer(); IOLockLock(contextLock); RgfxBlockPin pin = {}; @@ -997,17 +972,8 @@ IOReturn RgfxAccelerator::allocBlock(RgfxAcceleratorUserClient *owner, UInt32 co const UInt64 bytes = rgfxBlockAlignUp(size, kRgfxBlockPage); // the block's, as allocated // The block is in the process's window, and the device learns of it, before the // process does: the grant is on the control ring before any message naming it, on that - // ring or a queue's. The block is pinned until then, so no free overtakes either. Its - // bytes from `writeCombinedFrom` are the ones the process only writes (a submission - // ring's data, command buffers' bodies, a library's staging chunks), mapped - // write-combined: an AMD host applies the guest's memory type, and the region's pages, - // 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; - IOReturn posted = combined ? owner->mapSpan(pin.offset, combined) : kIOReturnSuccess; - if (posted == kIOReturnSuccess && combined < bytes) - posted = owner->mapSpan(pin.offset + combined, bytes - combined, true); + // ring or a queue's. The block is pinned until then, so no free overtakes either. + IOReturn posted = owner->mapSpan(pin.offset, bytes); if (posted == kIOReturnSuccess) { RgfxBlockGrant grant = {}; grant.context = context; diff --git a/guest/kext/src/RgfxAcceleratorUserClient.hpp b/guest/kext/src/RgfxAcceleratorUserClient.hpp index 224eda71d..4875db33e 100644 --- a/guest/kext/src/RgfxAcceleratorUserClient.hpp +++ b/guest/kext/src/RgfxAcceleratorUserClient.hpp @@ -57,11 +57,8 @@ public: // bytes back over the span, read-only, before the span can be anyone else's. Any thread; the // window's lock alone, which a caller may take under the accelerator's context or surface // lock. mapSpan fails, the op it serves with it, where the window is closed or the span is not - // one; an unmapSpan that cannot map the window back closes the window. The span is mapped - // write-back, or write-combined where `writeCombined` (the bytes the process only writes, - // allocBlock's), from the region's descriptor for those spans - // (RgfxAccelerator::copyWriteCombinedRegion), and write-back where that is missing. - IOReturn mapSpan(UInt64 offset, UInt64 length, bool writeCombined = false); + // one; an unmapSpan that cannot map the window back closes the window. + IOReturn mapSpan(UInt64 offset, UInt64 length); void unmapSpan(UInt64 offset, UInt64 length); // Whether the client's task is WindowServer (initWithTask): its blocks may take the reserve diff --git a/guest/kext/src/RgfxAcceleratorUserClientContexts.cpp b/guest/kext/src/RgfxAcceleratorUserClientContexts.cpp index a0b6ad431..45009d61a 100644 --- a/guest/kext/src/RgfxAcceleratorUserClientContexts.cpp +++ b/guest/kext/src/RgfxAcceleratorUserClientContexts.cpp @@ -108,8 +108,7 @@ IOReturn RgfxAcceleratorUserClient::contextMethod(uint32_t selector, return r; } case kRgfxAccelMethodAlloc: { - // Two inputs, or a third of the block's write-combined part (kRgfxKextFeatureWriteCombined). - if ((a->scalarInputCount != 2 && a->scalarInputCount != 3) || a->scalarOutputCount != 1) + if (a->scalarInputCount != 2 || a->scalarOutputCount != 1) return kIOReturnBadArgument; // An ask, so no block is made for a context releaseContext has closed. UInt32 ctx = 0; @@ -117,8 +116,7 @@ IOReturn RgfxAcceleratorUserClient::contextMethod(uint32_t selector, if (!ask.begun) return kIOReturnBadArgument; UInt64 offset = 0; - const UInt64 writeCombinedFrom = a->scalarInputCount == 3 ? in[2] : UINT64_MAX; - IOReturn r = owner->allocBlock(this, ctx, in[0], in[1], writeCombinedFrom, &offset); + IOReturn r = owner->allocBlock(this, ctx, in[0], in[1], &offset); out[0] = offset; return r; } diff --git a/guest/kext/src/RgfxAcceleratorUserClientWindow.cpp b/guest/kext/src/RgfxAcceleratorUserClientWindow.cpp index b2c30bca0..f7b585ffe 100644 --- a/guest/kext/src/RgfxAcceleratorUserClientWindow.cpp +++ b/guest/kext/src/RgfxAcceleratorUserClientWindow.cpp @@ -105,18 +105,11 @@ void RgfxAcceleratorUserClient::closeWindow() context, n, ns / 1000, r, rns / 1000); } -IOReturn RgfxAcceleratorUserClient::mapSpan(UInt64 offset, UInt64 length, bool writeCombined) +IOReturn RgfxAcceleratorUserClient::mapSpan(UInt64 offset, UInt64 length) { if (!windowLock) return kIOReturnNotReady; - // A write-combined span from the region's descriptor of its own, and every other span from - // the region's (RgfxAccelerator::copyWriteCombinedRegion says why); where the first is - // missing, the span is mapped write-back. - IODeviceMemory *region = - owner && writeCombined ? owner->copyWriteCombinedRegion() : nullptr; - const bool combined = region != nullptr; - if (!region) - region = owner ? owner->copyRegion() : nullptr; + IODeviceMemory *region = owner ? owner->copyRegion() : nullptr; if (!region) return kIOReturnNotReady; IOReturn r = kIOReturnSuccess; @@ -127,12 +120,10 @@ IOReturn RgfxAcceleratorUserClient::mapSpan(UInt64 offset, UInt64 length, bool w r = kIOReturnBadArgument; } else { const UInt64 start = mach_absolute_time(); - // The region's bytes over the window's: write-back as asked, which xnu's pmap maps - // uncached for a page it does not manage (PCD, the guest PAT's UC-), or write-combined - // (PCD and PAT, the guest PAT's WC). - const IOOptionBits cache = combined ? kIOMapWriteCombineCache : kIOMapCopybackCache; + // The region's bytes, write-back as the kernel maps them, over the window's. IOMemoryMap *map = region->createMappingInTask(task, windowBase + offset, - kIOMapOverwrite | cache, offset, length); + kIOMapOverwrite | kIOMapCopybackCache, + offset, length); if (map) { // The mapping stays: an overwrite mapping is never taken down by its release. map->release(); diff --git a/guest/kext/src/RgfxUserClientABI.hpp b/guest/kext/src/RgfxUserClientABI.hpp index 9514ff421..74e7eedc2 100644 --- a/guest/kext/src/RgfxUserClientABI.hpp +++ b/guest/kext/src/RgfxUserClientABI.hpp @@ -44,11 +44,7 @@ enum { // asks for five holds RGFX_CONTEXT_VA_SPAN from the base. kIOReturnNoResources where the // kext has no range left (RgfxContextSlots.hpp). kRgfxAccelMethodOpenContext = 0x100, - // A region block: in size, align (a power of two); out: its region offset. A third input, - // where the kext's word holds kRgfxKextFeatureWriteCombined, is the byte of the block from - // which the process's mapping of it is write-combined (kIOMapWriteCombineCache), a multiple of - // a page; the bytes before it, and the whole block where it is the block's size or more, are - // mapped write-back as any block's. + // A region block: in size, align (a power of two); out: its region offset. kRgfxAccelMethodAlloc = 0x101, // Frees the client's block at the region offset scalarInput[0], and returns at once: the // block is no longer the client's to name, and is given out again once the device answers @@ -221,8 +217,6 @@ enum { // A shared event's port (kRgfxSharedEventUserClientType) and its selectors, 0x113 and 0x114, // which the bundle uses where the feature word holds RGFX_FEATURE_SHARED_EVENT_NAMES too. kRgfxKextFeatureSharedEvents = 1 << 6, - // Alloc's (0x101) third input, the part of the block mapped write-combined. - kRgfxKextFeatureWriteCombined = 1 << 7, }; // Where a cached block's header holds its surface's CPU generation, a 64-bit count the kext diff --git a/guest/kext/tests/user_client_words_test.cpp b/guest/kext/tests/user_client_words_test.cpp index 777d5dc4f..9c9b4e78e 100644 --- a/guest/kext/tests/user_client_words_test.cpp +++ b/guest/kext/tests/user_client_words_test.cpp @@ -143,7 +143,6 @@ static void features() kRgfxKextFeatureDeferredAnswers, kRgfxKextFeatureContextSpan, kRgfxKextFeatureSharedEvents, - kRgfxKextFeatureWriteCombined, }; for (unsigned i = 0; i < sizeof(bits) / sizeof(bits[0]); i++) CHECK(bits[i] == uint64_t(1) << i);