import Base import ../src/lib/u64.bend as U import ../src/lib/bytes.bend as B import ../src/vmm/abi.bend as V import ../src/boot/image.bend as I import ../src/vmm/loggate.bend as Lg import ../src/net/flush.bend as Fl import ../src/net/limits.bend as Lm import ../src/arm64/kvmexit.bend as Kx import ../src/arm64/gicplan.bend as Gp import ../src/arm64/regplan.bend as Rp import ../src/net/fds.bend as Fd import ../src/vmm/hostfiles.bend as Hf import ../src/host/range.bend as Rg import ../src/host/transfer.bend as Xf # Hypervisor.framework effects (host/hvf.c) and their typed wrappers. # Words cross the boundary as U32; 64-bit values as (hi, lo) pairs. def Hvf.init(ram_mb: U32, gicd_hi: U32, gicd_lo: U32, gicr_hi: U32, gicr_lo: U32, ram_hi: U32, ram_lo: U32, cpus: U32) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.load(path: String, gpa_hi: U32, gpa_lo: U32, gz: U32) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.write(gpa_hi: U32, gpa_lo: U32, bytes: +List) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.read(gpa_hi: U32, gpa_lo: U32, len: U32) -> IO(+List): import "./hvf.c" import "./hvf.js" def Hvf.vcpu(cpu: U32, op: U32, a_hi: U32, a_lo: U32, b_hi: U32, b_lo: U32) -> IO(+List): import "./hvf.c" import "./hvf.js" def Hvf.vcpu_queue(cpu: U32, op: U32, reg: U32, hi: U32, lo: U32) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.irq(intid: U32, level: U32) -> IO(U32): import "./hvf.c" import "./hvf.js" # 0 arm64 guests (Hypervisor.framework, KVM on arm64), 1 x86_64 (KVM on x86_64). def Hvf.arch() -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.x86_setup(gsis: +List, cpuid: +List, words: +List, pics: +List) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.x86_boot(cpu: U32, words: +List) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.x86_pio_put(words: +List) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.x86_pio_get() -> IO(+List): import "./hvf.c" import "./hvf.js" def Hvf.set_args(bytes: +List) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.exe_path() -> IO(String): import "./hvf.c" import "./hvf.js" def Hvf.out_limits(backlog: U32, batch_us: U32) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.tty_mode(raw: U32, crlf: U32) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.sock_path_max() -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.args() -> IO(List): import "./hvf.c" import "./hvf.js" def Hvf.clone(from: String, to: String, copy: U32) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.remove(path: String) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.ctl_call(path: String, line: String, ms: U32) -> IO(+List): import "./hvf.c" import "./hvf.js" def Hvf.fsync(path: String, dir: U32) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.chmod(path: String, mode: U32) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.path_facts(path: String) -> IO(+List): import "./hvf.c" import "./hvf.js" def Hvf.set_mtime(path: String, hi: U32, lo: U32) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.ls(dir: String) -> IO(List): import "./hvf.c" import "./hvf.js" def Hvf.rmdir(path: String) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.ram_kib(kind: U32) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.ram_settle() -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.size_kib(path: String) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.set_exec(path: String) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.reexec() -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.file_size(path: String) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.out(bytes: +List) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.out_flush() -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.nofile() -> IO(+List): import "./hvf.c" import "./hvf.js" def Hvf.set_nofile(n: U32) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.fd_room(n: U32) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.log_state() -> IO(+List): import "./hvf.c" import "./hvf.js" def Hvf.log_put(tokens: U32, last_hi: U32, last_lo: U32, dropped: U32) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.log_gate.put(o: Lg.GateOut) -> IO(U32): match o: case Lg.GateOut{Lg.Gate{t, U.U64{h, l}, d}, v}: do IO: _ : U32 <- Hvf.log_put(t, h, l, d) return v # Whether a VMM message may go to stderr (src/vmm/loggate.bend decides): 0 # drops it; otherwise 1 + the messages dropped since the last one printed. def Hvf.log_gate() -> IO(U32): do IO: +ws : +List <- Hvf.log_state() Hvf.log_gate.put(Lg.Gate.step(Lg.Gate.wire(ws), Lg.Gate.now(ws))) def Hvf.stdin_tty() -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.stdin_wait(max: U32) -> IO(+List): import "./hvf.c" import "./hvf.js" def Hvf.random(len: U32) -> IO(+List): import "./hvf.c" import "./hvf.js" def Hvf.disk_open_with(path: String, flags: U32) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.disk_err() -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.disk_status(disk: U32) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.signals(mask: U32) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.accept_retry(ms: U32) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.signal_wait() -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.disk_size(disk: U32) -> IO(+List): import "./hvf.c" import "./hvf.js" def Hvf.disk_discard(disk: U32, off_hi: U32, off_lo: U32, len_hi: U32, len_lo: U32) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.disk_space(disk: U32) -> IO(+List): import "./hvf.c" import "./hvf.js" def Hvf.disk_set_hint(disk: U32, hi: U32, lo: U32) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.disk_zero(disk: U32, off_hi: U32, off_lo: U32, len: U32) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.disk_zero_keep(disk: U32, off_hi: U32, off_lo: U32, len: U32) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.disk_flush(disk: U32) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.now() -> IO(+List): import "./hvf.c" import "./hvf.js" def Hvf.disk_read(disk: U32, off_hi: U32, off_lo: U32, len: U32) -> IO(+List): import "./hvf.c" import "./hvf.js" def Hvf.disk_write(disk: U32, off_hi: U32, off_lo: U32, bytes: +List) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.disk_copy(disk: U32, dir: U32, off_hi: U32, off_lo: U32, iovs: +List) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.disk_short(disk: U32) -> IO(+List): import "./hvf.c" import "./hvf.js" def Hvf.disk_mark(disk: U32) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.ram_release(iovs: +List) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.zero_ram(iovs: +List) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.disk_alloc(disk: U32, len: U32) -> IO(+List): import "./hvf.c" import "./hvf.js" def Hvf.disk_xcopy(src: U32, soff_hi: U32, soff_lo: U32, dst: U32, doff_hi: U32, doff_lo: U32, len: U32) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.inflate_ram(disk: U32, off_hi: U32, off_lo: U32, clen: U32, skip: U32, iovs: +List) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.inflate_disk(disk: U32, off_hi: U32, off_lo: U32, clen: U32, dst: U32, doff_hi: U32, doff_lo: U32, skip: U32, len: U32) -> IO(U32): import "./hvf.c" import "./hvf.js" # Host sockets for the NAT (see host/hvf.c, "Network"). def Hvf.set_dns(ip: U32) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.dns() -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.tcp_connect(conn: U32, ip: U32, port: U32) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.tcp_wait(conn: U32) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.tcp_recv(conn: U32) -> IO(+List): import "./hvf.c" import "./hvf.js" def Hvf.tcp_release(conn: U32) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.net_holds() -> IO(+List): import "./hvf.c" import "./hvf.js" def Hvf.tcp_fill(conn: U32) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.net_copy(conn: U32, off: U32, iovs: +List) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.stage(iovs: +List) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.stage_drop(slot: U32) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.tcp_send_staged(conn: U32, slot: U32) -> IO(+List): import "./hvf.c" import "./hvf.js" # Guest data on a NAT connection, never waiting: 1 when a flusher must start # (Hvf.tcp_flush), else 0. def Hvf.nat_send(conn: U32, bytes: +List) -> IO(+List): import "./hvf.c" import "./hvf.js" # A NAT connection's flusher: sends its pending data as the socket takes it. # 0: TcpUnblocked, then again; 3: TcpBlocked, then again; 2: TcpReset; 1: # done. def Hvf.console_log(path: String, mode: U32) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.console_size() -> IO(+List): import "./hvf.c" import "./hvf.js" def Hvf.console_reopen() -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.meta_load(share: U32, gdir: String, words: +List) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.meta_resolve(share: U32, gpath: String) -> IO(+List): import "./hvf.c" import "./hvf.js" def Hvf.meta_list(share: U32, gdir: String) -> IO(+List): import "./hvf.c" import "./hvf.js" def Hvf.share_ro(share: U32) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.share_quota(share: U32, hi: U32, lo: U32) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.share_usage(share: U32) -> IO(+List): import "./hvf.c" import "./hvf.js" def Hvf.udp_ages() -> IO(+List): import "./hvf.c" import "./hvf.js" def Hvf.udp_close(flow: U32) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.tcp_over(conn: U32) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.tcp_flush(conn: U32, park: U32) -> IO(+List): import "./hvf.c" import "./hvf.js" def Hvf.tcp_flush_act(conn: U32, act: U32) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.tcp_shutdown(conn: U32) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.tcp_close_facts(conn: U32) -> IO(+List): import "./hvf.c" import "./hvf.js" def Hvf.tcp_close(conn: U32, how: U32) -> IO(U32): import "./hvf.c" import "./hvf.js" # The guest reset a NAT connection: the host resets its socket at once and # drops the data it has not sent. def Hvf.tcp_abort(conn: U32) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.udp_flows() -> IO(+List): import "./hvf.c" import "./hvf.js" def Hvf.fdc_stamps() -> IO(+List): import "./hvf.c" import "./hvf.js" def Hvf.fdc_put(slot: U32) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.udp_send(flow: U32, sport: U32, ip: U32, port: U32, bytes: +List) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.udp_recv(flow: U32) -> IO(+List): import "./hvf.c" import "./hvf.js" # Shared directories for virtio-9p (host/fs.bend wraps these). def Hvf.share_open(path: String) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.fs(share: U32, op: U32, path: String, path2: String, a_hi: U32, a_lo: U32, b: U32, bytes: +List) -> IO(+List): import "./hvf.c" import "./hvf.js" # Pause, snapshots and restore (host/hvf.c). def Hvf.pause() -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.resume() -> IO(U32): import "./hvf.c" import "./hvf.js" # Exits so far, per vCPU: [cpu, n, n pairs of (bucket, count)] # (Ctl.exit_name names a bucket). def Hvf.exit_counts() -> IO(+List): import "./hvf.c" import "./hvf.js" def Hvf.pause_state() -> IO(+List): import "./hvf.c" import "./hvf.js" def Hvf.snap_save(words: +List) -> IO(+List): import "./hvf.c" import "./hvf.js" def Hvf.snap_commit(path: String, plan: +List) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.snap_rebase(path: String, off_hi: U32, off_lo: U32) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.lan_open(dir: String, name: String, mode: U32, replace: U32) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.lan_send(name: String, iovs: +List) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.lan_names() -> IO(List): import "./hvf.c" import "./hvf.js" def Hvf.lan_recv() -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.lan_to_ram(iovs: +List) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.lan_release() -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.set_prog(n: U32) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.prog() -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.net_limits(words: +List) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.rename(from: String, to: String) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.sock_facts(path: String) -> IO(+List): import "./hvf.c" import "./hvf.js" def Hvf.sock_remove(path: String) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.mkdir(path: String, mode: U32) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.dir_facts(path: String) -> IO(+List): import "./hvf.c" import "./hvf.js" def Hvf.snap_plans(gic: +List, regs: +List) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.snap_drop() -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.snap_peek(off_hi: U32, off_lo: U32, len: U32) -> IO(+List): import "./hvf.c" import "./hvf.js" def Hvf.snap_load(layout: +List) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.snap_open(path: String) -> IO(+List): import "./hvf.c" import "./hvf.js" def Hvf.snap_info() -> IO(+List): import "./hvf.c" import "./hvf.js" def Hvf.snap_args() -> IO(List): import "./hvf.c" import "./hvf.js" def Hvf.snap_machine() -> IO(+List): import "./hvf.c" import "./hvf.js" def Hvf.snap_gic() -> IO(U32): import "./hvf.c" import "./hvf.js" # The control socket (host/hvf.c). def Hvf.ctl_listen(path: String, mode: U32, backlog: U32) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.ctl_next(deadline_ms: U32) -> IO(+List): import "./hvf.c" import "./hvf.js" def Hvf.ctl_reply(text: String) -> IO(U32): import "./hvf.c" import "./hvf.js" # vsock over Unix sockets (host/hvf.c). Sockets share the tcp_* table under # the key conn XOR 0x40000000. def Hvf.unix_connect(key: U32, path: String) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.vsock_listen(path: String, mode: U32, backlog: U32) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.vsock_base() -> IO(String): import "./hvf.c" import "./hvf.js" def Hvf.vsock_file(ok: U32) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.vsock_accept(deadline_ms: U32) -> IO(+List): import "./hvf.c" import "./hvf.js" # Port forwarding (host/hvf.c): forward idx listens on addr:port; each # accepted client is a NAT connection, its id fresh (0x20000000 up). def Hvf.fwd_listen(idx: U32, addr: String, port: U32, backlog: U32) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.fwd_accept(idx: U32) -> IO(U32): import "./hvf.c" import "./hvf.js" # An errno's message (strerror). def Hvf.err_text(code: U32) -> IO(String): import "./hvf.c" import "./hvf.js" # UEFI flash (host/hvf.c): the firmware and variable banks. def Hvf.fw_load(code: String, vars: String) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.flash(op: U32, a: U32, b: U32, bytes: +List) -> IO(U32): import "./hvf.c" import "./hvf.js" # The framebuffer and its window (host/hvf.c). def Hvf.fb_init(width: U32, height: U32) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.fb_dump(path: String, mode: U32) -> IO(U32): import "./hvf.c" import "./hvf.js" def Hvf.fb_present(window: Window) -> IO(Window & List): import "./hvf.c" import "./hvf.js" # vCPU request codes (host/hvf.c). def Op.create() -> U32: 0 def Op.run() -> U32: 1 def Op.set_reg() -> U32: 2 def Op.get_reg() -> U32: 3 def Op.set_vtimer_mask() -> U32: 4 def Op.advance_pc() -> U32: 5 # Register numbers beyond V.Reg: 32 is CPSR. def Reg.cpsr() -> U32: 32 # Makes the VM, then hands over what a snapshot keeps (the vGIC replay and the # register keys). def Host.vm_init(ram_mb: U32, gicd: U.U64, gicr: U.U64, ram: U.U64, +cpus: U32) -> IO(U32): match gicd gicr ram: case U.U64{dh, dl} U.U64{rh, rl} U.U64{mh, ml}: do IO: +rc : U32 <- Hvf.init(ram_mb, dh, dl, rh, rl, mh, ml, cpus) _ : U32 <- Hvf.snap_plans(Gp.Gic.plan(cpus, Gp.Gic.nirqs()), Rp.Regs.hvf()) return rc # A file's first bytes (at most 16; [] when it cannot be read). def Host.head.read(r: File & Result<&1, &1, U32 & String, List<&2, U32>>) -> IO(+List): match r: case (f, Done{bs}): do IO<+List>: File.close(f) return bs case (f, Fail{e}): do IO<+List>: File.close(f) return [] def Host.head.opened(r: Result<&1, &1, U32 & String, File>) -> IO(+List): match r: case Fail{e}: IO.pure(+List, []) case Done{f}: do IO<+List>: got : File & Result<&1, &1, U32 & String, List<&2, U32>> <- File.read_bytes(f, 16) Host.head.read(got) def Host.head(path: String) -> IO(+List): do IO<+List>: r : Result<&1, &1, U32 & String, File> <- File.open(path, "r") Host.head.opened(r) # A file's first n bytes ([] when it cannot be read). def Host.head_n.opened(r: Result<&1, &1, U32 & String, File>, n: U32) -> IO(+List): match r: case Fail{e}: IO.pure(+List, []) case Done{f}: do IO<+List>: got : File & Result<&1, &1, U32 & String, List<&2, U32>> <- File.read_bytes(f, n) Host.head.read(got) def Host.head_n(path: String, n: U32) -> IO(+List): do IO<+List>: r : Result<&1, &1, U32 & String, File> <- File.open(path, "r") Host.head_n.opened(r, n) # Copies a file into RAM at gpa, inflating it when it is gzip-compressed # (src/boot/image.bend decides from its first bytes): its size in RAM, or # 0xFFFFFFFF. def Host.load(+path: String, gpa: U.U64) -> IO(U32): match gpa: case U.U64{h, l}: do IO: +head : +List <- Host.head(path) Hvf.load(path, h, l, Bool.to_u32(I.Image.is_gzip(head))) def Host.write(gpa: U.U64, bytes: +List) -> IO(U32): match gpa: case U.U64{h, l}: Hvf.write(h, l, bytes) def Host.read(gpa: U.U64, len: U32) -> IO(+List): match gpa: case U.U64{h, l}: Hvf.read(h, l, len) def Host.first(ws: +List) -> U32: Maybe.default(&2, U32, List.head(&2, U32, ws), 4294967295) def Host.vcpu_create(cpu: U32) -> IO(U32): do IO: ws : +List <- Hvf.vcpu(cpu, Op.create(), 0, 0, 0, 0) return Host.first(ws) # Queued: applied by the vCPU thread before its next request. def Host.set_reg(cpu: U32, reg: U32, v: U.U64) -> IO(U32): match v: case U.U64{h, l}: Hvf.vcpu_queue(cpu, Op.set_reg(), reg, h, l) # Queued, like Host.set_reg: PC += 4. def Host.advance_pc(cpu: U32) -> IO(U32): Hvf.vcpu_queue(cpu, Op.advance_pc(), 0, 0, 0) def Host.get_reg(cpu: U32, reg: U32) -> IO(U.U64): do IO: +ws : +List <- Hvf.vcpu(cpu, Op.get_reg(), 0, reg, 0, 0) return U.U64{Host.first(ws), Host.first(List.drop(&2, U32, ws, 1n))} def Host.vtimer_mask(cpu: U32, masked: U32) -> IO(U32): do IO: ws : +List <- Hvf.vcpu(cpu, Op.set_vtimer_mask(), 0, masked, 0, 0) return Host.first(ws) # An exit record: [kind, esr, far, ipa, x0, x1, x2, x3, xt], each 64-bit # value as (hi, lo). One pattern match decodes it (walking the list per field # was the hottest code on the exit path). def Host.exception(ws: +List) -> V.RawExit: match ws: case Con{eh, Con{el, Con{fh, Con{fl, Con{ih, Con{il, Con{ah, Con{al, Con{bh, Con{bl, Con{ch, Con{cl, Con{dh, Con{dl, Con{th, Con{tl, rest}}}}}}}}}}}}}}}}: V.Exception{U.U64{eh, el}, U.U64{fh, fl}, U.U64{ih, il}, U.U64{ah, al}, U.U64{bh, bl}, U.U64{ch, cl}, U.U64{dh, dl}, U.U64{th, tl}} case _: V.UnknownExit{4294967295} def Host.decode_exit.kind(is0: Bool, is1: Bool, is2: Bool, kind: U32, rest: +List) -> V.RawExit: match is0 is1 is2: case True{} _ _: Host.exception(rest) case False{} True{} _: V.VtimerActivated{} case False{} False{} True{}: V.Canceled{} case False{} False{} False{}: Kx.KvmExit.decode(kind, rest) # Decodes Hvf.vcpu(.., Op.run(), ..) words into a RawExit. def Host.decode_exit(ws: +List) -> V.RawExit: match ws: case Nil{}: V.UnknownExit{4294967295} case Con{+kind, rest}: Host.decode_exit.kind(U32.is_eq(kind, 0), U32.is_eq(kind, 1), U32.is_eq(kind, 2), kind, rest) def Host.run(cpu: U32) -> IO(V.RawExit): do IO: +ws : +List <- Hvf.vcpu(cpu, Op.run(), 0, 0, 0, 0) return Host.decode_exit(ws) def Host.disk_read(disk: U32, off: U.U64, len: U32) -> IO(+List): match off: case U.U64{h, l}: Hvf.disk_read(disk, h, l, len) def Host.disk_write(disk: U32, off: U.U64, bytes: +List) -> IO(U32): match off: case U.U64{h, l}: Hvf.disk_write(disk, h, l, bytes) # Pieces of guest memory as the host takes them: each addr (8 bytes) then # len (4), little-endian. def Host.iovs(xs: +List) -> +List: match xs: case Nil{}: [] case V.Iov{a, l} <> t: List.append(&2, U32, B.Bytes.le64(a), List.append(&2, U32, B.Bytes.le32(l), Host.iovs(t))) # Pieces as src/host/range.bend has them, and back as the host takes them. def Host.pieces(xs: +List) -> +List: match xs: case Nil{}: [] case V.Iov{a, l} <> t: Con{Rg.Piece{a, l}, Host.pieces(t)} def Host.piece_bytes(xs: +List) -> +List: match xs: case Nil{}: [] case Rg.Piece{a, l} <> t: List.append(&2, U32, B.Bytes.le64(a), List.append(&2, U32, B.Bytes.le32(l), Host.piece_bytes(t))) # What an attempt that fell short did (Hvf.disk_short). def Host.io_of(+ws: +List) -> Xf.Io: +moved = Host.first(ws) +errno = Host.first(List.drop(&2, U32, ws, 1n)) Bool.pick(Xf.Io, U32.is_lt(0, moved), Xf.IoGot{moved}, Bool.pick(Xf.Io, U32.is_eq(errno, 0), Xf.IoEof{}, Xf.IoErr{errno})) # A failure is latched for Hvf.disk_status. def Host.disk_fail(+disk: U32, +rc: U32) -> IO(U32): do IO: _ : U32 <- Bool.pick(IO(U32), U32.is_eq(rc, 0), IO.pure(U32, 0), Hvf.disk_mark(disk)) return rc # A transfer that fell short, as src/host/transfer.bend decides: another # attempt from the next byte (Range.advance), until every byte moved, a # stop (a read zero-fills the rest; a write that stopped short fails) or a # failure. fuel: the most attempts. def Host.disk_go(fuel: Nat, nx: Xf.Next, +disk: U32, +dir: U32, +off: U.U64, +ps: +List, +want: U32) -> IO(U32): match fuel: case 0n: Host.disk_fail(disk, 4294967295) case 1n+k: match nx: case Xf.XMore{+d}: do IO: +at : U.U64 = U.U64.add_u32(off, d) +rc : U32 <- Hvf.disk_copy(disk, dir, U.U64.hi(at), U.U64.lo(at), Host.piece_bytes(Rg.Range.advance(ps, d))) +ws : +List <- Hvf.disk_short(disk) Host.disk_go(k, Bool.pick(Xf.Next, U32.is_eq(rc, 0), Xf.XStop{want}, Xf.Xfer.retry_at(d, Xf.Xfer.after(want, d, Host.io_of(ws)))), disk, dir, off, ps, want) case Xf.XRetry{}: Host.disk_fail(disk, 4294967295) case Xf.XStop{+d}: +read = U32.is_eq(dir, 0) do IO: _ : U32 <- Bool.pick(IO(U32), read && U32.is_lt(d, want), Hvf.zero_ram(Host.piece_bytes(Rg.Range.advance(ps, d))), IO.pure(U32, 0)) Host.disk_fail(disk, Xf.Xfer.status(Bool.not(read), want, Xf.XStop{d})) case Xf.XFail{e}: Host.disk_fail(disk, 4294967295) def Host.disk_copy.after(ok: Bool, +disk: U32, +dir: U32, +off: U.U64, +iovs: +List) -> IO(U32): match ok: case True{}: IO.pure(U32, 0) case False{}: do IO: +ws : +List <- Hvf.disk_short(disk) +want : U32 = V.Iov.total(iovs) Host.disk_go(4096n, Xf.Xfer.retry_at(0, Xf.Xfer.after(want, 0, Host.io_of(ws))), disk, dir, off, Host.pieces(iovs), want) def Host.disk_copy.at(+disk: U32, +dir: U32, +oh: U32, +ol: U32, +iovs: +List) -> IO(U32): do IO: +rc : U32 <- Hvf.disk_copy(disk, dir, oh, ol, Host.iovs(iovs)) Host.disk_copy.after(U32.is_eq(rc, 0), disk, dir, U.U64{oh, ol}, iovs) # Moves bytes between a disk and pieces of guest RAM on the host: dir 0 disk # to RAM, 1 RAM to disk. A complete transfer is one host call; one that falls # short comes back to Bend (Host.disk_go). (The signature and shape stay as # they were: interp calls this on the exit path's dispatch.) def Host.disk_copy(disk: U32, dir: U32, off: U.U64, iovs: +List) -> IO(U32): match off: case U.U64{oh, ol}: Host.disk_copy.at(disk, dir, oh, ol, iovs) # Copies the bytes from offset off of what the host holds for TCP connection # conn into pieces of guest RAM. def Host.net_copy(conn: U32, off: U32, iovs: +List) -> IO(U32): Hvf.net_copy(conn, off, Host.iovs(iovs)) # Copies pieces of guest RAM into a staged slot on the host. def Host.stage(iovs: +List) -> IO(U32): Hvf.stage(Host.iovs(iovs)) def Host.disk_alloc(disk: U32, len: U32) -> IO(U.U64): do IO: +ws : +List <- Hvf.disk_alloc(disk, len) return U.U64{Host.first(ws), Host.first(List.drop(&2, U32, ws, 1n))} # VmDiskSpace on the host: op 0 the hint and size, 1 set the hint, 2 zero. def Host.disk_space.go(+op: U32, +disk: U32, +h: U32, +l: U32, +len: U32) -> IO(+List): Bool.pick(IO(+List), U32.is_eq(op, 0), Hvf.disk_space(disk), Bool.pick(IO(+List), U32.is_eq(op, 1), IO.bind(U32, +List, Hvf.disk_set_hint(disk, h, l), r => IO.pure(+List, [])), Bool.pick(IO(+List), U32.is_eq(op, 2), IO.bind(U32, +List, Hvf.disk_zero(disk, h, l, len), r => IO.pure(+List, [])), IO.bind(U32, +List, Hvf.disk_zero_keep(disk, h, l, len), r => IO.pure(+List, []))))) def Host.disk_space(disk: U32, op: U32, at: U.U64, len: U32) -> IO(+List): match at: case U.U64{h, l}: Host.disk_space.go(op, disk, h, l, len) def Host.disk_discard(disk: U32, off: U.U64, len: U.U64) -> IO(U32): match off len: case U.U64{oh, ol} U.U64{lh, ll}: Hvf.disk_discard(disk, oh, ol, lh, ll) def Host.disk_xcopy(src: U32, soff: U.U64, dst: U32, doff: U.U64, len: U32) -> IO(U32): match soff doff: case U.U64{sh, sl} U.U64{dh, dl}: Hvf.disk_xcopy(src, sh, sl, dst, dh, dl, len) def Host.inflate_ram(disk: U32, off: U.U64, clen: U32, skip: U32, iovs: +List) -> IO(U32): match off: case U.U64{h, l}: Hvf.inflate_ram(disk, h, l, clen, skip, Host.iovs(iovs)) def Host.inflate_disk(disk: U32, off: U.U64, clen: U32, dst: U32, doff: U.U64, skip: U32, len: U32) -> IO(U32): match off doff: case U.U64{oh, ol} U.U64{dh, dl}: Hvf.inflate_disk(disk, oh, ol, clen, dst, dh, dl, skip, len) def Host.disk_size(disk: U32) -> IO(U.U64): do IO: +ws : +List <- Hvf.disk_size(disk) return U.U64{Host.first(ws), Host.first(List.drop(&2, U32, ws, 1n))} def Host.now() -> IO(U.U64): do IO: +ws : +List <- Hvf.now() return U.U64{Host.first(ws), Host.first(List.drop(&2, U32, ws, 1n))} # After a send (Hvf.nat_send, Hvf.tcp_send_staged answer [start, pending, # total]): past the limits (src/net/flush.bend, Flush.over) the host drops # what the connection holds and its flusher resets it. 1 when a flusher must # start, else 0. def Host.sent.over(over: Bool, +conn: U32, +start: U32) -> IO(U32): match over: case True{}: Hvf.tcp_over(conn) case False{}: IO.pure(U32, start) def Host.sent(+conn: U32, +ws: +List) -> IO(U32): Host.sent.over(Fl.Flush.over(U.U64{Fl.Flush.word(ws, 1n), Fl.Flush.word(ws, 2n)}, U.U64{Fl.Flush.word(ws, 3n), Fl.Flush.word(ws, 4n)}, Lm.Lim.default()), conn, Fl.Flush.word(ws, 0n)) def Host.nat_send(+conn: U32, bytes: +List) -> IO(U32): do IO: ws : +List <- Hvf.nat_send(conn, bytes) Host.sent(conn, ws) def Host.tcp_send_staged(+conn: U32, slot: U32) -> IO(U32): do IO: ws : +List <- Hvf.tcp_send_staged(conn, slot) Host.sent(conn, ws) # A close the NAT asked for: Flush.close decides from the host's facts. def Host.tcp_close(+conn: U32) -> IO(U32): do IO: +ws : +List <- Hvf.tcp_close_facts(conn) Hvf.tcp_close(conn, Fl.Flush.close.code(Fl.Flush.close(U32.is_ne(Fl.Flush.word(ws, 0n), 0), Fl.Flush.word(ws, 1n), Lm.Lim.default()))) # Sets the descriptor limit Bend chooses (src/net/fds.bend, Fds.soft) from # what the host reports, then hands over the guest flows' budget (Fds.room). def Host.nofile.set(ws: +List) -> IO(U32): match ws: case Con{+cur, Con{+max, Con{+top, Con{x, rest}}}}: Hvf.set_nofile(Fd.Fds.soft(cur, max, top, U32.is_ne(x, 0))) case _: Hvf.set_nofile(1024) def Host.fds() -> IO(U32): do IO: +ws : +List <- Hvf.nofile() +lim : U32 <- Host.nofile.set(ws) Hvf.fd_room(Fd.Fds.room(lim)) # What a path to listen on holds, as src/vmm/hostfiles.bend decides # (Files.listen) from the host's facts (Hvf.sock_facts). def Host.listen.of(ws: +List) -> Hf.Listen: match ws: case Con{+kind, Con{live, rest}}: Hf.Files.listen(kind, U32.is_ne(live, 0)) case _: Hf.LNotSocket{} # May the VMM listen at path? "" when it may (a dead socket there is # removed first), else why not. def Host.listen.clear(l: Hf.Listen, path: String) -> IO(String): match l: case Hf.LReplace{}: do IO: +rc : U32 <- Hvf.sock_remove(path) return Bool.pick(String, U32.is_zero(rc), "", "cannot remove the old socket (" ++ U32.show(rc) ++ ")") case _: IO.pure(String, Hf.Files.listen.why(l)) def Host.listen.facts(fits: Bool, +path: String, +max: U32) -> IO(String): match fits: case False{}: IO.pure(String, Hf.Files.sock_path_why(path, max)) case True{}: do IO: +ws : +List <- Hvf.sock_facts(path) Host.listen.clear(Host.listen.of(ws), path) # (a path too long for a socket's address is refused first, saying so) def Host.listen.ok(+path: String) -> IO(String): do IO: +max : U32 <- Hvf.sock_path_max() Host.listen.facts(Hf.Files.sock_path_fits(path, max), path, max) # Whether the LAN directory may be used (Files.dir_trusted), made 0700 when # missing. def Host.lan_dir.of(ws: +List) -> Bool: match ws: case Con{+kind, Con{mine, Con{+perm, rest}}}: Hf.Files.dir_trusted(kind, U32.is_ne(mine, 0), perm) case _: False{} def Host.lan_dir.ok(+dir: String) -> IO(Bool): do IO: _ : U32 <- Hvf.mkdir(dir, Hf.Files.private_dir()) +ws : +List <- Hvf.dir_facts(dir) return Host.lan_dir.of(ws) # Joins the LAN in dir: Files decides whether the directory is trusted and # whether an old socket of this MAC may be replaced; 0, or an errno. def Host.lan_open.go(l: Hf.Listen, +dir: String, mac: +List) -> IO(U32): match l: case Hf.LBind{}: Hvf.lan_open(dir, Hf.Files.lan_sock(mac), Hf.Files.private(), 0) case Hf.LReplace{}: Hvf.lan_open(dir, Hf.Files.lan_sock(mac), Hf.Files.private(), 1) case Hf.LInUse{}: IO.pure(U32, 48) # EADDRINUSE (macOS) / Linux 98: only shown case Hf.LNotSocket{}: IO.pure(U32, 17) # EEXIST def Host.lan_open.trusted(ok: Bool, +dir: String, +mac: +List) -> IO(U32): match ok: case True{}: do IO: +ws : +List <- Hvf.sock_facts(dir ++ "/" ++ Hf.Files.lan_sock(mac)) Host.lan_open.go(Host.listen.of(ws), dir, mac) case False{}: IO.pure(U32, 13) # EACCES def Host.lan_open(+dir: String, +mac: +List) -> IO(U32): do IO: +ok : Bool <- Host.lan_dir.ok(dir) Host.lan_open.trusted(ok, dir, mac)