"""A guest's whole life, the way a macOS guest drives apple-gfx under QEMU, in one session (scenarios/interplay_boot.py). Where the marathon (pgtest/marathon_util.py) picks each step at random among every legal one, a boot program follows the phases of a real guest's life, and the seed varies what happens inside each phase: how many frames, presents and batches, the order of independent steps, and which subsystems are busy when a phase ends. 1. Firmware: the EFI framebuffer (0x1200-0x1228) presents on port 0's parked display at the fresh device's version 0 (efi_before_version), or after the driver's version negotiation, and its frames go on (trace fires) while the driver comes up. 2. The driver's takeover: version negotiation (asks the device answers with 0xf5 or ignores), the root page and root FIFO, task 0 with its backings, glyph regions and gamma tables, device info (58), the child FIFOs, the gate and the GPU tasks; EFI turned off (0x1200 = 0), and only then port 0 connected: each head's page (opcode 1, every interrupt enabled), its ack, the cursor glyph, show and position. 3. The window server: the IOSurface host device's ring and descriptor table, every mapper surface mapped, and a compositor task (tid past the GPU tasks and the control) holding a mapper buffer (type 15) over each surface and a window buffer, whose pixels (the guest writes them) the GPU copies into a surface (blit 301) before the frame is presented with opcode 8 from a FIFO behind the copy. 4. Steady state: compositions (GPU copy, then opcode 8, with or without a gamma table), opcode-7 presents of task 0's backings, colour matrices, ICC profiles, cursor moves and glyphs, beside app-like GPU work (the marathon's dag batches over the root FIFO, children and tasks, some held on the gate), app faults and their recovery, and lifetime and memory changes. 5. Hot plug and mode changes: the host unplugs and plugs displays and sets new mode lists; the driver acks each update. 6. Sleep and wake of the heads (9), with presents while asleep. 7. Now and then a host pause, then the suspend onto a fresh device (rvg_device_will_suspend, rvg_device_finish_suspend, recreate, rvg_device_will_resume, rvg_device_did_resume), with work held on the gate across it, and steady state after the resume. 8. Shutdown: every held batch released and every result compared; the cursors hidden, the heads put to sleep or not and disconnected; the compositor's mapper buffers deleted (37) and its task deleted, every surface unmapped; the backings deleted (54); the child FIFOs, the gate and every task deleted (49, 32), task 0 last; then the root FIFO disabled, and the device released, or reset (a reboot) and the firmware's EFI frames again. Before phases 2 (EFI off), 5, 6, 7 and 8 the seed decides what is busy: GPU batches held on the gate, batches that run free, a fault on an idle child (not before a pause or a suspend, randmix 7.1), EFI frames. Programs with a death stop at a realistic moment instead of going on (docs/testing.md 7.3, 7.4), with work held on the gate: a one-entry gamma table in steady state, a guest mode count over 128 when the host sets a new list in the hot-plug phase, and at the suspend a second pause, or the cursor doorbell or rvg_display_cursor_position while the host has the device paused. The model is the display walk's (pgtest/randdisplay.py) and the marathon's, which this class extends: every callback, interrupt, 0x1014 and 0x1018, shared page, encoded pixel, present count, GPU slot and chain counter, scratch page and fault register is predicted. What this module adds is predicted the same way: a surface the GPU wrote is compared with the model's bytes and encoded on every display that shows it (the frame is a view of the surface, docs/testing.md 6.21), the unmap statuses, and the root FIFO's enable register after the shutdown. python3 -m pgtest.boot_util reads the expected files back against the model. Invariants, on top of the marathon's: - EFI is on only while port 0 has no page, and is off before port 0 connects, a pause, a suspend or a reset. - A composition's copy runs on the root FIFO or on an idle child, and its present on the root FIFO or on another idle child, with a barrier on the copy's stamp: each stamp publishes alone, and the present follows the copy. - Surfaces are written only in 8-bit formats, with palette bytes and alpha 0xff, so the model predicts their pixels. - Surfaces are unmapped only in the shutdown, after the compositor's mapper buffers are deleted and every head disconnected, so nothing holds them. QEMU (vendor/qemu/hw/display/apple-gfx.c) creates one display at port 0 with its mode list before the guest runs, encodes a frame on each new_frame, and resets the device on a device reset; it gives no trace-range callbacks, so its EFI presents come from the 100 ms timer. These programs give them (as randdisplay does) so that every EFI present is one new_frame, and use the suspend and pause calls a migration would (QEMU's apple-gfx makes none yet). Nothing here is imported from the library's code. """ import json import struct from . import abi from .diff_util import exec_payload from .display_guest import p_set_shared, p_show, p_swap from .efi_guest import EFI from .iosfc_guest import CMD_UNMAP, SUCCESS from .lifetime_util import delete_resource from .marathon_util import E3, GATE, Marathon from .randdisplay import EFI_SIZES, EFI_STRIDE, M32, O_ACKP, O_ENABLED, Death, PageM, check_obs, pixels from .randgen import Rng from .resources_build import b_copy_buf_buf, mapper_ref_rec from .task import GPUTask, seg # Phase lengths: steady-state steps before the hot-plug phase, while the # heads sleep, and after the resume. LENGTHS = {"short": ((6, 10), (1, 3), (4, 8)), "medium": ((14, 26), (2, 5), (10, 18)), "long": ((70, 110), (4, 8), (60, 90))} # Where each death lands. DEATH_PHASE = {"gamma_one": "steady", "mode_count_over": "hotplug", "cursor_paused": "sleep", "doorbell_paused": "suspend", "pause_twice": "suspend"} MISC = ("matrix", "icc", "compositor", "glyph", "move", "encode", "rewrite_backing", "present6", "clear_raised") class Boot(Marathon): """One boot program (the module docstring).""" def __init__(self, seed, doc, length="medium", death=None): super().__init__(seed, 0, doc, death=death) self.name = self.s.name = "interplay_boot_%d" % seed p = self.p = Rng(seed * 0xB0075 + 0x1D) self.length = length self.efi_on = True self.efi_first = p.chance(0.7) self.gpu_early = p.chance(0.5) # GPU set-up before EFI goes off self.info_first = p.chance(0.5) self.late_port = p.chance(0.4) # port 1 connected while EFI still shows self.defer_ack = p.chance(0.3) self.pause_on = p.chance(0.4) self.sleep_first = p.chance(0.5) # heads asleep before they disconnect self.disable_fifo = p.chance(0.6) self.reboot = p.chance(0.45) and death is None lo = LENGTHS[length] self.n_steady = [lo[0][0] + p.below(lo[0][1] - lo[0][0] + 1), lo[1][0] + p.below(lo[1][1] - lo[1][0] + 1), lo[2][0] + p.below(lo[2][1] - lo[2][0] + 1)] total = sum(self.n_steady) + 40 self.s._config["watchdog_s"] = 120 + total self.phase = "boot" self.nstep = 0 self.comp = None self.phist = {} self.fills = 0 # ---- labels ---- def mark(self, text): self.nstep += 1 lab = "%s %d: %s" % (self.phase, self.nstep, text) self.s.mark(lab) return lab def enter(self, phase): self.phase = phase self.s.mark("== %s" % phase) def did(self, kind): self.phist[kind] = self.phist.get(kind, 0) + 1 # ---- firmware: EFI (reims-vgpu-core's display::efi) ---- def efi_up(self): """0x1200 = 1 and the measured sequence, on port 0's parked display: mode_change and the first frame; the registers.""" s, p = self.s, self.p d = self.parked[0] if not hasattr(self, "efi_fb"): self.efi_fb = EFI(s) w, h = p.choice(EFI_SIZES) lab = self.mark("EFI on port 0's display %d, %dx%d" % (d.index, w, h)) raw = self.efi_frame(w, h) self.efi = dict(w=w, h=h, raw=raw) self.efi_fb.setup(w, h, EFI_STRIDE) self.efi_bound = d self.efi_present(d) self.events(lab) self.efi_regs(d, lab) self.encode(d, lab) self.did("efi frame") def efi_tick(self): """The firmware (or the console before the driver's takeover) draws a frame: a trace hit presents it; now and then a mode change.""" s, p = self.s, self.p d = self.efi_bound if d is None: return if p.chance(0.25): w, h = p.choice(EFI_SIZES) lab = self.mark("EFI mode %dx%d" % (w, h)) raw = self.efi_frame(w, h) self.efi.update(w=w, h=h, raw=raw) s.w(abi.R_EFI_MODE, w | h << 16) else: lab = self.mark("an EFI frame") self.efi["raw"] = self.efi_frame(self.efi["w"], self.efi["h"]) s.trace_fire(self.efi_fb.gpa(), abi.PAGE) self.efi_present(d) self.events(lab) self.encode(d, lab) self.did("efi frame") def efi_down(self): """The driver's handover: the mode it selects read back, then 0x1200 = 0, which zeroes the registers; the display keeps a frame over a mapping EFI released, never encoded again.""" s, p = self.s, self.p d = self.efi_bound lab = self.mark("EFI off") if d is not None: sel = p.below(len(d.modes) + 1) if d.modes else 0 s.w(abi.R_EFI_MODE_SELECT, sel) s.r(abi.R_EFI_MODE_SELECTED, "%s: mode %d" % (lab, sel)) m = d.modes[sel] if d.modes and sel < len(d.modes) else None self.predict(val=hex(m[0] | m[1] << 16) if m else hex(M32)) d.frame = ("stale",) s.w(abi.R_EFI_ENABLE, 0) self.efi_bound = None self.efi = None self.events(lab) for reg in (abi.R_EFI_ENABLE, abi.R_EFI_START, abi.R_EFI_LENGTH, abi.R_EFI_MODE): s.r(reg, lab) self.predict(val="0x0") # ---- the driver ---- def negotiate(self): """What a guest probes before it settles on 245: 0x100 is answered with 0xf5; a request with bit 31 set is ignored; 0xf4 is not a version and is answered with the one below it that is (set_binary_version).""" s, p = self.s, self.p lab = self.mark("version negotiation") asks = p.sample([0x100, 0x800000F0, 0xF4], 1 + p.below(3)) for v in asks: s.w(abi.R_VERSION, v) s.r(abi.R_VERSION, "%s: asked 0x%x" % (lab, v)) if v == 0x100: self.predict(val=hex(0xF5)) s.w(abi.R_VERSION, self.version) s.r(abi.R_VERSION, lab + ": settled") self.predict(val=hex(self.version)) s.r(abi.R_PORT_COUNT, lab + ": port count") self.predict(val=hex(self.nports)) s.r(abi.R_PORT_TYPES, lab + ": port types") self.predict(val="0x0") def device_info(self): """GetDeviceInfo (58) {maxKey 45, maxCount 64, page} into a page of 0xa5: host-keyed.""" s = self.s lab = self.mark("device info") page = s.alloc_page() s.fill(s.pa(page), 0x180, 0xA5) self.rsub(abi.OP_GET_DEVICE_INFO, struct.pack(" 700: return keep = self.gate_p if gate_p is not None: self.gate_p = gate_p try: self.g_dag(gate_ok=gate_p != 0) finally: self.gate_p = keep self.idle_tasks() self.did("gpu") def held(self): return [n for n in self.pending() if "gate" in n.locks] def release(self): if self.held(): self.g_release() self.idle_tasks() self.did("release") def busy(self, allow_fault=True): """What is running when a phase ends: batches held on the gate, batches that run free, a fault on an idle child.""" p = self.p what = [] if p.chance(0.6): what.append("held") if p.chance(0.4): what.append("free") if allow_fault and self.faults_on and p.chance(0.3) and self.idle_fifos() and len(self.faulted) < 2: what.append("fault") self.s.mark("%s: busy at the transition: %s" % (self.phase, ", ".join(what) or "nothing")) for w in p.sample(what, len(what)): if w == "held": self.gpu_work(gate_p=1.0) elif w == "free": self.gpu_work(gate_p=0) elif self.idle_fifos() and len(self.faulted) < 2: self.g_fault() self.did("fault") return what def resolve(self): """After a transition the guest opens the gate and recovers its faults, in the seed's order.""" p = self.p acts = [] if self.held(): acts.append("release") if self.faulted: acts.append("recover") for a in p.sample(acts, len(acts)): if a == "release": self.release() else: while self.faulted: self.g_recover() self.idle_tasks() self.did("recover") def quiet(self): """No fault outstanding (pause and suspend need it).""" while self.faulted: self.g_recover() self.idle_tasks() # ---- steady state ---- def steady(self, n): for _ in range(n): self.steady_step() def steady_step(self): p = self.p heads = self.heads() table = [("gpu", 8), ("check", 1)] if heads: table += [("move", 8), ("present", 5), ("misc", 4)] if self.surfaces_on: table.append(("compose", 10)) if self.held(): table.append(("release", 5)) if self.faults_on and self.idle_fifos() and len(self.faulted) < 2: table.append(("fault", 1)) if self.faulted: table.append(("recover", 6)) table += [("life", 1), ("scr_move", 1)] kind = p.weighted(table) if kind == "gpu": self.gpu_work() elif kind == "check": self.g_check() elif kind == "move": self.a_move(p.choice(heads)) elif kind == "present": self.a_present_backing(p.choice(heads)) elif kind == "compose": self.compose() elif kind == "misc": self.misc() elif kind == "release": self.release() elif kind == "fault": self.g_fault() elif kind == "recover": self.g_recover() elif kind == "life": self.g_life() elif kind == "scr_move": self.g_scr_move() if kind != "misc": self.did(kind) self.idle_tasks() def misc(self): p = self.p heads = self.heads() k = p.choice(MISC) self.did("misc " + k) if k == "rewrite_backing": self.a_rewrite_backing() elif k == "encode": self.a_encode(p.choice(heads)) else: getattr(self, "a_" + k)(p.choice(heads)) # ---- hot plug, sleep, pause, suspend ---- def hotplug(self): """The host changes the displays: an empty port gets a display, one is unplugged and a new one plugged in, a mode list changes; the driver acks each update, with steady steps between.""" p = self.p acts = [] for port in range(self.nports): if port not in self.plugged: acts.append(("plug", port)) port = p.below(self.nports) if port in self.plugged and p.chance(0.7): acts.append(("replug", port)) for port in range(self.nports): if p.chance(0.6): acts.append(("modes", port)) if not acts: acts.append(("modes", 0)) for k, port in p.sample(acts, len(acts)): if k == "plug" and port not in self.plugged: self.a_plug(port) elif k == "replug": if port in self.plugged: self.a_unplug(port) self.acks() if p.chance(0.5): self.steady_step() self.a_plug(port) elif k == "modes": if port not in self.plugged: self.a_plug(port) else: self.a_set_modes(port) self.did("hotplug " + k) if p.chance(0.3): self.steady_step() self.acks() if p.chance(0.5): self.steady_step() for port in range(self.nports): if port in self.plugged and port not in self.nubs: self.connect_head(port) self.acks() def sleep_heads(self, asleep): for port in self.p.sample(self.heads(client=False), len(self.heads(client=False))): nub = self.nubs[port] if nub.asleep != bool(asleep): self.a_sleep(port) self.did("sleep" if asleep else "wake") def pause_cycle(self): """The host pauses the device (QEMU's migration pre-copy would), maybe changes a display while it is paused, and unpauses.""" self.a_pause() if self.p.chance(0.5): port = self.p.below(self.nports) self.a_host_while_paused(port) self.a_unpause() self.acks() self.did("pause") # ---- shutdown ---- def shutdown(self): s, p = self.s, self.p self.gpu_final() for port in self.heads(client=False): if self.nubs[port].page.b[0xF20]: self.show(port, 0) if self.sleep_first: self.sleep_heads(1) for port in p.sample(sorted(self.nubs), len(self.nubs)): self.a_disconnect(port) self.did("disconnect") if self.comp is not None: t = self.comp lab = self.mark("the window server exits: its mapper buffers deleted (37), task %d deleted" % t.tid) for mid in sorted(t.mbuf): op, pl = delete_resource(t.tid, t.mbuf[mid]) self.rsub(op, pl, lab + ": 37 surface %d's buffer" % mid) self.rsub(abi.OP_DELETE_TASK, struct.pack(" 1 else "", "mapper surfaces and a compositor" if self.surfaces_on else "no mapper surfaces", "at version 0" if self.efi_first else "after the version", "before" if self.gpu_early else "after", self.nchildren, self.ntasks, "s" if self.ntasks > 1 else "", "/".join(str(n) for n in self.n_steady), ", ".join("%s %d" % kv for kv in sorted(self.phist.items())), ("; a pause before the suspend" if self.pause_on else ""), ("ends in a death (%s)" % self.death if self.death else "ends in a reboot to the firmware" if self.reboot else "ends released")) # ---- entry points ---- def boot(seed, doc="", length="medium", death=None): b = Boot(seed, doc, length, death) b.build() return b def build(seed, doc, length="medium", death=None): return boot(seed, doc, length, death).s def check(paths=None): """Every expected file of interplay_boot against the model.""" import glob import os from scenarios.interplay_boot import SEEDS, __doc__ as doc here = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) root = paths or os.path.join(here, "expected") bad = 0 for seed, length, death in SEEDS: b = boot(seed, doc, length, death) for pth in sorted(glob.glob(os.path.join(root, "interplay_boot", b.name, "*.jsonl"))): lines = [json.loads(l) for l in open(pth) if l.strip() and not l.startswith("#")] obs = [o for o in lines if "ev" in o or "kind" in o] complaints = check_obs(b, obs) for o in obs: if o.get("ev") == "cmp" and o.get("equal") is False: complaints.append("cmp differs: %s" % o.get("label")) for c in complaints: print("%s: %s" % (os.path.relpath(pth, here), c)) bad += len(complaints) return bad if __name__ == "__main__": import sys sys.exit(1 if check(sys.argv[1] if len(sys.argv) > 1 else None) else 0)