garage44.eu/jeroen/pinnacle

logclone: git clone https://garage44.eu/jeroen/pinnacle.git

.. /abi/src/lib.rs

raw

//! What the kernel and its programs agree on.
//!
//! A system call is `svc #0` with its number in x8 and its arguments in x0
//! up; results come back in x0 (and x1, x2 where a call says so). Programs
//! start at their ELF entry with their argument in x0, the handles they were
//! given numbered from 1, and their stack set up.

#![no_std]

/// System call numbers (x8).
pub mod call {
    /// `write(bytes, len)`: puts bytes on the console. Returns `len`.
    pub const WRITE: u64 = 0;
    /// `yield()`: gives the rest of the time slice away.
    pub const YIELD: u64 = 1;
    /// `exit(code)`: ends the calling task; does not return.
    pub const EXIT: u64 = 2;
    /// `pid()`: the calling task's id.
    pub const PID: u64 = 3;
    /// `time()`: milliseconds since boot. The counter it reads, the generic
    /// timer's virtual one (`CNTVCT_EL0`, at `CNTFRQ_EL0` Hz), a program
    /// may also read itself, with no call.
    pub const TIME: u64 = 4;
    /// `sleep_until(ms)`: returns once `time()` has reached `ms`.
    pub const SLEEP_UNTIL: u64 = 5;
    /// `map_memory(size)`: zeroed, writable memory for the caller alone.
    /// Returns its address. [`UNMAP_MEMORY`] gives it back.
    pub const MAP_MEMORY: u64 = 6;
    /// `channel()`: a new channel; its two ends' handles in x0 and x1.
    pub const CHANNEL: u64 = 7;
    /// `send(handle, bytes, len, handles, count)`: a message to the peer of
    /// channel end `handle`, carrying `count` handles, which leave the
    /// caller. Returns 0, or [`ERROR`](super::ERROR) when the peer is gone.
    pub const SEND: u64 = 8;
    /// `receive(handles, count, buffer, len, handles_out, max, deadline)`:
    /// waits for a message on any of `count` channel ends, until `deadline`
    /// (ms since boot; 0 waits for ever). Returns the index of the end in
    /// x0 ([`TIMEOUT`](super::TIMEOUT) when the deadline passed), the
    /// message's length in x1 ([`CLOSED`](super::CLOSED) when that end's
    /// peer is gone), and how many handles it carried in x2.
    pub const RECEIVE: u64 = 9;
    /// `close(handle)`.
    pub const CLOSE: u64 = 10;
    /// `memory(size)`: new shared memory. Returns its handle.
    pub const MEMORY: u64 = 11;
    /// `map(handle)`: maps shared memory into the caller. Returns its
    /// address in x0 and its size in x1.
    pub const MAP: u64 = 12;
    /// `register(name, len, handle)`: offers the service `name`; each
    /// connection arrives on channel end `handle` as an empty message
    /// carrying the new channel's server end.
    pub const REGISTER: u64 = 13;
    /// `connect(name, len)`: a channel to the service `name`. Returns the
    /// caller's end.
    pub const CONNECT: u64 = 14;
    /// `spawn(name, len, argument, handles, count, flags)`: starts the program
    /// `name` (built in, or `/apps/<name>/program` on the system disk) with
    /// `argument`, giving it `count` handles, which leave the
    /// caller. Returns its task id in x0, and in x1 a handle to it, which
    /// can end it ([`KILL`]). With [`SPAWN_WATCH`](super::SPAWN_WATCH) in
    /// `flags`, x2 is a channel on which its exit code arrives as it ends
    /// (eight bytes), after which the channel closes.
    pub const SPAWN: u64 = 15;
    /// `framebuffer(width, height)`: the screen's pixels as shared memory
    /// (privileged programs only). Returns its handle in x0, width in x1,
    /// height in x2. A width and height other than the screen's change it to
    /// that size first, when it can have it (0, 0 leaves it).
    pub const FRAMEBUFFER: u64 = 16;
    /// `input()`: a channel end on which the input devices' events arrive
    /// as [`InputEvent`](super::InputEvent)s (privileged programs only).
    pub const INPUT: u64 = 17;
    /// `clock(op, ms)`. Op 0: the time of day, seconds since 1970 (UTC), in
    /// x0 (0 with no clock and nothing set), where it comes from in x1 (0
    /// nowhere, 1 the board's real-time clock, 2 the network) and its
    /// milliseconds in x2. Op 1 (not for an installed app): the time of day
    /// is `ms` since 1970, as the network said; the board's clock is set
    /// too where it can be.
    pub const CLOCK: u64 = 18;
    /// `programs(buffer, len)`: the names of the programs there are, one a
    /// line. Returns the length written.
    pub const PROGRAMS: u64 = 19;
    /// `info()`: cores running in x0, bytes of RAM in x1, bytes free in x2.
    pub const INFO: u64 = 20;
    /// `read_file(path, len, buffer, size)`: reads a whole file. Paths are
    /// "label:/path" (no label: the system disk). Returns the file's size; it
    /// is copied only when it fits.
    pub const READ_FILE: u64 = 21;
    /// `write_file(path, len, data, size)`: creates or replaces a file.
    pub const WRITE_FILE: u64 = 22;
    /// `list(path, len, buffer, size)`: a directory's names, one a line,
    /// directories ending in `/`. Returns the length written.
    pub const LIST: u64 = 23;
    /// `remove(path, len)`: removes a file, or a directory and all in it.
    pub const REMOVE: u64 = 24;
    /// `create_dir(path, len)`: makes a directory and any missing above it.
    pub const CREATE_DIR: u64 = 25;
    /// `connect(ip, port)`: a TCP connection to `ip` (four bytes, big-endian
    /// in a u32) as a channel: what the caller sends goes out on it, what
    /// arrives comes as messages, and the channel closes when it ends.
    pub const CONNECT_TCP: u64 = 26;
    /// `resolve(name, len)`: a channel on which the name's address arrives
    /// as four bytes (IPv4) or sixteen (IPv6: on a network without IPv4, or
    /// for a name with no IPv4 address); it closes without one if there is none.
    pub const RESOLVE: u64 = 27;
    /// `net_info()`: the address, router and name server (each four bytes,
    /// big-endian in a u32) in x0–x2, or [`ERROR`](super::ERROR) before the
    /// network is configured.
    pub const NET_INFO: u64 = 28;
    /// `udp_bind(port)`: a UDP socket on `port` (0: any free one) as a
    /// channel; each message, both ways, is the peer's address (four bytes),
    /// its port (two, big-endian), then the datagram. The handle in x0, the
    /// port in x1.
    pub const UDP_BIND: u64 = 29;
    /// `random(buffer, len)`: fills the buffer with random bytes, for keys.
    pub const RANDOM: u64 = 30;
    /// `thread(entry, stack, argument)`: starts a thread of the caller's
    /// program at `entry` with `stack` and `argument` in x0, sharing its
    /// memory and handles. Returns its task id.
    pub const THREAD: u64 = 31;
    /// `exit_process(code)`: ends the program, every thread of it; `exit`
    /// ends only the calling thread.
    pub const EXIT_PROCESS: u64 = 32;
    /// `unmap(address)`: unmaps the shared memory the caller mapped at
    /// `address` ([`MAP`]); its pages go back once nothing else holds them.
    pub const UNMAP: u64 = 33;
    /// `kill(handle)`: ends the program a SPAWN answered `handle` for, every
    /// thread of it, with [`KILLED`](super::KILLED) for its code. Returns 0,
    /// or [`ERROR`](super::ERROR) when it had already ended.
    pub const KILL: u64 = 34;
    /// `read_at(path, len, offset, buffer, size)`: up to `size` bytes of a
    /// file from `offset` (fewer at its end, at most [`MAX_COPY`](super::MAX_COPY)).
    /// Returns how many it read.
    pub const READ_AT: u64 = 35;
    /// `write_at(path, len, offset, data, size)`: writes into a file from
    /// `offset`, creating and growing it as needed (a hole up to `offset`).
    pub const WRITE_AT: u64 = 36;
    /// `stat(path, len)`: its size in x0, its kind (1 a file, 2 a directory)
    /// in x1, when it last changed (seconds since 1970, or 0 without a
    /// clock) in x2.
    pub const STAT: u64 = 37;
    /// `disk(label, len)`: the disk labelled so: bytes for files in all in
    /// x0, of them free in x1. Not for confined apps.
    pub const DISK: u64 = 38;
    /// `vault(op, passphrase, len)` (the display server; op 4 also other
    /// system programs, not installed apps): op 0 says
    /// whether a disk waits for a passphrase (0 none, 1 a blank disk for a
    /// new vault, 2 a locked vault); op 1 opens the vault with the
    /// passphrase, op 2 makes the blank disk a vault for it; either mounts
    /// it, as `data`. Op 3 checks a passphrase against the open vault (the
    /// lock screen); op 4 seals it under a new one (`vault(4, old, len, new,
    /// new_len)`). Returns 0, or [`ERROR`](super::ERROR) (a wrong
    /// passphrase). Stretching the passphrase takes a moment.
    pub const VAULT: u64 = 39;
    /// `cpu_features()`: the core's ID_AA64ISAR0_EL1, which says which
    /// crypto instructions it has (a program cannot read it itself).
    pub const CPU_FEATURES: u64 = 40;
    /// Kernel-internal: kernel code blocking until it is woken, resuming
    /// where it was (`sched::sleep`). From a program: [`ERROR`](super::ERROR).
    pub const BLOCK: u64 = 45;
    /// `audio_play(rate, channels)`: a playback stream as a channel. The
    /// caller sends messages of interleaved 16-bit samples at `rate` Hz
    /// (one the device's 48 000 is a multiple of) in `channels` channels (1
    /// or 2), mixed with every other player's; it gets back, as four bytes,
    /// how many of its frames are still waiting when that runs low, and 0
    /// when none is. Returns the handle, or [`ERROR`](super::ERROR) without a
    /// sound device.
    pub const AUDIO_PLAY: u64 = 46;
    /// `audio_record(rate, channels)`: a capture stream as a channel: each
    /// 10 ms period's samples arrive as one message, interleaved 16-bit at
    /// `rate` Hz. [`ERROR`](super::ERROR) without a device that records.
    pub const AUDIO_RECORD: u64 = 47;
    /// `audio_volume(percent)`: sets the volume (at most 100; more leaves
    /// it) and returns the one before.
    pub const AUDIO_VOLUME: u64 = 48;
    /// `bluetooth_port()`: the bytes to and from the device's Bluetooth
    /// controller (HCI over H4, a UART) as a channel: what the caller sends
    /// goes to the controller, what it says arrives as messages. One opener
    /// at a time (the `bt` service); an earlier one's channel closes. Not
    /// for confined apps. [`ERROR`](super::ERROR) without a controller.
    pub const BLUETOOTH_PORT: u64 = 49;
    /// `flush_screen(x, y, width, height)`: sends that rectangle of the
    /// framebuffer to the screen, where the display needs telling (a GPU
    /// scans out a copy; a plain framebuffer shows RAM as it is). Privileged
    /// programs only; clipped to the screen.
    pub const FLUSH_SCREEN: u64 = 50;
    /// `listen(port)`: accepts TCP connections on `port` as a channel: each
    /// connection arrives as a message carrying the connection's channel
    /// (as [`CONNECT_TCP`] gives one); closing it stops listening.
    /// [`ERROR`](super::ERROR) for a port already listened on. The network
    /// calls ([`CONNECT_TCP`], [`RESOLVE`], [`UDP_BIND`], this) are refused
    /// to an installed app unless its manifest says `network=yes`.
    pub const LISTEN_TCP: u64 = 51;
    /// `futex_wait(address, expected, deadline)`: waits while the aligned
    /// 32-bit word at `address` holds `expected`, until a [`FUTEX_WAKE`] on
    /// it or `deadline` (ms since boot; 0 waits for ever). Returns 0 (woken,
    /// or the word did not hold `expected`; it may also return so for no
    /// reason, and the caller checks the word again), or
    /// [`TIMEOUT`](super::TIMEOUT).
    pub const FUTEX_WAIT: u64 = 52;
    /// `futex_wake(address, count)`: wakes up to `count` of this program's
    /// threads waiting on the word at `address`. Returns how many it woke.
    pub const FUTEX_WAKE: u64 = 53;
    /// `wait(handles, count, deadline)`: as [`RECEIVE`] waits, without taking
    /// the message: the index of the first of `count` channel ends that has one
    /// (or whose peer is gone) in x0 ([`TIMEOUT`](super::TIMEOUT) when the
    /// deadline passed), and in x1 the length of its next message
    /// ([`CLOSED`](super::CLOSED) when nothing is left and the peer is gone).
    /// What `poll` is elsewhere: readiness, for event loops.
    pub const WAIT: u64 = 54;
    /// `devices(buffer, len)`: the hardware the kernel found, one device a
    /// line: class, driver, node name, what it is compatible with and the
    /// closed firmware its driver handed it (`Intel, <file>`),
    /// tab-separated; class and driver `-` for one no driver took, firmware
    /// `-` for none. Returns the length written.
    pub const DEVICES: u64 = 55;
    /// `gpu()`: a channel to a context of its own on the 3D GPU, whose Venus
    /// runs the program's Vulkan on the host's (the messages in
    /// [`gpu`](super::gpu)). Closing it ends the context. Fails without a 3D
    /// GPU, and for an installed app whose manifest does not say `gpu=yes`.
    pub const GPU: u64 = 56;
    /// `program(buffer, len)`: the path of the program the caller runs, as
    /// the file calls take it (`system:/apps/<name>/program` for an installed
    /// app). Returns its length; [`ERROR`](super::ERROR) for a program built
    /// into the system, which has no file, or a buffer too small.
    pub const PROGRAM: u64 = 57;
    /// `unmap_memory(address)`: gives back, whole, the memory a
    /// [`MAP_MEMORY`] answered `address` for; its pages and the addresses
    /// are free for what comes next. Returns 0, or [`ERROR`](super::ERROR)
    /// when `address` is not such memory, or on a machine that cannot yet
    /// take a page from every core at once (the PC).
    pub const UNMAP_MEMORY: u64 = 58;
    /// `open_file(path, len, rights)`: a handle to one file, with the
    /// [`file`](super::file) rights asked for, if the caller may have them
    /// (an installed app only in its own folder). With `WRITE` or `APPEND`
    /// a missing file is made, empty. The handle travels on a channel like
    /// any other, and its holder may use it whatever its own confinement:
    /// it is how a person gives a program one file and nothing else.
    pub const OPEN_FILE: u64 = 59;
    /// `file(handle, op, offset, buffer, len)`: an operation on a file
    /// handle (the ops in [`file`](super::file)).
    pub const FILE: u64 = 60;
    /// `modem_port()`: the bytes to and from the device's cellular modem
    /// (AT commands, a UART) as a channel, as [`BLUETOOTH_PORT`] gives the
    /// Bluetooth controller's: one opener at a time (the `phone` service),
    /// not for confined apps. [`ERROR`](super::ERROR) without a modem.
    pub const MODEM_PORT: u64 = 61;
    /// `wifi_port()`: the bytes to and from the device's Wi-Fi radio (AT
    /// commands, a UART) as a channel, as [`BLUETOOTH_PORT`] gives the
    /// Bluetooth controller's: one opener at a time (the `wifi` service),
    /// not for confined apps. [`ERROR`](super::ERROR) without a radio.
    pub const WIFI_PORT: u64 = 62;
    /// `net_switch(op)`: where the network is, one of [`net`](super::net)'s
    /// states. Op 0 only asks; 1 switches it on, 2 off first (not for
    /// confined apps: theirs is refused with [`ERROR`](super::ERROR)).
    pub const NET_SWITCH: u64 = 63;
    /// `system(op, buf, len)`: the system that runs. Op 0 writes into
    /// `buf` (up to `len` bytes) what it is, `key=value` lines: its
    /// `version`, the `board` it is built for, and the `slot` it runs
    /// from (`a`, `b`, or `boot`: the boot kernel's own,
    /// `pinnacle_pkg::system`); returns the length. Op 1 restarts the
    /// device, its disks' writes finished first (not for confined apps:
    /// theirs is refused with [`ERROR`](super::ERROR)); it does not return.
    pub const SYSTEM: u64 = 64;
    /// `connect6(high, low, port)`: a TCP connection to the IPv6 address
    /// whose sixteen bytes are `high` then `low` (each big-endian), as
    /// [`CONNECT_TCP`] gives one; an IPv4-mapped address (`::ffff:a.b.c.d`)
    /// connects over IPv4.
    pub const CONNECT_TCP6: u64 = 65;
    /// `net_info6(buffer, size)`: the IPv6 address, router and name server,
    /// sixteen bytes each (zeros for one not known), into `buffer`; returns
    /// 48, or [`ERROR`](super::ERROR) before a router has given an address.
    pub const NET_INFO6: u64 = 66;
    /// `install(op, buf, len, disk, disk_len)`, for system programs (not
    /// confined apps): putting Pinnacle on a disk of the device's own.
    /// Op 0 writes into `buf` the disks, a line each,
    /// `name|model|bytes|removable|holds|installable` (`holds`: `pinnacle`,
    /// `other`, `unknown`, `empty`; `installable` 1 when the system can go
    /// there: it was started from a disk, and this one is free and large
    /// enough); returns the length. Op 1 starts installing on the disk
    /// named `disk` (everything on it is lost), returning at once, or
    /// [`ERROR`](super::ERROR). Op 2 writes into `buf` how it goes: `idle`,
    /// `running|<what now>`, `done|<what was done>` or `failed|<why>`.
    pub const INSTALL: u64 = 67;
    /// `processes(op, buf, len)`, for system programs (not confined or
    /// sealed ones): op 0 writes into `buf` the programs that run, a line
    /// each, `id|name|threads|memory bytes|started ms|kind` (`kind`:
    /// `display` for the privileged one, `app`, `sealed` or `system`),
    /// oldest first; op 1 the names services registered, one a line.
    /// Returns the length written (all of it fits in 64 KiB).
    pub const PROCESSES: u64 = 68;
    /// `kill_id(id)`: ends the program [`PROCESSES`] numbered `id`, as
    /// [`KILL`] ends one by its handle. Refused (with
    /// [`ERROR`](super::ERROR)) to confined and sealed programs, for the
    /// display server, and for the caller itself.
    pub const KILL_ID: u64 = 69;
    /// `log(buf, len)`: what the kernel and its programs wrote to the
    /// serial port, its last 64 KiB, oldest first; returns the length
    /// written. Not for confined or sealed programs.
    pub const LOG: u64 = 70;
    /// `power(buf, len)`: the battery, the charger and the lid as the
    /// firmware says now, `key=value` lines into `buf`: `ac=1` (plugged
    /// in), `lid=open` or `closed`, and a line a battery,
    /// `battery=<n>|state=<charging, discharging, full or absent>|percent=…|minutes=…|remaining=…|full=…|design=…|unit=<mWh or mAh>|cycles=…|model=…|maker=…`
    /// (a field the firmware does not know left out). Returns the length;
    /// 0 on a machine with none of them. Not for confined or sealed
    /// programs.
    pub const POWER: u64 = 71;
    /// `brightness(op, value)`: the built-in screen's brightness, 0 to 100
    /// (Intel's graphics, or the firmware's ACPI methods). Op 0 returns
    /// it; op 1 sets it to `value` (never below 5, a black screen being
    /// one nobody can turn back up) and returns it as it is then.
    /// [`ERROR`](super::ERROR) when the screen has no brightness to set.
    /// Not for confined or sealed programs.
    pub const BRIGHTNESS: u64 = 72;
}

/// Where the network is, as [`NET_SWITCH`](call::NET_SWITCH) answers.
pub mod net {
    /// No network device.
    pub const NONE: u64 = 0;
    /// Switched off.
    pub const OFF: u64 = 1;
    /// On, waiting for DHCP to give an address.
    pub const CONNECTING: u64 = 2;
    /// On, with an address.
    pub const ONLINE: u64 = 3;
}

/// File handles ([`OPEN_FILE`](call::OPEN_FILE), [`FILE`](call::FILE)).
pub mod file {
    /// Rights: read it; write anywhere in it; add to its end only. A handle
    /// with `APPEND` and not `WRITE` cannot change what is there: an
    /// append-only log.
    pub const READ: u64 = 1;
    pub const WRITE: u64 = 2;
    pub const APPEND: u64 = 4;
    /// Up to `len` bytes from `offset` into `buffer`; returns how many.
    pub const OP_READ: u64 = 0;
    /// `len` bytes from `buffer` written at `offset`; needs `WRITE`.
    pub const OP_WRITE: u64 = 1;
    /// `len` bytes from `buffer` added at the end, at once (no other
    /// write comes between finding the end and writing); needs `APPEND`
    /// or `WRITE`. Returns the offset they were written at.
    pub const OP_APPEND: u64 = 2;
    /// Returns its size, 1 (a file), and when it last changed.
    pub const OP_STAT: u64 = 3;
    /// A second handle to the same file with the rights in `offset`, which
    /// must be among this one's: what to give on when less is enough.
    pub const OP_NARROW: u64 = 4;
    /// Returns its rights.
    pub const OP_RIGHTS: u64 = 5;
    /// The file becomes the `len` bytes from `buffer`, whole (what was
    /// past them is gone); needs `WRITE`.
    pub const OP_REPLACE: u64 = 6;
}

/// The messages on a [`GPU`](call::GPU) channel: a little-endian u32 kind,
/// then its body.
pub mod gpu {
    /// Program → kernel: a Venus command stream, the rest of the message
    /// (whole commands, at most [`MAX_MESSAGE`](super::MAX_MESSAGE) - 4
    /// bytes). Nothing is answered: replies are written to the reply
    /// stream the program set, in memory it shares with the host.
    pub const SUBMIT: u32 = 1;
    /// Program → kernel: memory the host shares — `blob_id` (u64: 0 for
    /// memory of Venus's own, as a reply stream is; else a `VkDeviceMemory`'s
    /// id, of a host-visible type) and `size` (u64). Answered with
    /// [`BLOB`]: a status (u32, 0 when made), the resource's id (u32, what
    /// Venus names it by), and a memory handle to [`MAP`](super::call::MAP).
    /// The memory is the host's until the last hold of the handle goes.
    pub const BLOB: u32 = 2;
    /// Program → kernel: memory of the guest's own that the host's GPU uses
    /// as it is — `size` (u64, at most 16 MiB). Answered as [`BLOB`] is: a
    /// status, the resource's id (for `VkImportMemoryResourceInfoMESA`) and
    /// a memory handle to [`MAP`](super::call::MAP). It takes no room in
    /// the host-visible window; a host without the means to share the
    /// guest's pages refuses it (status not 0).
    pub const GUEST_BLOB: u32 = 3;
}

/// `spawn`'s flag: hand back a channel the program's exit code arrives on.
pub const SPAWN_WATCH: u64 = 1;
/// `spawn`'s flag: the program has the handles it is given and nothing
/// else: no network, no service by name, no file by path, no programs of
/// its own. What the agent's gatekeeper runs as (`docs/agent-environment.md`):
/// its rights are its handles.
pub const SPAWN_SEALED: u64 = 2;

/// The argument a program built against Rust's std is started with when its
/// starter has more to give it (arguments, environment, standard streams):
/// handle 1 is then a channel holding one start message (`std/README.md`).
pub const STD_START: u64 = u64::from_be_bytes(*b"STDSTART");

/// The argument the kernel's self-check starts the command `frits` with:
/// check its examples and run one, on the console.
pub const FRITS_CHECK: u64 = u64::from_be_bytes(*b"FRITSCHK");

/// The argument the kernel's self-check starts the app `kees` with: sync a
/// whole account against the provider it holds in memory, on the console.
pub const KEES_CHECK: u64 = u64::from_be_bytes(*b"KEESCHCK");

/// What a call that cannot be done returns: an unknown number, an argument
/// that is not the caller's memory or handle.
pub const ERROR: u64 = u64::MAX;
/// `receive`: the deadline passed.
pub const TIMEOUT: u64 = u64::MAX - 1;
/// `receive`: the channel's peer is gone and nothing is left to read.
pub const CLOSED: u64 = u64::MAX - 2;

/// The largest message, and the most handles one carries.
pub const MAX_MESSAGE: usize = 4096;
pub const MAX_MESSAGE_HANDLES: usize = 8;
/// The most bytes one call copies between a program and the kernel: a
/// larger file is read and written a range at a time.
pub const MAX_COPY: usize = 16 << 20;

/// The exit code of a program the kernel ended for a fault; the exception
/// class (ESR_EL1.EC) is added to it.
pub const KILLED: u64 = 0x1000;

/// An input event, as the kernel sends it to the display server and the
/// display server to the app in front: 16 bytes.
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
pub struct InputEvent {
    pub kind: u16,
    pub code: u16,
    pub value: i32,
    /// For pointer events, the position in screen pixels.
    pub x: i32,
    pub y: i32,
    /// Made by the display server for an agent acting on the person's
    /// behalf, not by a hand on the device: an app may refuse it for what
    /// cannot be undone. Only the display server sets it; the kernel's
    /// events never carry it, and no app can send another input at all.
    /// On the wire, the top bit of `kind`.
    pub synthetic: bool,
}

impl InputEvent {
    /// A touch or pointer pressed (`value` 1) or released (0) at x, y.
    pub const PRESS: u16 = 1;
    /// A touch or pointer moved to x, y; `code` [`BY_MOUSE`](Self::BY_MOUSE)
    /// when a mouse or touchpad moved it, whose pointer is drawn.
    pub const MOVE: u16 = 2;
    /// The `code` of a [`MOVE`](Self::MOVE) a mouse or touchpad made (a
    /// pointer moved by hand, not a finger on the screen).
    pub const BY_MOUSE: u16 = 1;
    /// A key pressed (`value` 1) or released (0); `code` is a Linux key code.
    pub const KEY: u16 = 3;
    /// The screen's size changed to `x` × `y` (QEMU's window resized, a
    /// monitor plugged in): from the kernel to the display server only.
    pub const SCREEN: u16 = 4;
    /// Something of the machine's power changed, from the kernel to the
    /// display server only: `code` says what ([`LID`](Self::LID),
    /// [`CHARGER`](Self::CHARGER), [`BATTERY`](Self::BATTERY)), `value` is
    /// 1 for open or plugged in, 0 for closed or out. The rest is asked by
    /// [`POWER`](call::POWER).
    pub const POWER: u16 = 5;
    /// A laptop's lid opened (1) or closed (0).
    pub const LID: u16 = 0;
    /// The charger plugged in (1) or out (0).
    pub const CHARGER: u16 = 1;
    /// The battery's status changed (its level, charging or not).
    pub const BATTERY: u16 = 2;
    /// The bit of `kind` on the wire that is [`InputEvent::synthetic`].
    pub const SYNTHETIC: u16 = 0x8000;

    pub fn to_bytes(self) -> [u8; 16] {
        let mut b = [0; 16];
        let kind = self.kind & !Self::SYNTHETIC | if self.synthetic { Self::SYNTHETIC } else { 0 };
        b[0..2].copy_from_slice(&kind.to_le_bytes());
        b[2..4].copy_from_slice(&self.code.to_le_bytes());
        b[4..8].copy_from_slice(&self.value.to_le_bytes());
        b[8..12].copy_from_slice(&self.x.to_le_bytes());
        b[12..16].copy_from_slice(&self.y.to_le_bytes());
        b
    }

    pub fn from_bytes(b: &[u8]) -> Option<Self> {
        let b: &[u8; 16] = b.get(..16)?.try_into().ok()?;
        let i32_at = |at: usize| i32::from_le_bytes([b[at], b[at + 1], b[at + 2], b[at + 3]]);
        let kind = u16::from_le_bytes([b[0], b[1]]);
        Some(Self {
            kind: kind & !Self::SYNTHETIC,
            synthetic: kind & Self::SYNTHETIC != 0,
            code: u16::from_le_bytes([b[2], b[3]]),
            value: i32_at(4),
            x: i32_at(8),
            y: i32_at(12),
        })
    }
}

/// Arguments the test program (`user/`) understands.
pub mod arg {
    /// Write a value at the same address as every other copy, yield, and
    /// check it is still there: each program has its own memory.
    pub const ROUNDS: u64 = 0;
    /// Read the kernel's memory, which must kill the program.
    pub const TOUCH_KERNEL: u64 = 1;
    /// Serve `echo`: send back every message, mapping any memory it carries
    /// and adding one to each of its bytes first.
    pub const ECHO_SERVER: u64 = 2;
    /// Talk to `echo`: messages, a handle, shared memory, and a timeout.
    pub const ECHO_CLIENT: u64 = 3;
    /// Compute with FP/SIMD registers, yielding all along, and check the
    /// result matches the same sum computed without yielding.
    pub const FLOATS: u64 = 4;
    /// Fetch the package source's catalog over the network (HTTP over TCP,
    /// after DHCP) and check it is one.
    pub const NET: u64 = 5;
    /// What [`NET`] exits with when the catalog came but no stand-in model
    /// answers beside it: the source is a bundle's `start` script, which
    /// serves files only, not `cargo xtask`.
    pub const NET_WITHOUT_MODEL: u64 = 2;
    /// Run a pool of threads over a thousand jobs and check their sum; leave
    /// one thread asleep, which ending the program must end too.
    pub const THREADS: u64 = 6;
    /// Map and unmap shared memory, and check the free memory comes back
    /// while the program still runs.
    pub const UNMAP: u64 = 7;
    /// Run as the installed app "hello": reach its own folder and the fonts,
    /// and be refused everything else.
    pub const CONFINED: u64 = 8;
    /// Start a copy running [`SPIN`], end it (KILL), and see it gone.
    pub const KILL: u64 = 9;
    /// Never end: one thread busy, one asleep, and the first busy too.
    pub const SPIN: u64 = 10;
    /// Write a 2 MiB file a range at a time, read it back and check it: the
    /// disk's requests complete while the program sleeps.
    pub const IO: u64 = 11;
    /// Play 300 ms of a 440 Hz tone and wait for it to be played.
    pub const AUDIO: u64 = 12;
    /// Run the Bluetooth host over the controller's port: find the device
    /// "Pinnacle Test", connect, read its name and battery level.
    pub const BLUETOOTH: u64 = 13;
    /// Run as the installed app "hello" with a manifest granting the
    /// network: a connection and a UDP socket are had.
    pub const CONFINED_NETWORK: u64 = 14;
    /// Listen on TCP port 7080 for the host's connection, answer its line.
    pub const LISTEN: u64 = 15;
    /// Fetch the https URL `/settings/https-test` names, over TLS checked
    /// against the roots on disk (only when that file is there: the
    /// internet is not the gate's).
    pub const HTTPS: u64 = 16;
    /// Run Vulkan on the host's GPU through a context of its own: the
    /// host's version and GPUs, and a buffer the first one fills.
    pub const GPU: u64 = 17;
    /// Run as the installed app "hello" with handle 1 a file outside its
    /// folder, handed to it read-only: read it through the handle, and be
    /// refused it by its path, and refused writing it.
    pub const HANDED_FILE: u64 = 18;
    /// Keep an append-only log through a file handle that may only add:
    /// entries added and read back, the past refused a change.
    pub const LOG: u64 = 19;
    /// Serve "selfcheck-notes", a service that describes its verbs (notes
    /// kept and forgotten, a post out, a page fetched), until its client
    /// goes.
    pub const VERBS_SERVICE: u64 = 20;
    /// Describe "selfcheck-notes" and call its verbs: arguments checked,
    /// an undo carried back, a long note in pieces.
    pub const VERBS_CLIENT: u64 = 21;
    /// The `model` service: talk to the stand-in provider beside the
    /// package source with its key, whatever Settings says; end when the
    /// last client goes.
    pub const MODEL_STANDIN: u64 = 22;
    /// Ask the `model` service a question and for a tool call, and find
    /// both in the log of what was sent.
    pub const MODEL_CLIENT: u64 = 23;
    /// Play the terminal and the person for the agent's gatekeeper, against
    /// the stand-in model and "selfcheck-notes": an action done, one the
    /// person refuses, the task undone, a budget spent; and a sealed
    /// program refused everything but its handles.
    pub const AGENT: u64 = 24;
    /// Started sealed: try the network, a service, a path, a program, a
    /// service of its own, and be refused each.
    pub const SEALED: u64 = 25;
    /// Run the modem driver over the modem's port: on the network, a call
    /// to the number outside that answers ("+31 20 123 4567"), hung up.
    pub const MODEM: u64 = 26;
    /// Text beyond Latin and the languages: the fallback fonts read from
    /// the system disk, Arabic shaped with them, a date and a number in
    /// Dutch, a UI tree over the wire and back.
    pub const TEXT: u64 = 27;
}