"""Builders for the limits interplay scenarios (scenarios/interplay_limits.py): several subsystems at their limits at once. The limits, as the code states them (Rust paths name items of reims-vgpu-core): - 64 FIFOs at binary version 61 and up: the root and children 1-63 (maxFIFOCount, which the child FIFO define checks). - 128 pending stamps a FIFO (fifo::stamps::STAMP_QUEUE_CAPACITY). - 256 tasks, IDs 0-255 (device::tasks::MAX_TASKS); each remote task (1-255 under isolation) is a service instance of its own (the service's Info.plist sets _MultipleInstances). - the root FIFO's 0x20000 bytes; a child's 4096 ring pages. - in the blocks setup, the root task's range allocator of 0xffffff bytes in 16 KiB granules, which maps the root node, the root FIFO, every child's ring and page list, every local task's root page and every display's shared page. - 8 display ports (device::config::MAX_DISPLAY_PORT_COUNT), 128 modes a page (display::page::MAX_MODES), 1024 gamma entries, cursor glyphs of 256 x 256. - IOSurface mapping IDs below 8192. Everything here composes the per-area builders (fifo.py, task.py, newops_util.py, interplay_util.py); nothing is imported from the implementation. """ import struct from . import abi from .abi import PAGE from .newops_util import cmd_seq # Blit record 306, fill: {u32 buffer, u64 offset, u64 length, u8 value}, # 8 + 24 bytes. FILL = 306 FILL_REC = 32 SEG = 8 # the segment header def fill_seg(ref, off, n, byte): """One blit segment holding one fill.""" body = abi.REC_HDR.pack(FILL, FILL_REC) + struct.pack("> shift) & 0xFFF t = self._table(level, prefix) if level == depth - 1: rows.setdefault(t, {})[idx] = s.pfn(self.leaf(v)) | abi.PTE_WRITABLE else: nxt = self._table(level + 1, (prefix << 12) | idx) rows.setdefault(t, {})[idx] = s.pfn(nxt) prefix = (prefix << 12) | idx for t, row in rows.items(): lo, hi = min(row), max(row) s.mem(s.pa(t, 4 * lo), struct.pack("<%dI" % (hi - lo + 1), *[row.get(i, 0) for i in range(lo, hi + 1)])) def _table(self, level, prefix): if (level, prefix) not in self._tables: self._tables[(level, prefix)] = self._table_alloc() return self._tables[(level, prefix)] def gpa(self, va): return self.s.pa(self.leaf(va // PAGE), va % PAGE) def write(self, va, data): data = bytes(data) pos = 0 while pos < len(data): a = va + pos n = min(len(data) - pos, PAGE - a % PAGE) self.s.mem(self.gpa(a), data[pos:pos + n]) pos += n def dump(self, va, n, label): self.s.dump(self.gpa(va), n, label=label) def root_bytes(self, obj_off=0, obj_len=0): """The task root (guest::records::TaskRoot2): table root page, depth, object list offset and length; the host writes the length at +0x14.""" return abi.TASK_ROOT.pack(self.s.pfn(self.pt_root), self.depth, obj_off, obj_len, 0, 0, 0) class Roots: """Task root pages for tasks first..first+count-1 over one Space, in contiguous RAM pages, written with one mem_seq (stride one page).""" def __init__(self, s, space, first, count): self.s = s self.space = space self.first = first self.count = count self.page0 = s.alloc_page(count) s.mem_seq(s.pa(self.page0), count, PAGE, space.root_bytes()) def page(self, tid): return self.page0 + tid - self.first def pfn(self, tid): return self.s.pfn(self.page(tid)) # ---- 32-byte and larger commands in bulk (newops_util.cmd_seq) ---- def ring_seq(s, f, count, template, fields=()): """cmd_seq on any FIFO: the root FIFO, or a child whose ring pages are physically contiguous (ChildFIFO without scatter).""" if f.fid == 0: return cmd_seq(s, f, count, template, fields) assert f.pages == list(range(f.pages[0], f.pages[0] + len(f.pages))) class _View: pass v = _View() v.written, v.stamp, v.start = f.written, f.stamp, 0 v.ring_size = f.ring_size v.base_gpa = lambda: s.pa(f.pages[0]) last = cmd_seq(s, v, count, template, fields) f.written, f.stamp = v.written, v.stamp return last def define_seq(s, f, roots, first, count, length): """DefineTask2 (56) {u32 taskID << 1, u64 length, u32 task root page} for tasks first.., padded to 32 bytes.""" t = abi.HDR.pack(abi.OP_DEFINE_TASK2, 0, 32, 0) + struct.pack("