~/bend-docscommunity

src/vmm/abi.bend source

src/vmm/abi.bend on the hub · documented module

import Baseimport ../lib/u64.bend as U# The boundary between the pure VMM and the macOS host layer (host/).## The host runs a vCPU until it exits and hands the exit over as a RawExit.# The pure side answers with a Vm program: a description of the effects it# wants (read guest memory, set a register, print to the console, ...). The# host interprets that program against Hypervisor.framework; tests interpret# it against a pure memory (src/vmm/sim.bend). Nothing in src/ performs IO.# Register numbers used by VmSetReg: 0..30 are X0..X30, 31 is PC.def Reg.pc() -> U32:  31# A vCPU exit as Hypervisor.framework reports it, decoded no further.type RawExit is Data:  # An exception to EL2. esr is ESR_EL2, far is FAR_EL2, ipa the faulting  # guest physical address (data and instruction aborts). x0..x3 are the  # guest's X0..X3 at the exit (HVC/SMC arguments). xt is the value of the  # register ESR.ISS.SRT names when ISV is set (a store's data), else zero.  Exception{esr: U.U64, far: U.U64, ipa: U.U64, x0: U.U64, x1: U.U64, x2: U.U64, x3: U.U64, xt: U.U64}  # The guest's virtual timer fired (HV_EXIT_REASON_VTIMER_ACTIVATED).  VtimerActivated{}  # hv_vcpus_exit forced the exit.  Canceled{}  # Any other HV_EXIT_REASON value.  UnknownExit{reason: U32}type PowerOp is Data:  PowerOff{}  PowerReset{}# Host network commands from the NAT (src/net/nat.bend). Connections are# numbered by the NAT; addresses are IPv4 as U32 (10.0.2.2 = 167772674). The# host maps 10.0.2.2 to its own loopback and 10.0.2.3 to its DNS resolver.type NetCmd is Data:  TcpConnect{conn: U32, ip: U32, port: U32}  TcpSend{conn: U32, bytes: +List<U32>}  # Half-close: no more data from the guest (shutdown(SHUT_WR)).  TcpShutdown{conn: U32}  # Full close: forget the connection.  TcpClose{conn: U32}  # A datagram from guest port sport to ip:port.  UdpSend{sport: U32, ip: U32, port: U32, bytes: +List<U32>}  # Sends the bytes staged on the host in slot (VmStage) and frees it.  TcpSendStaged{conn: U32, slot: U32}  # Frees a staged slot unsent (its segment was out of order, or refused).  StageDrop{slot: U32}  # The guest acknowledged all the host holds for the connection (TcpReady):  # the host may drop it and read on.  TcpRelease{conn: U32}  # Abort: the guest reset the connection. The host drops the data it has  # not sent yet and resets its socket at once (SO_LINGER 0).  TcpAbort{conn: U32}  # The guest accepted a forwarded connection (TcpIncoming): the host reads  # its socket from now on.  TcpAccepted{conn: U32}  # A frame the guest sends on its LAN NIC (src/virtio/lan.bend): the pieces  # of guest memory, to the VM whose MAC is dst, or to every VM on the LAN  # when dst is [].  LanSend{dst: +List<U32>, iovs: +List<Iov>}  # The frame the host held for the guest's LAN NIC went to the guest  # (VmLanToRam) or was dropped: the host may take the next.  LanRelease{}# What the host reports back to the NAT.type NetEvent is Data:  TcpConnected{conn: U32}  TcpRefused{conn: U32}  TcpData{conn: U32, bytes: +List<U32>}  # The remote side finished sending (EOF).  TcpEof{conn: U32}  # A datagram from ip:port to guest port dport.  UdpData{ip: U32, port: U32, dport: U32, bytes: +List<U32>}  # len bytes from the remote side wait in the host's buffer for the  # connection (instead of TcpData when the guest takes big segments): they  # go to the guest by VmNetToRam, never through Bend.  TcpReady{conn: U32, len: U32}  # The host holds more of the guest's data for the connection than its cap  # (its peer reads slowly): the NAT takes no more until TcpUnblocked.  TcpBlocked{conn: U32}  # The host's pending data for the connection drained below its cap.  TcpUnblocked{conn: U32}  # The host reset the connection (its peer took no data for too long).  TcpReset{conn: U32}  # The host accepted a connection on a forwarded port (--forward) for guest  # port gport; conn is a fresh connection id, filed in the host's table.  TcpIncoming{conn: U32, gport: U32}  # A forwarded connection's SYN went unanswered for a while: the NAT sends  # it again or gives up.  TcpSynTimeout{conn: U32}# A piece of guest memory: len bytes at addr. Devices name the memory of a# descriptor chain this way, and the host moves bytes in and out of it.type Iov is Data:  Iov{addr: U.U64, len: U32}# The total length of some pieces.def Iov.total(xs: +List<Iov>) -> U32:  match xs:    case Nil{}:      0    case Iov{a, l} <> t:      (l + Iov.total(t) : U32)# Host file operations for virtio-9p (src/fs/p9.bend). Paths are relative# to the shared directory, "" being its root, components separated by "/",# never "." or ".." (the 9p server builds them from names it validated; the# host refuses anything else). Times are seconds since the Unix epoch.type FsOp is Data:  FsStat{path: String}  # Entries of a directory, in a stable order, from index `from` on, at most  # `max` of them.  FsList{path: String, from: U32, max: U32}  FsRead{path: String, offset: U.U64, len: U32}  FsWrite{path: String, offset: U.U64, bytes: +List<U32>}  # Creates a regular file (mode: permission bits); fails if it exists.  FsCreate{path: String, mode: U32}  FsMkdir{path: String, mode: U32}  # Removes a file, or an empty directory when dir is True.  FsRemove{path: String, dir: Bool}  FsRename{from: String, to: String}  FsTruncate{path: String, size: U.U64}  FsChmod{path: String, mode: U32}  FsSymlink{path: String, target: String}  FsReadlink{path: String}  FsStatfs{}  # Reads the file from offset straight into the pieces of guest memory, in  # order (FsCount: how many bytes; fewer at the end of the file).  FsReadRam{path: String, offset: U.U64, iovs: +List<Iov>}  # Writes the pieces of guest memory, in order, to the file at offset  # (FsCount).  FsWriteRam{path: String, offset: U.U64, iovs: +List<Iov>}  # An extended attribute's value (FsBytes), or with name "" every name the  # path has, each followed by a 0 byte: on a share marked meta (its  # records, src/fs/meta.bend), ENODATA (61) for a name it lacks; on any  # other share EOPNOTSUPP (macOS 45, answered as Linux's 95).  FsXattr{path: String, name: String}# A file's attributes (lstat): mode is st_mode (type and permission bits).type FsAttr is Data:  FsAttr{mode: U32, uid: U32, gid: U32, nlink: U32, size: U.U64, ino: U.U64, blocks: U.U64, atime: U.U64, mtime: U.U64, ctime: U.U64}# A directory entry: name, inode, and its type (DT_DIR 4, DT_REG 8, DT_LNK 10, ...).type FsEntry is Data:  FsEntry{name: String, ino: U.U64, kind: U32}type FsResult is Data:  # An errno (ENOENT 2, EACCES 13, EEXIST 17, ENOTDIR 20, EISDIR 21,  # EINVAL 22, ENOSPC 28, ENOTEMPTY 66 on macOS, ...).  FsErr{errno: U32}  FsOk{}  FsGotAttr{attr: FsAttr}  # Attributes with what FsAttr cannot say: a device's numbers and the  # nanoseconds of mtime (src/fs/meta.bend: a share marked meta).  FsGotMeta{attr: FsAttr, major: U32, minor: U32, mnsec: U32}  FsBytes{bytes: +List<U32>}  FsCount{n: U32}  FsEntries{entries: +List<FsEntry>}  FsLink{target: String}  # statfs: block size, total, free and available blocks, total and free files.  FsStatfsOk{bsize: U32, blocks: U.U64, bfree: U.U64, bavail: U.U64, files: U.U64, ffree: U.U64}# Host commands from virtio-vsock (src/virtio/vsock.bend). Connections are# numbered by the device for guest-initiated ones and by the host (from# 2147483648 up) for host-initiated ones. The host maps guest port P on the# host CID to the Unix socket "<--vsock path>_P" (Firecracker's convention).type VsockCmd is Data:  # The guest connects to host port `port`.  VsConnect{conn: U32, port: U32}  VsSend{conn: U32, bytes: +List<U32>}  # The guest's payload of a staged slot (VmStage) to the host, never  # through Bend; the slot is freed.  VsSendStaged{conn: U32, slot: U32}  # A staged slot that goes nowhere (its packet was refused): freed.  VsStageDrop{slot: U32}  # Every byte the host held for the connection (VsReady) reached the guest:  # the host may read more.  VsRelease{conn: U32}  # The guest will send no more.  VsShutdown{conn: U32}  VsClose{conn: U32}  # The guest accepted a host-initiated connection; the host answers its  # client "OK <port>".  VsAccepted{conn: U32, port: U32}type VsockEvent is Data:  VsConnected{conn: U32}  VsRefused{conn: U32}  # len bytes from the host program wait in the host's buffer for the  # connection: they go to the guest by VmVsToRam, never through Bend, and  # the buffer is refilled after VsRelease.  VsReady{conn: U32, len: U32}  # The host side will send no more.  VsEof{conn: U32}  # A host client asks to connect to guest port `port` (it sent "CONNECT port").  VsIncoming{conn: U32, port: U32}  # The host program is not taking the guest's data as fast as it comes  # (Blocked: the guest gets no more credit) or has caught up (Unblocked).  VsBlocked{conn: U32}  VsUnblocked{conn: U32}# Host commands of the UEFI variable flash (src/dev/pflash.bend); offsets# are within the bank.type FlashCmd is Data:  # Reads size bytes (a trapped read in read-array mode; little-endian).  FlashRead{off: U32, size: U32}  FlashWrite{off: U32, bytes: +List<U32>}  # Sets len bytes to 0xFF.  FlashErase{off: U32, len: U32}  # Maps the bank into the guest read-only (on) or unmaps it.  FlashMap{on: Bool}# A program of host effects that ends with a value of type A. Each effect# applies to the vCPU whose exit is being handled, unless it names a cpu.type Vm<-A: Type> is Type:  VmRet{value: A}  # Reads len bytes of guest memory at addr, continues with them.  VmRead{addr: U.U64, len: U32, k: +List<U32> -> Vm<A>}  # Writes bytes to guest memory at addr.  VmWrite{addr: U.U64, bytes: +List<U32>, next: Vm<A>}  # Sets a register of the exiting vCPU (see Reg).  VmSetReg{reg: U32, value: U.U64, next: Vm<A>}  # Moves the exiting vCPU's PC past the trapping instruction (PC += 4).  VmAdvancePc{next: Vm<A>}  # Writes bytes to the VM's console (the host's stdout).  VmConsole{bytes: +List<U32>, next: Vm<A>}  # Sets the level of a GIC SPI by its INTID (32..1019).  VmIrq{intid: U32, level: Bool, next: Vm<A>}  # Powers the VM off or resets it, after the program ends.  VmPower{op: PowerOp, next: Vm<A>}  # PSCI CPU_ON: starts cpu at entry with X0 = ctx.  VmCpuOn{cpu: U32, entry: U.U64, ctx: U.U64, next: Vm<A>}  # A diagnostic line for the host's log (stderr), not the guest console.  VmLog{msg: String, next: Vm<A>}  # Reads len bytes of disk image `disk` at byte offset; short at its end.  VmDiskRead{disk: U32, offset: U.U64, len: U32, k: +List<U32> -> Vm<A>}  # Writes bytes to disk image `disk` at byte offset.  VmDiskWrite{disk: U32, offset: U.U64, bytes: +List<U32>, next: Vm<A>}  # len bytes from the host's entropy source.  VmRandom{len: U32, k: +List<U32> -> Vm<A>}  # Wall-clock time: seconds since the Unix epoch.  VmNow{k: U.U64 -> Vm<A>}  # Makes earlier writes to disk image `disk` durable (fsync).  VmDiskFlush{disk: U32, next: Vm<A>}  # Frees the host's blocks of disk image `disk` in [offset, offset+len): a  # hole that reads as zeros (virtio-blk DISCARD; advisory, never fails).  VmDiskDiscard{disk: U32, offset: U.U64, len: U.U64, next: Vm<A>}  # Free space in image `disk` (src/disk/store.bend, Store.alloc), one  # operation for the three (the exit path's dispatch grows by one case):  #   op 0: [hint hi, lo, size hi, lo], the hint where a free cluster may  #         be (kept by the host between requests, 0 at first)  #   op 1: the hint becomes at; []  #   op 2: [at, at+len) reads as zeros afterwards (a hole, or zeros  #         written where the host cannot punch one); []  VmDiskSpace{disk: U32, op: U32, at: U.U64, len: U32, k: +List<U32> -> Vm<A>}  # A network command for the host's sockets.  VmNet{cmd: NetCmd, next: Vm<A>}  # A file operation on the shared directory (`share` numbers the --share).  VmFs{share: U32, op: FsOp, k: FsResult -> Vm<A>}  # A vsock command for the host's Unix sockets.  VmVsock{cmd: VsockCmd, next: Vm<A>}  # A flash command; continues with the value a FlashRead read (0 for the  # others).  VmFlash{cmd: FlashCmd, k: U32 -> Vm<A>}  # Copies disk image `disk` from byte offset into the pieces of guest  # memory, in order (zeros past the image's end), on the host: no byte  # passes through Bend. The host does nothing when a piece leaves guest RAM.  VmDiskToRam{disk: U32, offset: U.U64, iovs: +List<Iov>, next: Vm<A>}  # Copies the pieces of guest memory, in order, to the disk from offset.  VmRamToDisk{disk: U32, offset: U.U64, iovs: +List<Iov>, next: Vm<A>}  # Copies the bytes from offset off of what the host holds for TCP  # connection conn (TcpReady) into the pieces of guest memory, in order.  # The hold stays until TcpRelease (the bytes may be sent again).  VmNetToRam{conn: U32, off: U32, iovs: +List<Iov>, next: Vm<A>}  # Copies the pieces of guest memory, in order, into a new host buffer (a  # staged slot, sent or freed later by a NetCmd); continues with its slot.  VmStage{iovs: +List<Iov>, k: U32 -> Vm<A>}  # Copies the bytes from offset off of what the host holds for vsock  # connection conn (VsReady) into the pieces of guest memory, in order.  VmVsToRam{conn: U32, off: U32, iovs: +List<Iov>, next: Vm<A>}  # Disk images by layout (src/disk): zeros over the pieces of guest memory.  VmZeroRam{iovs: +List<Iov>, next: Vm<A>}  # Lets go of the host memory behind pieces of guest RAM (free page  # reporting): touched again, they read as zeros or as their old contents.  VmRamRelease{iovs: +List<Iov>, next: Vm<A>}  # Copies the frame the host holds for the LAN NIC (V.LanRelease frees it)  # into the pieces of guest memory, in order.  VmLanToRam{iovs: +List<Iov>, next: Vm<A>}  # Grows disk image `disk` by len zero bytes, starting at a multiple of  # len past its end; continues with that start.  VmDiskAlloc{disk: U32, len: U32, k: U.U64 -> Vm<A>}  # Copies len bytes of disk image src at soff to disk image dst at doff  # (zeros past src's end).  VmDiskCopy{src: U32, soff: U.U64, dst: U32, doff: U.U64, len: U32, next: Vm<A>}  # Inflates the raw-deflate stream of clen bytes at off in disk image  # `disk` and copies its bytes from skip on over the pieces of guest memory  # (zeros past its end).  VmInflateToRam{disk: U32, off: U.U64, clen: U32, skip: U32, iovs: +List<Iov>, next: Vm<A>}  # Inflates that stream into disk image dst at doff: len of its bytes from  # skip on (zeros past its end).  VmInflateToDisk{disk: U32, off: U.U64, clen: U32, dst: U32, doff: U.U64, skip: U32, len: U32, next: Vm<A>}  # Whether a host operation on disk image `disk` failed since the last ask  # (1) or not (0); the ask clears it. The disk ops above answer nothing, so  # a device asks once its request is done.  VmDiskStatus{disk: U32, k: U32 -> Vm<A>}  # A pack's files (src/disk/pack.bend), in a cache of host descriptor slots  # where each holds one entry, named by its key (0: none). Opens path  # read-only into slot (a regular file; the slot's last file is closed  # first) and keeps it there as key: [0, dev hi, lo, ino hi, lo, size hi,  # lo, mtime hi, lo, mtime ns], or [errno].  VmPackOpen{slot: U32, key: U32, path: String, k: +List<U32> -> Vm<A>}  # Reads the file slot holds into the memory iovs from offset: the bytes it  # moved, or 4294967295 when the slot does not hold key.  VmPackRead{slot: U32, key: U32, offset: U.U64, iovs: +List<Iov>, k: U32 -> Vm<A>}  # Closes the file slot holds when it holds key.  VmPackClose{slot: U32, key: U32, next: Vm<A>}# Device code is written in Op, a continuation monad over Vm: its bind needs# no recursion, so `do Op<R, A>:` blocks pass Bend's termination check. R is# the type the whole exit handler finally returns (usually the machine).type Op<-R: Type, -A: Type> is Type:  Op{run: (A -> Vm<R>) -> Vm<R>}def Op.pure(-R: Type, -A: Type, x: A) -> Op<R, A>:  Op{k => k(x)}def Op.run(-R: Type, -A: Type, m: Op<R, A>, k: A -> Vm<R>) -> Vm<R>:  match m:    case Op{run}:      run(k)def Op.bind(-R: Type, -A: Type, -B: Type, m: Op<R, A>, f: A -> Op<R, B>) -> Op<R, B>:  Op{k => Op.run(R, A, m, a => Op.run(R, B, f(a), k))}# Ends an Op whose result is the final value.def Op.finish(-R: Type, m: Op<R, R>) -> Vm<R>:  Op.run(R, R, m, x => VmRet{x})def Op.read(-R: Type, addr: U.U64, len: U32) -> Op<R, +List<U32>>:  Op{k => VmRead{addr, len, k}}def Op.write(-R: Type, addr: U.U64, bytes: +List<U32>) -> Op<R, Unit>:  Op{k => VmWrite{addr, bytes, k(Unit{})}}def Op.set_reg(-R: Type, reg: U32, value: U.U64) -> Op<R, Unit>:  Op{k => VmSetReg{reg, value, k(Unit{})}}def Op.advance_pc(-R: Type) -> Op<R, Unit>:  Op{k => VmAdvancePc{k(Unit{})}}def Op.console(-R: Type, bytes: +List<U32>) -> Op<R, Unit>:  Op{k => VmConsole{bytes, k(Unit{})}}def Op.irq(-R: Type, intid: U32, level: Bool) -> Op<R, Unit>:  Op{k => VmIrq{intid, level, k(Unit{})}}def Op.power(-R: Type, op: PowerOp) -> Op<R, Unit>:  Op{k => VmPower{op, k(Unit{})}}def Op.cpu_on(-R: Type, cpu: U32, entry: U.U64, ctx: U.U64) -> Op<R, Unit>:  Op{k => VmCpuOn{cpu, entry, ctx, k(Unit{})}}def Op.log(-R: Type, msg: String) -> Op<R, Unit>:  Op{k => VmLog{msg, k(Unit{})}}def Op.disk_read(-R: Type, disk: U32, offset: U.U64, len: U32) -> Op<R, +List<U32>>:  Op{k => VmDiskRead{disk, offset, len, k}}def Op.disk_write(-R: Type, disk: U32, offset: U.U64, bytes: +List<U32>) -> Op<R, Unit>:  Op{k => VmDiskWrite{disk, offset, bytes, k(Unit{})}}def Op.disk_to_ram(-R: Type, disk: U32, offset: U.U64, iovs: +List<Iov>) -> Op<R, Unit>:  Op{k => VmDiskToRam{disk, offset, iovs, k(Unit{})}}def Op.ram_to_disk(-R: Type, disk: U32, offset: U.U64, iovs: +List<Iov>) -> Op<R, Unit>:  Op{k => VmRamToDisk{disk, offset, iovs, k(Unit{})}}def Op.net_to_ram(-R: Type, conn: U32, off: U32, iovs: +List<Iov>) -> Op<R, Unit>:  Op{k => VmNetToRam{conn, off, iovs, k(Unit{})}}def Op.vs_to_ram(-R: Type, conn: U32, off: U32, iovs: +List<Iov>) -> Op<R, Unit>:  Op{k => VmVsToRam{conn, off, iovs, k(Unit{})}}def Op.lan_to_ram(-R: Type, iovs: +List<Iov>) -> Op<R, Unit>:  Op{k => VmLanToRam{iovs, k(Unit{})}}def Op.stage(-R: Type, iovs: +List<Iov>) -> Op<R, U32>:  Op{k => VmStage{iovs, k}}def Op.zero_ram(-R: Type, iovs: +List<Iov>) -> Op<R, Unit>:  Op{k => VmZeroRam{iovs, k(Unit{})}}def Op.ram_release(-R: Type, iovs: +List<Iov>) -> Op<R, Unit>:  Op{k => VmRamRelease{iovs, k(Unit{})}}def Op.disk_alloc(-R: Type, disk: U32, len: U32) -> Op<R, U.U64>:  Op{k => VmDiskAlloc{disk, len, k}}def Op.disk_copy(-R: Type, src: U32, soff: U.U64, dst: U32, doff: U.U64, len: U32) -> Op<R, Unit>:  Op{k => VmDiskCopy{src, soff, dst, doff, len, k(Unit{})}}def Op.inflate_to_ram(-R: Type, disk: U32, off: U.U64, clen: U32, skip: U32, iovs: +List<Iov>) -> Op<R, Unit>:  Op{k => VmInflateToRam{disk, off, clen, skip, iovs, k(Unit{})}}def Op.inflate_to_disk(-R: Type, disk: U32, off: U.U64, clen: U32, dst: U32, doff: U.U64, skip: U32, len: U32) -> Op<R, Unit>:  Op{k => VmInflateToDisk{disk, off, clen, dst, doff, skip, len, k(Unit{})}}def Op.disk_status(-R: Type, disk: U32) -> Op<R, U32>:  Op{k => VmDiskStatus{disk, k}}def Op.pack_open(-R: Type, slot: U32, key: U32, path: String) -> Op<R, +List<U32>>:  Op{k => VmPackOpen{slot, key, path, k}}def Op.pack_read(-R: Type, slot: U32, key: U32, offset: U.U64, iovs: +List<Iov>) -> Op<R, U32>:  Op{k => VmPackRead{slot, key, offset, iovs, k}}def Op.pack_close(-R: Type, slot: U32, key: U32) -> Op<R, Unit>:  Op{k => VmPackClose{slot, key, k(Unit{})}}def Op.random(-R: Type, len: U32) -> Op<R, +List<U32>>:  Op{k => VmRandom{len, k}}def Op.now(-R: Type) -> Op<R, U.U64>:  Op{k => VmNow{k}}def Op.disk_flush(-R: Type, disk: U32) -> Op<R, Unit>:  Op{k => VmDiskFlush{disk, k(Unit{})}}def Op.disk_discard(-R: Type, disk: U32, offset: U.U64, len: U.U64) -> Op<R, Unit>:  Op{k => VmDiskDiscard{disk, offset, len, k(Unit{})}}def Op.disk_space(-R: Type, disk: U32) -> Op<R, +List<U32>>:  Op{k => VmDiskSpace{disk, 0, U.U64.zero(), 0, k}}def Op.disk_set_hint(-R: Type, disk: U32, at: U.U64) -> Op<R, Unit>:  Op{k => VmDiskSpace{disk, 1, at, 0, ws => k(Unit{})}}def Op.disk_zero(-R: Type, disk: U32, offset: U.U64, len: U32) -> Op<R, Unit>:  Op{k => VmDiskSpace{disk, 2, offset, len, ws => k(Unit{})}}# The range reads as zeros afterwards and stays allocated (virtio-blk# WRITE_ZEROES without UNMAP): zeros written, or the file system's zero# range where it has one.def Op.disk_zero_keep(-R: Type, disk: U32, offset: U.U64, len: U32) -> Op<R, Unit>:  Op{k => VmDiskSpace{disk, 3, offset, len, ws => k(Unit{})}}def Op.net(-R: Type, cmd: NetCmd) -> Op<R, Unit>:  Op{k => VmNet{cmd, k(Unit{})}}def Op.fs(-R: Type, share: U32, op: FsOp) -> Op<R, FsResult>:  Op{k => VmFs{share, op, k}}def Op.vsock(-R: Type, cmd: VsockCmd) -> Op<R, Unit>:  Op{k => VmVsock{cmd, k(Unit{})}}def Op.flash(-R: Type, cmd: FlashCmd) -> Op<R, U32>:  Op{k => VmFlash{cmd, k}}