An agent writes. fic checks, verifies and runs.
Asked for a debug tap that would let the Low side read High data, a coding agent adds it. fic rejects it, and the agent backs out and logs on the High side instead. Then every build derives the system model from the program itself: fire verify proves the C contracts and checks the model, and fire run boots the image in QEMU, where the frames cross the diode.
A real Claude Code session (Sonnet 5.5) with fic 2.81.3 on its demo/datadiode. Prompts and the agent's messages are shortened; file reads, warnings, repeated lines and two runs that logged to a mistyped path are left out; nothing is added.
Security and correctness, built in.
Fiducia closes the trust gap between the formally verified seL4 kernel and the applications built on top. It catches entire classes of bugs at compile time that would be invisible at runtime.
The language
Process-Oriented Programming
Each protection domain is a CSP-style process that orchestrates channel communication and computation events, calls into C, with guarded choice. Clear, composable and verifiable.
Secure by Design
A two-level security type system (Low/High) statically enforces that sensitive data never leaks to lower-security domains. No runtime checks needed.
Deadlock-Free by Design
Every well-formed system, with acyclic communication dependencies and guarded choices that partition the state space, is deadlock-free and deterministic.
The verification story
Design by Contract
Computation stays in C or Pancake. Its ACSL or Viper contracts are proved with Frama-C or Viper, and each contract becomes the event's specification in the system model.
Continuous Verification
Every build is also a verification run: fic derives a UPPAAL model of the whole system directly from your program (autoformalisation), so the model never drifts from the code that ships.
Code, formalise, verify, repair. Then build.
Every check that fails produces a counterexample, and the counterexample drives the next repair. Only a system where every check holds is compiled to an image.
.fis, each protection domain in .fi, computation in C or Pancake under ACSL or Viper contracts.fic checks information flow and channels; the UPPAAL model checker checks the generated model.Example
A data diode, all the way through.
Frames enter on the Low side, cross one forwarding domain, and leave on the High side, never the reverse. These are the files a developer writes; everything else is generated. Pick one to read it.
// Data diode: eth_outer → dd → eth_inner, one-way Low → High. // A frame crosses the diode by copy, as the channels' value; `data` holds // a virtio-net buffer (`VIRTIO_NET_BUF_SIZE`). struct Frame { len: uint16; // The NIC the frame came in on (ORIGIN_*), which the model tracks for // secrecy; the bytes stay in the C. origin: uint8; data: uint8[2048]; } // A frame's origin: the outer (Low) or inner (High) network. Neither is 0, // so a zeroed frame has no origin. const ORIGIN_OUTER: uint8 = 1; const ORIGIN_INNER: uint8 = 2; // The channels' rings, mapped into both endpoints of their channel. Low MemoryRegion eth_outer_output(0x200000, 0x200000); High MemoryRegion eth_inner_input(0x200000, 0x200000); // One 4 KiB page of QEMU virt's virtio-mmio slots 24..31, where both NICs // sit; each eth PD binds a different device in it. MemoryRegion virtio_window(0x1000, 0x1000, 0xa003000); // Virtio rings + buffers; physically contiguous, paddr patched by the tool. Low MemoryRegion outer_dma(0x200000, 0x200000); High MemoryRegion inner_dma(0x200000, 0x200000); // IRQ = 48 + mmio slot. QEMU puts the first -device in slot 31 (IRQ 79), // the second in slot 30 (IRQ 78). ProtectionDomain eth_outer(99, 79); ProtectionDomain dd(150); ProtectionDomain eth_inner(199, 78); Chan o2d = eth_outer --> dd via eth_outer_output<Frame>; Chan d2i = dd --> eth_inner via eth_inner_input<Frame>; System = eth_outer ||| dd ||| eth_inner;
// Low-side NIC: forwards frames not addressed to us across the diode, // answers ARP/ping for us locally. import "include/eth.h" as eth; // Mapping names match eth_inner's: one eth.c serves both PDs. Low Mapping vwin(virtio_window, 0x2_000_000, "rw", false); Low Mapping dma(outer_dma, 0x2_400_000, "rw", false); // Set by each poll: rx is true if a frame was read; kind 1 = forward, // 2 = ARP for us, 3 = IPv4 for us, 0 = drop. rx: bool; kind: int; // Set by handle_ping: replied is true if a reply was built, and reply_len // is its length. replied: bool; reply_len: uint16; // What `o2d` sends: the frame, copied into the ring. frame: Frame; // nth=1: the outer NIC (first -device) is in the higher mmio slot. // The slot the NIC came up in, or negative if it did not. nic: int; evt setup: nic = eth.eth_setup(1, dma.paddr); evt nic_up: dbg("virtio-net up on slot ", nic); evt nic_failed: dbg("FAIL virtio-net init"); evt handle_irq: eth.eth_clear_irq(); evt poll: { rx = eth.eth_poll(); kind = eth.eth_pkt_kind(); } evt stage_forward: { frame = eth.eth_frame(ORIGIN_OUTER); dbg("forward -> o2d"); } evt handle_ping: { replied = eth.eth_make_reply(); reply_len = eth.eth_reply_len(); } evt send_frame.|len: uint16|: { eth.eth_send_frame(len); dbg("Packet sent to network len=", len); } @bind(microkit.init) proc Init() { setup -> ReportNic(); } proc ReportNic() { [nic >= 0] nic_up -> Skip [] [nic < 0] nic_failed -> Skip; } @bind(microkit.notified) proc Notified() { irq?79 -> HandleRx(); Notified(); } proc HandleRx() { handle_irq -> Skip; Drain(); irq!79 -> Skip; } proc Drain() { poll -> Skip; [rx && kind == 1] stage_forward -> o2d!frame -> Drain() [] [rx && (kind == 2 || kind == 3)] handle_ping -> Reply() [] [rx && kind == 0] Drain() default Skip; } // Send the reply, if one was built: a request shorter than its headers // state gets none. proc Reply() { [replied] send_frame.reply_len -> Drain() [] [!replied] Drain(); }
// Forwards frames one way, o2d -> d2i. A full d2i loses the frame: the Low // side is never back-pressured. @bind(microkit.init) proc Init() { ready -> Skip; } @bind(microkit.notified) proc Notified() { o2d?f -> forward -> d2i!f -> Notified(); } evt ready: dbg("diode transport ready"); evt forward: dbg("pass o2d -> d2i");
// High-side NIC: transmits frames received on d2i; its own RX is acked // and dropped. import "include/eth.h" as eth; // Mapping names match eth_outer's: one eth.c serves both PDs. High Mapping vwin(virtio_window, 0x2_000_000, "rw", false); High Mapping dma(inner_dma, 0x2_400_000, "rw", false); // nth=0: the inner NIC (second -device) is in the lower mmio slot. // The slot the NIC came up in, or negative if it did not. nic: int; evt setup: nic = eth.eth_setup(0, dma.paddr); evt nic_up: dbg("virtio-net up on slot ", nic); evt nic_failed: dbg("FAIL virtio-net init"); evt handle_irq: eth.eth_clear_irq(); evt send_frame.|f: Frame|: { eth.eth_send_frame_value(f); dbg("delivered"); } @bind(microkit.init) proc Init() { setup -> ReportNic(); } proc ReportNic() { [nic >= 0] nic_up -> Skip [] [nic < 0] nic_failed -> Skip; } @bind(microkit.notified) proc Notified() { irq?78 -> DrainNic() [] d2i?f -> Deliver(f); Notified(); } proc Deliver(f: Frame) { send_frame.f -> Skip; } proc DrainNic() { handle_irq -> Skip; irq!78 -> Skip; }
/* NIC helpers shared by eth_outer and eth_inner, over virtio-net. */ #ifndef ETH_H #define ETH_H #include <stdbool.h> #include <stdint.h> #include "gen/types.h" /* Bring up the `nth` virtio-net device in the shared MMIO window: the * slot it is in, or negative if it did not come up. */ int64_t eth_setup(int nth, uintptr_t dma_paddr); /* Read-and-acknowledge the device interrupt (once per notification). */ void eth_clear_irq(void); /* Read and classify the next received frame; false if none. The model * reads the contract as a choice: a frame may or may not have arrived. */ /*@ ensures \result == 0 || \result == 1; */ bool eth_poll(void); /* Class of the current frame: 0 none, 1 forward across the diode, * 2 ARP request for us, 3 ICMP echo request for us. */ /*@ ensures 0 <= \result <= 3; */ int64_t eth_pkt_kind(void); /* The current frame, to send on o2d. */ /*@ ensures \result.origin == origin; */ struct Frame eth_frame(uint8_t origin); /* Build the ARP/ping reply to the current frame. Returns true if a reply * was built, false if the frame is shorter than its headers state. The model * reads the contract as a choice: a request may or may not get a reply. */ /*@ ensures \result == 0 || \result == 1; */ bool eth_make_reply(void); /* Length of the reply eth_make_reply built; 0 if it built none. */ uint16_t eth_reply_len(void); /* Transmit the reply built by eth_make_reply, if there is one. */ void eth_send_frame(uint16_t len); /* Transmit a frame received on d2i. */ void eth_send_frame_value(struct Frame f); #endif /* ETH_H */
#include "eth.h" /* Generated by fic: the memory routines every PD links, and their contracts. */ #include "gen/mem.h" #include "util.h" #include "virtio_net.h" #define ETHERTYPE_IPV4 0x0800 #define ETHERTYPE_ARP 0x0806 /* Byte offsets of a frame's fields. The frame is read and written through * these offsets, not through structs laid over the buffer, so that Frama-C * can follow every access. Multi-byte fields are big-endian. */ /* Ethernet: destination MAC, source MAC, ethertype. */ #define ETH_DST 0 #define ETH_SRC 6 #define ETH_TYPE 12 #define ETH_HDR_LEN 14 /* ARP for IPv4 over ethernet, after the ethernet header. */ #define ARP_OPER (ETH_HDR_LEN + 6) #define ARP_SHA (ETH_HDR_LEN + 8) #define ARP_SPA (ETH_HDR_LEN + 14) #define ARP_THA (ETH_HDR_LEN + 18) #define ARP_TPA (ETH_HDR_LEN + 24) #define ARP_LEN (ETH_HDR_LEN + 28) /* IPv4, after the ethernet header; its own header is at least 20 bytes. */ #define IP_VER_IHL (ETH_HDR_LEN + 0) #define IP_TOTAL_LEN (ETH_HDR_LEN + 2) #define IP_PROTOCOL (ETH_HDR_LEN + 9) #define IP_SRC (ETH_HDR_LEN + 12) #define IP_DST (ETH_HDR_LEN + 16) #define IP_MIN_HDR 20 #define IP_PROTOCOL_ICMP 1 /* ICMP, after the IP header: type, code, checksum. */ #define ICMP_TYPE 0 #define ICMP_CKSUM 2 #define ICMP_HDR_LEN 4 extern uintptr_t vwin_vaddr; extern uintptr_t dma_vaddr; static uint8_t cur[VIRTIO_NET_BUF_SIZE]; static uint16_t cur_len; static int cur_kind; /* The reply, and its length (0 when there is none). */ static uint8_t scratch[VIRTIO_NET_BUF_SIZE]; static uint16_t reply_len; static const uint8_t broadcast_mac[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; int64_t eth_setup(int nth, uintptr_t dma_paddr) { return virtio_net_up(vwin_vaddr, (unsigned)nth, dma_vaddr, dma_paddr); } void eth_clear_irq(void) { virtio_net_ack(); } bool eth_poll(void) { if (!virtio_net_rx(cur, &cur_len)) { cur_kind = 0; return false; } cur_kind = 0; if (cur_len >= ETH_HDR_LEN) { uint32_t ethertype = ((uint32_t)cur[ETH_TYPE] << 8) | cur[ETH_TYPE + 1]; if (!(mac_match(&cur[ETH_DST], fic_eth_mac) || mac_match(&cur[ETH_DST], broadcast_mac))) { cur_kind = 1; } else if (ethertype == ETHERTYPE_ARP) { cur_kind = 2; } else if (ethertype == ETHERTYPE_IPV4) { cur_kind = 3; } } return true; } int64_t eth_pkt_kind(void) { /* cur_kind is one of 0..3; the test lets WP see it. */ if (cur_kind < 0 || cur_kind > 3) { return 0; } return cur_kind; } struct Frame eth_frame(uint8_t origin) { struct Frame f; f.len = cur_len <= sizeof(f.data) ? cur_len : (uint16_t)sizeof(f.data); f.origin = origin; fi_memcpy_u8(f.data, cur, f.len); return f; } /* Build the reply to the current frame in `scratch`: swap the addresses, * then answer an ARP request with our MAC, or an ICMP echo with an echo * reply. Every length the frame states is checked against the received * length before it is used; a frame shorter than its headers state gets no * reply. Returns the reply's length, 0 for none. */ static uint16_t build_reply(void) { uint16_t len = cur_len; if (len < ETH_HDR_LEN || len > sizeof(scratch)) { return 0; } fi_memcpy_u8(scratch, cur, len); set_mac(&scratch[ETH_DST], &cur[ETH_SRC]); set_mac(&scratch[ETH_SRC], fic_eth_mac); uint32_t ethertype = ((uint32_t)scratch[ETH_TYPE] << 8) | scratch[ETH_TYPE + 1]; if (ethertype == ETHERTYPE_ARP) { if (len < ARP_LEN) { return 0; } scratch[ARP_OPER] = 0; scratch[ARP_OPER + 1] = 2; /* reply */ set_mac(&scratch[ARP_THA], &scratch[ARP_SHA]); set_ip(&scratch[ARP_TPA], &scratch[ARP_SPA]); set_mac(&scratch[ARP_SHA], fic_eth_mac); set_ip(&scratch[ARP_SPA], fic_eth_ip); } else { if (len < ETH_HDR_LEN + IP_MIN_HDR) { return 0; } if (scratch[IP_PROTOCOL] == IP_PROTOCOL_ICMP) { /* The IP header length is the low four bits of this byte, in * 32-bit words. `%` and `*` stand in for `&` and `<<`: the * prover bounds the arithmetic, but not the bit operations. */ uint32_t header_len = (scratch[IP_VER_IHL] % 16u) * 4; uint32_t total = scratch[IP_TOTAL_LEN] * 256u + scratch[IP_TOTAL_LEN + 1]; /* The ICMP header must fit in the IP datagram, and the datagram * in the frame. */ if (header_len < IP_MIN_HDR || total < header_len + ICMP_HDR_LEN || ETH_HDR_LEN + total > len) { return 0; } uint32_t icmp = ETH_HDR_LEN + header_len; set_ip(&scratch[IP_DST], &scratch[IP_SRC]); set_ip(&scratch[IP_SRC], fic_eth_ip); scratch[icmp + ICMP_TYPE] = 0; /* echo reply */ scratch[icmp + ICMP_CKSUM] = 0; scratch[icmp + ICMP_CKSUM + 1] = 0; /* The checksum's first byte is in `cksum`'s low 8 bits. */ uint16_t sum = cksum(&scratch[icmp], total - header_len); scratch[icmp + ICMP_CKSUM] = (uint8_t)(sum & 0xffu); scratch[icmp + ICMP_CKSUM + 1] = (uint8_t)(sum >> 8); } } return len; } bool eth_make_reply(void) { reply_len = build_reply(); return reply_len != 0; } uint16_t eth_reply_len(void) { return reply_len; } void eth_send_frame(uint16_t len) { /* No reply was built. */ if (len == 0) { return; } if (len > sizeof(scratch)) { len = (uint16_t)sizeof(scratch); } virtio_net_tx(scratch, len); } void eth_send_frame_value(struct Frame f) { /* `f.len` is the sender's word, from the other side of the diode. */ uint16_t len = f.len <= sizeof(f.data) ? f.len : (uint16_t)sizeof(f.data); virtio_net_tx(f.data, len); }
#ifndef UTIL_H #define UTIL_H #include <stddef.h> #include <stdint.h> /* eth_outer's own addresses; frames to any other MAC are forwarded. */ extern const uint8_t fic_eth_mac[6]; extern const uint8_t fic_eth_ip[4]; /* The contracts say what each helper reads and writes, which is what its * callers' proofs rest on; `fire verify` proves them on util.c. */ /*@ assigns \nothing; */ uint16_t swap16(uint16_t v); /*@ requires \valid_read(a + (0 .. 5)) && \valid_read(b + (0 .. 5)); assigns \nothing; */ int mac_match(const uint8_t *a, const uint8_t *b); /*@ requires \valid(dst + (0 .. 5)) && \valid_read(src + (0 .. 5)); assigns dst[0 .. 5]; */ void set_mac(uint8_t *dst, const uint8_t *src); /*@ requires \valid(dst + (0 .. 3)) && \valid_read(src + (0 .. 3)); assigns dst[0 .. 3]; */ void set_ip(uint8_t *dst, const uint8_t *src); /*@ requires \valid_read(data + (0 .. len - 1)); assigns \nothing; */ uint16_t cksum(const uint8_t *data, size_t len); #endif /* UTIL_H */
#include "util.h" const uint8_t fic_eth_mac[6] = {0x00, 0x16, 0x3e, 0x12, 0x34, 0x56}; const uint8_t fic_eth_ip[4] = {10, 141, 2, 80}; uint16_t swap16(uint16_t v) { return (uint16_t)(((v & 0xffu) << 8) | (v >> 8)); } int mac_match(const uint8_t *a, const uint8_t *b) { /*@ loop invariant 0 <= i <= 6; loop assigns i; loop variant 6 - i; */ for (unsigned i = 0; i < 6; i++) { if (a[i] != b[i]) { return 0; } } return 1; } void set_mac(uint8_t *dst, const uint8_t *src) { /*@ loop invariant 0 <= i <= 6; loop assigns i, dst[0 .. 5]; loop variant 6 - i; */ for (unsigned i = 0; i < 6; i++) { dst[i] = src[i]; } } void set_ip(uint8_t *dst, const uint8_t *src) { /*@ loop invariant 0 <= i <= 4; loop assigns i, dst[0 .. 3]; loop variant 4 - i; */ for (unsigned i = 0; i < 4; i++) { dst[i] = src[i]; } } /* RFC 1071 internet checksum. */ uint16_t cksum(const uint8_t *d, size_t len) { uint32_t sum = 0; /*@ loop invariant len <= \at(len, Pre); loop invariant d == \at(d, Pre) + (\at(len, Pre) - len); loop assigns sum, d, len; loop variant len; */ while (len > 1) { sum += ((uint32_t)d[0] << 8) | d[1]; d += 2; len -= 2; } if (len) { sum += (uint32_t)d[0] << 8; } /* Fold the carries back in: each pass makes `sum` smaller. */ /*@ loop assigns sum; loop variant sum; */ while (sum > 0xffff) { sum = (sum % 0x10000) + (sum / 0x10000); } return swap16((uint16_t)~sum); }
/* Minimal virtio-net driver over the virtio-mmio modern (v2) transport: * raw ethernet frames, no offloads. * * Adapted from sDDF's virtio network driver (github.com/au-ts/sddf, * BSD-2-Clause) and the OASIS VIRTIO 1.2 virtqueue layouts (BSD-3-Clause). */ #ifndef VIRTIO_NET_H #define VIRTIO_NET_H #include <stdbool.h> #include <stdint.h> /* Queue size for both RX and TX rings (power of two, <= QueueNumMax). */ #define VIRTIO_NET_QUEUE_SIZE 128 /* Per-frame buffer: 12-byte virtio-net header + frame, 2 KiB stride. */ #define VIRTIO_NET_BUF_SIZE 2048 /* Longest frame: a buffer less its virtio-net header. */ #define VIRTIO_NET_FRAME_MAX (VIRTIO_NET_BUF_SIZE - 12) /* Number of virtio-mmio slots in one 4 KiB window page (0x200 stride). */ #define VIRTIO_MMIO_SLOTS_PER_PAGE 8 /* These contracts are assumed, not proved: the driver reaches the device * through raw addresses and `volatile` rings, which WP does not model. A * caller's proof uses them as given. `assigns` lists only what a caller can * name; it leaves out the driver's own state and the device's. */ /* Bring up the `nth` virtio-net device (counting from slot 0) in the * window page at `window_vaddr`, with its virtqueues in * [dma_vaddr, +2 MiB), uncached and physically contiguous, seen by the * device at dma_paddr. * Returns the slot index (0..7), negative on failure. */ /*@ assigns \nothing; */ int virtio_net_up(uintptr_t window_vaddr, unsigned nth, uintptr_t dma_vaddr, uintptr_t dma_paddr); /* Pop one received frame, without its virtio-net header, into `dst` * (>= VIRTIO_NET_BUF_SIZE). `*len` is at most VIRTIO_NET_FRAME_MAX, whatever * the device reports. False if nothing pending or the frame is empty. */ /*@ requires \valid(dst + (0 .. VIRTIO_NET_BUF_SIZE - 1)) && \valid(len); assigns dst[0 .. VIRTIO_NET_BUF_SIZE - 1], *len; ensures *len <= VIRTIO_NET_FRAME_MAX; */ bool virtio_net_rx(uint8_t *dst, uint16_t *len); /* Queue one frame for transmission; `frame` is copied, its first * VIRTIO_NET_FRAME_MAX bytes if `len` is more. */ /*@ requires \valid_read(frame + (0 .. len - 1)); assigns \nothing; */ void virtio_net_tx(const uint8_t *frame, uint16_t len); /* Read and acknowledge the device's interrupt status. */ /*@ assigns \nothing; */ uint32_t virtio_net_ack(void); #endif /* VIRTIO_NET_H */
#include "virtio_net.h" /* ── virtio-mmio register block (VIRTIO 1.2 §4.2.2) ──────────────────── */ #define VIRTIO_MMIO_MAGIC 0x74726976u #define VIRTIO_DEVICE_ID_NET 1 typedef volatile struct { uint32_t MagicValue; uint32_t Version; uint32_t DeviceID; uint32_t VendorID; uint32_t DeviceFeatures; uint32_t DeviceFeaturesSel; uint32_t _reserved0[2]; uint32_t DriverFeatures; uint32_t DriverFeaturesSel; uint32_t _reserved1[2]; uint32_t QueueSel; uint32_t QueueNumMax; uint32_t QueueNum; uint32_t _reserved2[2]; uint32_t QueueReady; uint32_t _reserved3[2]; uint32_t QueueNotify; uint32_t _reserved4[3]; uint32_t InterruptStatus; uint32_t InterruptACK; uint32_t _reserved5[2]; uint32_t Status; uint32_t _reserved6[3]; uint32_t QueueDescLow; uint32_t QueueDescHigh; uint32_t _reserved7[2]; uint32_t QueueDriverLow; uint32_t QueueDriverHigh; uint32_t _reserved8[2]; uint32_t QueueDeviceLow; uint32_t QueueDeviceHigh; uint32_t _reserved9[21]; uint32_t ConfigGeneration; uint32_t Config[]; } virtio_mmio_regs_t; #define STATUS_ACKNOWLEDGE 0x1 #define STATUS_DRIVER 0x2 #define STATUS_DRIVER_OK 0x4 #define STATUS_FEATURES_OK 0x8 #define STATUS_FAILED 0x80 #define VIRTIO_F_VERSION_1 32 /* ── virtqueue layouts (OASIS VIRTIO 1.2) ────────────────────────────── */ #define VIRTQ_DESC_F_WRITE 2 struct virtq_desc { uint64_t addr; uint32_t len; uint16_t flags; uint16_t next; }; struct virtq_avail { uint16_t flags; uint16_t idx; uint16_t ring[]; }; struct virtq_used_elem { uint32_t id; uint32_t len; }; struct virtq_used { uint16_t flags; uint16_t idx; struct virtq_used_elem ring[]; }; /* 12-byte header preceding every frame under VIRTIO_F_VERSION_1. */ struct virtio_net_hdr { uint8_t flags; uint8_t gso_type; uint16_t hdr_len; uint16_t gso_size; uint16_t csum_start; uint16_t csum_offset; uint16_t num_buffers; }; #define NET_HDR_LEN ((uint32_t)sizeof(struct virtio_net_hdr)) /* ── driver state ────────────────────────────────────────────────────── */ #define Q VIRTIO_NET_QUEUE_SIZE struct vq { volatile struct virtq_desc *desc; volatile struct virtq_avail *avail; volatile struct virtq_used *used; volatile uint8_t *bufs; /* Q x VIRTIO_NET_BUF_SIZE */ uint64_t bufs_paddr; uint16_t last_used; }; static virtio_mmio_regs_t *regs; static struct vq rxq; static struct vq txq; static uint16_t tx_next; static inline void barrier(void) { __sync_synchronize(); } static int scan(uintptr_t window_vaddr, unsigned nth) { unsigned seen = 0; for (int slot = 0; slot < VIRTIO_MMIO_SLOTS_PER_PAGE; slot++) { virtio_mmio_regs_t *r = (virtio_mmio_regs_t *)(window_vaddr + 0x200u * slot); if (r->MagicValue == VIRTIO_MMIO_MAGIC && r->DeviceID == VIRTIO_DEVICE_ID_NET) { if (seen == nth) { return slot; } seen++; } } return -1; } /* Lay out one queue at DMA offset `off`, each ring on its own 4 KiB * page; returns the offset past it. */ static uintptr_t vq_carve(struct vq *q, uintptr_t dma_vaddr, uintptr_t dma_paddr, uintptr_t off) { uintptr_t desc_off = off; uintptr_t avail_off = desc_off + 0x1000; uintptr_t used_off = avail_off + 0x1000; uintptr_t bufs_off = used_off + 0x1000; q->desc = (volatile struct virtq_desc *)(dma_vaddr + desc_off); q->avail = (volatile struct virtq_avail *)(dma_vaddr + avail_off); q->used = (volatile struct virtq_used *)(dma_vaddr + used_off); q->bufs = (volatile uint8_t *)(dma_vaddr + bufs_off); q->bufs_paddr = dma_paddr + bufs_off; q->last_used = 0; for (unsigned i = 0; i < Q; i++) { q->desc[i].addr = q->bufs_paddr + (uint64_t)i * VIRTIO_NET_BUF_SIZE; q->desc[i].len = VIRTIO_NET_BUF_SIZE; q->desc[i].flags = 0; q->desc[i].next = 0; } q->avail->flags = 0; q->avail->idx = 0; q->used->flags = 0; return bufs_off + (uintptr_t)Q * VIRTIO_NET_BUF_SIZE; } static void vq_install(unsigned sel, const struct vq *q, uintptr_t dma_vaddr, uintptr_t dma_paddr) { uint64_t desc = (uintptr_t)q->desc - dma_vaddr + dma_paddr; uint64_t avail = (uintptr_t)q->avail - dma_vaddr + dma_paddr; uint64_t used = (uintptr_t)q->used - dma_vaddr + dma_paddr; regs->QueueSel = sel; if (regs->QueueNumMax < Q) { return; /* caller checks QueueReady */ } regs->QueueNum = Q; regs->QueueDescLow = (uint32_t)desc; regs->QueueDescHigh = (uint32_t)(desc >> 32); regs->QueueDriverLow = (uint32_t)avail; regs->QueueDriverHigh = (uint32_t)(avail >> 32); regs->QueueDeviceLow = (uint32_t)used; regs->QueueDeviceHigh = (uint32_t)(used >> 32); barrier(); regs->QueueReady = 1; } static int virtio_net_init(uintptr_t regs_vaddr, uintptr_t dma_vaddr, uintptr_t dma_paddr) { regs = (virtio_mmio_regs_t *)regs_vaddr; if (regs->MagicValue != VIRTIO_MMIO_MAGIC) { return -1; } if (regs->Version != 2) { return -2; /* legacy transport: launch QEMU with force-legacy=false */ } if (regs->DeviceID != VIRTIO_DEVICE_ID_NET) { return -3; } /* VIRTIO 1.2 §3.1.1 initialisation sequence. */ regs->Status = 0; regs->Status = STATUS_ACKNOWLEDGE; regs->Status = STATUS_ACKNOWLEDGE | STATUS_DRIVER; /* VERSION_1 only: no offloads. */ regs->DriverFeaturesSel = 0; regs->DriverFeatures = 0; regs->DriverFeaturesSel = 1; regs->DriverFeatures = 1u << (VIRTIO_F_VERSION_1 - 32); regs->Status = STATUS_ACKNOWLEDGE | STATUS_DRIVER | STATUS_FEATURES_OK; if (!(regs->Status & STATUS_FEATURES_OK)) { regs->Status = STATUS_FAILED; return -4; } uintptr_t off = vq_carve(&rxq, dma_vaddr, dma_paddr, 0); (void)vq_carve(&txq, dma_vaddr, dma_paddr, off); tx_next = 0; vq_install(0, &rxq, dma_vaddr, dma_paddr); vq_install(1, &txq, dma_vaddr, dma_paddr); regs->QueueSel = 0; if (!regs->QueueReady) { regs->Status = STATUS_FAILED; return -5; } for (unsigned i = 0; i < Q; i++) { rxq.desc[i].flags = VIRTQ_DESC_F_WRITE; rxq.avail->ring[i] = (uint16_t)i; } barrier(); rxq.avail->idx = Q; barrier(); regs->Status = STATUS_ACKNOWLEDGE | STATUS_DRIVER | STATUS_FEATURES_OK | STATUS_DRIVER_OK; regs->QueueNotify = 0; /* kick RX so the device sees the buffers */ return 0; } int virtio_net_up(uintptr_t window_vaddr, unsigned nth, uintptr_t dma_vaddr, uintptr_t dma_paddr) { int slot = scan(window_vaddr, nth); if (slot < 0) { return -100; } int rc = virtio_net_init(window_vaddr + 0x200u * (unsigned)slot, dma_vaddr, dma_paddr); return rc == 0 ? slot : rc; } bool virtio_net_rx(uint8_t *dst, uint16_t *len) { barrier(); if (rxq.last_used == rxq.used->idx) { return false; } volatile struct virtq_used_elem *e = &rxq.used->ring[rxq.last_used % Q]; uint16_t id = (uint16_t)e->id; uint32_t total = e->len; uint16_t frame_len = 0; if (total > NET_HDR_LEN) { /* The length is the device's word; the buffer it filled is ours. */ uint32_t n = total - NET_HDR_LEN; if (n > VIRTIO_NET_FRAME_MAX) { n = VIRTIO_NET_FRAME_MAX; } frame_len = (uint16_t)n; volatile const uint8_t *src = rxq.bufs + (uint32_t)id * VIRTIO_NET_BUF_SIZE + NET_HDR_LEN; for (uint16_t i = 0; i < frame_len; i++) { dst[i] = src[i]; } } *len = frame_len; rxq.last_used++; rxq.avail->ring[rxq.avail->idx % Q] = id; barrier(); rxq.avail->idx++; barrier(); regs->QueueNotify = 0; return frame_len != 0; } void virtio_net_tx(const uint8_t *frame, uint16_t len) { uint16_t id = tx_next % Q; tx_next++; /* Header and frame fit one buffer. */ if (len > VIRTIO_NET_FRAME_MAX) { len = VIRTIO_NET_FRAME_MAX; } volatile uint8_t *buf = txq.bufs + (uint32_t)id * VIRTIO_NET_BUF_SIZE; for (unsigned i = 0; i < NET_HDR_LEN; i++) { buf[i] = 0; /* no offloads: zeroed virtio-net header */ } for (uint16_t i = 0; i < len; i++) { buf[NET_HDR_LEN + i] = frame[i]; } txq.desc[id].len = NET_HDR_LEN + len; txq.desc[id].flags = 0; txq.avail->ring[txq.avail->idx % Q] = id; barrier(); txq.avail->idx++; barrier(); regs->QueueNotify = 1; } uint32_t virtio_net_ack(void) { uint32_t status = regs->InterruptStatus; regs->InterruptACK = status; return status; }
/* Queries for `fire verify demo/datadiode/`. Each buffered channel is a queue of its length, the signal word and Microkit's pending bit, at most 3 long in the model; the Environment raises each NIC's IRQ for ever, as inject.py keeps feeding frames, masked until the driver acks it, as seL4 does (#355, #601). */ /* Never stuck: the device always has a move unless a driver never takes or never acks its IRQ, so a deadlock would be a stuck driver. */ A[] not deadlock /* A frame crosses each hop: dd takes one from o2d, eth_inner one from d2i. */ E<> dd.L2 E<> eth_inner.Deliver /* No lost wake-up: a parked receiver with a value waiting is always notified. A receiver stuck this way is not a deadlock, since eth_outer keeps running; these two catch it. */ A[] not (!dd.awake && q_o2d.length > 0 && !pend_o2d) A[] not (!eth_inner.awake && q_d2i.length > 0 && !pend_d2i) /* Secrecy: no Low PD, and nothing in o2d, ever holds a frame that came in on the inner (High) network. Frames carry the NIC they came in on, set by eth_frame's contract and proved on eth.c. */ A[] not (eth_outer.frame.origin == ORIGIN_INNER || dd.f.origin == ORIGIN_INNER || (exists (i : int[0, 2]) q_o2d_val[i].origin == ORIGIN_INNER)) /* The origin travels: dd receives a frame tagged with the outer network. */ E<> dd.f.origin == ORIGIN_OUTER
fire verify demo/datadiode/ first proves the contracts in eth.h and util.h on eth.c and util.c with Frama-C, taking the virtio-net driver's contracts as assumed, then checks the model of all three domains against the seven queries in datadiode.q, both in the file list above.
Walk through it in the tutorial, or run it yourself with fire run demo/datadiode/ (fire run in the manual).
