src/virtio/device.bend source
src/virtio/device.bend on the hub · documented module
import Baseimport ../lib/u64.bend as Uimport ../lib/bytes.bend as Bimport ../vmm/abi.bend as Vimport ./mmio.bend as Mimport ./queue.bend as Qimport ./rng.bend as Rngimport ./blk.bend as Blkimport ../disk/store.bend as Stimport ./console.bend as Conimport ./net.bend as Nimport ../fs/p9.bend as Pimport ./vsock.bend as Vsimport ./input.bend as Inimport ./lan.bend as Lnimport ./balloon.bend as Bl# A virtio-mmio device: the transport register model plus the device logic# behind it, as the machine sees it.## Every kind offers VIRTIO_F_VERSION_1 (feature bit 32); queues hold 256# entries:# RngKind: device id 4, one queue, empty config space, no other feature# BlkKind: device id 2, one queue, config space# Blk.config_dis(sectors), also VIRTIO_BLK_F_FLUSH,# SIZE_MAX, SEG_MAX and DISCARD (Blk.offered_dis(False))# BlkRoKind: a read-only BlkKind, also VIRTIO_BLK_F_RO# BlkStoreKind: a BlkKind (BlkRoKind when ro) over a store (qcow2:# src/disk/store.bend)# (Blk.offered(True)); OUT requests answer IOERR# ConsoleKind: virtio-console, device id 3, two queues (0 receiveq0,# 1 transmitq0), empty config space, no other feature# InputKind: virtio-input, device id 18, two queues (0 eventq, 1# statusq), a keyboard or (tablet) a tablet; its config# space follows the driver's select/subsel writes# (In.Input.config)# NetKind: virtio-net, device id 1, two queues (0 receiveq1,# 1 transmitq1), also VIRTIO_NET_F_MAC, CSUM, GUEST_CSUM,# GUEST_TSO4 and HOST_TSO4 (U.U64{1, 2211}: see# src/virtio/net.bend), config# space N.Net.config(mac)# BalloonKind: virtio-balloon, device id 5, three queues (0 inflateq,# 1 deflateq, 2 reporting_vq), also FREE_PAGE_REPORTING# and DEFLATE_ON_OOM (Bl.Balloon.offered()), config Bl.Balloon.config()# (num_pages, actual: Dev.balloon sets the target)# P9Kind: virtio-9p on host share `share`, device id 9, one queue,# also VIRTIO_9P_MOUNT_TAG (U.U64{1, 1}), config space# P.P9.config(tag)## Dev.state holds what a kind keeps between passes: the console input not# yet delivered, the frames for the guest not yet delivered (at most# Dev.max_frames(), the oldest dropped first), the 9p server's state.## Writes go to the transport (M.VirtioMmio.write), then:# DeviceReset: the queue list empties# DriverOk: one Q.Vq per queue, from its QueueCfg (Q.Vq.from_cfg), in order# Notify{q}: when queue q exists, the kind's pass runs on it and the# updated queue is stored; when it used any chain, interrupt# status bit 1 is raised (M.VirtioMmio.raise). Passes:# Rng, Blk, BlkRo: Dev.process on its queue# Console: queue 1 Con.tx (its bytes go to DevOut.console),# queue 0 Con.rx with the pending input# Net: queue 1 Net.tx (its frames go to DevOut.frames),# queue 0 Net.rx with the pending frames# P9: queue 0 P9.process# NoEvent: nothing moretype DevKind is Data: RngKind{} BlkKind{disk: U32, sectors: U.U64} BlkRoKind{disk: U32, sectors: U.U64} # A virtio-blk over a store (a qcow2 image over its backing files), # read-only when ro. BlkStoreKind{store: St.Store, sectors: U.U64, ro: Bool} ConsoleKind{} NetKind{mac: +List<U32>} P9Kind{share: U32, tag: String} VsockKind{cid: U.U64} InputKind{tablet: Bool} # virtio-net on a host LAN (src/virtio/lan.bend): its MAC, and the # address its DHCP answers give (ip 0: none) with its prefix length. LanKind{mac: +List<U32>, ip: U32, prefix: U32} # virtio-balloon with free page reporting (src/virtio/balloon.bend). BalloonKind{auto: Bool} # The automatic balloon holding at most pct percent of the guest's RAM # (--balloon auto,max=PCT; BalloonKind{True{}} holds at most half). BalloonMaxKind{pct: U32}# The per-kind state of a device.type DevState is Data: NoState{} # Console input not yet delivered. ConState{input: +List<U32>} # Frames for the guest not yet delivered, oldest first. NetState{frames: +List<+List<U32>>} P9Dev{p9: P.P9State} VsState{vs: Vs.Vsock} InDev{input: In.InputState} LanDev{lan: Ln.Lan} # The balloon's target, count and partly inflated blocks. BalDev{bal: Bl.Bal, auto: Bl.AutoSt}type Dev is Data: Dev{kind: DevKind, mmio: M.VirtioMmio, queues: +List<Q.Vq>, state: DevState}# The device after a write, the bytes the guest wrote to hvc0 and the frames# the guest transmitted, in order.type DevOut is Data: DevOut{dev: Dev, console: +List<U32>, frames: +List<+List<U32>>, vsock: +List<V.VsockCmd>}# VIRTIO_F_VERSION_1: bit 32, so bit 0 of the high word (the rng features).def Dev.features() -> U.U64: U.U64{1, 0}# The most frames a net device keeps for the guest.def Dev.max_frames() -> Nat: 256n# The transport of a fresh device of the given kind.def Dev.mmio(kind: DevKind) -> M.VirtioMmio: match kind: case RngKind{}: M.VirtioMmio.new(4, Dev.features(), 1n, 256, []) case BlkKind{disk, sectors}: M.VirtioMmio.new(2, Blk.Blk.offered_wz(False{}), 1n, 256, Blk.Blk.config_wz(sectors)) case BlkRoKind{disk, sectors}: M.VirtioMmio.new(2, Blk.Blk.offered(True{}), 1n, 256, Blk.Blk.config_limits(sectors)) case BlkStoreKind{store, sectors, +ro}: M.VirtioMmio.new(2, Blk.Blk.offered_dis(ro), 1n, 256, Blk.Blk.config_dis(sectors)) case ConsoleKind{}: M.VirtioMmio.new(3, Dev.features(), 2n, 256, []) case NetKind{mac}: M.VirtioMmio.new(1, U.U64{1, 2211}, 2n, 256, N.Net.config(mac)) case P9Kind{share, tag}: M.VirtioMmio.new(9, U.U64{1, 1}, 1n, 256, P.P9.config(tag)) case VsockKind{cid}: M.VirtioMmio.new(19, Dev.features(), 3n, 256, Vs.Vs.config(cid)) case InputKind{tablet}: M.VirtioMmio.new(18, Dev.features(), 2n, 256, In.Input.config(tablet, 0, 0)) case LanKind{mac, ip, prefix}: M.VirtioMmio.new(1, Ln.Lan.features(), 2n, 256, Ln.Lan.config(mac)) case BalloonKind{+auto}: Bool.pick(M.VirtioMmio, auto, M.VirtioMmio.new(5, Bl.Balloon.offered_auto(), Bl.Balloon.queues_auto(), 256, Bl.Balloon.config()), M.VirtioMmio.new(5, Bl.Balloon.offered(), Bl.Balloon.queues(), 256, Bl.Balloon.config())) case BalloonMaxKind{pct}: M.VirtioMmio.new(5, Bl.Balloon.offered_auto(), Bl.Balloon.queues_auto(), 256, Bl.Balloon.config())# The state of a fresh device of the given kind.def Dev.state(kind: DevKind) -> DevState: match kind: case RngKind{}: NoState{} case BlkKind{disk, sectors}: NoState{} case BlkRoKind{disk, sectors}: NoState{} case BlkStoreKind{store, sectors, ro}: NoState{} case ConsoleKind{}: ConState{[]} case NetKind{mac}: NetState{[]} case P9Kind{share, tag}: P9Dev{P.P9State.new()} case VsockKind{cid}: VsState{Vs.Vsock.new(cid)} case InputKind{tablet}: InDev{In.InputState.new()} case LanKind{mac, ip, prefix}: LanDev{Ln.Lan.new(mac, ip, prefix)} case BalloonKind{auto}: BalDev{Bl.Balloon.new(), Bl.Balloon.auto_new()} case BalloonMaxKind{pct}: BalDev{Bl.Balloon.new(), Bl.Balloon.auto_new()}def Dev.new(kind: DevKind) -> Dev: +kind = kind Dev{kind, Dev.mmio(kind), [], Dev.state(kind)}def Dev.read.fin(k: DevKind, qs: +List<Q.Vq>, st: DevState, r: M.VirtioMmio & U32) -> Dev & U32: match r: case (m, v): (Dev{k, m, qs, st}, v)def Dev.read(d: Dev, off: U32, size: U32) -> Dev & U32: match d: case Dev{k, m, qs, st}: Dev.read.fin(k, qs, st, M.VirtioMmio.read(m, off, size))# One Q.Vq per queue configuration, in order.def Dev.build(cs: +List<M.QueueCfg>) -> +List<Q.Vq>: match cs: case Nil{}: [] case c <> t: Q.Vq.from_cfg(c) <> Dev.build(t)def Dev.cfgs(m: M.VirtioMmio) -> +List<M.QueueCfg>: match m: case M.VirtioMmio{id, df, drf, dsel, drsel, qsel, qs, st, ist, cg, cfg}: qs# Queue sel of the list; i counts the list position.def Dev.qget(qs: +List<Q.Vq>, +i: U32, +sel: U32) -> Maybe<&2, Q.Vq>: match qs: case Nil{}: None{} case +h <> t: Bool.pick(Maybe<&2, Q.Vq>, U32.is_eq(i, sel), Some{h}, Dev.qget(t, (i + 1 : U32), sel))# Replaces queue sel of the list by q; i counts the list position.def Dev.qput(qs: +List<Q.Vq>, +i: U32, +sel: U32, +q: Q.Vq) -> +List<Q.Vq>: match qs: case Nil{}: [] case +h <> t: Bool.pick(Q.Vq, U32.is_eq(i, sel), q, h) <> Dev.qput(t, (i + 1 : U32), sel, q)# Raises the used-buffer interrupt (status bit 1) when used.def Dev.raise_if(used: Bool, m: M.VirtioMmio) -> M.VirtioMmio: match used: case True{}: M.VirtioMmio.raise(m, 1) case False{}: m# Runs the process of a kind without state on a queue. The kinds with state# (console, net, 9p) run their passes in Dev.pass instead; here they use no# chain.def Dev.process(-R: Type, k: DevKind, q: Q.Vq) -> V.Op<R, Q.Vq & Bool>: match k: case RngKind{}: Rng.Rng.process(R, q) case BlkKind{disk, sectors}: Blk.Blk.process(R, disk, sectors, q) case BlkRoKind{disk, sectors}: Blk.Blk.serve(R, disk, sectors, True{}, q) case BlkStoreKind{store, sectors, ro}: Blk.Blk.serve_store(R, store, sectors, ro, q) case ConsoleKind{}: V.Op.pure(R, Q.Vq & Bool, (q, False{})) case NetKind{mac}: V.Op.pure(R, Q.Vq & Bool, (q, False{})) case P9Kind{share, tag}: V.Op.pure(R, Q.Vq & Bool, (q, False{})) case VsockKind{cid}: V.Op.pure(R, Q.Vq & Bool, (q, False{})) case InputKind{tablet}: V.Op.pure(R, Q.Vq & Bool, (q, False{})) case LanKind{mac, ip, prefix}: V.Op.pure(R, Q.Vq & Bool, (q, False{})) case BalloonKind{auto}: V.Op.pure(R, Q.Vq & Bool, (q, False{})) case BalloonMaxKind{pct}: V.Op.pure(R, Q.Vq & Bool, (q, False{}))# The pending console input of a state (none for other kinds).def Dev.input_of(st: DevState) -> +List<U32>: match st: case ConState{bs}: bs case NoState{}: [] case NetState{fs}: [] case P9Dev{p}: [] case VsState{v}: [] case InDev{i}: [] case LanDev{l}: [] case BalDev{b, a}: []# The pending frames of a state (none for other kinds).def Dev.frames_of(st: DevState) -> +List<+List<U32>>: match st: case NetState{fs}: fs case NoState{}: [] case ConState{bs}: [] case P9Dev{p}: [] case VsState{v}: [] case InDev{i}: [] case LanDev{l}: [] case BalDev{b, a}: []# The 9p server state of a state (a fresh one for other kinds).def Dev.p9_of(st: DevState) -> P.P9State: match st: case P9Dev{p}: p case NoState{}: P.P9State.new() case ConState{bs}: P.P9State.new() case NetState{fs}: P.P9State.new() case VsState{v}: P.P9State.new() case InDev{i}: P.P9State.new() case LanDev{l}: P.P9State.new() case BalDev{b, a}: P.P9State.new()# The last Dev.max_frames() frames.def Dev.cap(+fs: +List<+List<U32>>) -> +List<+List<U32>>: List.drop(&2, +List<U32>, fs, Nat.sub(List.length(&2, +List<U32>, fs), Dev.max_frames()))# What a pass on one queue did: the queue, the new state, the bytes for the# host console, the frames for the host, and whether it used any chain.type Pass is Data: Pass{q: Q.Vq, state: DevState, console: +List<U32>, frames: +List<+List<U32>>, vsock: +List<V.VsockCmd>, used: Bool}def Dev.pass.plain(st: DevState, r: Q.Vq & Bool) -> Pass: match r: case (q, used): Pass{q, st, [], [], [], used}def Dev.pass.con_tx(st: DevState, r: Con.ConTx) -> Pass: match r: case Con.ConTx{q, bs, used}: Pass{q, st, bs, [], [], used}def Dev.pass.con_rx(r: Con.ConRx) -> Pass: match r: case Con.ConRx{q, left, used}: Pass{q, ConState{left}, [], [], [], used}def Dev.pass.net_tx(st: DevState, r: N.NetTx) -> Pass: match r: case N.NetTx{q, fs, used}: Pass{q, st, [], fs, [], used}def Dev.pass.net_rx(r: N.NetRx) -> Pass: match r: case N.NetRx{q, left, used}: Pass{q, NetState{left}, [], [], [], used}def Dev.pass.p9(r: P.P9Pass) -> Pass: match r: case P.P9Pass{s, q, used}: Pass{q, P9Dev{s}, [], [], [], used}# The console's pass: transmitq0 (tx) or receiveq0.def Dev.pass.con(-R: Type, tx: Bool, st: DevState, q: Q.Vq) -> V.Op<R, Pass>: match tx: case True{}: do V.Op<R, Pass>: r : Con.ConTx <- Con.Con.tx(R, q) return Dev.pass.con_tx(st, r) case False{}: do V.Op<R, Pass>: r : Con.ConRx <- Con.Con.rx(R, q, Dev.input_of(st)) return Dev.pass.con_rx(r)# The net device's pass: transmitq1 (tx) or receiveq1.def Dev.pass.net(-R: Type, tx: Bool, st: DevState, q: Q.Vq) -> V.Op<R, Pass>: match tx: case True{}: do V.Op<R, Pass>: r : N.NetTx <- N.Net.tx(R, q) return Dev.pass.net_tx(st, r) case False{}: do V.Op<R, Pass>: r : N.NetRx <- N.Net.rx(R, q, Dev.frames_of(st)) return Dev.pass.net_rx(r)# The vsock state of a state (a fresh one, CID 3, for other kinds).def Dev.vs_of(st: DevState) -> Vs.Vsock: match st: case VsState{v}: v case NoState{}: Vs.Vsock.new(U.U64{0, 3}) case ConState{bs}: Vs.Vsock.new(U.U64{0, 3}) case NetState{fs}: Vs.Vsock.new(U.U64{0, 3}) case P9Dev{p}: Vs.Vsock.new(U.U64{0, 3}) case InDev{i}: Vs.Vsock.new(U.U64{0, 3}) case LanDev{l}: Vs.Vsock.new(U.U64{0, 3}) case BalDev{b, a}: Vs.Vsock.new(U.U64{0, 3})def Dev.pass.vs_tx(r: Vs.VsTx) -> Pass: match r: case Vs.VsTx{v, q, cmds, used}: Pass{q, VsState{v}, [], [], cmds, used}def Dev.pass.vs_rx(r: Vs.VsRx) -> Pass: match r: case Vs.VsRx{v, q, cmds, used}: Pass{q, VsState{v}, [], [], cmds, used}# The vsock device's pass: tx (queue 1), rx (queue 0); the event queue (2)# is never used.def Dev.pass.vsock(-R: Type, tx: Bool, rx: Bool, st: DevState, q: Q.Vq) -> V.Op<R, Pass>: match tx rx: case True{} _: do V.Op<R, Pass>: r : Vs.VsTx <- Vs.Vsock.tx(R, Dev.vs_of(st), q) return Dev.pass.vs_tx(r) case False{} True{}: do V.Op<R, Pass>: r : Vs.VsRx <- Vs.Vsock.rx(R, Dev.vs_of(st), q) return Dev.pass.vs_rx(r) case False{} False{}: V.Op.pure(R, Pass, Pass{q, st, [], [], [], False{}})# The input state of a state (a fresh one for other kinds).def Dev.in_of(st: DevState) -> In.InputState: match st: case InDev{i}: i case _: In.InputState.new()def Dev.pass.in_rx(r: In.InRx) -> Pass: match r: case In.InRx{s, q, used}: Pass{q, InDev{s}, [], [], [], used}def Dev.pass.in_status(st: DevState, r: Con.ConTx) -> Pass: match r: case Con.ConTx{q, bs, used}: Pass{q, st, [], [], [], used}# The input device's pass: eventq (ev) gets the pending events; statusq# chains are taken and dropped.def Dev.pass.input(-R: Type, ev: Bool, st: DevState, q: Q.Vq) -> V.Op<R, Pass>: match ev: case True{}: do V.Op<R, Pass>: r : In.InRx <- In.Input.rx(R, Dev.in_of(st), q) return Dev.pass.in_rx(r) case False{}: do V.Op<R, Pass>: r : Con.ConTx <- Con.Con.tx(R, q) return Dev.pass.in_status(st, r)# The LAN state of a state (a blank one for other kinds).def Dev.lan_of(st: DevState) -> Ln.Lan: match st: case LanDev{l}: l case _: Ln.Lan.new([0, 0, 0, 0, 0, 0], 0, 0)# The balloon's state (a fresh one for a snapshot taken before it had one).def Dev.bal_of(st: DevState) -> Bl.Bal: match st: case BalDev{b, a}: b case _: Bl.Balloon.new()# The automatic balloon's state (a fresh one for a snapshot taken before it# had one).def Dev.auto_of(st: DevState) -> Bl.AutoSt: match st: case BalDev{b, a}: a case _: Bl.Balloon.auto_new()def Dev.pass.bal(r: Bl.BalPass, a: Bl.AutoSt) -> Pass: match r: case Bl.BalPass{q, b, used}: Pass{q, BalDev{b, a}, [], [], [], used}def Dev.pass.stats(r: Bl.StatsPass) -> Pass: match r: case Bl.StatsPass{q, b, a}: Pass{q, BalDev{b, a}, [], [], [], False{}}# The balloon's pass on its queue sel (the module's own numbering:# Bl.Balloon.logical).# (auto: the automatic balloon, holding at most pct percent of the guest's# RAM; after its deflate pass, Bl.Balloon.pass_after)def Dev.pass.balloon(-R: Type, stats: Bool, +sel: U32, +st: DevState, q: Q.Vq, +auto: Bool, +pct: U32) -> V.Op<R, Pass>: match stats: case True{}: V.Op.bind(R, Bl.StatsPass, Pass, Bl.Balloon.stats_pass_at(R, Dev.bal_of(st), Dev.auto_of(st), q, pct), r => V.Op.pure(R, Pass, Dev.pass.stats(r))) case False{}: +a = Dev.auto_of(st) +oom = auto && Bl.Balloon.auto_on(a) && U32.is_eq(sel, 1) V.Op.bind(R, Bl.BalPass, Pass, Bl.Balloon.pass(R, sel, Dev.bal_of(st), q), r => V.Op.pure(R, Pass, Dev.pass.bal(Bl.Balloon.pass_after(oom, r), a)))def Dev.pass.lan_tx(r: Ln.LanTx) -> Pass: match r: case Ln.LanTx{q, l, used}: Pass{q, LanDev{l}, [], [], [], used}def Dev.pass.lan_rx(l: Ln.Lan, r: Ln.LanRxOut) -> Pass: match r: case Ln.LanRxOut{q, left, used}: Pass{q, LanDev{Ln.Lan.with_rx(l, left)}, [], [], [], used}# The LAN device's pass: transmit (queue 1) or receive (queue 0).def Dev.pass.lan(-R: Type, tx: Bool, st: DevState, q: Q.Vq) -> V.Op<R, Pass>: match tx: case True{}: do V.Op<R, Pass>: r : Ln.LanTx <- Ln.Lan.tx(R, Dev.lan_of(st), q) return Dev.pass.lan_tx(r) case False{}: +l = Dev.lan_of(st) do V.Op<R, Pass>: r : Ln.LanRxOut <- Ln.Lan.rx(R, l, q) return Dev.pass.lan_rx(l, r)# Runs the kind's pass on queue sel (q).def Dev.pass(-R: Type, k: DevKind, st: DevState, +sel: U32, q: Q.Vq) -> V.Op<R, Pass>: match k: case ConsoleKind{}: Dev.pass.con(R, U32.is_eq(sel, 1), st, q) case NetKind{mac}: Dev.pass.net(R, U32.is_eq(sel, 1), st, q) case P9Kind{share, tag}: do V.Op<R, Pass>: r : P.P9Pass <- P.P9.process(R, share, Dev.p9_of(st), q) return Dev.pass.p9(r) case VsockKind{cid}: Dev.pass.vsock(R, U32.is_eq(sel, 1), U32.is_eq(sel, 0), st, q) case InputKind{tablet}: Dev.pass.input(R, U32.is_eq(sel, 0), st, q) case LanKind{mac, ip, prefix}: Dev.pass.lan(R, U32.is_eq(sel, 1), st, q) case BalloonKind{auto}: Dev.pass.balloon(R, U32.is_eq(sel, Bl.Balloon.stats_queue()), sel, st, q, auto, 50) case BalloonMaxKind{pct}: Dev.pass.balloon(R, U32.is_eq(sel, Bl.Balloon.stats_queue()), sel, st, q, True{}, pct) case RngKind{}: do V.Op<R, Pass>: r : Q.Vq & Bool <- Dev.process(R, RngKind{}, q) return Dev.pass.plain(st, r) case BlkKind{disk, sectors}: do V.Op<R, Pass>: r : Q.Vq & Bool <- Dev.process(R, BlkKind{disk, sectors}, q) return Dev.pass.plain(st, r) case BlkRoKind{disk, sectors}: do V.Op<R, Pass>: r : Q.Vq & Bool <- Dev.process(R, BlkRoKind{disk, sectors}, q) return Dev.pass.plain(st, r) case BlkStoreKind{store, sectors, ro}: do V.Op<R, Pass>: r : Q.Vq & Bool <- Dev.process(R, BlkStoreKind{store, sectors, ro}, q) return Dev.pass.plain(st, r)# A device with nothing for the host.def Dev.out(d: Dev) -> DevOut: DevOut{d, [], [], []}# A balloon whose target moved (the automatic balloon: Bl.Balloon.stats_pass)# tells the driver (config change interrupt).def Dev.bal_sync(+st: DevState, +m: M.VirtioMmio) -> M.VirtioMmio: match st: case BalDev{b, a}: Bool.pick(M.VirtioMmio, U32.is_eq(Bl.Balloon.target(b), M.VirtioMmio.config_word(m)), m, M.VirtioMmio.config_changed(m, Bl.Balloon.config_of(b))) case _: mdef Dev.notify.fin(k: DevKind, m: M.VirtioMmio, qs: +List<Q.Vq>, sel: U32, r: Pass) -> DevOut: match r: case Pass{q, +st, bs, fs, vc, used}: DevOut{Dev{k, Dev.bal_sync(st, Dev.raise_if(used, m)), Dev.qput(qs, 0, sel, q), st}, bs, fs, vc}# The queue number a kind's pass takes: the balloon's own numbering# (Bl.Balloon.logical: it depends on whether the driver accepted the stats# queue, bit 1); others as the driver's.def Dev.sel_of(k: DevKind, +m: M.VirtioMmio, +sel: U32) -> U32: match k: case BalloonKind{auto}: Bl.Balloon.logical(M.VirtioMmio.accepts(m, 2), sel) case BalloonMaxKind{pct}: Bl.Balloon.logical(M.VirtioMmio.accepts(m, 2), sel) case _: sel# Runs the pass on queue sel when it exists.def Dev.notify(-R: Type, mq: Maybe<&2, Q.Vq>, +k: DevKind, +m: M.VirtioMmio, qs: +List<Q.Vq>, st: DevState, +sel: U32) -> V.Op<R, DevOut>: match mq: case None{}: V.Op.pure(R, DevOut, Dev.out(Dev{k, m, qs, st})) case Some{q}: do V.Op<R, DevOut>: r : Pass <- Dev.pass(R, k, st, Dev.sel_of(k, m, sel), q) return Dev.notify.fin(k, m, qs, sel, r)# Acts on the event of a transport write.def DevOut.dev(o: DevOut) -> Dev: match o: case DevOut{d, bs, fs, vc}: ddef Dev.flush(-R: Type, k: DevKind, m: M.VirtioMmio, qs: +List<Q.Vq>, st: DevState) -> V.Op<R, Dev>: +qs = qs do V.Op<R, Dev>: o : DevOut <- Dev.notify(R, Dev.qget(qs, 0, 0), k, m, qs, st, 0) return DevOut.dev(o)# The rx pass (Dev.flush) with its vsock commands kept (VsRelease).def Dev.flush.out(-R: Type, k: DevKind, m: M.VirtioMmio, qs: +List<Q.Vq>, st: DevState) -> V.Op<R, DevOut>: +qs = qs Dev.notify(R, Dev.qget(qs, 0, 0), k, m, qs, st, 0)def DevOut.join(bs: +List<U32>, fs: +List<+List<U32>>, vc: +List<V.VsockCmd>, o: DevOut) -> DevOut: match o: case DevOut{d, bs2, fs2, vc2}: DevOut{d, List.append(&2, U32, bs, bs2), List.append(&2, +List<U32>, fs, fs2), List.append(&2, V.VsockCmd, vc, vc2)}def Dev.after.vsock(-R: Type, again: Bool, o: DevOut) -> V.Op<R, DevOut>: match again: case False{}: V.Op.pure(R, DevOut, o) case True{}: match o: case DevOut{Dev{k, m, qs, st}, bs, fs, vc}: do V.Op<R, DevOut>: o2 : DevOut <- Dev.flush.out(R, k, m, qs, st) return DevOut.join(bs, fs, vc, o2)def Dev.is_vsock(k: DevKind) -> Bool: match k: case VsockKind{cid}: True{} case _: False{}def Dev.is_lan(k: DevKind) -> Bool: match k: case LanKind{mac, ip, prefix}: True{} case _: False{}def Dev.on_event(-R: Type, ev: M.MmioEvent, k: DevKind, m: M.VirtioMmio, qs: +List<Q.Vq>, st: DevState) -> V.Op<R, DevOut>: match ev: case M.NoEvent{}: V.Op.pure(R, DevOut, Dev.out(Dev{k, m, qs, st})) case M.DeviceReset{}: V.Op.pure(R, DevOut, Dev.out(Dev{k, m, [], st})) case M.DriverOk{}: +m = m V.Op.pure(R, DevOut, Dev.out(Dev{k, m, Dev.build(Dev.cfgs(m)), st})) case M.Notify{+q}: +qs = qs +k = k do V.Op<R, DevOut>: o : DevOut <- Dev.notify(R, Dev.qget(qs, 0, q), k, m, qs, st, q) Dev.after.vsock(R, (Dev.is_vsock(k) || Dev.is_lan(k)) && U32.is_eq(q, 1), o)def Dev.write.fin(-R: Type, k: DevKind, qs: +List<Q.Vq>, st: DevState, r: M.VirtioMmio & M.MmioEvent) -> V.Op<R, DevOut>: match r: case (m, ev): Dev.on_event(R, ev, k, m, qs, st)def Dev.is_input(k: DevKind) -> Bool: match k: case InputKind{tablet}: True{} case _: False{}def Dev.tablet_of(k: DevKind) -> Bool: match k: case InputKind{tablet}: tablet case _: False{}# A write to an input device's select or subsel byte rebuilds its config# space; other writes go to the transport.def Dev.write.go(-R: Type, select: Bool, +k: DevKind, m: M.VirtioMmio, qs: +List<Q.Vq>, st: DevState, +off: U32, v: U32) -> V.Op<R, DevOut>: match select: case True{}: +s = In.Input.select(Dev.in_of(st), (off - 256 : U32), v) V.Op.pure(R, DevOut, Dev.out(Dev{k, M.VirtioMmio.with_config(m, In.InputState.config(Dev.tablet_of(k), s)), qs, InDev{s}})) case False{}: Dev.write.fin(R, k, qs, st, M.VirtioMmio.write(m, off, v))def Dev.write(-R: Type, d: Dev, +off: U32, v: U32) -> V.Op<R, DevOut>: match d: case Dev{+k, m, qs, st}: Dev.write.go(R, Dev.is_input(k) && (U32.is_eq(off, 256) || U32.is_eq(off, 257)), k, m, qs, st, off, v)# Delivers a device's pending input or frames into its queue 0, when it# exists.def Dev.is_console(k: DevKind) -> Bool: match k: case ConsoleKind{}: True{} case RngKind{}: False{} case BlkKind{disk, sectors}: False{} case BlkRoKind{disk, sectors}: False{} case BlkStoreKind{store, sectors, ro}: False{} case NetKind{mac}: False{} case P9Kind{share, tag}: False{} case VsockKind{cid}: False{} case InputKind{tablet}: False{} case LanKind{mac, ip, prefix}: False{} case BalloonKind{auto}: False{} case BalloonMaxKind{pct}: False{}def Dev.is_net(k: DevKind) -> Bool: match k: case NetKind{mac}: True{} case RngKind{}: False{} case BlkKind{disk, sectors}: False{} case BlkRoKind{disk, sectors}: False{} case BlkStoreKind{store, sectors, ro}: False{} case ConsoleKind{}: False{} case P9Kind{share, tag}: False{} case VsockKind{cid}: False{} case InputKind{tablet}: False{} case LanKind{mac, ip, prefix}: False{} case BalloonKind{auto}: False{} case BalloonMaxKind{pct}: False{}def Dev.input.go(-R: Type, console: Bool, k: DevKind, m: M.VirtioMmio, qs: +List<Q.Vq>, st: DevState, bytes: +List<U32>) -> V.Op<R, Dev>: match console: case True{}: Dev.flush(R, k, m, qs, ConState{List.append(&2, U32, Dev.input_of(st), bytes)}) case False{}: V.Op.pure(R, Dev, Dev{k, m, qs, st})# Bytes typed on the host console: a console device appends them to its# pending input and delivers what fits into receiveq0 (raising interrupt# status bit 1 when it used a chain). Other kinds are unchanged.def Dev.input(-R: Type, d: Dev, bytes: +List<U32>) -> V.Op<R, Dev>: match d: case Dev{+k, m, qs, st}: Dev.input.go(R, Dev.is_console(k), k, m, qs, st, bytes)def Dev.deliver.go(-R: Type, net: Bool, k: DevKind, m: M.VirtioMmio, qs: +List<Q.Vq>, st: DevState, frames: +List<+List<U32>>) -> V.Op<R, Dev>: match net: case True{}: Dev.flush(R, k, m, qs, NetState{Dev.cap(List.append(&2, +List<U32>, Dev.frames_of(st), frames))}) case False{}: V.Op.pure(R, Dev, Dev{k, m, qs, st})# Frames for the guest: a net device appends them to its pending frames# (keeping the last Dev.max_frames()) and delivers what it can into# receiveq1 (raising interrupt status bit 1 when it used a chain). Other# kinds are unchanged.def Dev.deliver(-R: Type, d: Dev, frames: +List<+List<U32>>) -> V.Op<R, Dev>: match d: case Dev{+k, m, qs, st}: Dev.deliver.go(R, Dev.is_net(k), k, m, qs, st, frames)# Whether the device is a net device.def Dev.net(d: Dev) -> Bool: match d: case Dev{k, m, qs, st}: Dev.is_net(k)# The number of frames a net device holds for the guest (0 for other kinds).def Dev.backlog(d: Dev) -> U32: match d: case Dev{k, m, qs, st}: U32.from_nat(List.length(&2, +List<U32>, Dev.frames_of(st)))# Whether the device is a vsock device.def Dev.vsock(d: Dev) -> Bool: match d: case Dev{k, m, qs, st}: Dev.is_vsock(k)def Dev.big.mmio(m: M.VirtioMmio) -> Bool: match m: case M.VirtioMmio{id, df, U.U64{dh, dl}, dfs, drs, qs, cfgs, st, ist, cg, cfg}: U32.is_eq((dl .&. 130 : U32), 130)# Whether a net device's driver took GUEST_CSUM and GUEST_TSO4 (bits 1 and# 7): it then receives TCP segments of up to 64 KiB as host frames.def Dev.big(d: Dev) -> Bool: match d: case Dev{k, m, qs, st}: Dev.is_net(k) && Dev.big.mmio(m)# A vsock device's backlog (Vs.Vsock.backlog; 0 for other kinds).def Dev.vsock_backlog(d: Dev) -> U32: match d: case Dev{k, m, qs, st}: Vs.Vsock.backlog(Dev.vs_of(st))def Dev.host_vsock.fin(-R: Type, k: DevKind, m: M.VirtioMmio, qs: +List<Q.Vq>, o: Vs.VsOut) -> V.Op<R, DevOut>: match o: case Vs.VsOut{v, cmds}: do V.Op<R, DevOut>: o2 : DevOut <- Dev.flush.out(R, k, m, qs, VsState{v}) return DevOut.join([], [], cmds, o2)# An event from the host's vsock sockets: through the protocol, then the# packets it queued go into the rx queue. Other kinds are unchanged.def Dev.host_vsock(-R: Type, d: Dev, ev: V.VsockEvent) -> V.Op<R, DevOut>: match d: case Dev{k, m, qs, st}: Dev.host_vsock.fin(R, k, m, qs, Vs.Vsock.host_event(Dev.vs_of(st), ev))# After a vsock tx pass, the replies it queued go out through the rx queue.# The interrupt line's level.def Dev.irq(d: Dev) -> Bool: match d: case Dev{k, m, qs, st}: M.VirtioMmio.irq(m)def Dev.hid.go(-R: Type, input: Bool, +k: DevKind, m: M.VirtioMmio, qs: +List<Q.Vq>, st: DevState, ev: In.Hid) -> V.Op<R, Dev>: match input: case True{}: Dev.flush(R, k, m, qs, InDev{In.Input.push(Dev.tablet_of(k), Dev.in_of(st), ev)}) case False{}: V.Op.pure(R, Dev, Dev{k, m, qs, st})# A keyboard or pointer report from the host: an input device queues its# events (In.Input.push: the keyboard takes keys, the tablet moves and# buttons) and delivers what fits into eventq. Other kinds are unchanged.def Dev.hid(-R: Type, d: Dev, ev: In.Hid) -> V.Op<R, Dev>: match d: case Dev{+k, m, qs, st}: Dev.hid.go(R, Dev.is_input(k), k, m, qs, st, ev)def Dev.lan_ready.go(-R: Type, lan: Bool, k: DevKind, m: M.VirtioMmio, qs: +List<Q.Vq>, st: DevState, +len: U32) -> V.Op<R, Dev>: match lan: case True{}: Dev.flush(R, k, m, qs, LanDev{Ln.Lan.ready(Dev.lan_of(st), len)}) case False{}: V.Op.pure(R, Dev, Dev{k, m, qs, st})# The host holds a frame of len bytes for the LAN NIC: a LAN device queues# it and delivers what fits into its receive queue (raising interrupt# status bit 1 when it used a chain). Other kinds are unchanged.def Dev.lan_ready(-R: Type, d: Dev, +len: U32) -> V.Op<R, Dev>: match d: case Dev{+k, m, qs, st}: Dev.lan_ready.go(R, Dev.is_lan(k), k, m, qs, st, len)# The LAN state of a device (forget_held after a restore).def Dev.forget_lan(d: Dev) -> Dev: match d: case Dev{k, m, qs, LanDev{l}}: Dev{k, m, qs, LanDev{Ln.Lan.forget_held(l)}} case Dev{k, m, qs, st}: Dev{k, m, qs, st}# The host sets the balloon's target (`balloon MiB` on the control socket):# a balloon device takes it (pages of 4 KiB) and tells the driver (config# change interrupt); other kinds are unchanged.def Dev.balloon.go(bal: Bool, +k: DevKind, m: M.VirtioMmio, qs: +List<Q.Vq>, +st: DevState, +pages: U32) -> Dev: match bal: case True{}: +b = Bl.Balloon.set_target(Dev.bal_of(st), pages) Dev{k, M.VirtioMmio.config_changed(m, Bl.Balloon.config_of(b)), qs, BalDev{b, Bl.Balloon.auto_manual(Dev.auto_of(st), True{})}} case False{}: Dev{k, m, qs, st}def Dev.is_balloon(k: DevKind) -> Bool: match k: case BalloonKind{auto}: True{} case BalloonMaxKind{pct}: True{} case _: False{}# Whether this is the automatic balloon (--balloon auto, the default).def Dev.auto_kind(k: DevKind) -> Bool: match k: case BalloonKind{auto}: auto case BalloonMaxKind{pct}: True{} case _: False{}# The most the automatic balloon holds, in percent of the guest's RAM.def Dev.pct_of(k: DevKind) -> U32: match k: case BalloonMaxKind{pct}: pct case _: 50def Dev.is_auto_balloon(d: Dev) -> Bool: match d: case Dev{k, m, qs, st}: Dev.auto_kind(k)def Dev.balloon(d: Dev, +pages: U32) -> Dev: match d: case Dev{+k, m, qs, st}: Dev.balloon.go(Dev.is_balloon(k), k, m, qs, st, pages)# `balloon auto`: the automatic balloon takes over again from a target set# by hand.def Dev.balloon_auto.go(bal: Bool, +k: DevKind, m: M.VirtioMmio, qs: +List<Q.Vq>, +st: DevState) -> Dev: match bal: case True{}: Dev{k, m, qs, BalDev{Dev.bal_of(st), Bl.Balloon.auto_manual(Dev.auto_of(st), False{})}} case False{}: Dev{k, m, qs, st}def Dev.balloon_auto(d: Dev) -> Dev: match d: case Dev{+k, m, qs, st}: Dev.balloon_auto.go(Dev.is_balloon(k), k, m, qs, st)# Asking the automatic balloon's driver for fresh statistics: a stats# buffer it posted and the device has not read yet is read first (the# driver posts its first one before DRIVER_OK, when there are no queues to# take it), then the one held goes back (Bl.Balloon.poll: on the driver's# queue 2, with a used-buffer interrupt). Only when the driver accepted the# stats queue: else its queue 2 reports free pages.def Dev.poll.fin(+k: DevKind, m: M.VirtioMmio, qs: +List<Q.Vq>, b: Bl.Bal, r: (Q.Vq & Bl.AutoSt) & Bool) -> Dev: match r: case ((q, a), used): +st : DevState = BalDev{b, a} Dev{k, Dev.bal_sync(st, Dev.raise_if(used, m)), Dev.qput(qs, 0, 2, q), st}def Dev.poll.held(-R: Type, +k: DevKind, m: M.VirtioMmio, +qs: +List<Q.Vq>, sp: Bl.StatsPass) -> V.Op<R, Dev>: match sp: case Bl.StatsPass{q, b, a}: V.Op.bind(R, (Q.Vq & Bl.AutoSt) & Bool, Dev, Bl.Balloon.poll(R, a, q), r => V.Op.pure(R, Dev, Dev.poll.fin(k, m, qs, b, r)))def Dev.poll.go(-R: Type, mq: Maybe<&2, Q.Vq>, +k: DevKind, m: M.VirtioMmio, +qs: +List<Q.Vq>, +st: DevState) -> V.Op<R, Dev>: match mq: case None{}: V.Op.pure(R, Dev, Dev{k, m, qs, st}) case Some{q}: V.Op.bind(R, Bl.StatsPass, Dev, Bl.Balloon.stats_pass_at(R, Dev.bal_of(st), Dev.auto_of(st), q, Dev.pct_of(k)), sp => Dev.poll.held(R, k, m, qs, sp))def Dev.balloon_poll(-R: Type, d: Dev) -> V.Op<R, Dev>: match d: case Dev{+k, +m, +qs, st}: Dev.poll.go(R, Bool.pick(Maybe<&2, Q.Vq>, Dev.auto_kind(k) && M.VirtioMmio.accepts(m, 2), Dev.qget(qs, 0, 2), None{}), k, m, qs, st)# The balloon's target and count (4 KiB pages), when this is the balloon.def Dev.balloon_info(d: Dev) -> Maybe<&2, Bl.Bal>: match d: case Dev{+k, m, qs, st}: Bool.pick(Maybe<&2, Bl.Bal>, Dev.is_balloon(k), Some{Dev.bal_of(st)}, None{})