# 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. ## Driver-backed ordering adapter (`lease_io`) `Drain` exclusively owns a backend and authorizes the exact PID and owner generation before reading even its clock. A single attempt performs: 1. Observe the device/epoch/owner, held pages and allocator baseline. 2. Stop submissions and read back the admission state. 3. Mask the IRQ and observe zero active handlers. 4. Stop workers and observe zero live threads, retaining their stacks. 5. Drain DMA and observe zero requests in flight. 6. Reset and observe zero device status and ready queues. 7. Release pages and observe zero held pages and the exact allocator delta. Every observation must preserve device identity and all previously established conditions. Blocking operations receive one absolute monotonic deadline. A failed command/readback/deadline prevents further commands and all retries. The private release receipt is issued only after the final physical accounting check. A release failure can be partial: the outcome is `Unverified` and `held_pages` is `None`, never a fabricated zero or an assumed original count. The last snapshot is explicitly marked stale after an unsuccessful operation. The backend must provide actual register, queue, thread and allocator reads with the required hardware barriers and exclusive ownership. Non-release operations must retain backing pages, and dropping an unresolved backend must not free DMA. Host fixtures test these calls and refusal paths; the adapter alone does not establish native device drain or K2155 guest acceptance.