import Base import ../lib/u64.bend as U import ../lib/dec.bend as X import ../arm64/psci.bend as P import ../dev/pl011.bend as Uart import ../dev/pl031.bend as Rtc import ../dev/pflash.bend as Pf import ../net/nat.bend as Nt import ../virtio/mmio.bend as M import ../virtio/queue.bend as Q import ../virtio/device.bend as D import ../fs/p9.bend as F import ../virtio/vsock.bend as Vs import ../virtio/input.bend as In import ./machine.bend as Mc import ../disk/store.bend as St import ../disk/qcow2.bend as Qc import ../virtio/lan.bend as Ln import ../virtio/balloon.bend as Bl import ../virtio/balauto.bend as Au import ../disk/pack.bend as Pk # The machine's device state as a stream of 32-bit words, for snapshots. # Snap.encode and Snap.decode are inverses (laws/snapshot.bend). The stream # starts with the magic 1112428109 ("BVMS" read as a little-endian word, 0x534d5642) # and the format version 4 (2 added the UEFI flash state, 3 the NAT's flow # control, 4 vsock's host-held data counts); lists and strings are length-prefixed; a sum type # writes its constructor's index first. The host stores this next to guest # RAM, vCPU registers and the GIC state, as src/vmm/snapfile.bend lays the # file out. def Snap.magic() -> U32: 1112428109 def Snap.version() -> U32: 4 # Encoders # ======== def Enc.bools(bs: +List) -> +List: match bs: case Nil{}: [] case Con{b, rest}: Bool.to_u32(b) <> Enc.bools(rest) def Enc.frames(fs: +List<+List>) -> +List: match fs: case Nil{}: [] case Con{f, rest}: List.append(&2, U32, X.Enc.bytes(f), Enc.frames(rest)) def Enc.len(-A: Data, +xs: +List) -> U32: U32.from_nat(List.length(&2, A, xs)) def Enc.psci(s: P.PsciState) -> +List: match s: case P.PsciState{+on}: Enc.len(Bool, on) <> Enc.bools(on) def Enc.pl011(s: Uart.Pl011) -> +List: match s: case Uart.Pl011{i, f, l, c, fl, m, rx}: List.append(&2, U32, [i, f, l, c, fl, m], X.Enc.bytes(rx)) def Enc.pl031(s: Rtc.Pl031) -> +List: match s: case Rtc.Pl031{o, mr, lr, im, ris}: [o, mr, lr, im, ris] def Enc.conns(cs: +List) -> +List: match cs: case Nil{}: [] case Con{Nt.Conn{id, gp, rip, rp, st, gs, hs, Nt.Flow{wnd, una, dend, hbase, hlen, queue, eof, sc, dack, dwin, bk}}, rest}: List.append(&2, U32, [id, gp, rip, rp, st, gs, hs, wnd, una, dend, hbase, hlen, Bool.to_u32(eof), sc, dack, dwin, bk], List.append(&2, U32, X.Enc.bytes(queue), Enc.conns(rest))) def Enc.slirp(s: Nt.Slirp) -> +List: match s: case Nt.Slirp{+cs, next}: List.append(&2, U32, [Enc.len(Nt.Conn, cs), next], Enc.conns(cs)) def Enc.qcfgs(qs: +List) -> +List: match qs: case Nil{}: [] case Con{M.QueueCfg{mx, n, r, d, dr, dv}, rest}: List.append(&2, U32, [mx, n, Bool.to_u32(r)], List.append(&2, U32, X.Enc.u64(d), List.append(&2, U32, X.Enc.u64(dr), List.append(&2, U32, X.Enc.u64(dv), Enc.qcfgs(rest))))) def Enc.mmio(s: M.VirtioMmio) -> +List: match s: case M.VirtioMmio{id, df, drf, dsel, drsel, qsel, +qs, st, ist, cg, cfg}: +head = {List.append(&2, U32, [id], List.append(&2, U32, X.Enc.u64(df), X.Enc.u64(drf))) : +List} +mid = {[dsel, drsel, qsel, Enc.len(M.QueueCfg, qs)] : +List} List.append(&2, U32, head, List.append(&2, U32, mid, List.append(&2, U32, Enc.qcfgs(qs), List.append(&2, U32, [st, ist, cg], X.Enc.bytes(cfg))))) def Enc.vqs(qs: +List) -> +List: match qs: case Nil{}: [] case Con{Q.Vq{n, d, dr, dv, na, nu}, rest}: List.append(&2, U32, [n], List.append(&2, U32, X.Enc.u64(d), List.append(&2, U32, X.Enc.u64(dr), List.append(&2, U32, X.Enc.u64(dv), List.append(&2, U32, [na, nu], Enc.vqs(rest)))))) def Enc.qcow(q: Qc.Qcow) -> +List: match q: case Qc.Qcow{bits, size, l1, n1, rt, rtc, rb}: List.append(&2, U32, [bits], List.append(&2, U32, X.Enc.u64(size), List.append(&2, U32, X.Enc.u64(l1), List.append(&2, U32, [n1], List.append(&2, U32, X.Enc.u64(rt), [rtc, rb]))))) # A store, top layer first: 0 none, 1 raw, 2 qcow2 (then the layer below). def Enc.store(st: St.Store) -> +List: match st: case St.NoStore{}: [0] case St.RawStore{d}: [1, d] case St.QcowStore{d, q, below}: List.append(&2, U32, [2, d], List.append(&2, U32, Enc.qcow(q), Enc.store(below))) # A pack (tag 13): name, end, entries added, then each entry: number, # offset, length, path, identity (device, inode, size, mtime, its ns), live. def Enc.pack_id(i: Pk.PackId) -> +List: match i: case Pk.PackId{dv, ino, sz, mt, ns}: List.append(&2, U32, X.Enc.u64(dv), List.append(&2, U32, X.Enc.u64(ino), List.append(&2, U32, X.Enc.u64(sz), List.append(&2, U32, X.Enc.u64(mt), [ns])))) def Enc.pack_entry(e: Pk.PackEntry) -> +List: match e: case Pk.PackEntry{n, off, len, path, id, live}: +a = {n <> List.append(&2, U32, X.Enc.u64(off), X.Enc.u64(len)) : +List} +b = List.append(&2, U32, X.Enc.string(path), Enc.pack_id(id)) List.append(&2, U32, a, List.append(&2, U32, b, [Bool.to_u32(live)])) def Enc.pack_entries(es: +List) -> +List: match es: case Nil{}: [] case e <> t: List.append(&2, U32, Enc.pack_entry(e), Enc.pack_entries(t)) def Enc.pack(p: Pk.Pack) -> +List: match p: case Pk.Pack{nm, +es, nx, c}: List.append(&2, U32, 13 <> X.Enc.string(nm), List.append(&2, U32, X.Enc.u64(nx), List.append(&2, U32, [c, U32.from_nat(List.length(&2, Pk.PackEntry, es))], Enc.pack_entries(es)))) def Enc.kind(k: D.DevKind) -> +List: match k: case D.RngKind{}: [0] case D.BlkKind{d, s}: List.append(&2, U32, [1, d], X.Enc.u64(s)) case D.BlkRoKind{d, s}: List.append(&2, U32, [2, d], X.Enc.u64(s)) case D.ConsoleKind{}: [3] case D.NetKind{mac}: 4 <> X.Enc.bytes(mac) case D.P9Kind{sh, tag}: List.append(&2, U32, [5, sh], X.Enc.string(tag)) case D.VsockKind{cid}: 6 <> X.Enc.u64(cid) case D.InputKind{tablet}: [7, Bool.to_u32(tablet)] case D.BlkStoreKind{st, s, ro}: List.append(&2, U32, [8, Bool.to_u32(ro)], List.append(&2, U32, X.Enc.u64(s), Enc.store(st))) case D.LanKind{mac, ip, prefix}: List.append(&2, U32, 9 <> X.Enc.bytes(mac), [ip, prefix]) case D.BalloonKind{auto}: # 10: free page reporting only; 11: the automatic balloon too Bool.pick(+List, auto, [11], [10]) case D.BalloonMaxKind{pct}: # 12: the automatic balloon holding at most pct percent of RAM [12, pct] case D.PackKind{pk}: Enc.pack(pk) def Enc.fids(fs: +List) -> +List: match fs: case Nil{}: [] case Con{F.Fid{fid, path, open}, rest}: List.append(&2, U32, fid <> X.Enc.string(path), List.append(&2, U32, [Bool.to_u32(open)], Enc.fids(rest))) # A fid table in a snapshot: a trie is written as its list. def Enc.p9(+ms: U32, +fids: +List) -> +List: List.append(&2, U32, [3, ms, Enc.len(F.Fid, fids)], Enc.fids(fids)) def Enc.vconns(cs: +List) -> +List: match cs: case Nil{}: [] case Con{Vs.VConn{id, gp, hp, st, tx, pa, pf, fwd, lf, hd, sn}, rest}: List.append(&2, U32, [id, gp, hp, st, tx, pa, pf, fwd, lf, hd, sn], Enc.vconns(rest)) def Enc.vsock(v: Vs.Vsock) -> +List: match v: case Vs.Vsock{cid, +cs, nid, np, +out}: List.append(&2, U32, X.Enc.u64(cid), List.append(&2, U32, [nid, np, Enc.len(Vs.VConn, cs)], List.append(&2, U32, Enc.vconns(cs), Enc.len(+List, out) <> Enc.frames(out)))) # What waits for a LAN NIC's guest: 0 and a frame, or 1 and a held length. def Enc.lanrx(xs: +List) -> +List: match xs: case Nil{}: [] case Con{Ln.Local{f}, rest}: List.append(&2, U32, 0 <> X.Enc.bytes(f), Enc.lanrx(rest)) case Con{Ln.Held{n}, rest}: List.append(&2, U32, [1, n], Enc.lanrx(rest)) def Enc.parts(ps: +List) -> +List: match ps: case Nil{}: [] case Con{Bl.Part{b, m}, rest}: List.append(&2, U32, [b, m], Enc.parts(rest)) def Enc.state(s: D.DevState) -> +List: match s: case D.NoState{}: [0] case D.ConState{input}: 1 <> X.Enc.bytes(input) case D.NetState{+fs}: List.append(&2, U32, [2, Enc.len(+List, fs)], Enc.frames(fs)) case D.P9Dev{F.P9State{ms, +fids}}: List.append(&2, U32, [3, ms, Enc.len(F.Fid, fids)], Enc.fids(fids)) case D.P9Dev{F.P9Map{ms, n, m}}: Enc.p9(ms, F.FidMap.list(m)) case D.VsState{v}: 4 <> Enc.vsock(v) case D.InDev{In.InputState{sel, sub, +evs}}: List.append(&2, U32, [5, sel, sub, Enc.len(+List, evs)], Enc.frames(evs)) case D.LanDev{Ln.Lan{mac, ip, p, +rx}}: List.append(&2, U32, 6 <> X.Enc.bytes(mac), List.append(&2, U32, [ip, p, Enc.len(Ln.LanRx, rx)], Enc.lanrx(rx))) case D.BalDev{Bl.Bal{t, a, +ps}, Bl.AutoSt{h, asked, manual, Au.Stats{f, tot, av}}}: # 9: the balloon (16 KiB blocks), then the automatic balloon's state # (7 and 8, older: 64 KiB blocks, Bl.Balloon.parts16) List.append(&2, U32, [9, t, a, Enc.len(Bl.Part, ps)], List.append(&2, U32, Enc.parts(ps), [h, Bool.to_u32(asked), Bool.to_u32(manual), f, tot, av])) def Enc.dev(d: D.Dev) -> +List: match d: case D.Dev{k, m, +qs, s}: List.append(&2, U32, Enc.kind(k), List.append(&2, U32, Enc.mmio(m), List.append(&2, U32, Enc.len(Q.Vq, qs) <> Enc.vqs(qs), Enc.state(s)))) def Enc.devs(ds: +List) -> +List: match ds: case Nil{}: [] case Con{d, rest}: List.append(&2, U32, Enc.dev(d), Enc.devs(rest)) def Enc.pflash(f: Pf.Pflash) -> +List: match f: case Pf.Pflash{m, at, n, d}: List.append(&2, U32, [m, at, n], X.Enc.bytes(d)) def Snap.encode(m: Mc.Machine) -> +List: match m: case Mc.Machine{ps, uart, uirq, +devs, +lines, rtc, rirq, net, fl}: +a = {List.append(&2, U32, [Snap.magic(), Snap.version()], List.append(&2, U32, Enc.psci(ps), Enc.pl011(uart))) : +List} +b = {List.append(&2, U32, [Bool.to_u32(uirq), Enc.len(D.Dev, devs)], Enc.devs(devs)) : +List} +c = {List.append(&2, U32, Enc.len(Bool, lines) <> Enc.bools(lines), List.append(&2, U32, Enc.pl031(rtc), Bool.to_u32(rirq) <> List.append(&2, U32, Enc.slirp(net), Enc.pflash(fl)))) : +List} List.append(&2, U32, a, List.append(&2, U32, b, c)) # Decoders # ======== def Dec.bools(n: Nat) -> X.Dec<+List>: match n: case 0n: X.Dec.pure(+List, []) case 1n+p: do X.Dec<+List>: b : Bool <- X.Dec.bool() bs : +List <- Dec.bools(p) return b <> bs def Dec.count() -> X.Dec: do X.Dec: n : U32 <- X.Dec.u32() return U32.to_nat(n) def Dec.frames(n: Nat) -> X.Dec<+List<+List>>: match n: case 0n: X.Dec.pure(+List<+List>, []) case 1n+p: do X.Dec<+List<+List>>: f : +List <- X.Dec.bytes() fs : +List<+List> <- Dec.frames(p) return f <> fs def Dec.psci() -> X.Dec: do X.Dec: n : Nat <- Dec.count() on : +List <- Dec.bools(n) return P.PsciState{on} def Dec.pl011() -> X.Dec: do X.Dec: i : U32 <- X.Dec.u32() f : U32 <- X.Dec.u32() l : U32 <- X.Dec.u32() c : U32 <- X.Dec.u32() fl : U32 <- X.Dec.u32() m : U32 <- X.Dec.u32() rx : +List <- X.Dec.bytes() return Uart.Pl011{i, f, l, c, fl, m, rx} def Dec.pl031() -> X.Dec: do X.Dec: o : U32 <- X.Dec.u32() mr : U32 <- X.Dec.u32() lr : U32 <- X.Dec.u32() im : U32 <- X.Dec.u32() ris : U32 <- X.Dec.u32() return Rtc.Pl031{o, mr, lr, im, ris} def Dec.conns(n: Nat) -> X.Dec<+List>: match n: case 0n: X.Dec.pure(+List, []) case 1n+p: do X.Dec<+List>: id : U32 <- X.Dec.u32() gp : U32 <- X.Dec.u32() rip : U32 <- X.Dec.u32() rp : U32 <- X.Dec.u32() st : U32 <- X.Dec.u32() gs : U32 <- X.Dec.u32() hs : U32 <- X.Dec.u32() wnd : U32 <- X.Dec.u32() una : U32 <- X.Dec.u32() dend : U32 <- X.Dec.u32() hbase : U32 <- X.Dec.u32() hlen : U32 <- X.Dec.u32() eof : Bool <- X.Dec.bool() sc : U32 <- X.Dec.u32() dack : U32 <- X.Dec.u32() dwin : U32 <- X.Dec.u32() bk : U32 <- X.Dec.u32() queue : +List <- X.Dec.bytes() rest : +List <- Dec.conns(p) return Nt.Conn{id, gp, rip, rp, st, gs, hs, Nt.Flow{wnd, una, dend, hbase, hlen, queue, eof, sc, dack, dwin, bk}} <> rest def Dec.slirp() -> X.Dec: do X.Dec: n : Nat <- Dec.count() next : U32 <- X.Dec.u32() cs : +List <- Dec.conns(n) return Nt.Slirp{cs, next} def Dec.qcfgs(n: Nat) -> X.Dec<+List>: match n: case 0n: X.Dec.pure(+List, []) case 1n+p: do X.Dec<+List>: mx : U32 <- X.Dec.u32() num : U32 <- X.Dec.u32() r : Bool <- X.Dec.bool() d : U.U64 <- X.Dec.u64() dr : U.U64 <- X.Dec.u64() dv : U.U64 <- X.Dec.u64() rest : +List <- Dec.qcfgs(p) return M.QueueCfg{mx, num, r, d, dr, dv} <> rest def Dec.mmio() -> X.Dec: do X.Dec: id : U32 <- X.Dec.u32() df : U.U64 <- X.Dec.u64() drf : U.U64 <- X.Dec.u64() dsel : U32 <- X.Dec.u32() drsel : U32 <- X.Dec.u32() qsel : U32 <- X.Dec.u32() nq : Nat <- Dec.count() qs : +List <- Dec.qcfgs(nq) st : U32 <- X.Dec.u32() ist : U32 <- X.Dec.u32() cg : U32 <- X.Dec.u32() cfg : +List <- X.Dec.bytes() return M.VirtioMmio{id, df, drf, dsel, drsel, qsel, qs, st, ist, cg, cfg} def Dec.vqs(n: Nat) -> X.Dec<+List>: match n: case 0n: X.Dec.pure(+List, []) case 1n+p: do X.Dec<+List>: num : U32 <- X.Dec.u32() d : U.U64 <- X.Dec.u64() dr : U.U64 <- X.Dec.u64() dv : U.U64 <- X.Dec.u64() na : U32 <- X.Dec.u32() nu : U32 <- X.Dec.u32() rest : +List <- Dec.vqs(p) return Q.Vq{num, d, dr, dv, na, nu} <> rest def Dec.blk(ro: Bool, d: U32, s: U.U64) -> D.DevKind: match ro: case True{}: D.BlkRoKind{d, s} case False{}: D.BlkKind{d, s} def Dec.kind.blk(ro: Bool) -> X.Dec: do X.Dec: d : U32 <- X.Dec.u32() s : U.U64 <- X.Dec.u64() return Dec.blk(ro, d, s) def Dec.kind.net() -> X.Dec: do X.Dec: mac : +List <- X.Dec.bytes() return D.NetKind{mac} def Dec.kind.p9() -> X.Dec: do X.Dec: sh : U32 <- X.Dec.u32() tag : String <- X.Dec.string() return D.P9Kind{sh, tag} def Dec.kind.vsock() -> X.Dec: do X.Dec: cid : U.U64 <- X.Dec.u64() return D.VsockKind{cid} def Dec.kind.input() -> X.Dec: do X.Dec: tablet : Bool <- X.Dec.bool() return D.InputKind{tablet} def Dec.qcow() -> X.Dec: do X.Dec: bits : U32 <- X.Dec.u32() size : U.U64 <- X.Dec.u64() l1 : U.U64 <- X.Dec.u64() n1 : U32 <- X.Dec.u32() rt : U.U64 <- X.Dec.u64() rtc : U32 <- X.Dec.u32() rb : U32 <- X.Dec.u32() return Qc.Qcow{bits, size, l1, n1, rt, rtc, rb} def Dec.store.by(fuel: Nat, t0: Bool, t1: Bool, t2: Bool) -> X.Dec: match fuel: case 0n: X.Dec.fail(St.Store) case 1n+f: match t0 t1 t2: case True{} _ _: X.Dec.pure(St.Store, St.NoStore{}) case False{} True{} _: do X.Dec: d : U32 <- X.Dec.u32() return St.RawStore{d} case False{} False{} True{}: do X.Dec: d : U32 <- X.Dec.u32() q : Qc.Qcow <- Dec.qcow() +t : U32 <- X.Dec.u32() below : St.Store <- Dec.store.by(f, U32.is_eq(t, 0), U32.is_eq(t, 1), U32.is_eq(t, 2)) return St.QcowStore{d, q, below} case False{} False{} False{}: X.Dec.fail(St.Store) # A store of at most 16 layers. def Dec.store() -> X.Dec: do X.Dec: +t : U32 <- X.Dec.u32() Dec.store.by(16n, U32.is_eq(t, 0), U32.is_eq(t, 1), U32.is_eq(t, 2)) def Dec.kind.store() -> X.Dec: do X.Dec: ro : Bool <- X.Dec.bool() s : U.U64 <- X.Dec.u64() st : St.Store <- Dec.store() return D.BlkStoreKind{st, s, ro} def Dec.kind.by(t0: Bool, t1: Bool, t2: Bool, t3: Bool, t4: Bool, t5: Bool, t6: Bool, t7: Bool, t8: Bool) -> X.Dec: match t0 t1 t2 t3 t4 t5 t6 t7 t8: case True{} _ _ _ _ _ _ _ _: X.Dec.pure(D.DevKind, D.RngKind{}) case False{} True{} _ _ _ _ _ _ _: Dec.kind.blk(False{}) case False{} False{} True{} _ _ _ _ _ _: Dec.kind.blk(True{}) case False{} False{} False{} True{} _ _ _ _ _: X.Dec.pure(D.DevKind, D.ConsoleKind{}) case False{} False{} False{} False{} True{} _ _ _ _: Dec.kind.net() case False{} False{} False{} False{} False{} True{} _ _ _: Dec.kind.p9() case False{} False{} False{} False{} False{} False{} True{} _ _: Dec.kind.vsock() case False{} False{} False{} False{} False{} False{} False{} True{} _: Dec.kind.input() case False{} False{} False{} False{} False{} False{} False{} False{} True{}: Dec.kind.store() case False{} False{} False{} False{} False{} False{} False{} False{} False{}: X.Dec.fail(D.DevKind) def Dec.kind.lan() -> X.Dec: do X.Dec: mac : +List <- X.Dec.bytes() ip : U32 <- X.Dec.u32() prefix : U32 <- X.Dec.u32() return D.LanKind{mac, ip, prefix} def Dec.kind.top(lan: Bool, +t: U32) -> X.Dec: match lan: case True{}: Dec.kind.lan() case False{}: Dec.kind.by(U32.is_eq(t, 0), U32.is_eq(t, 1), U32.is_eq(t, 2), U32.is_eq(t, 3), U32.is_eq(t, 4), U32.is_eq(t, 5), U32.is_eq(t, 6), U32.is_eq(t, 7), U32.is_eq(t, 8)) def Dec.kind.top2(balloon: Bool, +t: U32) -> X.Dec: match balloon: case True{}: X.Dec.pure(D.DevKind, D.BalloonKind{False{}}) case False{}: Dec.kind.top(U32.is_eq(t, 9), t) def Dec.kind.top3(auto: Bool, +t: U32) -> X.Dec: match auto: case True{}: X.Dec.pure(D.DevKind, D.BalloonKind{True{}}) case False{}: Dec.kind.top2(U32.is_eq(t, 10), t) # (10 to 90, as --balloon auto,max= takes: another is a malformed file) def Dec.kind.max.of(ok: Bool, +pct: U32) -> X.Dec: match ok: case True{}: X.Dec.pure(D.DevKind, D.BalloonMaxKind{pct}) case False{}: X.Dec.fail(D.DevKind) def Dec.kind.max() -> X.Dec: do X.Dec: +pct : U32 <- X.Dec.u32() Dec.kind.max.of(U32.is_le(10, pct) && U32.is_le(pct, 90), pct) def Dec.kind.top4(max: Bool, +t: U32) -> X.Dec: match max: case True{}: Dec.kind.max() case False{}: Dec.kind.top3(U32.is_eq(t, 11), t) def Dec.pack_entries(n: Nat) -> X.Dec<+List>: match n: case 0n: X.Dec.pure(+List, []) case 1n+k: do X.Dec<+List>: en : U32 <- X.Dec.u32() off : U.U64 <- X.Dec.u64() len : U.U64 <- X.Dec.u64() path : String <- X.Dec.string() dv : U.U64 <- X.Dec.u64() ino : U.U64 <- X.Dec.u64() sz : U.U64 <- X.Dec.u64() mt : U.U64 <- X.Dec.u64() ns : U32 <- X.Dec.u32() live : Bool <- X.Dec.bool() rest : +List <- Dec.pack_entries(k) return Pk.PackEntry{en, off, len, path, Pk.PackId{dv, ino, sz, mt, ns}, live} <> rest # (a pack whose slots do not tile is a malformed file) def Dec.kind.pack.of(+p: Pk.Pack) -> X.Dec: Bool.pick(X.Dec, Pk.Pack.tiles(p), X.Dec.pure(D.DevKind, D.PackKind{p}), X.Dec.fail(D.DevKind)) def Dec.kind.pack() -> X.Dec: do X.Dec: nm : String <- X.Dec.string() nx : U.U64 <- X.Dec.u64() c : U32 <- X.Dec.u32() n : Nat <- Dec.count() es : +List <- Dec.pack_entries(n) Dec.kind.pack.of(Pk.Pack{nm, es, nx, c}) def Dec.kind.top5(pack: Bool, +t: U32) -> X.Dec: match pack: case True{}: Dec.kind.pack() case False{}: Dec.kind.top4(U32.is_eq(t, 12), t) def Dec.kind() -> X.Dec: do X.Dec: +t : U32 <- X.Dec.u32() Dec.kind.top5(U32.is_eq(t, 13), t) def Dec.fids(n: Nat) -> X.Dec<+List>: match n: case 0n: X.Dec.pure(+List, []) case 1n+p: do X.Dec<+List>: fid : U32 <- X.Dec.u32() path : String <- X.Dec.string() open : Bool <- X.Dec.bool() rest : +List <- Dec.fids(p) return F.Fid{fid, path, open} <> rest def Dec.state.con() -> X.Dec: do X.Dec: input : +List <- X.Dec.bytes() return D.ConState{input} def Dec.state.net() -> X.Dec: do X.Dec: n : Nat <- Dec.count() fs : +List<+List> <- Dec.frames(n) return D.NetState{fs} def Dec.state.p9() -> X.Dec: do X.Dec: ms : U32 <- X.Dec.u32() n : Nat <- Dec.count() fids : +List <- Dec.fids(n) return D.P9Dev{F.P9State{ms, fids}} def Dec.vconns(n: Nat) -> X.Dec<+List>: match n: case 0n: X.Dec.pure(+List, []) case 1n+p: do X.Dec<+List>: id : U32 <- X.Dec.u32() gp : U32 <- X.Dec.u32() hp : U32 <- X.Dec.u32() st : U32 <- X.Dec.u32() tx : U32 <- X.Dec.u32() pa : U32 <- X.Dec.u32() pf : U32 <- X.Dec.u32() fwd : U32 <- X.Dec.u32() lf : U32 <- X.Dec.u32() hd : U32 <- X.Dec.u32() sn : U32 <- X.Dec.u32() rest : +List <- Dec.vconns(p) return Vs.VConn{id, gp, hp, st, tx, pa, pf, fwd, lf, hd, sn} <> rest def Dec.state.vsock() -> X.Dec: do X.Dec: cid : U.U64 <- X.Dec.u64() nid : U32 <- X.Dec.u32() np : U32 <- X.Dec.u32() nc : Nat <- Dec.count() cs : +List <- Dec.vconns(nc) no : Nat <- Dec.count() out : +List<+List> <- Dec.frames(no) return D.VsState{Vs.Vsock{cid, cs, nid, np, out}} def Dec.state.input() -> X.Dec: do X.Dec: sel : U32 <- X.Dec.u32() sub : U32 <- X.Dec.u32() n : Nat <- Dec.count() evs : +List<+List> <- Dec.frames(n) return D.InDev{In.InputState{sel, sub, evs}} def Dec.state.by(t0: Bool, t1: Bool, t2: Bool, t3: Bool, t4: Bool, t5: Bool) -> X.Dec: match t0 t1 t2 t3 t4 t5: case True{} _ _ _ _ _: X.Dec.pure(D.DevState, D.NoState{}) case False{} True{} _ _ _ _: Dec.state.con() case False{} False{} True{} _ _ _: Dec.state.net() case False{} False{} False{} True{} _ _: Dec.state.p9() case False{} False{} False{} False{} True{} _: Dec.state.vsock() case False{} False{} False{} False{} False{} True{}: Dec.state.input() case False{} False{} False{} False{} False{} False{}: X.Dec.fail(D.DevState) def Dec.lanrx.one(local: Bool) -> X.Dec: match local: case True{}: do X.Dec: f : +List <- X.Dec.bytes() return Ln.Local{f} case False{}: do X.Dec: n : U32 <- X.Dec.u32() return Ln.Held{n} def Dec.lanrx(n: Nat) -> X.Dec<+List>: match n: case 0n: X.Dec.pure(+List, []) case 1n+p: do X.Dec<+List>: +t : U32 <- X.Dec.u32() x : Ln.LanRx <- Dec.lanrx.one(U32.is_zero(t)) rest : +List <- Dec.lanrx(p) return x <> rest def Dec.state.lan() -> X.Dec: do X.Dec: mac : +List <- X.Dec.bytes() ip : U32 <- X.Dec.u32() p : U32 <- X.Dec.u32() n : Nat <- Dec.count() rx : +List <- Dec.lanrx(n) return D.LanDev{Ln.Lan{mac, ip, p, rx}} def Dec.parts(n: Nat) -> X.Dec<+List>: match n: case 0n: X.Dec.pure(+List, []) case 1n+k: do X.Dec<+List>: b : U32 <- X.Dec.u32() m : U32 <- X.Dec.u32() rest : +List <- Dec.parts(k) return Con{Bl.Part{b, m}, rest} def Dec.state.bal() -> X.Dec: do X.Dec: t : U32 <- X.Dec.u32() a : U32 <- X.Dec.u32() n : Nat <- Dec.count() ps : +List <- Dec.parts(n) return D.BalDev{Bl.Bal{t, a, Bl.Balloon.parts16(ps)}, Bl.Balloon.auto_new()} def Dec.state.bal2(+wide: Bool) -> X.Dec: do X.Dec: t : U32 <- X.Dec.u32() a : U32 <- X.Dec.u32() n : Nat <- Dec.count() +ps : +List <- Dec.parts(n) h : U32 <- X.Dec.u32() asked : Bool <- X.Dec.bool() manual : Bool <- X.Dec.bool() f : U32 <- X.Dec.u32() tot : U32 <- X.Dec.u32() av : U32 <- X.Dec.u32() return D.BalDev{Bl.Bal{t, a, Bool.pick(+List, wide, Bl.Balloon.parts16(ps), ps)}, Bl.AutoSt{h, asked, manual, Au.Stats{f, tot, av}}} def Dec.state.top(lan: Bool, +t: U32) -> X.Dec: match lan: case True{}: Dec.state.lan() case False{}: Dec.state.by(U32.is_eq(t, 0), U32.is_eq(t, 1), U32.is_eq(t, 2), U32.is_eq(t, 3), U32.is_eq(t, 4), U32.is_eq(t, 5)) def Dec.state.top2(bal: Bool, +t: U32) -> X.Dec: match bal: case True{}: Dec.state.bal() case False{}: Dec.state.top(U32.is_eq(t, 6), t) def Dec.state.top3(auto: Bool, +t: U32) -> X.Dec: match auto: case True{}: Dec.state.bal2(U32.is_eq(t, 8)) case False{}: Dec.state.top2(U32.is_eq(t, 7), t) def Dec.state() -> X.Dec: do X.Dec: +t : U32 <- X.Dec.u32() Dec.state.top3(U32.is_eq(t, 8) || U32.is_eq(t, 9), t) def Dec.dev() -> X.Dec: do X.Dec: k : D.DevKind <- Dec.kind() m : M.VirtioMmio <- Dec.mmio() nq : Nat <- Dec.count() qs : +List <- Dec.vqs(nq) s : D.DevState <- Dec.state() return D.Dev{k, m, qs, s} def Dec.devs(n: Nat) -> X.Dec<+List>: match n: case 0n: X.Dec.pure(+List, []) case 1n+p: do X.Dec<+List>: d : D.Dev <- Dec.dev() ds : +List <- Dec.devs(p) return d <> ds def Dec.header.ok(ok: Bool) -> X.Dec: match ok: case True{}: X.Dec.pure(Unit, Unit{}) case False{}: X.Dec.fail(Unit) def Dec.pflash() -> X.Dec: do X.Dec: m : U32 <- X.Dec.u32() at : U32 <- X.Dec.u32() n : U32 <- X.Dec.u32() d : +List <- X.Dec.bytes() return Pf.Pflash{m, at, n, d} def Dec.machine() -> X.Dec: do X.Dec: magic : U32 <- X.Dec.u32() version : U32 <- X.Dec.u32() _ : Unit <- Dec.header.ok(U32.is_eq(magic, Snap.magic()) && U32.is_eq(version, Snap.version())) ps : P.PsciState <- Dec.psci() uart : Uart.Pl011 <- Dec.pl011() uirq : Bool <- X.Dec.bool() nd : Nat <- Dec.count() devs : +List <- Dec.devs(nd) nl : Nat <- Dec.count() lines : +List <- Dec.bools(nl) rtc : Rtc.Pl031 <- Dec.pl031() rirq : Bool <- X.Dec.bool() net : Nt.Slirp <- Dec.slirp() fl : Pf.Pflash <- Dec.pflash() return Mc.Machine{ps, uart, uirq, devs, lines, rtc, rirq, net, fl} def Snap.decode.end(m: Mc.Machine, rest: +List) -> Maybe<&2, Mc.Machine>: match rest: case Nil{}: Some{m} case Con{w, more}: None{} def Snap.decode.fin(r: Maybe<&1, Mc.Machine & +List>) -> Maybe<&2, Mc.Machine>: match r: case None{}: None{} case Some{(m, rest)}: Snap.decode.end(m, rest) # The machine a snapshot stream holds; None when the stream is malformed or # has words left over. def Snap.decode(ws: +List) -> Maybe<&2, Mc.Machine>: Snap.decode.fin(X.Dec.run(Mc.Machine, Dec.machine(), ws))