System calls

Every call a program can make, generated from include/syscall.h. Programs use the koi_* wrappers in koi.h; the number is what goes in RAX.

int 0x40
  RAX                 function number
  RDI, RSI, RDX, RCX  arguments
  RAX                 result (all other registers are kept)

A note shared by several calls stands with the first of them. The structures and constants they use are in syscall.h itself.

0x00SYS_EXITkoi_exit
(code), does not return

Console and process.

0x01SYS_PUTCHARkoi_putchar
(character)
0x02SYS_PUTSkoi_print
(text)
0x03SYS_GETCHARkoi_getchar
() -> key, blocking
0x04SYS_READLINEkoi_readline
(buffer, size) -> length
0x05SYS_CLSkoi_cls
()
0x06SYS_SETCOLORkoi_color
(foreground, background)
0x07SYS_SETTHEMEkoi_theme
(foreground, background, prompt, error)

Set the shell colours; values outside 0-15 keep the current one. Returns foreground | background << 8 | prompt << 16 | error << 24. Saving is the caller's job.

0x08SYS_KEYPRESSEDkoi_keypressed
() -> 1 when a key is ready, else 0

1 if a key is waiting (not taken).

0x09SYS_SLEEPkoi_sleep
(milliseconds) -> 0, or what was left when a signal or Ctrl+C woke it (235)

Sleep without spinning. Keys arriving meanwhile stay buffered.

0x0ASYS_KEYEVENTkoi_keyevent
(flags) -> key, key | KOI_KEY_RELEASED, or 0

Key down/up events (unshifted identity, KOI_KEY_* for special keys). Returns 0 when none. Independent of the character queue. A program in a terminal gets none (its keys are the terminal's) unless it asks with KOI_KEYEVENT_SCREEN: it has a desktop window of its own, which has the keyboard (SDL).

0x0BSYS_GOTOXYkoi_gotoxy
(point: column, row) -> 0

Cursor position (columns, rows; see KOI_INFO_TEXT_COLUMNS/ROWS) and visibility.

0x0CSYS_CURSORkoi_cursor
(visible) -> 0
0x0DSYS_SLEEP_US

As SYS_SLEEP, in microseconds (what was left, in microseconds); to the microsecond where the processor's timer has deadlines. Build 620.

0x10SYS_OPENkoi_open
(path, mode) -> handle

Files. Handles are small non-negative integers; -1 means failure.

0x11SYS_CLOSEkoi_close
(handle)
0x12SYS_READkoi_read
(handle, buffer, length) -> bytes read
0x13SYS_WRITEkoi_write
0x14SYS_SIZEkoi_filesize
(handle) -> bytes
0x15SYS_SEEKkoi_seek

Move the file position.

0x16SYS_REMOVEkoi_remove
(path) -> 0, or -1

Delete and rename.

0x17SYS_RENAMEkoi_rename
(from, to) -> 0, or -1
0x18SYS_FINDFIRSTkoi_findfirst
(pattern, KOI_FIND_DATA*) -> search, or -1

Change this program's current drive (the shell's is unaffected). The directory resets to the root. Directory enumeration.

0x19SYS_FINDNEXTkoi_findnext
(search, KOI_FIND_DATA*) -> 0, or -1 at the end
0x1ASYS_FINDCLOSEkoi_findclose
(search)
0x1BSYS_EXISTSkoi_exists
(path) -> 1 file, 2 directory, 0 none, -1 no volume

Does a path exist?

0x1CSYS_MKDIRkoi_mkdir
(path) -> 0, or -1

Make a directory.

0x1ESYS_STAT_PATH
(path, KOI_PATH_STAT*, flags) -> 0-3, or -1
0x1FSYS_PREAD
(handle, buffer, length, offset) -> bytes, or -1

What is at a path, in one call (build 470): a file 1, a folder 2, one of Kawa's symbolic links 3 (a small file beginning "!<symlink>"; its target in `link`), 0 nothing there, -1 refused, no such drive, or a name with wildcards. Kept by the kernel until something changes the path, so a name asked again needs no disk. Kawa's stat used a search, its close, and for every small file an open, a read and a close to see whether it was a link. Read `length` bytes from `offset` of an open file, its position left as it is (pread): bytes read, 0 at the end, or -1 (build 477). From what the page cache keeps it takes no lock; Kawa's pread was a seek, a read and a seek back.

0x20SYS_ARGSkoi_arguments
() -> command line tail

Environment.

0x21SYS_VERSIONkoi_version
() -> version, major in the high byte
0x22SYS_SYSINFOkoi_clock
(item, index) -> value, or -1

System information: one numeric and one text call, selected by item and index. Unknown items return -1.

0x23SYS_SYSTEXTkoi_systext
(item, index, buffer, size) -> length, or -1
0x24SYS_ALLOCkoi_alloc
(bytes) -> address, or 0

Memory in whole pages, released automatically when the program exits.

0x25SYS_FREEkoi_free
(address)
0x26SYS_PROGRAMS
(KOI_PROGRAM_STATS*, max) -> count

Programs and the machine, for any program (Kawa's /proc: ps, top), as SYS_PROGRAM_STATS and SYS_SYSTEM_STATS give them to the system (build 488). Another account's programs come without their arguments (a command line may hold a password) unless the asker is an administrator.

0x27SYS_MACHINE_STATS
(KOI_SYSTEM_STATS*) -> 0
0x28SYS_FILE_LOCK
(handle, KOI_LOCK_*, c, d) -> 0, -1, -2, -3

Advisory locks on an open file, kept by the kernel (build 509), as Linux's flock and fcntl locks: they stop only programs that ask for them too. The two kinds do not see each other, as on Linux. FLOCK c: KOI_LOCK_SHARED, _EXCLUSIVE or _UNLOCK, | KOI_LOCK_NOWAIT. The whole file, held by the open file (the copies a fork made of it share it), let go when the last copy closes. SET c: a KOI_FILE_LOCK (a range, absolute; type 0 lets it go). Held by the program, let go when it closes any handle of the file or ends; d 1 waits for it. TEST c: a KOI_FILE_LOCK -> filled with a lock in the way (its program in pid), or type 0 if none. -> 0; -2 someone else holds it (not waited for); -3 a signal ended the wait; -1 refused.

0x29SYS_SYNCkoi_sync
() -> 0

What was changed on the disks, committed now (fsync, sync). Changes gather for up to two seconds before that (build 536): closing a file no longer commits it.

0x2ASYS_KEYBOARD_GIVE

The keyboard is the screen's (build 955, K4): keys and key events outside a terminal or a pipe (SYS_GETCHAR, SYS_READLINE, SYS_KEYPRESSED, SYS_KEYEVENT, SYS_INPUT's) go only to the program on the screen's top level, and to one program it gives them to - anyone else gets nothing (0; SYS_GETCHAR and SYS_READLINE wait). With no screen taken (the text console) everyone reads as before. (program) -> 0, or -1: the program on top of the screen gives the keyboard to `program` (a message sender's id; its own account's, or any for the system), also reading it itself; 0 takes it back. The gift lapses by itself while another program is on top (the administrator's box, a full-screen game) and comes back with the screen; it ends with either program.

0x30SYS_GFX_ENTERkoi_gfx_enter
(KOI_SCREEN*) -> 0, or -1

Graphics. A program takes the screen, draws into its buffer, presents, and gives it back; the console is restored exactly. Nothing appears until SYS_GFX_PRESENT. The KOI_SCREEN buffer may be written directly.

0x31SYS_GFX_LEAVEkoi_gfx_leave
()
0x32SYS_GFX_PRESENTkoi_gfx_present
()
0x33SYS_GFX_COLORkoi_gfx_color
(red, green, blue) -> packed pixel
0x34SYS_GFX_CLEARkoi_gfx_clear
(colour)
0x35SYS_GFX_PIXELkoi_gfx_pixel
(point, colour)
0x36SYS_GFX_LINEkoi_gfx_line
(point, point, colour)
0x37SYS_GFX_RECTkoi_gfx_rect
(point, size, colour), outline
0x38SYS_GFX_FILLkoi_gfx_fill
(point, size, colour), filled
0x39SYS_GFX_TEXTkoi_gfx_text
(point, text, colour, background)
0x3ASYS_GFX_PRESENT_RECTkoi_gfx_present_rect
(point, size, flags)

Present one rectangle (clipped).

0x3BSYS_GFX_TEXT_STYLEDkoi_gfx_text_styled
(point, text, colour, packed)

(point, text, colour, background | style << 32): bold, italic, underline.

0x3CSYS_GFX_SCISSORkoi_gfx_scissor
(point, size), intersect the clip rectangle
0x3DSYS_GFX_SCISSOR_RESETkoi_gfx_reset_scissor
(), full-screen clipping
0x3ESYS_GFX_DIMkoi_gfx_dim
(point, size, keep)

Darken a rectangle; `keep` out of 256.

0x3FSYS_GFX_BLITkoi_gfx_blit
(point, size, pixels, stride)

Copy a rectangle of pixels (stride in pixels).

0x40SYS_SOUND_PLAYkoi_sound_play
(KOI_SOUND*) -> voice, or -1

Sound. One 48 kHz stereo stream always runs; a sound gets a voice, or -1. Samples are not copied: keep the buffer until the sound ends. Voices stop when the program exits.

0x41SYS_SOUND_TONEkoi_sound_tone
(hertz, milliseconds, volume) -> voice, or -1
0x42SYS_SOUND_STOPkoi_sound_stop
(voice), -1 for all
0x43SYS_SOUND_ACTIVEkoi_sound_active
(voice) -> 1 while playing
0x44SYS_SOUND_VOLUMEkoi_sound_volume
(volume 0-255, or -1 to ask) -> volume
0x45SYS_SOUND_PARAMSkoi_sound_params
(voice, volume, pan) -> 0, or -1
0x46SYS_SOUND_WHEREkoi_sound_where
(voice) -> frames played

Change volume or pan of a playing sound (-1 keeps).

0x48SYS_SOUND_SEEKkoi_sound_seek
(voice, frame) -> 0, or -1
0x50SYS_MOUSEkoi_mouse
(KOI_POINTER*) -> 1, 0 without a pointer, -1

Pointer: one snapshot per call. `scroll` is a running total; callers subtract.

0x51SYS_MOUSE_PLACEkoi_mouse_place
(point) -> 0, or -1

Move the pointer.

0x52SYS_CHAINkoi_chain
(command) -> 1, or 0 when full

Chaining: run a command line after this program exits, most recent request first: koi_chain("MIZU Z:\\GAMES"); runs second koi_chain("DOOM"); runs first koi_exit(0); The program restarts fresh; pass state as arguments. SYS_RUN keeps the caller in memory instead.

0x53SYS_CLIP_PUTkoi_clip_put
(text, length) -> length kept, or -1

Clipboard: text in kernel memory, outliving the program. SYS_CLIP_GET with a null buffer returns the length.

0x54SYS_CLIP_GETkoi_clip_get
(buffer, size) -> length, 0 when empty or another account's (build 965, K23: the clipboard is the account's that put it, and the system's; the system's put of nothing empties it)
0x55SYS_LOGkoi_log
(text) -> 0

Log: programs write into the kernel log. SYS_LOG_BYTES adds a hex dump. SYS_SECTOR_READ reads a raw sector (no write counterpart).

0x59SYS_RUNkoi_run
(command) -> exit code, or -1

Run a command and return when it finishes (the caller stays in memory). A program holding the screen should give it back first.

0x5ASYS_GETENVkoi_getenv
(name, buffer, size) -> length, or 0
0x5BSYS_ENVATkoi_env_at
(index, name, size) -> length, or 0
0x5CSYS_LAYOUT_GESTUREkoi_layout_gesture
(enabled) -> 0

Environment, read-only for programs. SYS_GETENV returns the value length (0 if none); SYS_ENVAT lists by index (0 past the end). Enable the Alt+Shift layout switch while this program runs (reset on exit).

0x5DSYS_LOADkoi_capture
(path, KOI_MODULE*) -> 0, or -1
0x5ESYS_UNLOADkoi_unload
(base) -> 0, or -1
0x5FSYS_CAPTUREkoi_capture
(line, buffer, size) -> bytes, or -1

Loadable modules: load and relocate an image without running it and return its entry point. The caller calls it like a function; its meaning is the caller's (Mizu applications). Same format and ABI check as programs. SYS_LOAD fills a KOI_MODULE and returns 0, or -1 with the reason printed. SYS_UNLOAD frees by `base` (unknown bases, and modules a thread is still in, are refused). Run a command and collect its console output into the caller's buffer (no screen change). Returns the bytes collected, zero-terminated. The caller waits; commands that wait for a key are stopped. For programs that draw, use SYS_RUN.

0x60SYS_BREAKkoi_break
(enabled) -> 0

Whether Ctrl+C stops this program (0 makes it an ordinary character). Reset on exit.

0x61SYS_SOUND_OPENkoi_sound_open
(rate, KOI_SOUND_SHAPE(bits, channels), volume) -> voice, or -1

Streaming sound: open a stream and queue decoded frames (copied into a kernel ring). Fewer frames taken than offered means the ring is nearly full.

0x62SYS_SOUND_QUEUEkoi_sound_queue
(voice, samples, frames) -> frames taken, or -1
0x63SYS_SOUND_SPACEkoi_sound_space
(voice) -> frames of room, or -1
0x64SYS_SOUND_PAUSEkoi_sound_pause
(voice, paused) -> 0, or -1

Pause any voice; flush a stream's queue (for seeking).

0x66SYS_SOUND_FLUSHkoi_sound_flush
(voice, frames heard so far) -> 0, or -1
0x67SYS_NET_RESOLVEkoi_net_resolve
(name, &address) -> 0, or -1

Network for programs: DNS and TCP. Calls block with a timeout and poll the network. Addresses in host order.

0x68SYS_TCP_CONNECTkoi_tcp_connect
(address, port, timeout ms) -> handle, or -1
0x69SYS_TCP_SENDkoi_tcp_send
(handle, data, KOI_NET_ARG(length, timeout))
0x6ASYS_TCP_RECEIVEkoi_tcp_receive
(handle, buffer, KOI_NET_ARG(size, timeout))
0x6BSYS_TCP_CLOSEkoi_tcp_close
(handle) -> 0
0x6CSYS_TCP_OPENkoi_tcp_open
(handle) -> 1 while there is more to read
0x6DSYS_TLS_CONNECTkoi_tls_connect
(address, KOI_NET_ARG(port, timeout), name, flags)

TLS: the same calls over TLS. SYS_TLS_CONNECT also takes the host name (SNI and certificate check). SYS_TLS_CHECKED returns 1 when the server's identity was verified.

0x6ESYS_TLS_SENDkoi_tls_send
(handle, data, KOI_NET_ARG(length, timeout))
0x6FSYS_TLS_RECEIVEkoi_tls_receive
(handle, buffer, KOI_NET_ARG(size, timeout))
0x70SYS_TLS_CLOSEkoi_tls_close
(handle) -> 0
0x71SYS_TLS_CHECKEDkoi_tls_checked
(handle) -> 1 when the identity was verified
0x72SYS_THREAD_STARTkoi_thread_start
(entry, stack top, argument) -> 1 or 0

Threads: same memory and files, own stack (provided by the program, top passed in). No protection between threads; use the lock in koi.h.

0x73SYS_THREAD_EXITkoi_thread_exit
(), ends this thread
0x74SYS_YIELDkoi_lock
() -> 0

Yield the rest of the time slice.

0x76SYS_SET_THREAD_POINTER
(address) -> 0

Set the calling thread's FS base (thread pointer), kept across switches (like arch_prctl(ARCH_SET_FS)).

0x77SYS_GPU_INFO
(item) -> value

virtio-gpu for programs (see kernel/virtio_gpu.h): SYS_GPU_INFO KOI_GPU_PRESENT, KOI_GPU_3D or KOI_GPU_CAPSETS SYS_GPU_COMMAND one command and its reply; returns the reply length or -1 (commands touching guest memory are refused) SYS_GPU_ATTACH backing memory for a resource; returns its address or 0 SYS_GPU_DETACH release it before unreferencing the resource

0x78SYS_GPU_COMMAND
(request, request size, reply, reply size)
0x79SYS_GPU_ATTACH
(resource, size) -> address, or 0
0x7ASYS_GPU_DETACH
(resource) -> 1, or 0
0x7BSYS_INTEL_INFOkoi_intel_info
(item) -> value

Intel graphics for programs (see kernel/i915.h): SYS_INTEL_INFO KOI_INTEL_DEVICE (0 without a driver) or KOI_INTEL_GTT_SIZE SYS_INTEL_ALLOC `size` zeroed bytes; returns the CPU address, writes the GPU address to *gpu, or returns 0 SYS_INTEL_FREE by CPU address SYS_INTEL_EXEC run the batch at a GPU address (ends in MI_BATCH_BUFFER_END); returns a sequence or 0 SYS_INTEL_WAIT 1 when the sequence finished, 0 on timeout

0x7CSYS_INTEL_ALLOCkoi_intel_alloc
(size, &gpu) -> address, or 0
0x7DSYS_INTEL_FREEkoi_intel_free
(address) -> 1, or 0
0x7ESYS_INTEL_EXECkoi_intel_exec
(gpu address) -> sequence, or 0
0x7FSYS_INTEL_WAITkoi_intel_wait
(sequence, timeout ms) -> 1, or 0
0x81SYS_NET_INFOkoi_screen_set
(KOI_NET_INFO*) -> 0, or -1

Network settings: SYS_NET_INFO fill a KOI_NET_INFO SYS_NET_START DHCP again (may take seconds): 1 with an address, else 0 SYS_NET_SET static: address, netmask, gateway << 32 | dns; 1 when set Not saved across restarts.

0x88SYS_DEVICESkoi_devices
(KOI_DEVICE_INFO*, max) -> count

Devices on the PCI bus and the driver running each (Device Manager).

0x89SYS_SHA256koi_sha256
(data, length, digest) -> 0

SHA-256 of `length` bytes into 32 (the kernel's, for passwords).

0x8FSYS_CHDIRkoi_chdir
(path) -> 1, 0, -1 or -2

The command shell (COMMAND.EXE). SYS_CHDIR moves the one current drive and directory that programs and commands share ("X:" changes the drive): 1, or 0 no such drive, -1 not found, -2 not a directory. SYS_SETENV sets a variable, an empty value removes it: 1, or 0 when there is no room.

0x90SYS_SETENV
(name, value) -> 1 or 0
0x91SYS_TCP_LISTENkoi_tcp_listen

Serving: listen on a port, then accept connections (with a timeout).

0x92SYS_TCP_ACCEPTkoi_tcp_accept
0x97SYS_RANDOMkoi_random

Random bytes from the kernel's generator (the one TLS uses): for keys and nonces. At most 4096 per call. Returns the count, or -1.

0x9CSYS_GFX_SURFACEkoi_gfx_dim
(operation, a, b)

Surfaces: pixels of the program's own (a window's, say), drawn into with the SYS_GFX_* calls and copied onto the screen. Numbers from 1; 0 is the screen. NEW a: KOI_POINT(width, height), b: KOI_SCREEN* to fill -> number, or -1 FREE a: number -> 0, or -1 TARGET a: number (0: the screen) for this thread's drawing calls -> the one before, or -1 COPY a: number, b: KOI_POINT where, onto the current target (clipped) -> 0 ORIGIN a: number, b: KOI_POINT: drawing at that point lands at the surface's (0, 0), so a window draws in screen places -> 0, or -1 The kernel frees a program's surfaces when it ends.

0x9DSYS_MESSAGE_SENDkoi_message_send
(program id, data, length) -> 1, 0 or -1

Messages between programs (19): a mailbox per program; a program is addressed by its id (KOI_INFO_PROGRAM_ID, slot and generation, so a reused slot is not mistaken for it). Messages are 1 to 4096 bytes and arrive in order. SYS_MESSAGE_SEND: 1 sent, 0 the mailbox is full (try again), -1 no such program. SYS_MESSAGE_RECEIVE waits up to the timeout: the length, 0 nothing came, -1 the buffer was too small (the message is dropped); the sender's id goes to `from`. SYS_MESSAGE_NAME registers this program under a name or finds the one holding it (names starting MIZU. only for programs of the system; the desktop is MIZU.DESKTOP).

0x9ESYS_MESSAGE_RECEIVEkoi_message_receive
(buffer, size, timeout ms, &from) -> length, 0 or -1
0x9FSYS_MESSAGE_NAMEkoi_name_register
(KOI_NAME_*, name) -> 1 / id, or 0
0xA0SYS_TERMINALkoi_name_find
(operation, a, b, c)

Terminals (20): a window that a program (a shell) runs in. Everything the program and its children print comes to the window, which reads it with READ; keys the window passes with WRITE are what they read (a line is edited and echoed by the kernel). Ctrl+C (key 3) stops the program that has not turned it off; the shell gets it as a key. A program that draws on the screen is refused while attached to one. OPEN a: command line, b: KOI_POINT(columns, rows) -> terminal, or -1; KOI_TERMINAL_ALL_TAKEN (-2) when all TERMINAL_MAX (32) are in use (open windows, and closed ones a program started apart still writes to) READ a: terminal, b: buffer, c: size -> bytes (0 none yet), -1 the program ended and all it said has been read WRITE a: terminal, b: int keys, c: count -> keys taken SIZE a: terminal, b: KOI_POINT(columns, rows) -> 0 CLOSE a: terminal -> 0 (its programs stop at their next call; one run by `start` runs on)

0xA1SYS_TCP_STATUSkoi_tcp_status
(handle) -> bytes | KOI_TCP_*

A connection's state without waiting (21): the bytes waiting to be read in the low 32 bits, and flags. -1 for no such connection.

0xA2SYS_PIPE
(operation, a, b, c)

Pipes between programs (23). An end is a number the program owns until it closes it or ends. CREATE b: int[2] gets the reading and the writing end -> 0, or -1 READ b: end, c: buffer, d: size -> bytes, waiting for at least one; 0 once every writer has gone; -3 if a signal (or Ctrl+C) for the reader came first (build 235) WRITE b: end, c: data, d: size -> bytes, waiting for room; -1 once every reader has gone CLOSE b: end -> 0 NEXT b: 0 stdin, 1 stdout, 2 stderr; c: an end, or 0 -> the next program this one starts gets it as that stream STANDARD b: 0, 1 or 2 -> this program's end for that stream, 0 if the stream is its terminal PENDING b: a reading end -> bytes waiting, -2 at the end, 0 none yet Meeting by name (AF_UNIX sockets bound to a path, build 515): LISTEN b: a socket file's path, or "@" and a name of no file; c: how many may wait -> a listener (1 and up), -1, -2 if the name is taken CONNECT b: as LISTEN's; c: int[2] gets this side's reading and writing end -> 0; -1 nobody listens there, -2 too many wait already Both: -3 the caller may not write the socket file, -4 it is not there. ACCEPT b: listener, c: int[2] gets the reading and writing end of the next one to connect, d: 1 to wait -> 0; -2 none waiting (d 0); -3 a signal came first; -1 the listener is gone KNOCKS b: listener -> how many wait to be accepted UNLISTEN b: listener -> 0; those waiting see their ends closed A listener goes when its program ends. Passed along with the bytes (SCM_RIGHTS, build 518): SEND_PARCEL b: a writing end, c: KOI_PARCEL_ITEM[], d: how many (16 at most) -> 0, or -1: copies of the caller's pipe ends and files wait with the pipe for whoever reads it TAKE_PARCEL b: a reading end, c: KOI_PARCEL_ITEM[16] -> how many items were in the oldest parcel, now the caller's own; 0 none waiting Send the parcel before the bytes it goes with: whoever has read them finds it. A parcel nobody takes goes when the pipe's readers have. A program whose stdout is a pipe prints into it (SYS_PUTS and the rest); one whose stdin is a pipe reads its lines and keys from it.

0xA3SYS_SIGNAL
(operation, a, b, c)

Signals and process groups (24), Linux's signal numbers. A signal is acted on at the program's next system call. Handled ones are ORed into the word the program named (bit 1 << signal); ignored ones go; the rest end it with 128 + the signal, except CHLD, CONT, URG, WINCH and the stopping ones, which do nothing. Ctrl+C is SIGINT to the terminal's foreground group; a program that ends sends SIGCHLD to the one that started it. WATCH b: the word, c: handled mask, d: ignored mask -> 0 SEND b: a program's name, or -group, or 0 for its own group; c: the signal (0 to ask if it is there) -> 0, or -1 SELF -> its name (the pid) PARENT -> its parent's, 0 none GROUP b: a name, 0 itself -> its group SET_GROUP b: itself (0) or a child; c: a group, 0 its own -> 0 or -1 SESSION b: a name, 0 itself -> its session NEW_SESSION -> a session and group of its own, their name FOREGROUND -> its terminal's foreground group, 0 without a terminal SET_FOREGROUND b: a group -> 0, or -1

0xA4SYS_FUTEX
(operation, word, value, milliseconds)

A futex: threads of one program waiting on a word of its memory. WAIT b: the word (4 bytes), c: the value it must still hold, d: milliseconds at most (<= 0: a long while) -> 0 woken or the time ran out, -1 the word held another value WAKE b: the word, c: how many to wake -> how many were woken A wait may also end for no reason; the caller looks at the word again. Added in build 150 without a new interface version: before it, the call answers -1 for both, and a caller gives its turn away and looks again.

0xA5SYS_PROTECT

Pages of the program's own made runnable (c = 1) or not (c = 0): what it allocates is data, which the processor will not run (no-execute), until it asks - a JIT for the code it writes (mprotect, mmap with PROT_EXEC). (address, length, executable) -> 0, or -1 if a page is not its own. Added in build 173 without a new interface version: before it, all of a program's memory could be run, and the call answers -1. Rights, when KOI_INFO_PROTECT_RIGHTS says the kernel has them: c = 2 the pages may be read, not written or run; 3 read and run, not written; 4 no access at all (the pages stay, a touch faults); and 0 now also gives back write, and 1 read, write and run, as c = 0 and 1 did. Pages the program gave no access to are its own still. A kernel without them takes any other c for 1 (run): so a program asks KOI_INFO_PROTECT_RIGHTS first.

0xA6SYS_TOKENkoi_install_status

Who the program runs as - its token, held by the kernel (no program can change its own): SELF -> the account's number (0 the system itself), with KOI_TOKEN_ADMIN set when it acts as an administrator LAUNCH b: an account, c: administrator or not -> 0, or -1: what the programs it starts get from now on. The system may give any; anybody else only its own token or less. Added in build 175 without a new interface version (before it: -1).

0xA7SYS_PERMISSIONSkoi_token_launch

A file's permissions (on KFS; elsewhere none, and everything is open): GET b: path, c: &KOI_PERMISSIONS -> 0, or -1 SET b: path, c: &KOI_PERMISSIONS -> 0, or -1. The rwx bits: its owner, an administrator or the system; the owner and group: an administrator or the system. Setting puts them in force. Groups: KOI_GROUP_ADMINISTRATORS, KOI_GROUP_USERS. Build 180.

0xA8SYS_LINUX
0xA9SYS_INPUTkoi_uptime_us
(KOI_POINTER*, KOI_INPUT_EVENT*, max | flags, wait_ms)

Linux programs (Kawa, the Mizu Subsystem for Linux). Memory a program is given otherwise is somewhere the system picks; a Linux program wants its own addresses (an ELF's segments, MAP_FIXED, a stack), and gets them in the bottom of its space, below KOI_LINUX_TOP: MAP b: address, c: size, d: KOI_LINUX_WRITE | KOI_LINUX_EXEC -> 0, or -1: fresh pages, all zero, there; what was there before goes (Linux's MAP_FIXED). Whole pages. UNMAP b: address, c: size -> 0, or -1 PROTECT b: address, c: size, d: as for MAP -> 0, or -1 (every page in the range must be there) COMMIT b: address, c: size, d: as for MAP -> 0, or -1: pages that are not there yet made (all zero), and every page in the range given those rights (a reservation coming into use). FORK b: entry, c: argument -> the new program's name (its pid), or -1: a copy of this program - its own pages copied, where it is, its terminal, token, group, signals and pipe ends taken over - with one task, which starts at `entry` with `argument` in RDI (and RSP), this task's thread pointer and FPU state. Only a program in its own pages (Kawa) can be copied. FORK_ENDS b: int pairs[][2], c: room -> how many: in a forked program, which of its pipe ends (second) stand for its parent's (first). FORK_FILES b: int pairs[][2], c: room -> how many: the same for open files (SYS_OPEN handles), each the child's own from the fork on, at the position it had (build 234). DISCARD b: address, c: size -> 0, or -1: the program's own pages in the range given back; read again, they are all zero, with the rights they had (Linux's madvise MADV_DONTNEED). Pages not there, or not the program's own, are left (build 254). HANDLER b: entry, or 0 -> 0, or -1: where the `syscall` instruction goes in this program. The program's RSP comes down by 128 (the red zone) and 16 more: [RSP] its flags, [RSP+8] where to go back to; every register as it was. Without a handler the instruction gives -38 (ENOSYS) in RAX. TASK -> this task's number (for POKE). SIGNALS b: entry, or 0 -> 0, or -1: where a poked task goes. POKE b: a task of this program -> 0, or -1: it is to come to the SIGNALS entry (a signal for that thread, Linux's tkill). One asleep in SYS_FUTEX or SYS_SLEEP wakes early. One in the program's own code (below KOI_LINUX_TOP) is taken there at its next interrupt, its stack made ready as for HANDLER: the entry saves what it uses and ends with the flags and `ret $128`. Elsewhere (in the handler, say) it comes when it is back in the program's code; it may look for itself before. A signal the program handles (its signal word) pokes every task of a program with a SIGNALS entry: each looks whether it may take it (build 260). Pages are readable whenever they are there, but with KOI_LINUX_NONE. Build 209. Input in one call (the desktop's loop: was MOUSE, KEYPRESSED, GETCHAR and SLEEP each turn). `pointer` (may be 0): what SYS_MOUSE gives; as it comes in, the snapshot the caller has, so that a change in it (moved, pressed, scrolled) ends the wait. Up to `max` & 0xFFFF events: what was typed (KOI_INPUT_CHAR, from the queue SYS_GETCHAR reads), and with KOI_INPUT_KEYS keys up and down too (as SYS_KEYEVENT with KOI_KEYEVENT_SCREEN). Nothing new: waits up to `wait_ms` for something (0: not at all), woken at once by it; a signal ends the wait. -> how many events were written, -1 on bad arguments. Build 413.

0xAASYS_TCP_BIND
(address, port, flags 0) -> handle, or KOI_TCP_BIND_*

An address and port of one's own (build 1139), for SYS_TCP_LISTEN's KOI_TCP_LISTEN_BOUND. address 0 any, 127.x.y.z loopback only (a listener there hears 127.0.0.0/8 and nothing from the wire), or the card's own; port 0 for a free one from 49152-65535 (SYS_TCP_INFO says which). Taken: only by another listener or bound handle on an address that meets this one (connections on the port do not count; no TIME_WAIT). An older kernel answers -1 (SYSCALL_ERROR) to every call: none of the answers below. Connections to 127.0.0.0/8 and to the card's own address stay inside the machine (both ends here).

0xABSYS_TCP_INFO
(handle, KOI_TCP_INFO_*) -> value

What is known of a connection (build 1139): -1 for no such connection, or not known.

0xACSYS_UDP_OPEN

UDP sockets (build 1142). An older kernel answers -1 (SYSCALL_ERROR) to each. OPEN (address, port, flags 0) -> handle, or KOI_TCP_BIND_* as SYS_TCP_BIND: address 0 any, 127.x.y.z loopback only (nothing from the wire), or the card's; port 0 for a free one (SYS_UDP_INFO says which); below 1024 only for the system and an elevated administrator, the kernel's own (68, 40001, TFTP's) for nobody. Taken by another socket on an address that meets. SEND (handle, data, length, address << 16 | port) -> length, -1, or -2 too big (the wire: 1472; 127/8 and our own address, inside the machine: 65507). Always from the socket's own address; broadcast refused. RECEIVE (handle, buffer, KOI_NET_ARG(size, timeout ms), KOI_UDP_FROM* or 0) -> the datagram's whole length (at most `size` copied: the rest is lost), -1 after the timeout or for no such socket. INFO (handle, KOI_UDP_INFO_*) -> value, -1 for none CLOSE (handle) -> 0, or -1 Each socket keeps what comes in for it (256 KiB; more is dropped and counted).

0xADSYS_UDP_SEND
0xAESYS_UDP_RECEIVE
0xAFSYS_UDP_INFO
0xB0SYS_UDP_CLOSE
0xB1SYS_SET_TIMES

A file's times set (build 1198; KOI_INFO_SET_TIMES says the kernel has it): (path, const KOI_FILE_TIMES*) -> 0, KOI_REFUSED (to a program asking with KOI_TOKEN_REFUSALS), KOI_OPEN_READ_ONLY (the system image), or -1. `which`: the times to set, from the struct or now. Both "now": its owner, an administrator acting as one (elevated), the system, or whoever may write it. Any time given: its owner, elevated, or the system - as Linux has it. Seconds as the rest of Mizu's times are (the machine's local time since 1970); KFS keeps them to the nanosecond, FAT in its two-second steps from 1980 to 2107 (clamped there, as Linux's vfat does) and its access date alone. A folder's times too.

0xB2SYS_FILE_KIND
(path) -> KOI_KIND_*, or -1

What runs a file, by its content, not its name (ABI 27): the loader's own test, under the caller's right to read it (opened as SYS_OPEN would). A Mizu program (an ELF PIE whose lowest loaded part starts with Mizu's ABI header, "KOI!"), a Linux program (Kawa's: any other ELF), a Windows program ("MZ": Mizu refuses to start it, "This is a Windows program..."), a script (a .BAT/.CMD by its name, or a file whose first line starts "#!"), or none of these. -1: no such file, a folder, or not the caller's to read. A "#!" file is a script for what to offer: opening it still belongs to an editor, unless the user asks for it to run.

0xB3SYS_SNAPSHOTkoi_token_grant

SYS_SNAPSHOT (a: operation, b: a drive letter, ...): a KFS volume's snapshots (build 1347). LIST c: KOI_SNAPSHOT_INFO[], d: most -> how many there are (up to KOI_SNAPSHOTS_MAX), or -1 (no such volume, not KFS). `held` is 0 except for the system or an administrator. SPACE c: &KOI_SNAPSHOT_SPACE -> 0, or -1. For everyone. The system or an administrator (admin 1) only - else -1, nothing changed: TAKE c: name, d: KOI_SNAPSHOT_PINNED, KOI_SNAPSHOT_AUTOMATIC (a schedule's: the cap takes them as unpinned ones) or 0 - not both -> its epoch; -1 refused, -2 the table full, -3 no room (none is removed for it), -4 busy: a removal, or a folder moved into Users\\ still being taken in (try again shortly), -5 Z: is in memory (started from a CD: nothing is kept, build 1367) PIN, UNPIN c: epoch -> 0, or -1 RENAME c: epoch, d: name -> 0, or -1 REMOVE c: epoch -> 0 (being removed: the syncer frees it in pieces), -1, -4 a removal under way (one at a time) A name: UTF-8, no control characters, cut at 24 bytes on a character's boundary; "" for none; not unique. The folder under X:\.SNAPSHOTS is the time it was taken and never changes. A snapshot keeps files as they were, with their owners and rights then: a file made private later stays readable in older snapshots by whoever could read it then.

0xB4SYS_SYSTEM_SETTING

SYS_SYSTEM_SETTING (a: operation, b: a key's id, c: a value's id; build 1378): SYSTEM.CFG's lines from a fixed table, no text crossing (Settings' Power button). GET -> what the file says (the value's id, 0 for no line) | what this start runs with << 8 (1 or 2: the default's choice included), or -1 for an unknown key. For everyone. SET -> 1 written (in force at the next start; value 0 removes the line, the default), 0 not written, -1 an unknown key or value, or not the system or an administrator (admin 1).

0xB5SYS_POWER_STATE

SYS_POWER_STATE (out: KOI_POWER_STATE*, size: its size; build 1393): the lid, the charger and the batteries as the firmware's ACPI methods give them (uACPI, SYSTEM.CFG `acpi = take`) - 0, or -1. Anyone may call it. `known` 0: no uACPI or no such devices (everything -1). The numbers are the firmware's, with their unit (_BIX or _BIF, _BST); -1 is unknown (ACPI's 0xFFFFFFFF too). The derived ones are computed in the kernel only, so every program shows the same: percent remaining * 100 / last full capacity (the design capacity when that is unknown or 0), rounded down, at most 100; -1 unknown minutes_to_empty remaining * 60 / rate while discharging (state bit 1) minutes_to_full (full - remaining) * 60 / rate while charging (state bit 2) - both -1 when the rate is 0 or unknown (some ECs say 0 for minutes after the charger goes in), or in the other state total_percent the sum's percent over the present batteries, when they share a unit; else -1 `changes` goes up by one at every change, so a program may redraw only then. A battery icon hides when no battery is present: total_percent == -1 (`batteries` counts the slots the firmware has, an empty one too: then that battery's present is 0, its numbers -1).

0xB6SYS_BACKLIGHTkoi_snapshot_change

SYS_BACKLIGHT (a: operation; build 1414): the screen's brightness, through the ACPI video output (with acpi = take; backlight-design.md). -1 everywhere when there is no way (desktops, QEMU). GET -> the level 0-100, or -1 STEPS -> how many real levels (draw that many marks), or -1 SET b: 0-100 -> 0, or -1; set to the nearest real level, never black (the lowest above 0) CLAIM -> 0: the caller handles KOI_KEY_BRIGHTNESS_UP/DOWN while it runs (the desktop); with no program claiming them the kernel steps the brightness itself on those keys. GET and STEPS for everyone; SET and CLAIM for the system, an administrator and the program that holds the screen (as the master volume).

The system's own

Calls from 0x100 up are for the programs of the system itself (Setup, Updates, mizupkg, the desktop). The kernel refuses them to any program that was not read from the system image, and they are outside the frozen interface: they may change between versions.

0x100SYS_LOG_BYTESkoi_log_bytes
(label, data, length) -> 0
0x101SYS_SECTOR_READkoi_sector_read
(disk, lba, buffer) -> bytes read, or -1
0x102SYS_SECTOR_SIZEkoi_sector_size
(disk) -> bytes per sector, or -1

Disk count and sector size.

0x103SYS_SETCLOCKkoi_set_clock
(date, time) -> 1, or 0

Set the clock: (KOI_DATE_ARG(year, month, day), KOI_TIME_ARG(hour, minute, second)) -> 1, or 0 if invalid.

0x104SYS_SCREEN_SETkoi_gfx_enter_copy
(width, height, KOI_SCREEN*) -> 1, 0 or -1

SYS_SCREEN_SET: change the screen size now (Intel Sandy Bridge, QEMU std VGA; modes that `screen` lists). Only the caller may hold the screen; if it does, *screen gets the new buffer. Not saved (use `screen WxH`). Returns 1 done, 0 not possible, -1 another program holds the screen.

0x105SYS_NET_STARTkoi_net_start
() -> 1, or 0
0x106SYS_NET_SETkoi_net_set
(address, netmask, gateway << 32 | dns) -> 1, or 0
0x107SYS_DISKSkoi_inject_key
(KOI_DISK_INFO*, max) -> count

Installing onto a disk (Setup /I). SYS_DISKS every disk: KOI_DISK_INFO into `array`, up to `max` SYS_PARTITIONS partitions of disk `disk`: KOI_PARTITION_INFO SYS_INSTALL install as KOI_INSTALL says; blocks until done SYS_INSTALL_STATUS progress of a running SYS_INSTALL (from another thread)

0x108SYS_PARTITIONSkoi_partitions
(disk, KOI_PARTITION_INFO*, max) -> count
0x109SYS_INSTALLkoi_install
(const KOI_INSTALL*) -> 0, or -1
0x10ASYS_INSTALL_STATUSkoi_install_status
(KOI_INSTALL_STATUS*) -> 0
0x10BSYS_PKG_FETCH
(server, path, buffer, size) -> bytes, or -1

Packages (MIZUPKG.EXE). The kernel fetches from the package server, checks signatures with its own key, and writes system updates; the rest is the program's.

0x10CSYS_PKG_VERIFY
(manifest, length, signature, sig length) -> 1 or 0
0x10DSYS_SYSTEM_UPDATE
(const KOI_SYSTEM_UPDATE*) -> 1 or 0
0x10ESYS_DRIVERS_RELOAD
() -> 0: load new drivers, start them
0x10FSYS_PROGRAM_PATH
(directory, 1 add or 0 remove) -> 1 or 0
0x110SYS_INPUT_INJECTkoi_inject_pointer
(kind, a, b) -> 0, or -1

Input from elsewhere (remote desktop), fed in as if typed or moved. KOI_INPUT_POINTER: (point, buttons | wheel << 8), absolute position. KOI_INPUT_KEY: (key, typed | KOI_KEY_RELEASED): `key` is the key's identity as in SYS_KEYEVENT, `typed` what it types (0 for none): a KOI_KEY_ value, a control code, or a character (Unicode, not passed through the layout).

0x111SYS_LOG_READkoi_log_read
(offset, buffer, size) -> count; -2 dropped (see KOI_TRACE_LOG_START)

The kernel log (what `log` shows) from `offset`: bytes copied, 0 at the end.

0x112SYS_PROFILEkoi_profile_mark
(label, slow_ms) -> 0

Turn profiler: marks the start of a unit of work; the one before, if it took `slow_ms` or more, is logged ("SLOW:") with where its time went.

0x113SYS_POINTER_SPRITEkoi_pointer_sprite
(operation, data) -> 1, or 0

Pointer drawn and moved by the kernel over the caller's screen level, so it moves while the program is busy. KOI_SPRITE_SHAPE: data = { width, height, pixels... } (at most 32 x 32; opaque where the top byte is set). Since build 465 width and height may carry the hotspot in their top 16 bits (width | hot_x << 16, height | hot_y << 16): that point of the shape is at the pointer. An older kernel refuses such a shape (returns 0). Since build 1111: up to 64 x 64, and KOI_SPRITE_ALPHA in the width word (bit 31) makes the top byte each pixel's opacity (straight, not premultiplied: 0 clear, 255 opaque, between them blended over what is under it). A kernel before it reads the bit as part of hot_x, past the width, and refuses the shape (0) without changing the pointer: fall back to a 32 x 32 shape without it. (A new operation number would not do: older kernels take any other operation as KOI_SPRITE_OFF.)

0x114SYS_POWERkoi_power

(what) -> -1 if the machine stayed on: KOI_POWER_OFF, KOI_POWER_RESTART, or KOI_POWER_RESTART_MENU (restart into the loader's boot menu, build 280). File operations in progress end first, the rest wait; disk caches are written out.

0x115SYS_LAYOUTkoi_inject_pointer

Keyboard layouts (kernel/layout.h): what a := KOI_LAYOUT_*. GET the one typing now, as two letters packed ('r' << 8 | 'u') LIST the set into b ("en ru el", size c) SET_LIST the set from b; 1 if any code was known SWITCH how to switch (b: KOI_LAYOUT_SWITCH_*, -1 asks) NEXT the next one in the set Not saved: the SYSTEM settings `layouts` and `layout_switch` are.

0x116SYS_SOUND_OUTPUTkoi_sound_output

Sound outputs (the built-in device, USB ones plugged in): what a := COUNT how many NAME the name of output b into buffer c (64 bytes) CURRENT the one playing (an index) SELECT play through output b CHANGES a count that moves when one comes or goes

0x117SYS_TRACEkoi_trace

Flight recorder: what the kernel did lately, as 32-byte records (time in microseconds since boot, kind, processor, task), and copies of it from around each hitch (a desktop turn over the threshold, or Ctrl+Alt+M: the last three seconds, kept by hand). What a := RECENT the newest records into c (KOI_TRACE_MAX), oldest first -> count HITCHES the kept hitches' headers into c (KOI_HITCH_KEEP) -> count HITCH hitch b's records into c (KOI_TRACE_MAX) -> count THRESHOLD set it to b ms (0: leave it) -> the threshold LINES log line times into c (KOI_LINE_MAX pairs: offset, time) -> count LOG_START the oldest log offset still kept after the boot part, and the boot part's length in c (two numbers) -> 0 NAMES the tasks' names now into c (KOI_TRACE_TASKS x 32 bytes) -> 0

0x118SYS_USB_DEVICESkoi_devices
(KOI_USB_INFO*, max) -> count

Devices on USB, hubs included, from every controller (Device Manager).

0x119SYS_PROGRAM_STATS
(KOI_PROGRAM_STATS*, max) -> count

Programs and the machine for a task manager. Counters only go up (from the program's start, or the system's): read them twice, a second apart, and the difference is the rate - processor thousandths are (cpu_ms now - cpu_ms then) * 1000 / (uptime_ms now - uptime_ms then), of one processor; divide by `processors` for the whole machine's. SYS_PROGRAM_STATS fills up to `max` and says how many programs there are (more than `max`: ask with room for that many).

0x11ASYS_SYSTEM_STATS
(KOI_SYSTEM_STATS*) -> 0
0x11BSYS_FILE_TAGS
(path, tags or -1) -> its tags, or -1

A file's tags: a byte of marks its owner gives it (Files' coloured drops), kept on KFS drives. Read with -1 (needs leave to list its folder); set needs leave to write the file. A search gives them too (KOI_ATTRIBUTE_TAGS). -1 elsewhere. Every program's since build 1051 (before: the system's only, as its number says).

0x11CSYS_EVENTS
(from id, KOI_RECORD*, max) -> count

The event log (Event Viewer): what happened, as records, kept on the system drive across starts (\BOOT\EVENTS.BIN, the latest 8192 or so). Ids go up by one, never reused, across starts; `from` 0 is the oldest kept. Up to `max` records with ids from `from` on, oldest first; read again from the last id + 1 until 0 come. `max` 0: the id the next event will have.

0x11DSYS_MIXER
(KOI_MIXER_*, a, b)

The sound mixer (a volume mixer, an equalizer). A program's volume is kept by its path while the system runs, so it holds when the program is started again; the settings themselves (to keep across starts) are the caller's to save and set again. The equalizer has ten bands, 31, 62, 125, 250, 500 Hz, 1, 2, 4, 8 and 16 kHz, each -12..+12 dB, over everything played; all 0 is off. With a boost the whole is turned down by the largest one first, so nothing clips.

0x11ESYS_BOOT_ENTRY
(KOI_BOOT_ENTRY_*, b, c)

The firmware's boot menu (UEFI variables Boot#### and BootOrder), written by the kernel itself (build 557): the installer needs no restart from the stick for it. An administrator's, or the installer's on a stick with no accounts. ADD b: KOI_BOOT_ENTRY (disk, partition: the ESP as SYS_DISKS and SYS_PARTITIONS number them; path; description; first) -> 0, number filled (an identical entry is used again) LIST b: KOI_BOOT_ENTRY_INFO list, c: max -> count, in boot order REMOVE b: the entry's number -> 0 -> -1 refused or a bad request, -2 no UEFI runtime services (an older loader, a BIOS boot: restart from the stick instead), -3 the firmware refused, -4 the entry was written but could not be put first.

0x11FSYS_EVENT_NOTE
(text, number)

A line in Event Viewer from a service of the system's (uid 0 only; build 1078): (text, a number) -> 0; KOI_RECORD_INFO, kind KOI_RECORD_NOTE, its source the program.

0x120SYS_PKG_FETCH_PART
(KOI_PKG_PART*) -> bytes, -1 or -2