diff --git a/guest/kext/src/ReimsGraphicsMobile.cpp b/guest/kext/src/ReimsGraphicsMobile.cpp index e4ccbb116..b050c10ae 100644 --- a/guest/kext/src/ReimsGraphicsMobile.cpp +++ b/guest/kext/src/ReimsGraphicsMobile.cpp @@ -9,8 +9,7 @@ * answers the present. The device's refreshes reach the class as its vblank note. Each completed * swap's presentation time reaches WindowServer as frame info, which the paravirtual display has * none of: supported_frame_info answers the tags the device fills, and a record of the swap goes to - * each notify request as the paravirtual display sends its swap-info records - * (RgfxLegacySwapInfo.hpp). The class's other pure methods answer what a display with no factory + * each notify request's client, through its shared data queue (RgfxLegacySwapInfo.hpp). The class's other pure methods answer what a display with no factory * configuration and no CRC answers. * * None of the class's methods a subclass overrides is forwarded to the class: what the class @@ -44,7 +43,6 @@ #include #include -#include #include #include @@ -94,15 +92,12 @@ extern "C" void rgfxLegacySwapInfoNotifyDispatch(IOMobileFramebufferLegacy *self RgfxSwapInfoRecord *record) __asm__("__ZN25IOMobileFramebufferLegacy31swap_info_notify_dispatch_gatedEPNS_12SwapInfoBlobE"); -// IOUserClient's async result, which it keeps for its subclasses: sent through a subclass of its -// own, as the paravirtual display sends it through its user client class's. Never made. -struct RgfxAsyncResult : IOUserClient { - static IOReturn send(io_user_reference_t *reference, IOReturn result, io_user_reference_t *args, - UInt32 count, IOOptionBits options) - { - return sendAsyncResult64WithOptions(reference, result, args, count, options); - } -}; +// IOMobileFramebufferUserClient's enqueue of `bytes` bytes at `words` in its shared data queue, +// which IOMobileFramebuffer.framework reads a frame info record from (RgfxLegacySwapInfo.hpp): +// called directly, under the name every release from 14 to 27.0.1 exports, on the user client a +// notify request is for. Answers kIOReturnSuccess, or an error where the queue holds no room. +extern "C" IOReturn rgfxUserClientEnqueueFrameInfo(void *client, uint64_t *words, uint32_t bytes) + __asm__("__ZN29IOMobileFramebufferUserClient26enqueueInIOSharedDataQueueEPyj"); // IOAVFamily's constructors of a timing element and of a colour element, and a timing's colour // elements, which the paravirtual display calls to make the elements it publishes @@ -672,8 +667,9 @@ RgfxIOMFBWord ReimsGraphics::set_notify_tag_mask_gated(IOMFB_NotificationType ty // The frame info tags the device fills (RgfxLegacySwapInfo.hpp), one a call: the count, and tag // `index`'s name in `names[0]`, which IOMobileFramebuffer.framework asks for each index below the -// count it is given at index 0. The records go as async results (`dataQueue` 0; the library reads -// 1 as a shared data queue's, which no display here has). +// count it is given at index 0. The records go through the client's shared data queue +// (`dataQueue` 1, which the user client keeps and makes the queue at its frame info port by), as +// the class adds frame info requests with no async reference (RgfxLegacySwapInfo.hpp). RgfxIOMFBWord ReimsGraphics::supported_frame_info(uint32_t index, uint32_t *count, char (*names)[64], uint32_t *dataQueue) { @@ -682,7 +678,7 @@ RgfxIOMFBWord ReimsGraphics::supported_frame_info(uint32_t index, uint32_t *coun if (count) *count = kRgfxFrameInfoTagCount; if (dataQueue) - *dataQueue = 0; + *dataQueue = 1; if (tag && names) { strlcpy(names[0], tag, sizeof(names[0])); r = kIOReturnSuccess; @@ -714,23 +710,24 @@ RgfxIOMFBWord ReimsGraphics::set_frame_info_notification_state_gated(IOMFBNotify return word(kIOReturnSuccess); } -// A swap-info record sent to one notify request's client, as the paravirtual display sends it: an -// async result of the values the request's tag mask asks for (rgfxSwapInfoArgs), which the -// client may drop, answering the send's result. WindowServer learns its swaps' times from them. +// A swap's frame info record sent to one notify request's client: the values the request's tag +// mask asks for (rgfxFrameInfoRecord), enqueued in the shared data queue of the user client the +// request is for, where the client's library reads them (RgfxLegacySwapInfo.hpp), answering the +// enqueue's result: an error where the queue holds no room, or kIOReturnNotAttached where the +// request names no client. WindowServer learns its swaps' presentation times from them. RgfxIOMFBWord ReimsGraphics::do_swap_info_notify_gated(IOMFBNotifyRequest *request, const IOMobileFramebufferLegacy::SwapInfoBlob *blob) { IOReturn r = kIOReturnBadArgument; if (request && blob) { uint8_t *bytes = reinterpret_cast(request); - const uint32_t mask = *reinterpret_cast(bytes + kRgfxNotifyRequestMask); - uint64_t *last = reinterpret_cast(bytes + kRgfxNotifyRequestLast); - uint64_t args[kRgfxSwapInfoMaxArgs + 1]; - const uint32_t n = - rgfxSwapInfoArgs(mask, last, *reinterpret_cast(blob), args); - r = RgfxAsyncResult::send( - reinterpret_cast(bytes + kRgfxNotifyRequestReference), - kIOReturnSuccess, args, n, kIOUserNotifyOptionCanDrop); + void *client = *reinterpret_cast(bytes + kRgfxNotifyRequestClient); + uint64_t words[kRgfxSwapInfoMaxArgs + 1]; + const uint32_t n = rgfxFrameInfoRecord( + bytes, *reinterpret_cast(blob), words); + r = client ? rgfxUserClientEnqueueFrameInfo(client, words, + static_cast(n * sizeof(words[0]))) + : kIOReturnNotAttached; } if (logged(kDoSwapInfoNotify)) IOLog("ReimsGraphics: do_swap_info_notify_gated(%p, %p) = 0x%x\n", request, blob, r); diff --git a/guest/kext/src/RgfxLegacySwapInfo.hpp b/guest/kext/src/RgfxLegacySwapInfo.hpp index 2c91625a6..c4d973fdb 100644 --- a/guest/kext/src/RgfxLegacySwapInfo.hpp +++ b/guest/kext/src/RgfxLegacySwapInfo.hpp @@ -6,8 +6,10 @@ // the completed swap's ID. The paravirtual display makes the swap's record (RgfxSwapInfoRecord) // and hands it to the class's swap_info_notify_dispatch_gated, which calls the display's // do_swap_info_notify_gated for each notify request the class holds. -// - do_swap_info_notify_gated sends the request's client an async result (rgfxSwapInfoArgs): the -// request's tag mask, the swap's ID, and the record's values the mask asks for. +// - do_swap_info_notify_gated sends the request's client the record's values (rgfxSwapInfoArgs): the +// request's tag mask, the swap's ID, and the record's values the mask asks for. The paravirtual +// display sends them as an async result; ReimsGraphics enqueues them in the client's shared data +// queue (below). // - set_notify_tag_mask_gated sets a request's tag mask, for the swap-info notification alone. // // The paravirtual display answers supported_frame_info kIOReturnUnsupported, and so a client never @@ -19,11 +21,22 @@ // SkyLight (display_present_handler) and IOMobileFramebuffer.framework (InfoKeyInitialize, // figure_frame_info_mask, FrameInfoNotifyFunc). ReimsGraphics answers the tags it fills // (rgfxFrameInfoTag): the library asks each tag's name by its index, and a client's tag mask sets -// bit i for tag i; a result's values come in the tags' order, each one argument where the mask's +// bit i for tag i; a record's values come in the tags' order, each one word where the mask's // bit 31 is clear (two, its low and then its high word, where it is set), and the library turns // them into a dictionary of the tags' names. Its timestamps are mach_continuous_time's, which the // library turns into mach_absolute_time's (kern_egest_timestamp), as the class's own // get_current_absolute_time reads mach_continuous_time. +// +// The records go through the client's shared data queue, as on an Apple display: on every release +// the class adds a frame info request without an async reference (add_notification_gated, where +// its finfo_support_io_shared_queue answers true, as it does on each), so an async result would +// reach no port. supported_frame_info answers that the records come in a data queue, which +// IOMobileFramebufferUserClient keeps (s_supported_frame_info) and makes at the client's frame +// info port (registerNotificationPort), and which IOMobileFramebuffer.framework maps +// (clientMemoryForType 3) and reads (FrameInfoNotifyFuncIOShq, PackAndDispatchFrameInfoNotifictionData): +// each entry is a record's words, the same as an async result's arguments, copied into a buffer of +// 0x100 bytes. The display enqueues each through the request's user client +// (IOMobileFramebufferUserClient::enqueueInIOSharedDataQueue, which every release exports). // Header-only and free of the kernel's headers, for the native tests. // // Our own code. @@ -52,18 +65,19 @@ struct RgfxSwapInfoRecord { static_assert(sizeof(RgfxSwapInfoRecord) == 0x30, "a swap-info record is 0x30 bytes"); static_assert(offsetof(RgfxSwapInfoRecord, field_08) == 0x08, "its doublewords start at 8"); -// In a notify request (IOMFBNotifyRequest), the same on each release: the async reference its -// client gave, which the result is sent to; the tag mask; and the field_08 of the last record -// sent, which the next one's second value is taken from. -constexpr uint32_t kRgfxNotifyRequestReference = 0x18; +// In a notify request (IOMFBNotifyRequest), the same on each release: the user client it is for, +// the first word of the request's arguments, which add_notification_gated stores at 0x10 and reads +// the task of (its 0xe8, as IOMobileFramebufferUserClient's registerNotificationPort does); the tag +// mask; and the field_08 of the last record sent, which the next one's second value is taken from. +constexpr uint32_t kRgfxNotifyRequestClient = 0x10; constexpr uint32_t kRgfxNotifyRequestMask = 0x78; constexpr uint32_t kRgfxNotifyRequestLast = 0x80; // The notification type set_notify_tag_mask_gated takes a mask for (IOMFB_NotificationType). constexpr uint32_t kRgfxSwapInfoNotification = 3; -// The most values an async result carries here, and the records the paravirtual display keeps -// while the class holds them. +// The most words a record carries here, and the records the paravirtual display keeps while the +// class holds them. constexpr uint32_t kRgfxSwapInfoMaxArgs = 12; constexpr uint32_t kRgfxSwapInfoKept = 6; @@ -80,14 +94,15 @@ inline RgfxSwapInfoRecord rgfxSwapInfoRecord(uint32_t swapId, uint64_t presented return r; } -// The async result's values for `record`, to a request whose tag mask is `mask` and whose last +// A record's words for `record`, to a request whose tag mask is `mask` and whose last // record's field_08 is `*last`, as the paravirtual display's do_swap_info_notify_gated makes them: // the mask, the swap's ID, then for each of the record's five doublewords whose bit (0 to 4) the // mask sets, the doubleword, the second one being field_08 less `*last` instead. With the mask's -// bit 31 a value is two arguments, its low and then its high word, otherwise one, as the -// paravirtual display sends them and FrameInfoNotifyFunc reads them. A value that would start at -// the twelfth argument is left out. `*last` becomes the record's field_08. -// Answers the count of `args` written. +// bit 31 a value is two words, its low and then its high word, otherwise one, as the paravirtual +// display sends them and the library reads them (FrameInfoNotifyFunc from an async result, +// PackAndDispatchFrameInfoNotifictionData from a queue's entry). A value that would start at the +// twelfth word is left out. `*last` becomes the record's field_08. Answers the count of `args` +// written. inline uint32_t rgfxSwapInfoArgs(uint32_t mask, uint64_t *last, const RgfxSwapInfoRecord &record, uint64_t args[kRgfxSwapInfoMaxArgs + 1]) { @@ -110,6 +125,17 @@ inline uint32_t rgfxSwapInfoArgs(uint32_t mask, uint64_t *last, const RgfxSwapIn return n; } +// The record do_swap_info_notify_gated enqueues for the notify request at `request`, whose tag mask +// and last time lie at their offsets (the last becomes the record's field_08): its words, in +// `words`, and their count (rgfxSwapInfoArgs). +inline uint32_t rgfxFrameInfoRecord(uint8_t *request, const RgfxSwapInfoRecord &record, + uint64_t words[kRgfxSwapInfoMaxArgs + 1]) +{ + const uint32_t mask = *reinterpret_cast(request + kRgfxNotifyRequestMask); + uint64_t *last = reinterpret_cast(request + kRgfxNotifyRequestLast); + return rgfxSwapInfoArgs(mask, last, record, words); +} + // The frame info tags ReimsGraphics answers (supported_frame_info), by index: those it fills from // the device. A swap's presentation time, the refresh the host showed its layer-0 surface in, // mapped from the host's stamp into the guest's clock (RgfxVblankClock), and the time since the diff --git a/guest/kext/src/graft-arm64e.json b/guest/kext/src/graft-arm64e.json index e08baf90e..47612ca0a 100644 --- a/guest/kext/src/graft-arm64e.json +++ b/guest/kext/src/graft-arm64e.json @@ -95,6 +95,9 @@ "__ZN22IOAVVideoTimingElement16setColorElementsEP7OSArray": { "library": 2 }, + "__ZN29IOMobileFramebufferUserClient26enqueueInIOSharedDataQueueEPyj": { + "library": 1 + }, "__ZN25IOMobileFramebufferLegacy22dropAbortedSwaps_gatedEb": { "library": 1, "candidates": [ diff --git a/guest/kext/tests/legacy_swap_info_test.cpp b/guest/kext/tests/legacy_swap_info_test.cpp index 955c67df8..2161c6547 100644 --- a/guest/kext/tests/legacy_swap_info_test.cpp +++ b/guest/kext/tests/legacy_swap_info_test.cpp @@ -1,9 +1,10 @@ // The native test of RgfxLegacySwapInfo.hpp and the swap-info columns of RgfxLegacySwapLayout.hpp: // each release's offsets as its AppleParavirtGPU's notify_swap_done_gated, do_swap_info_notify_gated // and set_notify_tag_mask_gated and its IOMobileGraphicsFamily's note_vbl_ISR, swap_submit, -// abort_swap_gated and dropSwap_gated use them; the record the paravirtual display makes; the -// async result's values as IOMobileFramebuffer.framework reads them; the frame info tags the device -// answers and its presentation time; and the records kept while the class holds them. +// abort_swap_gated and dropSwap_gated use them; the record the paravirtual display makes; its +// words as IOMobileFramebuffer.framework reads them from the client's shared data queue; the frame +// info tags the device answers and its presentation time; and the records kept while the class +// holds them. #include "RgfxLegacySwapInfo.hpp" #include "RgfxLegacySwapLayout.hpp" @@ -12,6 +13,7 @@ #include #include #include +#include #include #include @@ -31,21 +33,21 @@ void check(bool ok, const char *what, const char *about) // What each release's kexts read and write, by byte offset: in the class, the time note_vbl_ISR // writes and the halfword notify_swap_done_gated tests; in a swap, the doubleword swap_submit // copies from its record and the word abort_swap_gated sets bit 0 of, both of which -// notify_swap_done_gated reads, with the state word it tests; and in a notify request, the -// async reference, tag mask and last time do_swap_info_notify_gated and set_notify_tag_mask_gated -// use. Read from 14.6.1, 15.6.1, 26.6.2 and 27.0.1. +// notify_swap_done_gated reads, with the state word it tests; and in a notify request, the user +// client add_notification_gated stores, and the tag mask and last time do_swap_info_notify_gated +// and set_notify_tag_mask_gated use. Read from 14.6.1, 15.6.1, 26.6.2 and 27.0.1. struct Release { const char *name; uint32_t classSize, vblankTime, notifyHeld; uint32_t swapState, swapRecordField00, swapFlags; - uint32_t requestReference, requestMask, requestLast; + uint32_t requestClient, requestMask, requestLast; }; constexpr Release kReleases[] = { - {"14", 0x18a0, 0x108, 0x189e, 0x32c, 0x2d0, 0x310, 0x18, 0x78, 0x80}, - {"15", 0x19c0, 0x108, 0x19be, 0x32c, 0x2d0, 0x310, 0x18, 0x78, 0x80}, - {"26", 0x1af8, 0x108, 0x1af6, 0x34c, 0x2f0, 0x330, 0x18, 0x78, 0x80}, - {"27", 0x1dd8, 0x108, 0x1dd2, 0x3ec, 0x390, 0x3d0, 0x18, 0x78, 0x80}, + {"14", 0x18a0, 0x108, 0x189e, 0x32c, 0x2d0, 0x310, 0x10, 0x78, 0x80}, + {"15", 0x19c0, 0x108, 0x19be, 0x32c, 0x2d0, 0x310, 0x10, 0x78, 0x80}, + {"26", 0x1af8, 0x108, 0x1af6, 0x34c, 0x2f0, 0x330, 0x10, 0x78, 0x80}, + {"27", 0x1dd8, 0x108, 0x1dd2, 0x3ec, 0x390, 0x3d0, 0x10, 0x78, 0x80}, }; void layouts() @@ -58,7 +60,7 @@ void layouts() check(l->state == r.swapState, "the swap's state word", r.name); check(l->swapRecordField00 == r.swapRecordField00, "the swap's copy of field_00", r.name); check(l->swapFlags == r.swapFlags, "the swap's flags word", r.name); - check(kRgfxNotifyRequestReference == r.requestReference && + check(kRgfxNotifyRequestClient == r.requestClient && kRgfxNotifyRequestMask == r.requestMask && kRgfxNotifyRequestLast == r.requestLast, "the notify request's offsets", r.name); @@ -159,6 +161,75 @@ void frameInfo() check(a[2] == 1440 && a[3] == 400, "Presentation_time, then Presentation_delta", "frameInfo"); } +// What IOMobileFramebuffer.framework makes of an entry of the client's shared data queue +// (FrameInfoNotifyFuncIOShq, PackAndDispatchFrameInfoNotifictionData; read from 14.6.1's and +// 27.0.1's): the entry's bytes copied into a buffer of 0x100 bytes; word 0 the mask, whose bit 31 +// makes a value two words, low first; word 1's low half the swap's ID; then a value for each bit +// of the mask, in the bits' order, under the name of the tag of the bit's index. +struct Parsed { + uint32_t swapId = 0; + std::map values; +}; + +Parsed readEntry(const uint64_t *words, uint32_t bytes, const char *about) +{ + check(bytes >= 16 && bytes % 8 == 0 && bytes <= 0x100, "an entry of whole words that fits", + about); + uint64_t buffer[0x100 / 8] = {}; + std::memcpy(buffer, words, bytes); + Parsed p; + const uint64_t first = buffer[0]; + const uint32_t mask = static_cast(first) & 0x7fffffffu; + p.swapId = static_cast(buffer[1]); + uint32_t at = 2; + for (uint32_t i = 0; i < 31; i++) { + if ((mask & (1u << i)) == 0) + continue; + uint64_t v = buffer[at++]; + if (first & 0x80000000u) + v |= buffer[at++] << 32; + const char *tag = rgfxFrameInfoTag(i); + p.values[tag ? tag : "none"] = v; + } + return p; +} + +// Each release's notify request, as do_swap_info_notify_gated reads it, gives the record +// IOMobileFramebuffer.framework reads QuartzCore's two tags from: the swap's ID, its presentation +// time and the time since the last record. +void queueRecord() +{ + check(kRgfxSwapInfoMaxArgs * sizeof(uint64_t) <= 0x100, "the longest record fits the copy", + "queueRecord"); + for (const Release &r : kReleases) { + alignas(8) uint8_t request[0x88] = {}; + const uint32_t mask = 0x3; + std::memcpy(request + r.requestMask, &mask, sizeof(mask)); + uint64_t words[kRgfxSwapInfoMaxArgs + 1]; + const uint64_t first = rgfxFrameInfoTime(1000, 5000, 5040); + const uint64_t second = rgfxFrameInfoTime(1400, 5400, 5440); + const uint32_t n1 = rgfxFrameInfoRecord(request, rgfxSwapInfoRecord(11, first, 7, 1), words); + Parsed p = readEntry(words, n1 * 8, r.name); + check(n1 == 4 && p.swapId == 11, "the first swap's record", r.name); + check(p.values.size() == 2 && p.values["Presentation_time"] == 1040 && + p.values["Presentation_delta"] == 1040, + "its time, and its time since none", r.name); + uint64_t last = 0; + std::memcpy(&last, request + r.requestLast, sizeof(last)); + check(last == 1040, "the request keeps the time sent", r.name); + const uint32_t n2 = + rgfxFrameInfoRecord(request, rgfxSwapInfoRecord(12, second, 7, 1), words); + p = readEntry(words, n2 * 8, r.name); + check(n2 == 4 && p.swapId == 12, "the next swap's record", r.name); + check(p.values["Presentation_time"] == 1440 && p.values["Presentation_delta"] == 400, + "Presentation_time, then Presentation_delta, in the tags' order", r.name); + const uint32_t n3 = rgfxFrameInfoRecord(request, rgfxSwapInfoRecord(13, 0, 7, 1), words); + p = readEntry(words, n3 * 8, r.name); + check(p.swapId == 13 && p.values["Presentation_time"] == 0, + "a swap that showed nothing reads 0", r.name); + } +} + void kept() { RgfxSwapInfoKept k = {}; @@ -197,9 +268,18 @@ void imports() check(source.find("\"__ZN25IOMobileFramebufferLegacy31swap_info_notify_dispatch_gatedEPNS_" "12SwapInfoBlobE\"") != std::string::npos, "the source calls the class's dispatch", "imports"); - check(source.find("sendAsyncResult64WithOptions") != std::string::npos && - source.find("kIOUserNotifyOptionCanDrop") != std::string::npos, - "the result is sent as one the client may drop", "imports"); + check(source.find("\"__ZN29IOMobileFramebufferUserClient26enqueueInIOSharedDataQueueEPyj\"") != + std::string::npos, + "the record goes in the user client's shared data queue", "imports"); + check(source.find("*dataQueue = 1;") != std::string::npos, + "supported_frame_info answers the records come in a data queue", "imports"); + check(source.find("sendAsyncResult") == std::string::npos, "no async result", "imports"); + const std::string graft = readFile("src/graft-arm64e.json"); + const size_t policy = + graft.find("\"__ZN29IOMobileFramebufferUserClient26enqueueInIOSharedDataQueueEPyj\": {"); + check(policy != std::string::npos && + graft.find("\"library\": 1", policy) == graft.find("\"library\"", policy), + "Graft binds the enqueue from IOMobileGraphicsFamily", "imports"); check(source.find("rgfxFrameInfoTag(index)") != std::string::npos, "supported_frame_info answers the tags", "imports"); check(source.find("rgfxFrameInfoTime(shown, absolute, mach_continuous_time())") != @@ -215,6 +295,7 @@ int main() record(); args(); frameInfo(); + queueRecord(); kept(); imports(); std::printf("legacy_swap_info_test: %d checks passed (%zu releases)\n", checks,