# K2151–K2155 Shutdown IRQ/DMA/Thread Lease Drain Contract (`shutdown_io.leases`) ## Purpose and Invariant - **Feature**: `shutdown_io.leases` (`K2151–K2155`) - **Invariant**: Every active device resets and closes its lease lifetime before its backing pages are reclaimed (`Her aygıt reset ve lease ömrü kapanır`). - **Scope**: Coordinates shutdown teardown of active network (`DeviceKind::Network`) and disk (`DeviceKind::Disk`) queues (`mask_irq -> complete_dma -> reset_device -> retire_lease`), refuses any owner attempt to free/retire a lease while DMA is still live or before device reset (`Error::DmaLive`), and produces an explicit shutdown accounting snapshot (`Outcome::AllZero` when all leases and DMA are retired, or `Outcome::ExplicitHeld` when any lease remains retained). ## Allowed Teardown Order per Queue The current `LeaseDrain` implementation and K2155 EL0 experiment exercise a state model. Registering a queue does not allocate device DMA, and calling `mask_irq`, `complete_dma` or `reset_device` only changes model fields. The reported reclaimed-page count is not evidence of physical page reclamation. This experiment cannot grant K2155 guest acceptance. A native integration must capture actual network/disk queue ownership, IRQ state, DMA completion, device reset/readback, thread drain and allocator state, including retained resources when teardown cannot complete. 1. `register_queue(kind, owner_pid, backing_pages, dma_inflight)` 2. `mask_irq(id)` 3. `complete_dma(id, count)` until `dma_inflight == 0` 4. `reset_device(id)` once `irq_masked && dma_inflight == 0` 5. `retire_lease(id, caller_pid)` once `caller_pid == owner_pid && irq_masked && dma_inflight == 0 && device_reset` ## Error Codes (`Error`) - `InvalidQueue = 1`: Invalid queue parameters or out-of-range queue ID. - `Capacity = 2`: Queue table (`MAX_QUEUES = 4`) full. - `Order = 3`: Out-of-order teardown step (e.g., `complete_dma` or `reset_device` before `mask_irq`, or duplicate `mask_irq`/`reset_device`). - `Owner = 4`: Non-owner PID attempted `retire_lease`. - `DmaLive = 5`: Attempted `reset_device` while `dma_inflight != 0`, or attempted `retire_lease` before `irq_masked`, `dma_inflight == 0`, and `device_reset`. Increments `live_dma_free_refusals` on premature `retire_lease` and keeps backing pages held. - `AlreadyRetired = 6`: Duplicate `retire_lease` on an already-retired queue.