# import ion # Included in ion.nim # Error type for future use type FiberPanic* = object of CatchableError # Forward declarations for utilities defined in kernel.nim (included there) proc kernel_panic*(msg: cstring) {.importc: "panic", cdecl.} proc kprintln*(s: cstring) {.importc: "kprintln", cdecl.} template secure_send*(ring_ptr: pointer, data: uint64) = ## Verifies invariants before pushing to a command ring. # 1. PRE-CONDITION: Alignment if (cast[uint](ring_ptr) and 0b11) != 0: kernel_panic("Invariant Violation: Unaligned Ring Pointer") # 2. OPERATION: Try to push via HAL # We cast uint64 back to CmdPacket for the FFI call let success = hal_cmd_push(cast[uint64](ring_ptr), cast[CmdPacket](data)) # 3. POST-CONDITION: Flow Control Warning if not success: kprintln("[Invariant] Warning: Command Ring Full, Drop.") template secure_push_packet*(ring_ptr: pointer, pkt: IonPacket) = ## Verifies invariants for IonPacket transfers. if (cast[uint](ring_ptr) and 0b11) != 0: kernel_panic("Invariant Violation: Unaligned Ring Pointer") if not hal_channel_push(cast[uint64](ring_ptr), pkt): kprintln("[Invariant] Warning: Packet Ring Full, Drop.") template secure_recv_cmd*(ring_ptr: pointer, out_pkt: var CmdPacket): bool = if (cast[uint](ring_ptr) and 0b11) != 0: kernel_panic("Invariant Violation: Unaligned Ring Pointer") hal_cmd_pop(cast[uint64](ring_ptr), addr out_pkt)