diff --git a/guest/kext/Makefile b/guest/kext/Makefile index b17836d2f..7f9279cd6 100644 --- a/guest/kext/Makefile +++ b/guest/kext/Makefile @@ -141,7 +141,7 @@ TESTS = $(BUILD)/tests/spans_test $(BUILD)/tests/blocks_test $(BUILD)/tests/surf $(BUILD)/tests/defer_carry_test $(BUILD)/tests/arm64_region_test \ $(BUILD)/tests/legacy_overrides_test $(BUILD)/tests/legacy_swap_queue_test \ $(BUILD)/tests/mobile_modes_test $(BUILD)/tests/swap_rate_test \ - $(BUILD)/tests/legacy_swap_info_test + $(BUILD)/tests/legacy_swap_info_test $(BUILD)/tests/mobile_color_test $(BUILD)/tests/%: tests/%.cpp $(HEADERS) ../../crates/reims-vgpu-graphics/include/reims_graphics_wire.h Makefile @mkdir -p $(@D) diff --git a/guest/kext/src/ReimsGraphics.hpp b/guest/kext/src/ReimsGraphics.hpp index 69bc5f534..aa759a52f 100644 --- a/guest/kext/src/ReimsGraphics.hpp +++ b/guest/kext/src/ReimsGraphics.hpp @@ -191,6 +191,10 @@ public: // And the mode WindowServer sets of those the display publishes, by its colour and timing IDs // (ReimsGraphicsMobile.cpp), which the class answers kIOReturnUnsupported. RgfxIOMFBWord set_digital_out_mode(uint32_t colorId, uint32_t timingId) override; + // And the colour matrix WindowServer sets with a swap (ReimsGraphicsMobile.cpp, + // RgfxMobileColor.hpp), which the class answers kIOReturnUnsupported. + RgfxIOMFBWord swap_set_color_matrix(IOMFBColorFixedMatrix *matrix, + IOMFBColorMatrixFunction function, uint32_t field_2) override; #endif // The display pipe (RgfxDisplayPipeUserClient, on the accelerator) presents through the diff --git a/guest/kext/src/ReimsGraphicsMobile.cpp b/guest/kext/src/ReimsGraphicsMobile.cpp index 402176491..21e78b483 100644 --- a/guest/kext/src/ReimsGraphicsMobile.cpp +++ b/guest/kext/src/ReimsGraphicsMobile.cpp @@ -33,6 +33,10 @@ * kIOReturnUnsupported, to set the mode WindowServer picks of those it publishes. Unlike the * paravirtual display, which publishes three colour modes, it publishes one, and the device lists * one mode, so WindowServer picks none: the display starts in its preferred mode (start_hardware). + * And it overrides swap_set_color_matrix, which the class answers kIOReturnUnsupported, so + * WindowServer, which sets its accessibility matrix at each swap until the call succeeds, sets it + * once: as the paravirtual display does, it answers 0 and the display shows the matrix + * (RgfxMobileColor.hpp). * * A process finds the display as IOMobileFramebuffer.framework counts displays: by a node of the * device tree (attachMobileNode), then the framebuffer's service and the properties it publishes @@ -47,6 +51,7 @@ #include #include "RgfxDisplayIdentity.hpp" +#include "RgfxMobileColor.hpp" #include "RgfxMobileModes.hpp" #if defined(__arm64e__) @@ -142,6 +147,7 @@ enum MobileCall : unsigned { kSurfaceLayerUnmap, kGetDotPitch, kSetDigitalOutMode, + kSwapSetColorMatrix, kVblank, kPresented, kSwapShown, @@ -947,6 +953,36 @@ RgfxIOMFBWord ReimsGraphics::set_digital_out_mode(uint32_t colorId, uint32_t tim return word(r); } +// The colour matrix WindowServer sets with a swap, by IOMobileFramebufferSwapSetColorMatrix: +// QuartzCore sets its accessibility matrix so (function 2) at each swap until the call succeeds +// (RgfxMobileColor.hpp). As the paravirtual display does, any function and field are answered 0, +// NULL sets the identity, and the display shows the matrix from the next present: the device's +// DISPLAY_COLOR matrix stage, which the host stages for the next PRESENT_SURFACE it takes and +// keeps until it is set again, as the x86-64 display pipe sets it (RgfxPipeColor.hpp); an +// identity turns the stage off. The user client calls it ungated, in WindowServer's thread, so +// the post may wait for the ring's room. A host without RGFX_FEATURE_DISPLAY_COLOR shows no +// matrix, and the call is still answered 0, as the paravirtual display answers whatever its host +// does with it. +RgfxIOMFBWord ReimsGraphics::swap_set_color_matrix(IOMFBColorFixedMatrix *matrix, + IOMFBColorMatrixFunction function, + uint32_t field_2) +{ + RgfxColorEntry rows[kRgfxMobileMatrixRows]; + rgfxMobileColorMatrix(reinterpret_cast(matrix), rows); + // A swap's colour has no pipe transaction: its parts carry 0, which the host only logs. + const IOReturn posted = + displayColor() ? postDisplayColor(0, RGFX_COLOR_MATRIX, kRgfxMobileMatrixRows, + reinterpret_cast(rows)) + : kIOReturnUnsupported; + if (logged(kSwapSetColorMatrix)) + IOLog("ReimsGraphics: swap_set_color_matrix(%s, %u, %u): rows %08x %08x %08x / %08x %08x " + "%08x / %08x %08x %08x, posted 0x%x\n", + matrix ? "matrix" : "NULL", static_cast(function), field_2, rows[0].x, + rows[0].y, rows[0].z, rows[1].x, rows[1].y, rows[1].z, rows[2].x, rows[2].y, rows[2].z, + posted); + return word(kIOReturnSuccess); +} + IOReturn ReimsGraphics::setMobileMode(uint32_t colorId, uint32_t timingId, Mode *shown) { IOLockLock(stateLock); diff --git a/guest/kext/src/RgfxMobileColor.hpp b/guest/kext/src/RgfxMobileColor.hpp new file mode 100644 index 000000000..16f474a3d --- /dev/null +++ b/guest/kext/src/RgfxMobileColor.hpp @@ -0,0 +1,127 @@ +// RgfxMobileColor: the colour matrix an arm64 WindowServer sets on its IOMobileFramebuffer +// (ReimsGraphicsMobile.cpp's swap_set_color_matrix), as the DISPLAY_COLOR matrix stage the host +// applies (reims_graphics_wire.h, RGFX_COLOR_MATRIX). Header-only and free of the kernel's headers, +// so its native test (tests/mobile_color_test.cpp) builds it as the kext does. +// +// IOMobileFramebufferSwapSetColorMatrix(fb, matrix, function, field) sends IOMobileFramebuffer's +// user client selector 69 a structure of 0x88 bytes: a byte at 0x00, set for no matrix; fifteen +// signed 64-bit words from 0x08, each a float of the caller's times 2^32 rounded toward zero; the +// function at 0x80 and the field at 0x84. The user client's s_swap_set_color_matrix passes the +// display's swap_set_color_matrix the words (NULL where the byte is set), the function and the +// field, ungated. The words, as the display reads them: +// 0x00 field_00: three words +// 0x18 the matrix's nine words, row by row: row i is output channel i's red, green and blue +// 0x60 field_60: three words +// (read from IOMobileFramebuffer.framework's kern_SwapSetColorMatrix and IOMobileGraphicsFamily's +// s_swap_set_color_matrix on macOS 14.6.1, 15.6.1, 26.6.2 and 27.0.1, which lay it out alike). +// QuartzCore's IOMFBDisplay::set_framebuffer_ax_matrix sets WindowServer's accessibility matrix so, +// at each swap until the call succeeds, with function 2, field 0, its matrix's three rows at 0x18 +// and zeros in field_00 and field_60; it logs "IOMFB was not able to set accessibility matrix." at +// each failure (27.0.1). IOMobileFramebufferLegacy's own swap_set_color_matrix answers +// kIOReturnUnsupported (0xe00002c7) on each of those releases. +// +// Apple's paravirtual display (27.0.1's AppleParavirtGPU, AppleParavirtDisplay) answers 0 for any +// function and field. It keeps the nine words, or the identity for NULL, reading neither field_00 +// nor field_60, and its swap gives its host the matrix as three rows of four floats, row i's +// first three each word converted to a double, times 2^-32, rounded to a float, and its fourth 0 +// (in its first colour remap mode). rgfxMobileColorMatrix gives the same rows, in the wire's +// entries: out_i = row_i[0] * r + row_i[1] * g + row_i[2] * b + row_i[3]. Its conversion is integer +// arithmetic with the same two roundings (to nearest, ties to even: a 64-bit integer to a double, +// then a double to a float), so the kext needs no floating point. +// +// Our own code; it depends only on the committed wire header. +#ifndef RGFX_MOBILE_COLOR_HPP_ +#define RGFX_MOBILE_COLOR_HPP_ + +#include + +#include "reims_graphics_wire.h" + +// The matrix's rows, the words before them, and the words in all. +constexpr uint32_t kRgfxMobileMatrixRows = 3; +constexpr uint32_t kRgfxMobileMatrixFirstWord = 3; +constexpr uint32_t kRgfxMobileMatrixWords = 15; + +// The function QuartzCore sets its accessibility matrix with (read). +constexpr uint32_t kRgfxMobileAccessibilityMatrix = 2; + +// The bits of `v` rounded to nearest, ties to even, at `drop` low bits (drop < 64): the kept bits. +inline uint64_t rgfxRoundNearestEven(uint64_t v, uint32_t drop) +{ + if (drop == 0) + return v; + const uint64_t kept = v >> drop; + const uint64_t rest = v & ((uint64_t(1) << drop) - 1); + const uint64_t half = uint64_t(1) << (drop - 1); + return kept + ((rest > half || (rest == half && (kept & 1))) ? 1 : 0); +} + +// The number of significant bits of a nonzero `v`. +inline uint32_t rgfxBitLength(uint64_t v) +{ + uint32_t n = 0; + while (v) { + n++; + v >>= 1; + } + return n; +} + +// The bits of the float `(float)((double)fixed * 2^-32)`: the signed 64-bit word converted to a +// double, scaled, and rounded to a float, each rounding to nearest, ties to even. +inline uint32_t rgfxFixed32ToFloatBits(int64_t fixed) +{ + if (fixed == 0) + return 0; + const uint32_t sign = fixed < 0 ? 0x80000000u : 0; + uint64_t mag = fixed < 0 ? uint64_t(0) - uint64_t(fixed) : uint64_t(fixed); + // To a double: 53 significant bits, value mag * 2^shift. + int32_t shift = 0; + uint32_t n = rgfxBitLength(mag); + if (n > 53) { + const uint32_t drop = n - 53; + mag = rgfxRoundNearestEven(mag, drop); + shift += static_cast(drop); + if (mag >> 53) { + mag >>= 1; + shift++; + } + n = 53; + } + // To a float: 24 significant bits. + if (n > 24) { + const uint32_t drop = n - 24; + mag = rgfxRoundNearestEven(mag, drop); + shift += static_cast(drop); + if (mag >> 24) { + mag >>= 1; + shift++; + } + } else { + mag <<= 24 - n; + shift -= static_cast(24 - n); + } + // mag * 2^(shift - 32), mag in [2^23, 2^24): its exponent is shift - 32 + 23, biased by 127. + // A word's magnitude is 2^-32 to 2^31, inside a float's normal range. + const uint32_t exponent = static_cast(shift - 32 + 23 + 127); + return sign | (exponent << 23) | static_cast(mag & 0x7fffff); +} + +// The DISPLAY_COLOR matrix rows for the words swap_set_color_matrix takes, or the identity for +// NULL: row i's first three entries the matrix's row i, its fourth 0. +inline void rgfxMobileColorMatrix(const int64_t *words, RgfxColorEntry rows[kRgfxMobileMatrixRows]) +{ + constexpr uint32_t kOne = 0x3f800000u; + for (uint32_t i = 0; i < kRgfxMobileMatrixRows; i++) { + uint32_t row[4] = {0, 0, 0, 0}; + for (uint32_t j = 0; j < 3; j++) + row[j] = words ? rgfxFixed32ToFloatBits(words[kRgfxMobileMatrixFirstWord + 3 * i + j]) + : (i == j ? kOne : 0); + rows[i].x = row[0]; + rows[i].y = row[1]; + rows[i].z = row[2]; + rows[i].w = row[3]; + } +} + +#endif // RGFX_MOBILE_COLOR_HPP_ diff --git a/guest/kext/src/graft-arm64e.json b/guest/kext/src/graft-arm64e.json index 47612ca0a..b4b4deb74 100644 --- a/guest/kext/src/graft-arm64e.json +++ b/guest/kext/src/graft-arm64e.json @@ -81,7 +81,8 @@ "surface_layer_map": {}, "surface_layer_unmap": {}, "get_dot_pitch": { "required": true }, - "set_digital_out_mode": { "required": true } + "set_digital_out_mode": { "required": true }, + "swap_set_color_matrix": { "required": true } } } ], diff --git a/guest/kext/tests/legacy_overrides_test.cpp b/guest/kext/tests/legacy_overrides_test.cpp index 48286f198..5a9bd7d90 100644 --- a/guest/kext/tests/legacy_overrides_test.cpp +++ b/guest/kext/tests/legacy_overrides_test.cpp @@ -1,9 +1,11 @@ // The native test of what ReimsGraphics overrides of IOMobileFramebufferLegacy's own methods // (ReimsGraphicsMobile.cpp): get_dot_pitch, because the class's start leaves its state unset for a -// PCI device, and set_digital_out_mode, which the class answers kIOReturnUnsupported and the -// paravirtual display overrides to set its modes. start keeps its provider as an AppleARMIODevice in -// one field, NULL where the provider is not one, and the class's get_dot_pitch calls through that -// field unchecked. +// PCI device; set_digital_out_mode, which the class answers kIOReturnUnsupported and the +// paravirtual display overrides to set its modes; and swap_set_color_matrix, which the class +// answers kIOReturnUnsupported and the paravirtual display overrides to keep the matrix +// (RgfxMobileColor.hpp, tests/mobile_color_test.cpp). start keeps its provider as an +// AppleARMIODevice in one field, NULL where the provider is not one, and the class's get_dot_pitch +// calls through that field unchecked. // // For each release the arm64e kexts run on, kReleases records that field and the vtable slot of // each method ReimsGraphics overrides for it, read from the release's IOMobileGraphicsFamily. Graft @@ -49,6 +51,15 @@ constexpr Method kMethods[] = { "virtual graft_iomfb_word set_digital_out_mode(uint32_t field_0, uint32_t field_1);", "RgfxIOMFBWord set_digital_out_mode(uint32_t colorId, uint32_t timingId) override;", "__ZN25IOMobileFramebufferLegacy20set_digital_out_modeEjj"}, + {"swap_set_color_matrix", + "virtual graft_iomfb_word swap_set_color_matrix(IOMFBColorFixedMatrix *matrix,\n" + " IOMFBColorMatrixFunction function,\n" + " uint32_t field_2);", + "RgfxIOMFBWord swap_set_color_matrix(IOMFBColorFixedMatrix *matrix,\n" + "\t IOMFBColorMatrixFunction function, uint32_t field_2) " + "override;", + "__ZN25IOMobileFramebufferLegacy21swap_set_color_matrixEP21IOMFBColorFixedMatrix" + "24IOMFBColorMatrixFunctionj"}, }; constexpr size_t kMethodCount = sizeof(kMethods) / sizeof(kMethods[0]); @@ -62,10 +73,10 @@ struct Release { }; constexpr Release kReleases[] = { - {"14.6.1", 0x18a0, 0x1640, {292, 289}}, - {"15.6.1", 0x19c0, 0x1758, {292, 289}}, - {"26.6.2", 0x1af8, 0x1890, {293, 290}}, - {"27.0.1", 0x1dd8, 0x1b80, {294, 291}}, + {"14.6.1", 0x18a0, 0x1640, {292, 289, 331}}, + {"15.6.1", 0x19c0, 0x1758, {292, 289, 334}}, + {"26.6.2", 0x1af8, 0x1890, {293, 290, 335}}, + {"27.0.1", 0x1dd8, 0x1b80, {294, 291, 343}}, }; // The SDK stand-in's virtual slots past its own: GRAFT_IOMFB_VTABLE_RESERVE. diff --git a/guest/kext/tests/mobile_color_test.cpp b/guest/kext/tests/mobile_color_test.cpp new file mode 100644 index 000000000..7f252dc3b --- /dev/null +++ b/guest/kext/tests/mobile_color_test.cpp @@ -0,0 +1,186 @@ +// The native test of RgfxMobileColor.hpp: the DISPLAY_COLOR matrix rows the arm64 display posts for +// the colour matrix WindowServer sets with a swap (ReimsGraphicsMobile.cpp's +// swap_set_color_matrix), against the paravirtual display's conversion done in floating point +// here: each word converted to a double, times 2^-32, rounded to a float. Also the override's +// source: it converts the matrix, posts it as the matrix stage, and answers 0. +#include "RgfxMobileColor.hpp" + +#include +#include +#include +#include +#include +#include + +namespace { + +int checks = 0; + +void check(bool ok, const char *what, const char *about) +{ + checks++; + if (!ok) { + std::fprintf(stderr, "mobile_color_test: FAILED (%s): %s\n", about, what); + std::exit(1); + } +} + +// The paravirtual display's conversion, in floating point. +uint32_t paravirtBits(int64_t word) +{ + const float f = static_cast(static_cast(word) * 0x1p-32); + uint32_t bits = 0; + std::memcpy(&bits, &f, sizeof(bits)); + return bits; +} + +// A float's word as kern_SwapSetColorMatrix makes it: times 2^32, rounded toward zero. +int64_t wordOf(float f) +{ + return static_cast(static_cast(f) * 0x1p32); +} + +uint32_t bitsOf(float f) +{ + uint32_t bits = 0; + std::memcpy(&bits, &f, sizeof(bits)); + return bits; +} + +// Each word converts as the paravirtual display converts it: edges, the boundaries where a double +// and then a float round (ties among them), and a pseudo-random spread over every magnitude. +void conversion() +{ + const int64_t edges[] = { + 0, 1, -1, 2, 3, 0x7fffffff, int64_t(1) << 32, -(int64_t(1) << 32), (int64_t(1) << 32) + 1, + INT64_MAX, INT64_MIN, INT64_MIN + 1, INT64_MAX - 1, int64_t(1) << 62, -(int64_t(1) << 62), + }; + for (int64_t w : edges) + check(rgfxFixed32ToFloatBits(w) == paravirtBits(w), + "an edge word converts as two roundings do", "edge"); + // Around each power of two, for the first rounding (past 53 bits) and the second (past 24): + // halfway points and one either side, which a single rounding gets otherwise than two. + for (int p = 1; p < 63; p++) { + const int64_t base = int64_t(1) << p; + for (int drop : {p - 53, p - 24, p - 25, p - 54}) { + if (drop < 1) + continue; + const int64_t half = int64_t(1) << (drop - 1); + for (int64_t k = 0; k < 6; k++) { + const int64_t mid = base + (k << drop) + half; + for (int64_t d = -2; d <= 2; d++) { + for (int64_t w : {mid + d, -(mid + d)}) + check(rgfxFixed32ToFloatBits(w) == paravirtBits(w), + "a word near a boundary converts as two roundings do", "tie"); + } + } + } + } + uint64_t x = 0x9e3779b97f4a7c15ull; + for (int i = 0; i < 2000000; i++) { + x ^= x << 13; + x ^= x >> 7; + x ^= x << 17; + const int64_t w = static_cast(x) >> (x % 63); + check(rgfxFixed32ToFloatBits(w) == paravirtBits(w), "a word converts as two roundings do", + "spread"); + } +} + +// NULL is the identity; a matrix gives its nine words' rows with a fourth 0, reading neither +// field_00 nor field_60. +void rows() +{ + RgfxColorEntry r[kRgfxMobileMatrixRows]; + rgfxMobileColorMatrix(nullptr, r); + const uint32_t one = bitsOf(1.0f); + for (uint32_t i = 0; i < kRgfxMobileMatrixRows; i++) { + const uint32_t row[4] = {r[i].x, r[i].y, r[i].z, r[i].w}; + for (uint32_t j = 0; j < 4; j++) + check(row[j] == (i == j ? one : 0), "NULL is the identity", "identity"); + } + + // A grey matrix, as WindowServer's words of floats carry it: each row the same weights. + const float grey[3] = {0.2126f, 0.7152f, 0.0722f}; + int64_t words[kRgfxMobileMatrixWords]; + for (uint32_t k = 0; k < kRgfxMobileMatrixWords; k++) + words[k] = int64_t(0x5a5a5a5a5a5a5a5a) - k; // field_00 and field_60: not read + for (uint32_t i = 0; i < 3; i++) + for (uint32_t j = 0; j < 3; j++) + words[kRgfxMobileMatrixFirstWord + 3 * i + j] = wordOf(grey[j]); + rgfxMobileColorMatrix(words, r); + for (uint32_t i = 0; i < kRgfxMobileMatrixRows; i++) { + const uint32_t row[4] = {r[i].x, r[i].y, r[i].z, r[i].w}; + for (uint32_t j = 0; j < 3; j++) + check(row[j] == paravirtBits(wordOf(grey[j])), "row i is the matrix's row i", "grey"); + check(row[3] == 0, "the fourth entry is 0", "grey"); + } + + // Rows and columns apart: entry (i, j) is word 3 + 3i + j. + for (uint32_t i = 0; i < 3; i++) + for (uint32_t j = 0; j < 3; j++) + words[kRgfxMobileMatrixFirstWord + 3 * i + j] = + wordOf(static_cast(10 * i + j) - 4.5f); + rgfxMobileColorMatrix(words, r); + for (uint32_t i = 0; i < kRgfxMobileMatrixRows; i++) { + const uint32_t row[3] = {r[i].x, r[i].y, r[i].z}; + for (uint32_t j = 0; j < 3; j++) + check(row[j] == bitsOf(static_cast(10 * i + j) - 4.5f), + "entry (i, j) is word 3 + 3i + j", "order"); + } + // An identity matrix's words give the identity's rows, which the host stages off. + for (uint32_t i = 0; i < 3; i++) + for (uint32_t j = 0; j < 3; j++) + words[kRgfxMobileMatrixFirstWord + 3 * i + j] = i == j ? int64_t(1) << 32 : 0; + rgfxMobileColorMatrix(words, r); + for (uint32_t i = 0; i < kRgfxMobileMatrixRows; i++) { + const uint32_t row[4] = {r[i].x, r[i].y, r[i].z, r[i].w}; + for (uint32_t j = 0; j < 4; j++) + check(row[j] == (i == j ? one : 0), "the identity's words are the identity", "ones"); + } +} + +std::string readFile(const char *path) +{ + std::ifstream in(path); + check(in.good(), "the file can be read", path); + return std::string((std::istreambuf_iterator(in)), std::istreambuf_iterator()); +} + +// The override converts the matrix, posts it as the matrix stage where the host took the colour, +// and answers 0 whatever the post did, as the paravirtual display answers. +void source() +{ + const std::string src = readFile("src/ReimsGraphicsMobile.cpp"); + const size_t at = src.find("RgfxIOMFBWord ReimsGraphics::swap_set_color_matrix("); + check(at != std::string::npos, "the override is defined", "source"); + const size_t end = src.find("\n}\n", at); + check(end != std::string::npos, "the override ends", "source"); + const std::string body = src.substr(at, end - at); + check(body.find("rgfxMobileColorMatrix(reinterpret_cast(matrix), rows);") != + std::string::npos, + "it converts the matrix", "source"); + const char *post = + "displayColor() ? postDisplayColor(0, RGFX_COLOR_MATRIX, kRgfxMobileMatrixRows,"; + check(body.find(post) != std::string::npos, + "it posts the matrix stage where the host took the colour", "source"); + check(body.find("return word(kIOReturnSuccess);") != std::string::npos, "it answers 0", + "source"); + check(body.find("return word(posted)") == std::string::npos, + "the post's result is not the answer", "source"); +} + +} // namespace + +int main() +{ + static_assert(kRgfxMobileMatrixFirstWord + 3 * kRgfxMobileMatrixRows + 3 == + kRgfxMobileMatrixWords, + "field_00, the matrix and field_60"); + static_assert(kRgfxMobileMatrixRows == 3, "the wire's matrix stage has three entries"); + conversion(); + rows(); + source(); + std::printf("mobile_color_test: %d checks passed\n", checks); + return 0; +}