diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..4777297 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,165 @@ +name: CI + +on: + push: + branches: [main] + pull_request: + workflow_dispatch: + inputs: + mcpp_ref: + description: "build mcpp from this branch or tag of mcpp-community/mcpp" + required: false + default: "" + +env: + # THE ENGINE THAT KNOWS THE TARGET. `x86_64-freebsd' is a row of mcpp + # 2026.11.10.1. A run given `mcpp_ref' builds the engine from that branch or + # tag, with the released one as the compiler of that build. + MCPP_VERSION: 2026.11.10.1 + MCPP_SOURCE_REF: ${{ github.event.inputs.mcpp_ref || '' }} + XLINGS_VERSION: v2026.10.26.1 + XLINGS_NON_INTERACTIVE: '1' + +jobs: + # THE CONFORMANCE SUITE ON FREEBSD. Built on Linux for `x86_64-freebsd' above + # FreeBSD's own C library (the base system's sysroot, ci/freebsd-platform), + # and run in a FreeBSD virtual machine, as the system a user runs it on. The + # three-way check of the manifest, kal_interfaces() and the exported names is + # made on both sides: the exported names when the suite is built, the other + # two by the suite when it runs. + # + # BOTH ARCHITECTURES. arm64 runs in an emulated machine (vmactions' + # `arch: aarch64', without hardware assistance), which is why its job is + # given longer. + # THE STANDALONE ARRANGEMENT is run the same way: openkal-musl over this + # implementation with the feature `standalone', built for + # `-freebsd-musl' from the working trees (tools/run-conformance-standalone.sh), + # its C++ runtime openkal-llvm-runtime, each at its branch of the same name when + # it has one. + conformance: + name: conformance (freebsd ${{ matrix.arch }}, ${{ matrix.arrangement }}) + runs-on: ubuntu-24.04 + timeout-minutes: ${{ matrix.minutes }} + strategy: + fail-fast: false + matrix: + include: + - { arch: x86_64, arrangement: hosted, minutes: 90 } + - { arch: aarch64, arrangement: hosted, minutes: 240 } + - { arch: x86_64, arrangement: standalone, minutes: 90 } + - { arch: aarch64, arrangement: standalone, minutes: 240 } + steps: + - uses: actions/checkout@v4 + + - name: The committed manifest names no local directory + run: | + set -euo pipefail + bad=$(grep -nE '^[a-z-]+ = \{[^}]*path = "(/|[A-Za-z]:)' mcpp.toml || true) + if [ -n "$bad" ]; then + echo "::error::the committed manifest names an absolute path"; printf '%s\n' "$bad"; exit 1 + fi + + - name: The specification + run: | + git clone --quiet https://github.com/mcpplibs/openkal.git .spec + branch='${{ github.head_ref || github.ref_name }}' + if git -C .spec rev-parse --verify --quiet "origin/$branch" > /dev/null; then + git -C .spec checkout --quiet "origin/$branch" + echo "the specification is at $branch" + else + echo "the specification has no $branch; its default branch is used" + fi + + - name: Install xlings + run: | + curl -fsSL https://raw.githubusercontent.com/openxlings/xlings/main/tools/other/quick_install.sh \ + | bash -s "$XLINGS_VERSION" + echo "$HOME/.xlings/subos/current/bin" >> "$GITHUB_PATH" + + - name: Install mcpp + run: | + set -euo pipefail + xlings update + xlings install "mcpp@$MCPP_VERSION" -y -g + mcpp self config --mirror GLOBAL + if [ -n "${MCPP_SOURCE_REF:-}" ]; then + src="$RUNNER_TEMP/mcpp-src" + git clone --quiet --depth 1 --branch "$MCPP_SOURCE_REF" https://github.com/mcpp-community/mcpp.git "$src" + rm -f "$src/.xlings.json" + ( cd "$src" && mcpp build --release ) + built=$(find "$src/target" -type f -name mcpp | head -1) + [ -n "$built" ] || { echo "::error::mcpp did not build from $MCPP_SOURCE_REF"; exit 1; } + echo "$(cd "$(dirname "$built")" && pwd)" >> "$GITHUB_PATH" + echo "under review: $("$built" --version) (from $MCPP_SOURCE_REF)" + fi + + - name: The suite, built for FreeBSD + env: + OPENKAL_CONFORMANCE_RUNNER: ${{ github.workspace }}/tools/defer-runner + OKF_DEFERRED: ${{ github.workspace }}/.deferred + run: | + set -euo pipefail + mcpp --version + features=full,exec,random,terminal,timeout,map,region + if [ '${{ matrix.arrangement }}' = standalone ]; then + branch='${{ github.head_ref || github.ref_name }}' + # openkal-musl and openkal-llvm-runtime (the suite's C and C++ + # runtimes) at their branch of the same name, else their default + # branch. + for repo in openkal-musl openkal-llvm-runtime; do + git clone --quiet "https://github.com/mcpplibs/$repo.git" ".$repo" + if git -C ".$repo" rev-parse --verify --quiet "origin/$branch" > /dev/null; then + git -C ".$repo" checkout --quiet "origin/$branch"; echo "$repo is at $branch" + fi + done + bash tools/run-conformance-standalone.sh .spec .openkal-musl '${{ matrix.arch }}' "$features" .openkal-llvm-runtime + else + bash .spec/tools/run-conformance.sh openkal-freebsd . "$features" \ + --target '${{ matrix.arch }}-freebsd' --platform "path:$GITHUB_WORKSPACE/ci/freebsd-platform" + fi + file .deferred/openkal-conformance + + - name: The suite, run in FreeBSD + uses: vmactions/freebsd-vm@v1 + with: + arch: ${{ matrix.arch }} + usesh: true + run: | + set -e + uname -a + ./.deferred/run.sh > .deferred/run.log 2>&1 || echo "status $?" >> .deferred/run.log + cat .deferred/run.log + + - name: The verdict + run: | + set -euo pipefail + log=.deferred/run.log + { echo '### openkal-freebsd conformance on FreeBSD (${{ matrix.arch }}, ${{ matrix.arrangement }})'; echo '```'; grep -E '^observations|DID NOT HOLD|^the implementation' "$log" | sort -u; echo '```'; } >> "$GITHUB_STEP_SUMMARY" + ! grep -q '^status ' "$log" || { echo "::error::the suite did not finish with status 0"; grep '^status ' "$log"; exit 1; } + grep -qE '^observations: [0-9]+ held, 0 did not hold' "$log" || { echo "::error::an observation did not hold"; exit 1; } + + # THE STANDALONE ARRANGEMENT, COMPILED FOR BOTH ARCHITECTURES from this + # package's sources and openkal's headers alone (tools/check-standalone.sh): + # no system header, no C library, and the objects carry the FreeBSD brand. + standalone: + name: standalone (compiled for x86_64 and aarch64) + runs-on: ubuntu-24.04 + timeout-minutes: 30 + steps: + - uses: actions/checkout@v4 + - name: The specification + run: git clone --quiet --depth 1 https://github.com/mcpplibs/openkal.git .spec + - name: Install xlings and mcpp + run: | + curl -fsSL https://raw.githubusercontent.com/openxlings/xlings/main/tools/other/quick_install.sh \ + | bash -s "$XLINGS_VERSION" + export PATH="$HOME/.xlings/subos/current/bin:$PATH" + xlings update + xlings install "mcpp@$MCPP_VERSION" -y -g + mcpp toolchain install llvm 23.1.3 + - name: Every source, for both architectures + run: | + set -euo pipefail + cxx=$(find "$HOME/.mcpp" -path '*xim-x-llvm/23.1.3/bin/clang++' | head -1) + [ -n "$cxx" ] || { echo "::error::clang++ 23.1.3 was not found"; exit 1; } + bash tools/check-standalone.sh "$cxx" .spec/include diff --git a/README.md b/README.md new file mode 100644 index 0000000..15e3a1e --- /dev/null +++ b/README.md @@ -0,0 +1,117 @@ +# openkal-freebsd + +An implementation of [openkal](https://github.com/mcpplibs/openkal) for FreeBSD, +written on the kernel's own system-call interface. + +```toml +[target.'cfg(os = "freebsd")'.dependencies] +openkal-freebsd = "0.1.0" +``` + +## What it is + +FreeBSD's kernel interface is stable: a program built for FreeBSD 14 runs on +every later 14.x and, through `COMPAT_FREEBSD14`, on 15. This implementation +issues that interface directly, on amd64 and arm64, and reads no header of a C +library. `src/sys.h` states FreeBSD 14's system-call numbers, flags and +structure layouts. They were read from the base system's headers by compiling +them for both architectures, where they are identical, and each structure's +size is asserted. The kernel reports a failure with the carry flag and the +error number in the first result register; `okf::call6` folds the two into one +value, the result or the error number negated, as openkal-linux's system calls +answer. + +## Two arrangements + +**Hosted (the default).** The program carries a runtime that has received +control: FreeBSD's own C library and `libthr`, as in a program built for +`x86_64-freebsd` or `aarch64-freebsd`. This implementation borrows what that +runtime already does, as openkal-linux does above a C library: + +- the program's arguments and environment, from the `(argc, argv, envp)` that + FreeBSD's run-time linker passes to every `.init_array` function; +- a context, which is the runtime's thread (`pthread_create`), and its stack, + which the runtime states (`pthread_attr_get_np`). + +`-lthr` is named only on those rows. + +**Standalone (feature `standalone`).** Nothing in the program has received +control: either it supplies its own runtime, or it is built above a C library +written on openkal (openkal-musl, the rows `x86_64-freebsd-musl` and +`aarch64-freebsd-musl`). This implementation then: + +- begins the program with `_start`, which takes the argument block from `%rdi` + or `x0`, and the auxiliary vector after it; +- carries the `.note.tag` ABI brand by which the kernel recognises a FreeBSD + image; +- lays out the image's `PT_TLS` block and sets the thread pointer + (`sysarch(AMD64_SET_FSBASE)` on amd64, `tpidr_el0` on arm64); +- hands over to a linked C library's `__libc_start_main`, or else runs the + initialisers and calls `main`; +- creates contexts with `thr_new` on stacks it maps with a guard, and joins + them on the word `thr_exit` sets; +- states the first stack from `AT_USRSTACKBASE` and `AT_USRSTACKLIM`. + +In both arrangements everything else is a system call: + +| interface | how | +|---|---| +| `openkal.abort` | `exit`; SIGABRT at its default disposition | +| `openkal.stream` | `read`, `write`; a terminal is what answers `TIOCGETA` | +| `openkal.memory` | size classes carved from `mmap`ed chunks; `hw.physmem` and the free and inactive page counts from `sysctl` | +| `openkal.env` | the recorded vectors; `kern.proc.pathname` for the image's name | +| `openkal.time` | `clock_gettime`, `clock_getres`, `nanosleep` | +| `openkal.random` | `getrandom` | +| `openkal.fs` | the `*at` calls relative to directory descriptors; `getdirentries`; `flock` for a lock on a whole file; two preopens, the working directory (`__getcwd`) and the root | +| `openkal.process` | `pdfork` (`PD_DAEMON`; the descriptor is held while the handle is) and `fexecve`; `wait6`; process groups as units; SIGINT, SIGTERM and SIGHUP taken over with `sigaction` | +| `openkal.task` | a word waited upon and woken with `_umtx_op` (`UMTX_OP_WAIT_UINT_PRIVATE`, `UMTX_OP_WAKE_PRIVATE`) | +| `openkal.exec` | `mmap`, `mprotect`, never writable and executable at once; a region at an address is `MAP_FIXED \| MAP_EXCL` | +| `openkal.terminal` | `TIOCGETA`, `TIOCSETAF`, `TIOCGWINSZ` | +| `openkal.timeout` | `ppoll`; a program awaited with a bound | +| `openkal.map` | `mmap` of a descriptor | +| `openkal.region` | reserved `PROT_NONE` mappings backed by `mprotect` | + +Absent in 0.1, neither stated nor defined: `openkal.space`, `openkal.net`, +`openkal.datagram`, `openkal.prof` and `openkal.debug`. Within the stated +interfaces, the property words do not claim the following, and the operations +answer `kal_err_not_supported` as clause 6.2 requires: + +- confinement, a bound lifetime and a one-string command line for a started + program; +- the observable units of 0.24, and a unit's usage and limits; +- a rename that does not replace (FreeBSD has none); +- the timeout forms of `accept` and `recv_from`, whose resources belong to the + absent network interfaces. + +The stop word is counted and never zero again (openkal 0.25). The handler wakes +the word's waiters itself, because a wake is a system call, which a handler may +make. + +## How it is examined + +**Hosted.** openkal's conformance suite is built on Linux for `x86_64-freebsd` +and `aarch64-freebsd` above FreeBSD's C library, using the base-system sysroots +`xim:-freebsd-sysroot@14.5` named by `ci/freebsd-platform`. It is then run +in a FreeBSD virtual machine (vmactions/freebsd-vm; the arm64 machine is +emulated). On FreeBSD 14.5 amd64 it reports 334 observations held, none +failed, and 12 not observed (interfaces not provided, properties not claimed). +The suite's three-way check holds: the manifest's `provides-interfaces`, +`kal_interfaces()` and the exported names state the same fourteen interfaces. + +**Standalone.** Every source compiles for both architectures from openkal's +headers alone, `-ffreestanding` and without a system header, and the objects +carry the brand (`tools/check-standalone.sh`). The suite in this arrangement +runs above openkal-musl on the `*-freebsd-musl` rows. + +- `tools/defer-runner` puts the suite and the variables it is run with in a + directory, where CI runs it inside a FreeBSD VM. +- `tools/vm-run.py` runs a program in a local FreeBSD VM image for development + (`OKF_VM_IMAGE`, `tools/vm-runner`). It forwards `OPENKAL_*`, `KAL_*` and the + mtest protocol's variables, so the suite can report through mtest once the + runner's listing pass honours `--run-under` (mtest 0.6.0). + +The targets need mcpp 2026.11.10.1 or later. + +## License + +Apache-2.0. diff --git a/ci/freebsd-platform/mcpp.toml b/ci/freebsd-platform/mcpp.toml new file mode 100644 index 0000000..e1bf018 --- /dev/null +++ b/ci/freebsd-platform/mcpp.toml @@ -0,0 +1,29 @@ +# The FreeBSD target as this repository's CI builds it: the system's own C +# library and runtimes from the FreeBSD base sysroots (xim:-freebsd-sysroot, +# FreeBSD 14.5's base.txz), compiled by LLVM. openkal-freebsd is the kernel-abi +# layer above them; the platform names none. +[package] +name = "openkal-freebsd-ci-platform" +version = "0.1.0" + +[platform] +toolchain = "llvm@23.1.3" + +[[platform.provider]] +serves = ["x86_64-freebsd"] +sysroot = "xim:x86_64-freebsd-sysroot@14.5" +hosted = true + +[[platform.provider]] +serves = ["aarch64-freebsd"] +sysroot = "xim:aarch64-freebsd-sysroot@14.5" +hosted = true + +[platform.link] +driver = "compiler" + +[platform.layers] +"kernel-abi" = false +"c-abi" = false +"c++-abi" = false +"compiler-runtime" = false diff --git a/mcpp.toml b/mcpp.toml new file mode 100644 index 0000000..f225bc1 --- /dev/null +++ b/mcpp.toml @@ -0,0 +1,72 @@ +[package] +namespace = "mcpplibs" +name = "openkal-freebsd" +version = "0.1.0" +description = "An implementation of openkal for FreeBSD, written on the kernel's own system-call interface." +license = "Apache-2.0" +authors = ["mcpplibs"] +repo = "https://github.com/mcpplibs/openkal-freebsd" + +provides = ["mcpp:kernel-abi=openkal"] + +# WHICH INTERFACES THIS IMPLEMENTATION PROVIDES, AND NO OTHER. An interface it +# does not provide is absent: it is not in this list, not in kal_interfaces(), +# and none of its names is defined (SPEC.md clause 6.1). The conformance +# suite's three-way check compares this list with kal_interfaces() and with the +# names the artefact exports. +# +# Absent in 0.1: openkal.space, openkal.net, openkal.datagram, openkal.prof and +# openkal.debug. +[kernel-abi] +provides-interfaces = [ + "openkal.abort", + "openkal.stream", + "openkal.memory", + "openkal.env", + "openkal.time", + "openkal.random", + "openkal.fs", + "openkal.process", + "openkal.task", + "openkal.exec", + "openkal.terminal", + "openkal.timeout", + "openkal.map", + "openkal.region", +] + +[dependencies] +openkal = "0.25.0" + +[build] +sources = ["src/*.cpp"] +# No exception propagates out of a C entry point, and nothing here is +# identified at run time. The flags are attached to this package's sources; +# the conformance tests are consumers. +flags = [ + { glob = "src/**", cxxflags = ["-fno-exceptions", "-fno-rtti"] }, +] + +# THE THREADS OF THE PROGRAM'S RUNTIME, which a context is in the hosted +# arrangement (src/task.cpp): FreeBSD's own C library's rows. A row whose C +# library is written above openkal (`*-freebsd-musl') has no libthr, and this +# implementation is then standalone. +[target.'cfg(all(os = "freebsd", not(env = "musl")))'.build] +ldflags = ["-lthr"] + +[features] +default = [] + +# Whether this implementation is the whole of the program's environment. +# +# Ordinarily the program carries a runtime that has already received control +# and creates its contexts --- FreeBSD's C library and libthr --- and this +# implementation borrows both. That is the default. +# +# Where nothing in the program has received control --- the program supplies +# its own runtime, or it is a C library written above openkal (openkal-musl) --- +# this implementation does both: it begins the program (`_start', the image's +# .note.tag brand, the thread pointer through sysarch) and creates contexts with +# thr_new. That is what `standalone' selects, a statement about the program +# that the consumer which knows it makes. +standalone = { defines = ["OKF_STANDALONE"] } diff --git a/src/common.h b/src/common.h new file mode 100644 index 0000000..83f0038 --- /dev/null +++ b/src/common.h @@ -0,0 +1,108 @@ +// What the files of this implementation share: handles, the kernel's error +// numbers in openkal's words, names as the kernel takes them, and the program's +// arguments and environment. +#pragma once + +#include "sys.h" +#include + +namespace okf { + +// A HANDLE IS A DESCRIPTOR PLUS ONE, so that zero is never a valid handle. +inline kal_uintptr pack(int fd) { return fd < 0 ? 0 : static_cast(fd) + 1u; } +inline int unpack(kal_uintptr h) { return h == 0 ? -1 : static_cast(h - 1u); } + +// The kernel's error number (positive), in openkal's words. +inline int translate(word e) { + switch (e) { + case 0: return kal_ok; + case EAGAIN: return kal_err_again; + case ENOMEM: return kal_err_no_memory; + case ENOSPC: + case EDQUOT: return kal_err_no_space; + case EACCES: + case EPERM: + case EROFS: return kal_err_permission; + case ENOSYS: + case EOPNOTSUPP: return kal_err_not_supported; + case EPIPE: + case ECONNRESET: return kal_err_closed; + case ENOENT: return kal_err_not_found; + case EEXIST: return kal_err_exists; + case ENOTEMPTY: return kal_err_not_empty; + case EISDIR: return kal_err_is_directory; + case ENOTDIR: return kal_err_not_directory; + case ENOEXEC: return kal_err_not_program; + case EBADF: + case EINVAL: + case ENAMETOOLONG: + case ELOOP: + case EFAULT: return kal_err_invalid; + default: return kal_err_io; + } +} +// A system call's answer, when it failed, in openkal's words. +inline int fail(word r) { return translate(-r); } +inline kal_intptr nfail(word r) { return -static_cast(translate(-r)); } + +inline unsigned long length(const char* s) { unsigned long n = 0; while (s && s[n]) ++n; return n; } +inline void copy(void* to, const void* from, unsigned long n) { + auto* d = static_cast(to); auto* s = static_cast(from); + for (unsigned long i = 0; i < n; ++i) d[i] = s[i]; +} +inline void zero(void* to, unsigned long n) { auto* d = static_cast(to); for (unsigned long i = 0; i < n; ++i) d[i] = 0; } +inline bool same(const char* a, const char* b, unsigned long n) { for (unsigned long i = 0; i < n; ++i) if (a[i] != b[i]) return false; return true; } + +// What an operation that answers a length writes: as much as fits, and the +// whole length. +inline kal_intptr put(const char* v, kal_uintptr n, char* out, kal_uintptr cap) { + if (out && cap) copy(out, v, n < cap ? n : cap); + return static_cast(n); +} + +constexpr unsigned long kPath = 1024; // the kernel's MAXPATHLEN + +// A name of openkal's (bytes and a length) as the kernel takes it: terminated, +// relative, with no component that ascends and no zero byte within. +struct cname { + char buf[kPath]; + bool ok = false; + cname(const char* s, kal_uintptr n) { + if (s == nullptr || n == 0 || n >= sizeof buf) return; + if (s[0] == '/') return; + for (kal_uintptr i = 0; i < n; ++i) { if (s[i] == 0) return; buf[i] = s[i]; } + buf[n] = 0; + for (kal_uintptr i = 0; i < n; ) { + kal_uintptr j = i; + while (j < n && buf[j] != '/') ++j; + if (j - i == 2 && buf[i] == '.' && buf[i + 1] == '.') return; + i = j + 1; + } + ok = true; + } +}; + +// The program's arguments and environment, as its initialisers were given them. +extern int g_argc; +extern char** g_argv; +extern char** g_envp; +extern unsigned long* g_auxv; // standalone only +unsigned long auxval(unsigned long key); + +// A value of the environment, or null. +const char* variable(const char* name, unsigned long n); + +// A lock of one word, for the few tables this implementation keeps; the wait is +// the kernel's (sched_yield), never the C library's. +struct spin { + volatile int held = 0; + void lock() { while (__atomic_exchange_n(&held, 1, __ATOMIC_ACQUIRE)) okf::sched_yield(); } + void unlock() { __atomic_store_n(&held, 0, __ATOMIC_RELEASE); } +}; + +// openkal.process's own, for openkal.timeout: the identifier of a program this +// one started, and its status when it has ended (`done'). +int reap_program(int pid, int opts, int* status, int* terminated, bool* done); +int started_pid(kal_process p, int* pid); + +} // namespace okf diff --git a/src/core.cpp b/src/core.cpp new file mode 100644 index 0000000..c1d9737 --- /dev/null +++ b/src/core.cpp @@ -0,0 +1,188 @@ +// The interfaces every program reaches first: the version and the interfaces +// stated, ending, streams, the environment, time and entropy. +#include "common.h" + +namespace okf { + +int g_argc = 0; +char** g_argv = nullptr; +char** g_envp = nullptr; + +const char* variable(const char* name, unsigned long n) { + for (char** e = g_envp; e && *e; ++e) + if (same(*e, name, n) && (*e)[n] == '=') return *e + n + 1; + return nullptr; +} + +} // namespace okf + +namespace { + +#ifndef OKF_STANDALONE +// THE PROGRAM'S ARGUMENTS, AS ITS INITIALISERS ARE GIVEN THEM (hosted). FreeBSD's +// run-time linker, and the start of a static program, call each function of +// .init_array with (argc, argv, envp): the program's own runtime received +// control. Standalone, this implementation's start records them (start.cpp), +// and the C library's start calls the initialisers with no arguments (musl's +// `libc_start_init'): a function reading three here read what the registers +// held, and the arguments it recorded over the start's were not pointers +// (the conformance suite stopped with SIGBUS in its first `kal_env_arg'). +[[gnu::constructor(101)]] void record(int argc, char** argv, char** envp) { + okf::g_argc = argc; + okf::g_argv = argv; + okf::g_envp = envp; +} +#endif + +kal_u64 ns_of(int clock) { + okf::ktimespec t{}; + okf::clock_gettime(clock, &t); + return static_cast(t.tv_sec) * 1000000000ull + static_cast(t.tv_nsec); +} + +} // namespace + +extern "C" { + +// ================================================================ version +kal_u64 kal_version(void) { return KAL_VERSION; } +kal_u64 kal_interfaces(void) { + return KAL_IFACE_ABORT | KAL_IFACE_STREAM | KAL_IFACE_MEMORY | KAL_IFACE_ENV + | KAL_IFACE_TIME | KAL_IFACE_RANDOM | KAL_IFACE_FS | KAL_IFACE_PROCESS + | KAL_IFACE_TASK | KAL_IFACE_EXEC | KAL_IFACE_TERMINAL | KAL_IFACE_TIMEOUT + | KAL_IFACE_MAP | KAL_IFACE_REGION; +} + +// ================================================================ abort +[[noreturn]] void kal_exit(int code) { okf::exit(code); } + +[[noreturn]] void kal_abort(const char* msg, kal_uintptr len) { + if (msg && len) { okf::write(2, msg, len); okf::write(2, "\n", 1); } + // SIGABRT, at its default disposition and not blocked, sent to this program; + // a program that survived it ends with the status of an abort. + okf::ksigaction dfl{}; + dfl.sa_handler = SIG_DFL; + okf::sigaction(SIGABRT, &dfl, nullptr); + okf::ksigset only{}; only.__bits[(SIGABRT - 1) / 32] = 1u << ((SIGABRT - 1) % 32); + okf::sigprocmask(SIG_UNBLOCK, &only, nullptr); + okf::kill(static_cast(okf::getpid()), SIGABRT); + okf::exit(134); +} + +// ================================================================ stream +kal_stream kal_stdin (void) { return kal_stream{ okf::pack(0) }; } +kal_stream kal_stdout(void) { return kal_stream{ okf::pack(1) }; } +kal_stream kal_stderr(void) { return kal_stream{ okf::pack(2) }; } + +kal_intptr kal_stream_write(kal_stream s, const void* buf, kal_uintptr len) { + const int fd = okf::unpack(s.h); + if (fd < 0) return -kal_err_invalid; + const char* p = static_cast(buf); + kal_uintptr done = 0; + while (done < len) { + const okf::word r = okf::write(fd, p + done, len - done); + if (r == -EINTR) continue; + if (okf::failed(r)) return done ? static_cast(done) : okf::nfail(r); + done += static_cast(r); + } + return static_cast(done); +} +kal_intptr kal_stream_read(kal_stream s, void* buf, kal_uintptr len) { + const int fd = okf::unpack(s.h); + if (fd < 0) return -kal_err_invalid; + for (;;) { + const okf::word r = okf::read(fd, buf, len); + if (r == -EINTR) continue; + return okf::failed(r) ? okf::nfail(r) : static_cast(r); + } +} +int kal_stream_flush(kal_stream s) { return okf::unpack(s.h) < 0 ? kal_err_invalid : kal_ok; } +kal_uintptr kal_stream_props(kal_stream s) { + const int fd = okf::unpack(s.h); + okf::ktermios t{}; + return (fd >= 0 && !okf::failed(okf::ioctl(fd, TIOCGETA, &t))) ? KAL_STREAM_PROP_INTERACTIVE : 0; +} + +// ================================================================ env +kal_uintptr kal_env_arg_count(void) { return static_cast(okf::g_argc); } +kal_intptr kal_env_arg(kal_uintptr i, char* out, kal_uintptr cap) { + if (i >= static_cast(okf::g_argc)) return -kal_err_invalid; + const char* a = okf::g_argv[i] ? okf::g_argv[i] : ""; + return okf::put(a, okf::length(a), out, cap); +} +kal_intptr kal_env_var(const char* name, kal_uintptr n, char* out, kal_uintptr cap) { + if (!name) return -kal_err_invalid; + const char* v = okf::variable(name, n); + return v ? okf::put(v, okf::length(v), out, cap) : -kal_err_not_found; +} +kal_uintptr kal_env_var_count(void) { + kal_uintptr n = 0; + for (char** e = okf::g_envp; e && *e; ++e) ++n; + return n; +} +kal_intptr kal_env_var_at(kal_uintptr i, char* out, kal_uintptr cap) { + char** e = okf::g_envp; + for (kal_uintptr k = 0; e && e[k]; ++k) { + if (k != i) continue; + unsigned long n = 0; + while (e[k][n] && e[k][n] != '=') ++n; + return okf::put(e[k], n, out, cap); + } + return -kal_err_invalid; +} +// The image's name as the kernel recorded it (kern.proc.pathname). +kal_intptr kal_env_image(char* out, kal_uintptr cap) { + const int name[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 }; + char buf[okf::kPath]; + unsigned long n = sizeof buf; + if (okf::failed(okf::sysctl(name, 4, buf, &n)) || n == 0) return -kal_err_not_found; + if (buf[n - 1] == 0) --n; + return okf::put(buf, n, out, cap); +} +kal_intptr kal_env_loader(char*, kal_uintptr) { return -kal_err_not_found; } +kal_intptr kal_env_system(char* out, kal_uintptr cap) { return okf::put("freebsd", 7, out, cap); } +// THE RESOURCE PREFERENCE (0.23), which a start conveys in KAL_RESOURCES, as +// the other implementations convey it. +int kal_env_resources(void) { + static int known = -1; + int v = __atomic_load_n(&known, __ATOMIC_RELAXED); + if (v >= 0) return v; + v = KAL_RESOURCES_BALANCED; + if (const char* r = okf::variable("KAL_RESOURCES", 13)) { + const unsigned long n = okf::length(r); + if (n == 11 && okf::same(r, "performance", 11)) v = KAL_RESOURCES_PERFORMANCE; + else if (n == 7 && okf::same(r, "economy", 7)) v = KAL_RESOURCES_ECONOMY; + } + __atomic_store_n(&known, v, __ATOMIC_RELAXED); + return v; +} + +// ================================================================ time +kal_duration kal_time_monotonic(void) { return ns_of(CLOCK_MONOTONIC); } +kal_duration kal_time_wall(void) { return ns_of(CLOCK_REALTIME); } +kal_duration kal_time_monotonic_granularity(void) { + okf::ktimespec t{}; + if (okf::failed(okf::clock_getres(CLOCK_MONOTONIC, &t))) return 1; + const kal_u64 g = static_cast(t.tv_sec) * 1000000000ull + static_cast(t.tv_nsec); + return g ? g : 1; +} +void kal_time_sleep(kal_duration ns) { + okf::ktimespec t{ static_cast(ns / 1000000000ull), static_cast(ns % 1000000000ull) }; + while (okf::nanosleep(&t, &t) == -EINTR) {} +} +kal_uintptr kal_time_props(void) { return KAL_TIME_PROP_WALL_AVAILABLE | KAL_TIME_PROP_SLEEP_PRECISE; } + +// ================================================================ random +int kal_random_fill(void* out, kal_uintptr len) { + auto* p = static_cast(out); + while (len) { + const okf::word r = okf::getrandom(p, len > 256 ? 256 : len, 0); + if (r == -EINTR) continue; + if (okf::failed(r) || r == 0) return kal_err_io; + p += r; len -= static_cast(r); + } + return kal_ok; +} +kal_uintptr kal_random_props(void) { return 0; } + +} // extern "C" diff --git a/src/fs.cpp b/src/fs.cpp new file mode 100644 index 0000000..b81fb70 --- /dev/null +++ b/src/fs.cpp @@ -0,0 +1,376 @@ +// openkal.fs: names relative to directory descriptors (the *at system calls), +// two preopens --- the working directory and the root --- and listings read +// with getdirentries. +#include "common.h" + +namespace { + +using okf::word; + +struct preopen { char name[okf::kPath]; unsigned long len; int fd; }; +preopen g_pre[2]; +kal_uintptr g_pre_n = 0; +int g_pre_state = 0; // 0: not yet, 1: being opened, 2: open +const int kDirFlags = O_RDONLY | O_DIRECTORY | O_CLOEXEC; + +void open_preopens() { + if (__atomic_load_n(&g_pre_state, __ATOMIC_ACQUIRE) == 2) return; + int expected = 0; + if (!__atomic_compare_exchange_n(&g_pre_state, &expected, 1, false, __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE)) { + while (__atomic_load_n(&g_pre_state, __ATOMIC_ACQUIRE) != 2) okf::sched_yield(); + return; + } + // The working directory, by its name, and the root, as the other + // implementations grant them. + if (okf::failed(okf::getcwd(g_pre[0].name, sizeof g_pre[0].name))) { g_pre[0].name[0] = '.'; g_pre[0].name[1] = 0; } + g_pre[0].len = okf::length(g_pre[0].name); + const word here = okf::openat(AT_FDCWD, ".", kDirFlags); + g_pre[0].fd = okf::failed(here) ? -1 : static_cast(here); + g_pre[1].name[0] = '/'; g_pre[1].name[1] = 0; g_pre[1].len = 1; + const word root = okf::openat(AT_FDCWD, "/", kDirFlags); + g_pre[1].fd = okf::failed(root) ? -1 : static_cast(root); + g_pre_n = 2; + __atomic_store_n(&g_pre_state, 2, __ATOMIC_RELEASE); +} + +int kind_of(unsigned mode) { + if (S_ISREG(mode)) return kal_node_file; + if (S_ISDIR(mode)) return kal_node_directory; + if (S_ISLNK(mode)) return kal_node_link; + return kal_node_other; +} + +// The fields an enquiry fills: no more of the structure than the caller stated +// (`self_size'), and, where `mask' is not all of them, only those asked for. +void fill_info(const okf::kstat& st, kal_node_info* out, kal_u32 mask = ~kal_u32{0}) { + const kal_u32 cap = out->self_size; + kal_node_info i{}; + i.self_size = cap; + i.present = KAL_INFO_KIND | KAL_INFO_SIZE | KAL_INFO_MODIFIED | KAL_INFO_WRITABLE | KAL_INFO_IDENTITY; + i.kind = kind_of(st.st_mode); + i.size = static_cast(st.st_size); + i.modified_ns = static_cast(st.st_mtim.tv_sec) * 1000000000ull + static_cast(st.st_mtim.tv_nsec); + i.identity[0] = static_cast(st.st_dev); + i.identity[1] = static_cast(st.st_ino); + i.writable = (st.st_mode & 0222) != 0; + if (i.kind == kal_node_file) { i.present |= KAL_INFO_EXECUTABLE; i.executable = (st.st_mode & 0111) != 0; } + i.present &= mask | KAL_INFO_KIND; + struct { kal_u32 bit; unsigned long end; } const fields[] = { + { KAL_INFO_SIZE, __builtin_offsetof(kal_node_info, size) + 8 }, + { KAL_INFO_MODIFIED, __builtin_offsetof(kal_node_info, modified_ns) + 8 }, + { KAL_INFO_IDENTITY, __builtin_offsetof(kal_node_info, identity) + 16 }, + { KAL_INFO_KIND, __builtin_offsetof(kal_node_info, kind) + sizeof(int) }, + { KAL_INFO_WRITABLE, __builtin_offsetof(kal_node_info, writable) + sizeof(int) }, + { KAL_INFO_EXECUTABLE, __builtin_offsetof(kal_node_info, executable) + sizeof(int) }, + }; + for (const auto& f : fields) if (cap < f.end) i.present &= ~f.bit; + if (!(i.present & KAL_INFO_SIZE)) i.size = 0; + if (!(i.present & KAL_INFO_MODIFIED)) i.modified_ns = 0; + if (!(i.present & KAL_INFO_IDENTITY)) i.identity[0] = i.identity[1] = 0; + if (!(i.present & KAL_INFO_WRITABLE)) i.writable = 0; + if (!(i.present & KAL_INFO_EXECUTABLE)) i.executable = 0; + okf::copy(out, &i, cap < sizeof i ? cap : sizeof i); +} + +// A LISTING IN PROGRESS: its own descriptor of the directory, and the entries +// getdirentries last read. +struct lister { + int fd; + long pos, end; + long long base; + alignas(8) char buf[8192]; +}; + +int set_time(int dir, const char* name, int fd, kal_u64 ns) { + okf::ktimespec t[2]; + t[0].tv_sec = 0; t[0].tv_nsec = UTIME_OMIT; + t[1].tv_sec = static_cast(ns / 1000000000ull); t[1].tv_nsec = static_cast(ns % 1000000000ull); + const word r = name ? okf::utimensat(dir, name, t, 0) : okf::futimens(fd, t); + return okf::failed(r) ? okf::fail(r) : kal_ok; +} + +// The next entry other than `.' and `..', or none (*iter becomes zero). +int next_entry(int dfd, kal_uintptr* iter, char* name, kal_uintptr cap, kal_uintptr* nlen, int* kind, okf::kstat* st) { + if (!iter) return kal_err_invalid; + auto* l = reinterpret_cast(*iter); + if (!l) { if (nlen) *nlen = 0; return kal_ok; } + for (;;) { + if (l->pos >= l->end) { + const word r = okf::getdirentries(l->fd, l->buf, sizeof l->buf, &l->base); + if (r == -EINTR) continue; + if (okf::failed(r) || r == 0) { + okf::close(l->fd); + kal_free(l, sizeof *l, alignof(lister)); + *iter = 0; + if (nlen) *nlen = 0; + return okf::failed(r) ? okf::fail(r) : kal_ok; + } + l->pos = 0; l->end = r; + } + auto* e = reinterpret_cast(l->buf + l->pos); + if (e->d_reclen == 0) { l->pos = l->end; continue; } + l->pos += e->d_reclen; + if (e->d_fileno == 0) continue; + const unsigned long n = e->d_namlen; + if ((n == 1 && e->d_name[0] == '.') || (n == 2 && e->d_name[0] == '.' && e->d_name[1] == '.')) continue; + okf::put(e->d_name, n, name, cap); + if (nlen) *nlen = n; + okf::kstat s{}; + const bool have = !okf::failed(okf::fstatat(dfd, e->d_name, &s, AT_SYMLINK_NOFOLLOW)); + if (kind) *kind = have ? kind_of(s.st_mode) : kal_node_other; + if (st) { if (have) *st = s; else okf::zero(st, sizeof *st); } + return kal_ok; + } +} + +} // namespace + +extern "C" { + +kal_uintptr kal_fs_preopen_count(void) { open_preopens(); return g_pre_n; } +int kal_fs_preopen(kal_uintptr i, kal_dir* out, char* name, kal_uintptr cap, kal_uintptr* len) { + open_preopens(); + if (i >= g_pre_n || !out) return kal_err_invalid; + if (g_pre[i].fd < 0) return kal_err_permission; + *out = kal_dir{ okf::pack(g_pre[i].fd) }; + okf::put(g_pre[i].name, g_pre[i].len, name, cap); + if (len) *len = g_pre[i].len; + return kal_ok; +} +kal_uintptr kal_fs_props(kal_dir) { + return KAL_FS_PROP_CASE_SENSITIVE | KAL_FS_PROP_LINKS | KAL_FS_PROP_MODIFIED_TIME + | KAL_FS_PROP_ATOMIC_RENAME | KAL_FS_PROP_MAKE_LINKS | KAL_FS_PROP_LOCKS + | KAL_FS_PROP_CAPACITY | KAL_FS_PROP_EXECUTABLE | KAL_FS_PROP_REMOVE_OPEN + | KAL_FS_PROP_HARD_LINKS; +} +kal_uintptr kal_fs_max_name(void) { return okf::kPath - 1; } + +int kal_fs_open_dir(kal_dir base, const char* name, kal_uintptr len, kal_dir* out) { + const int b = okf::unpack(base.h); okf::cname n(name, len); + if (b < 0 || !out || !n.ok) return kal_err_invalid; + const word fd = okf::openat(b, n.buf, kDirFlags); + if (okf::failed(fd)) return okf::fail(fd); + *out = kal_dir{ okf::pack(static_cast(fd)) }; + return kal_ok; +} +int kal_fs_open(kal_dir base, const char* name, kal_uintptr len, kal_uintptr f, kal_file* out) { + const int b = okf::unpack(base.h); okf::cname n(name, len); + if (b < 0 || !out || !n.ok) return kal_err_invalid; + int o = O_CLOEXEC; + const bool r = f & KAL_OPEN_READ, w = f & (KAL_OPEN_WRITE | KAL_OPEN_APPEND); + o |= (r && w) ? O_RDWR : w ? O_WRONLY : O_RDONLY; + if (f & KAL_OPEN_CREATE) o |= O_CREAT; + if (f & KAL_OPEN_EXCLUSIVE) o |= O_EXCL; + if (f & KAL_OPEN_TRUNCATE) o |= O_TRUNC; + if (f & KAL_OPEN_APPEND) o |= O_APPEND; + const word fd = okf::openat(b, n.buf, o, 0666); + if (okf::failed(fd)) return okf::fail(fd); + okf::kstat st{}; + if (!okf::failed(okf::fstat(static_cast(fd), &st)) && S_ISDIR(st.st_mode)) { + okf::close(static_cast(fd)); + return kal_err_is_directory; + } + *out = kal_file{ okf::pack(static_cast(fd)) }; + return kal_ok; +} +void kal_fs_close_dir (kal_dir d) { const int fd = okf::unpack(d.h); if (fd >= 0) okf::close(fd); } +void kal_fs_close_file(kal_file f) { const int fd = okf::unpack(f.h); if (fd >= 0) okf::close(fd); } +kal_stream kal_fs_stream(kal_file f) { return kal_stream{ f.h }; } +int kal_fs_seek(kal_file f, kal_i64 off, int whence, kal_u64* result) { + const int w = whence == KAL_SEEK_SET ? SEEK_SET : whence == KAL_SEEK_CURRENT ? SEEK_CUR : SEEK_END; + const word r = okf::lseek(okf::unpack(f.h), off, w); + if (okf::failed(r)) return okf::fail(r); + if (result) *result = static_cast(r); + return kal_ok; +} +int kal_fs_truncate(kal_file f, kal_u64 size) { + const word r = okf::ftruncate(okf::unpack(f.h), static_cast(size)); + return okf::failed(r) ? okf::fail(r) : kal_ok; +} +kal_intptr kal_fs_read_at(kal_file f, void* buf, kal_uintptr len, kal_u64 off) { + for (;;) { + const word r = okf::pread(okf::unpack(f.h), buf, len, static_cast(off)); + if (r == -EINTR) continue; + return okf::failed(r) ? okf::nfail(r) : r; + } +} +kal_intptr kal_fs_write_at(kal_file f, const void* buf, kal_uintptr len, kal_u64 off) { + kal_uintptr done = 0; const char* p = static_cast(buf); + while (done < len) { + const word r = okf::pwrite(okf::unpack(f.h), p + done, len - done, static_cast(off + done)); + if (r == -EINTR) continue; + if (okf::failed(r)) return done ? static_cast(done) : okf::nfail(r); + done += static_cast(r); + } + return static_cast(done); +} +int kal_fs_info(kal_dir base, const char* name, kal_uintptr len, kal_uintptr flags, kal_u32, kal_node_info* out) { + const int b = okf::unpack(base.h); okf::cname n(name, len); + if (b < 0 || !out || !n.ok) return kal_err_invalid; + okf::kstat st{}; + const word r = okf::fstatat(b, n.buf, &st, (flags & KAL_FS_NO_RESOLVE) ? AT_SYMLINK_NOFOLLOW : 0); + if (okf::failed(r)) { + if (r == -ENOENT) { + const kal_u32 cap = out->self_size; + kal_node_info i{}; i.self_size = cap; + i.present = KAL_INFO_KIND; i.kind = kal_node_absent; + okf::copy(out, &i, cap < sizeof i ? cap : sizeof i); + return kal_ok; + } + return okf::fail(r); + } + fill_info(st, out); + return kal_ok; +} +int kal_fs_file_info(kal_file f, kal_u32, kal_node_info* out) { + if (!out) return kal_err_invalid; + okf::kstat st{}; + const word r = okf::fstat(okf::unpack(f.h), &st); + if (okf::failed(r)) return okf::fail(r); + fill_info(st, out); + return kal_ok; +} +int kal_fs_mkdir(kal_dir base, const char* name, kal_uintptr len) { + okf::cname n(name, len); if (!n.ok) return kal_err_invalid; + const word r = okf::mkdirat(okf::unpack(base.h), n.buf, 0777); + return okf::failed(r) ? okf::fail(r) : kal_ok; +} +int kal_fs_remove(kal_dir base, const char* name, kal_uintptr len) { + okf::cname n(name, len); if (!n.ok) return kal_err_invalid; + const int b = okf::unpack(base.h); + word r = okf::unlinkat(b, n.buf, 0); + if (!okf::failed(r)) return kal_ok; + if (r == -EISDIR || r == -EPERM) { + const word d = okf::unlinkat(b, n.buf, AT_REMOVEDIR); + if (!okf::failed(d)) return kal_ok; + if (d != -ENOTDIR) r = d; + } + return okf::fail(r); +} +int kal_fs_rename(kal_dir from, const char* a, kal_uintptr al, kal_dir to, const char* b, kal_uintptr bl) { + okf::cname x(a, al), y(b, bl); if (!x.ok || !y.ok) return kal_err_invalid; + const word r = okf::renameat(okf::unpack(from.h), x.buf, okf::unpack(to.h), y.buf); + return okf::failed(r) ? okf::fail(r) : kal_ok; +} +// FreeBSD has no rename that refuses to replace, so the property is not +// claimed and the flag is refused as unsupported. +int kal_fs_rename_flags(kal_dir from, const char* a, kal_uintptr al, kal_dir to, const char* b, kal_uintptr bl, kal_uintptr flags) { + if (flags == 0) return kal_fs_rename(from, a, al, to, b, bl); + return flags == KAL_RENAME_NO_REPLACE ? kal_err_not_supported : kal_err_invalid; +} +int kal_fs_set_modified(kal_file f, kal_u64 ns) { return set_time(-1, nullptr, okf::unpack(f.h), ns); } +int kal_fs_set_modified_at(kal_dir base, const char* name, kal_uintptr len, kal_u64 ns) { + okf::cname n(name, len); if (!n.ok) return kal_err_invalid; + return set_time(okf::unpack(base.h), n.buf, -1, ns); +} +int kal_fs_set_executable_at(kal_dir base, const char* name, kal_uintptr len, int executable) { + okf::cname n(name, len); if (!n.ok) return kal_err_invalid; + okf::kstat st{}; const int b = okf::unpack(base.h); + const word r = okf::fstatat(b, n.buf, &st, 0); + if (okf::failed(r)) return okf::fail(r); + if (S_ISDIR(st.st_mode)) return kal_err_is_directory; + unsigned m = st.st_mode & 07777; + if (executable) m |= (m & 0444) >> 2; else m &= ~0111u; + const word c = okf::fchmodat(b, n.buf, static_cast(m), 0); + return okf::failed(c) ? okf::fail(c) : kal_ok; +} +// A LOCK BELONGS TO THE OPEN FILE, as openkal states it. FreeBSD's flock does +// (whole files), and its record locks belong to the process; a lock upon part +// of a file is the record lock, upon the whole of it flock. +int kal_fs_lock(kal_file f, kal_u64 start, kal_u64 len, kal_uintptr mode) { + const int fd = okf::unpack(f.h); + if (fd < 0) return kal_err_invalid; + const kal_uintptr kinds = mode & (KAL_LOCK_SHARED | KAL_LOCK_EXCLUSIVE); + if (kinds != KAL_LOCK_SHARED && kinds != KAL_LOCK_EXCLUSIVE) return kal_err_invalid; + if (mode & ~(KAL_LOCK_SHARED | KAL_LOCK_EXCLUSIVE | KAL_LOCK_WAIT)) return kal_err_invalid; + const bool exclusive = kinds == KAL_LOCK_EXCLUSIVE, wait = mode & KAL_LOCK_WAIT; + if (start == 0 && len == 0) { + for (;;) { + const word r = okf::flock(fd, (exclusive ? LOCK_EX : LOCK_SH) | (wait ? 0 : LOCK_NB)); + if (r == -EINTR) continue; + if (okf::failed(r)) return r == -EWOULDBLOCK ? kal_err_again : okf::fail(r); + return kal_ok; + } + } + okf::kflock l{}; l.l_type = exclusive ? F_WRLCK : F_RDLCK; + l.l_whence = SEEK_SET; l.l_start = static_cast(start); l.l_len = static_cast(len); + for (;;) { + const word r = okf::fcntl(fd, wait ? F_SETLKW : F_SETLK, okf::w(&l)); + if (r == -EINTR) continue; + if (okf::failed(r)) return (r == -EACCES || r == -EAGAIN) ? kal_err_again : okf::fail(r); + return kal_ok; + } +} +int kal_fs_unlock(kal_file f, kal_u64 start, kal_u64 len) { + const int fd = okf::unpack(f.h); + if (fd < 0) return kal_err_invalid; + if (start == 0 && len == 0) { + const word r = okf::flock(fd, LOCK_UN); + return okf::failed(r) ? okf::fail(r) : kal_ok; + } + okf::kflock l{}; l.l_type = F_UNLCK; l.l_whence = SEEK_SET; + l.l_start = static_cast(start); l.l_len = static_cast(len); + const word r = okf::fcntl(fd, F_SETLK, okf::w(&l)); + return okf::failed(r) ? okf::fail(r) : kal_ok; +} +int kal_fs_capacity(kal_dir d, kal_u64* total, kal_u64* avail) { + okf::kstatfs v{}; + const word r = okf::fstatfs(okf::unpack(d.h), &v); + if (okf::failed(r)) return okf::fail(r); + if (total) *total = static_cast(v.f_blocks) * static_cast(v.f_bsize); + if (avail) *avail = (v.f_bavail > 0 ? static_cast(v.f_bavail) : 0) * static_cast(v.f_bsize); + return kal_ok; +} +int kal_fs_link_create(kal_dir base, const char* name, kal_uintptr len, const char* target, kal_uintptr tlen, kal_uintptr) { + okf::cname n(name, len); + if (!n.ok || !target || tlen == 0 || tlen >= okf::kPath) return kal_err_invalid; + char t[okf::kPath]; okf::copy(t, target, tlen); t[tlen] = 0; + const word r = okf::symlinkat(t, okf::unpack(base.h), n.buf); + return okf::failed(r) ? okf::fail(r) : kal_ok; +} +kal_intptr kal_fs_link_read(kal_dir base, const char* name, kal_uintptr len, char* out, kal_uintptr cap) { + okf::cname n(name, len); if (!n.ok) return -kal_err_invalid; + char t[okf::kPath]; + const word r = okf::readlinkat(okf::unpack(base.h), n.buf, t, sizeof t); + if (okf::failed(r)) return okf::nfail(r); + return okf::put(t, static_cast(r), out, cap); +} +int kal_fs_hard_link(kal_dir from, const char* a, kal_uintptr al, kal_dir to, const char* b, kal_uintptr bl) { + okf::cname x(a, al), y(b, bl); + const int f = okf::unpack(from.h), t = okf::unpack(to.h); + if (f < 0 || t < 0 || !x.ok || !y.ok) return kal_err_invalid; + // A directory is refused before the call: the kernel's answer (EPERM) + // names a permission. Two volumes are the kernel's EXDEV. + okf::kstat st{}; + if (!okf::failed(okf::fstatat(f, x.buf, &st, AT_SYMLINK_NOFOLLOW)) && S_ISDIR(st.st_mode)) return kal_err_is_directory; + const word r = okf::linkat(f, x.buf, t, y.buf, 0); + if (!okf::failed(r)) return kal_ok; + return r == -EXDEV ? kal_err_not_supported : okf::fail(r); +} +kal_intptr kal_fs_resolved_name(kal_dir, const char*, kal_uintptr, char*, kal_uintptr) { return -kal_err_not_supported; } +int kal_fs_dir_attenuate(kal_dir, const kal_dir_rights*, kal_dir*) { return kal_err_not_supported; } + +int kal_fs_list_begin(kal_dir d, kal_uintptr* iter) { + const int fd = okf::unpack(d.h); + if (fd < 0 || !iter) return kal_err_invalid; + const word own = okf::openat(fd, ".", kDirFlags); + if (okf::failed(own)) return okf::fail(own); + auto* l = static_cast(kal_alloc(sizeof(lister), alignof(lister))); + if (!l) { okf::close(static_cast(own)); return kal_err_no_memory; } + l->fd = static_cast(own); l->pos = 0; l->end = 0; l->base = 0; + *iter = reinterpret_cast(l); + return kal_ok; +} +int kal_fs_list_next(kal_dir d, kal_uintptr* iter, char* name, kal_uintptr cap, kal_uintptr* nlen, int* kind) { + if (okf::unpack(d.h) < 0) return kal_err_invalid; + return next_entry(okf::unpack(d.h), iter, name, cap, nlen, kind, nullptr); +} +int kal_fs_list_next_info(kal_dir d, kal_uintptr* iter, char* name, kal_uintptr cap, kal_uintptr* nlen, kal_u32 wanted, kal_node_info* info) { + if (okf::unpack(d.h) < 0 || !info) return kal_err_invalid; + okf::kstat st{}; int kind = 0; + const int r = next_entry(okf::unpack(d.h), iter, name, cap, nlen, &kind, &st); + if (r == kal_ok && *iter) fill_info(st, info, wanted); + return r; +} + +} // extern "C" diff --git a/src/memory.cpp b/src/memory.cpp new file mode 100644 index 0000000..376b0da --- /dev/null +++ b/src/memory.cpp @@ -0,0 +1,128 @@ +// openkal.memory: blocks of size classes carved from mappings, and mappings of +// their own for the large; the machine's memory from sysctl. +#include "common.h" + +namespace { + +constexpr unsigned long kPage = 4096; +constexpr unsigned long kMinBlock = 16; +constexpr unsigned long kMaxSmall = 32768; +constexpr unsigned long kChunk = 1u << 20; +constexpr int kClasses = 12; // 16, 32, ... 32768 + +okf::spin g_lock; +void* g_free[kClasses]; +unsigned char* g_next[kClasses]; +unsigned char* g_end[kClasses]; + +int class_of(unsigned long n) { unsigned long s = kMinBlock; int c = 0; while (s < n) { s <<= 1; ++c; } return c; } +unsigned long size_of(int c) { return kMinBlock << c; } +unsigned long round_up(unsigned long n, unsigned long to) { return (n + to - 1) & ~(to - 1); } + +void* map(unsigned long bytes) { + const okf::word r = okf::mmap(nullptr, bytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); + return okf::failed(r) ? nullptr : reinterpret_cast(r); +} +void unmap(void* p, unsigned long bytes) { okf::munmap(p, bytes); } + +// A class's blocks are carved in turn from a chunk of their own, so a block of +// a class is aligned to the class's size up to the page (chunks are mapped +// whole pages, and every class divides the chunk). +void* carve(int c) { + const unsigned long bs = size_of(c); + if (g_next[c] == g_end[c]) { + auto* base = static_cast(map(kChunk)); + if (!base) return nullptr; + g_next[c] = base; + g_end[c] = base + kChunk; + } + void* b = g_next[c]; + g_next[c] += bs; + return b; +} + +bool sysctl_u64(const char* name, kal_u64& out) { + unsigned long v = 0, n = sizeof v; + if (okf::failed(okf::sysctlbyname(name, &v, &n))) return false; + if (n == 4) { unsigned u = 0; okf::copy(&u, &v, 4); out = u; } + else out = v; + return true; +} + +} // namespace + +extern "C" { + +void* kal_alloc(kal_uintptr size, kal_uintptr align) { + if (size == 0) return nullptr; + if (align < kMinBlock) align = kMinBlock; + if (align & (align - 1)) return nullptr; + const unsigned long want = size < align ? align : size; + if (want <= kMaxSmall && align <= kPage) { + const int c = class_of(want); + g_lock.lock(); + void* b = g_free[c]; + if (b) g_free[c] = *reinterpret_cast(b); + else b = carve(c); + g_lock.unlock(); + return b; + } + const unsigned long bytes = round_up(size, kPage); + if (align <= kPage) return map(bytes); + // An alignment wider than a page: map more, and record before the region + // what is to be released. + const unsigned long total = bytes + align; + auto* base = static_cast(map(total)); + if (!base) return nullptr; + auto addr = reinterpret_cast(base) + 16; + addr = (addr + align - 1) & ~(align - 1); + auto* user = reinterpret_cast(addr); + user[-1] = total; + user[-2] = reinterpret_cast(base); + return user; +} + +void kal_free(void* p, kal_uintptr size, kal_uintptr align) { + if (!p || size == 0) return; + if (align < kMinBlock) align = kMinBlock; + const unsigned long want = size < align ? align : size; + if (want <= kMaxSmall && align <= kPage) { + const int c = class_of(want); + g_lock.lock(); + *reinterpret_cast(p) = g_free[c]; + g_free[c] = p; + g_lock.unlock(); + return; + } + if (align <= kPage) { unmap(p, round_up(size, kPage)); return; } + auto* user = static_cast(p); + unmap(reinterpret_cast(user[-2]), user[-1]); +} + +kal_uintptr kal_memory_granularity(void) { + kal_u64 page = 0; + return sysctl_u64("hw.pagesize", page) && page ? static_cast(page) : kPage; +} + +// The machine's memory (hw.physmem), and what is available now: the pages the +// system holds free and those it would give up first (inactive). +int kal_memory_info(kal_memory_status* out) { + if (out == nullptr || out->self_size < 2 * sizeof(kal_u32)) return kal_err_invalid; + const kal_u32 self = out->self_size; + kal_memory_status v{}; + v.self_size = self; + kal_u64 total = 0, page = 0, free = 0, inactive = 0; + if (sysctl_u64("hw.physmem", total) && total) { v.total_bytes = total; v.present |= KAL_MSTATUS_TOTAL; } + if (sysctl_u64("hw.pagesize", page) && sysctl_u64("vm.stats.vm.v_free_count", free)) { + sysctl_u64("vm.stats.vm.v_inactive_count", inactive); + v.available_bytes = (free + inactive) * page; + if ((v.present & KAL_MSTATUS_TOTAL) && v.available_bytes > v.total_bytes) v.available_bytes = v.total_bytes; + v.present |= KAL_MSTATUS_AVAILABLE; + } + if (self < __builtin_offsetof(kal_memory_status, total_bytes) + 8) v.present &= ~KAL_MSTATUS_TOTAL; + if (self < __builtin_offsetof(kal_memory_status, available_bytes) + 8) v.present &= ~KAL_MSTATUS_AVAILABLE; + okf::copy(out, &v, self < sizeof v ? self : sizeof v); + return kal_ok; +} + +} // extern "C" diff --git a/src/memory_maps.cpp b/src/memory_maps.cpp new file mode 100644 index 0000000..b4063ca --- /dev/null +++ b/src/memory_maps.cpp @@ -0,0 +1,139 @@ +// openkal.exec, openkal.map and openkal.region: mappings and their protection. +// +// Memory a loader writes and then executes is never writable and executable at +// once (W^X): it is mapped writable and published read and execute. A region at +// an address is FreeBSD's MAP_FIXED | MAP_EXCL, which refuses what is already +// mapped there. +#include "common.h" + +namespace { +using okf::word; +kal_uintptr page() { return kal_memory_granularity(); } +} // namespace + +extern "C" { + +// ================================================================ exec +kal_uintptr kal_exec_granularity(void) { return page(); } +void* kal_exec_alloc(kal_uintptr size) { + if (size == 0) return nullptr; + const word p = okf::mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); + return okf::failed(p) ? nullptr : reinterpret_cast(p); +} +void* kal_exec_alloc_at(void* at, kal_uintptr size) { + if (!at || !size || reinterpret_cast(at) % page()) return nullptr; + const word p = okf::mmap(at, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_EXCL, -1, 0); + if (okf::failed(p)) return nullptr; + if (reinterpret_cast(p) != at) { okf::munmap(reinterpret_cast(p), size); return nullptr; } + return reinterpret_cast(p); +} +int kal_exec_publish(void* p, kal_uintptr size) { + const word r = okf::mprotect(p, size, PROT_READ | PROT_EXEC); + return okf::failed(r) ? okf::fail(r) : kal_ok; +} +int kal_exec_publish_part(void* r, kal_uintptr off, kal_uintptr size) { + const kal_uintptr g = page(); + if (!r || off % g || size % g) return kal_err_invalid; + const word e = okf::mprotect(static_cast(r) + off, size, PROT_READ | PROT_EXEC); + return okf::failed(e) ? okf::fail(e) : kal_ok; +} +int kal_exec_seal_part(void* r, kal_uintptr off, kal_uintptr size, kal_uintptr flags) { + const kal_uintptr g = page(); + if (flags & ~KAL_EXEC_SEAL_FOREVER) return kal_err_invalid; + if (!r || off % g || size % g) return kal_err_invalid; + const word e = okf::mprotect(static_cast(r) + off, size, PROT_READ); + return okf::failed(e) ? okf::fail(e) : kal_ok; +} +int kal_exec_map_part(void* r, kal_uintptr off, kal_uintptr size, kal_file f, kal_u64 at, kal_uintptr how) { + const kal_uintptr g = page(); + if (!r || size == 0 || off % g || size % g || at % KAL_MAP_ALIGNMENT) return kal_err_invalid; + if (how & ~(KAL_EXEC_MAP_EXECUTE | KAL_EXEC_MAP_PREFETCH | KAL_EXEC_MAP_BTI | KAL_EXEC_MAP_WRITE)) + return kal_err_invalid; + if ((how & KAL_EXEC_MAP_WRITE) && (how & KAL_EXEC_MAP_EXECUTE)) return kal_err_invalid; + okf::kstat st{}; + const word s = okf::fstat(okf::unpack(f.h), &st); + if (okf::failed(s)) return okf::fail(s); + if (at + size > static_cast(st.st_size)) return kal_err_invalid; + int prot = PROT_READ; + if (how & KAL_EXEC_MAP_EXECUTE) prot |= PROT_EXEC; + if (how & KAL_EXEC_MAP_WRITE) prot |= PROT_WRITE; + // BTI is an ARM feature this amd64 implementation does not enforce, and the + // part is mapped as without it. + const word p = okf::mmap(static_cast(r) + off, size, prot, MAP_PRIVATE | MAP_FIXED, okf::unpack(f.h), + static_cast(at)); + if (okf::failed(p)) return okf::fail(p); + if (how & KAL_EXEC_MAP_PREFETCH) okf::madvise(reinterpret_cast(p), size, MADV_WILLNEED); + return kal_ok; +} +void kal_exec_free(void* p, kal_uintptr size) { if (p) okf::munmap(p, size); } +kal_uintptr kal_exec_props(void) { + return KAL_EXEC_PROP_AVAILABLE | KAL_EXEC_PROP_FILE | KAL_EXEC_PROP_REPUBLISH | KAL_EXEC_PROP_PLACE; +} + +// ================================================================ map +int kal_map_file(kal_file f, kal_u64 offset, kal_uintptr length, kal_uintptr how, void** out) { + if (!out || length == 0 || offset % KAL_MAP_ALIGNMENT || (how & ~(KAL_MAP_WRITE | KAL_MAP_SHARED))) return kal_err_invalid; + okf::kstat st{}; + const word s = okf::fstat(okf::unpack(f.h), &st); + if (okf::failed(s)) return okf::fail(s); + if (offset + length > static_cast(st.st_size)) return kal_err_invalid; + const int prot = PROT_READ | ((how & KAL_MAP_WRITE) ? PROT_WRITE : 0); + const word p = okf::mmap(nullptr, length, prot, (how & KAL_MAP_SHARED) ? MAP_SHARED : MAP_PRIVATE, + okf::unpack(f.h), static_cast(offset)); + if (okf::failed(p)) return p == -ENODEV ? kal_err_not_supported : okf::fail(p); + *out = reinterpret_cast(p); + return kal_ok; +} +int kal_map_sync(void* region, kal_uintptr length) { + const word r = okf::msync(region, length, MS_SYNC); + return okf::failed(r) ? okf::fail(r) : kal_ok; +} +void kal_map_release(void* region, kal_uintptr length) { if (region) okf::munmap(region, length); } + +// ================================================================ region +// Address space reserved without backing (PROT_NONE), backed in parts. +void* kal_region_reserve(kal_uintptr size, kal_uintptr align, kal_uintptr flags) { + if (flags & ~KAL_REGION_COMMIT) return nullptr; + const kal_uintptr g = page(); + if (size == 0 || size % g) return nullptr; + if (align < g) align = g; + if (align & (align - 1)) return nullptr; + const int prot = (flags & KAL_REGION_COMMIT) ? (PROT_READ | PROT_WRITE) : PROT_NONE; + const kal_uintptr span = size + (align > g ? align : 0); + const word m = okf::mmap(nullptr, span, prot, MAP_PRIVATE | MAP_ANON, -1, 0); + if (okf::failed(m)) return nullptr; + char* p = reinterpret_cast(m); + if (span == size) return p; + char* a = reinterpret_cast((reinterpret_cast(p) + align - 1) & ~(align - 1)); + if (a > p) okf::munmap(p, static_cast(a - p)); + if (a + size < p + span) okf::munmap(a + size, static_cast(p + span - (a + size))); + return a; +} +// A part begins and ends at multiples of the granularity; FreeBSD's mprotect +// would round an address that does not, so it is refused here. +static bool whole_pages(void* addr, kal_uintptr len) { + const kal_uintptr g = page(); + return addr && len && reinterpret_cast(addr) % g == 0 && len % g == 0; +} +int kal_region_commit(void* addr, kal_uintptr len) { + if (!whole_pages(addr, len)) return kal_err_invalid; + const word r = okf::mprotect(addr, len, PROT_READ | PROT_WRITE); + return okf::failed(r) ? okf::fail(r) : kal_ok; +} +int kal_region_decommit(void* addr, kal_uintptr len) { + if (!whole_pages(addr, len)) return kal_err_invalid; + // Replaced by fresh reserved pages: the contents are gone and the backing + // is returned. + const word p = okf::mmap(addr, len, PROT_NONE, MAP_PRIVATE | MAP_ANON | MAP_FIXED, -1, 0); + return okf::failed(p) ? okf::fail(p) : kal_ok; +} +int kal_region_discard(void* addr, kal_uintptr len) { + if (!whole_pages(addr, len)) return kal_err_invalid; + // The contents need not be kept; the part stays backed. + okf::madvise(addr, len, MADV_FREE); + return kal_ok; +} +void kal_region_release(void* addr, kal_uintptr size) { if (addr) okf::munmap(addr, size); } +kal_uintptr kal_region_props(void) { return KAL_REGION_PROP_OVERCOMMIT; } + +} // extern "C" diff --git a/src/process.cpp b/src/process.cpp new file mode 100644 index 0000000..6bd406e --- /dev/null +++ b/src/process.cpp @@ -0,0 +1,408 @@ +// openkal.process: a program started by pdfork and fexecve, awaited by wait6, +// units as process groups, and the requests to end taken over by sigaction. +#include "common.h" + +namespace { + +using okf::word; + +// HANDLES. A program this one started is its identifier plus one; a program +// found by its identifier carries kOpened, and this program itself kSelf, so +// that the operations a handle admits are told apart: a program does not wait +// for itself, and a program found by its identifier is not the caller's to end. +constexpr kal_uintptr kOpened = kal_uintptr(1) << 63; +constexpr kal_uintptr kSelf = kal_uintptr(1) << 62; +kal_uintptr started_handle(int pid) { return static_cast(pid) + 1u; } +int pid_of(kal_process p) { return static_cast((p.h & ~(kOpened | kSelf)) - 1u); } + +// WHAT AN AWAITED PROGRAM REPORTED, kept while its handle is held: a second +// wait answers the same, and its processor time remains (0.21). +struct ended_program { int pid; int status; int terminated; kal_u64 cpu_ns; }; +constexpr int kEndedMax = 256; +ended_program g_ended[kEndedMax]; +int g_ended_count = 0; +okf::spin g_ended_lock; + +bool ended_record(int pid, ended_program* out) { + g_ended_lock.lock(); + bool found = false; + for (int i = 0; i < g_ended_count; ++i) + if (g_ended[i].pid == pid) { if (out) *out = g_ended[i]; found = true; break; } + g_ended_lock.unlock(); + return found; +} +// THE DESCRIPTORS OF THE PROGRAMS STARTED (pdfork), held while their handles +// are: PD_DAEMON, so that releasing one does not end its program. +struct started { int pid; int pd; }; +constexpr int kStartedMax = 1024; +started g_started[kStartedMax]; +int g_started_count = 0; +void started_keep(int pid, int pd) { + g_ended_lock.lock(); + if (g_started_count < kStartedMax) g_started[g_started_count++] = started{ pid, pd }; + else okf::close(pd); + g_ended_lock.unlock(); +} +void started_release(int pid) { + int pd = -1; + g_ended_lock.lock(); + for (int i = 0; i < g_started_count; ++i) + if (g_started[i].pid == pid) { pd = g_started[i].pd; g_started[i] = g_started[--g_started_count]; break; } + g_ended_lock.unlock(); + if (pd >= 0) okf::close(pd); +} + +void ended_forget(int pid) { + g_ended_lock.lock(); + for (int i = 0; i < g_ended_count; ++i) + if (g_ended[i].pid == pid) { g_ended[i] = g_ended[--g_ended_count]; break; } + g_ended_lock.unlock(); +} + +kal_u64 cpu_of(const okf::krusage& ru) { + return (static_cast(ru.ru_utime.tv_sec) + static_cast(ru.ru_stime.tv_sec)) * 1000000000ull + + (static_cast(ru.ru_utime.tv_usec) + static_cast(ru.ru_stime.tv_usec)) * 1000ull; +} + +int reap(int pid, int opts, int* status, int* terminated, bool* done) { + ended_program e{}; + if (!ended_record(pid, &e)) { + int s = 0; okf::kwrusage wr{}; word r; + do r = okf::wait6(P_PID, pid, &s, opts | WEXITED | WTRAPPED, &wr); while (r == -EINTR); + const okf::krusage& ru = wr.wru_self; + if (okf::failed(r)) return okf::fail(r); + *done = r != 0; + if (!*done) return kal_ok; + e.pid = pid; + if (WIFEXITED(s)) { e.status = WEXITSTATUS(s); e.terminated = 0; } + else { e.status = WIFSIGNALED(s) ? 128 + WTERMSIG(s) : 1; e.terminated = 1; } + e.cpu_ns = cpu_of(ru); + g_ended_lock.lock(); + if (g_ended_count < kEndedMax) g_ended[g_ended_count++] = e; + g_ended_lock.unlock(); + } + *done = true; + if (status) *status = e.status; + if (terminated) *terminated = e.terminated; + return kal_ok; +} + +// The vectors a start hands the kernel, in memory of kal_alloc's. +char* copy_of(const char* s, kal_uintptr n) { + auto* c = static_cast(kal_alloc(n + 1, 1)); + if (c) { okf::copy(c, s, n); c[n] = 0; } + return c; +} + +// ---------------------------------------------------------------- the stop word +// THE REQUESTS TO END, TAKEN OVER. SIGINT, SIGTERM and SIGHUP set the word, +// counting each and never zero again once one has arrived; a request the +// program's starter arranged to ignore stays ignored. The handler wakes the +// word's waiters itself: the wake is a system call, which a handler may make. +kal_u32 g_stop_word = 0; +int g_stop_signal = 0; +int g_stop_state = 0; // 0: not armed, 1: arming, 2: armed +const int kStopSignals[] = { SIGINT, SIGTERM, SIGHUP }; + +void stop_handler(int signo) { + int none = 0; + __atomic_compare_exchange_n(&g_stop_signal, &none, signo, false, __ATOMIC_RELAXED, __ATOMIC_RELAXED); + kal_u32 seen = __atomic_load_n(&g_stop_word, __ATOMIC_RELAXED), next; + do { + next = seen + 1u; + if (next == 0) next = 1u; + } while (!__atomic_compare_exchange_n(&g_stop_word, &seen, next, false, __ATOMIC_RELEASE, __ATOMIC_RELAXED)); + okf::umtx(&g_stop_word, UMTX_OP_WAKE_PRIVATE, 0x7fffffff, nullptr, nullptr); +} + +void arm_stop() { + if (__atomic_load_n(&g_stop_state, __ATOMIC_ACQUIRE) == 2) return; + int expected = 0; + if (!__atomic_compare_exchange_n(&g_stop_state, &expected, 1, false, __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE)) { + while (__atomic_load_n(&g_stop_state, __ATOMIC_ACQUIRE) != 2) okf::sched_yield(); + return; + } + for (const int sig : kStopSignals) { + okf::ksigaction old{}; + if (!okf::failed(okf::sigaction(sig, nullptr, &old)) && old.sa_handler == SIG_IGN) continue; + okf::ksigaction act{}; + act.sa_handler = stop_handler; + act.sa_flags = SA_RESTART; + okf::sigaction(sig, &act, nullptr); + } + __atomic_store_n(&g_stop_state, 2, __ATOMIC_RELEASE); +} + +} // namespace + +extern "C" { + +kal_uintptr kal_process_props(void) { + return KAL_PROCESS_PROP_TERMINATE | KAL_PROCESS_PROP_STREAM_PASSING | KAL_PROCESS_PROP_EXIT_STATUS + | KAL_PROCESS_PROP_CHANNEL | KAL_PROCESS_PROP_JOB | KAL_PROCESS_PROP_NULL_STREAMS + | KAL_PROCESS_PROP_OPEN_ID | KAL_PROCESS_PROP_STOP_REQUESTED; +} + +int kal_process_spawn(const kal_spawn* how, const char* path, kal_uintptr path_len, + const char** argv, const kal_uintptr* argv_lens, kal_uintptr argc, + const char** envp, const kal_uintptr* envp_lens, kal_uintptr envc, + const kal_spawn_streams* streams, kal_process* out) { + if (!how || !path || !out || path_len == 0 || path_len >= okf::kPath) return kal_err_invalid; + if (how->grant_count) return kal_err_not_supported; + const kal_uintptr unsupported = KAL_SPAWN_BOUND_LIFETIME | KAL_SPAWN_ONE_STRING | KAL_SPAWN_CONFINE; + if (how->flags & unsupported) return kal_err_not_supported; + // One preference at most, and the null device and a placed stream are not + // both at one position. + const kal_uintptr prefs = how->flags & (KAL_SPAWN_RESOURCES_BALANCED | KAL_SPAWN_RESOURCES_PERFORMANCE + | KAL_SPAWN_RESOURCES_ECONOMY); + if (prefs & (prefs - 1)) return kal_err_invalid; + const kal_uintptr nulls[3] = { KAL_SPAWN_NULL_IN, KAL_SPAWN_NULL_OUT, KAL_SPAWN_NULL_ERR }; + if (streams) { + const kal_uintptr placed[3] = { streams->in.h, streams->out.h, streams->err.h }; + for (int i = 0; i < 3; ++i) if (placed[i] && (how->flags & nulls[i])) return kal_err_invalid; + } + for (kal_uintptr i = 0; i < envc; ++i) { + if (!envp || !envp[i] || !envp_lens) return kal_err_invalid; + kal_uintptr eq = 0; + while (eq < envp_lens[i] && envp[i][eq] != '=') ++eq; + if (eq == 0 || eq == envp_lens[i]) return kal_err_invalid; + } + const int base = okf::unpack(how->base.h), work = okf::unpack(how->work.h); + if (base < 0 || work < 0) return kal_err_invalid; + + // The program is named relative to `base': resolved to a descriptor now, + // so that a start that cannot happen is reported by its reason. + char p[okf::kPath]; okf::copy(p, path, path_len); p[path_len] = 0; + const word exe = okf::openat(base, p, O_RDONLY | O_CLOEXEC); + if (okf::failed(exe)) return okf::fail(exe); + okf::kstat st{}; + const word sr = okf::fstat(static_cast(exe), &st); + if (okf::failed(sr)) { okf::close(static_cast(exe)); return okf::fail(sr); } + if (S_ISDIR(st.st_mode)) { okf::close(static_cast(exe)); return kal_err_is_directory; } + if (!(st.st_mode & 0111)) { okf::close(static_cast(exe)); return kal_err_permission; } + + // A position with no stream placed and no null device keeps the caller's. + int fds[3] = { -1, -1, -1 }; + if (streams) { fds[0] = okf::unpack(streams->in.h); fds[1] = okf::unpack(streams->out.h); fds[2] = okf::unpack(streams->err.h); } + for (int i = 0; i < 3; ++i) { + if (!(how->flags & nulls[i])) continue; + const word n = okf::openat(AT_FDCWD, "/dev/null", (i == 0 ? O_RDONLY : O_WRONLY) | O_CLOEXEC); + if (okf::failed(n)) { + for (int k = 0; k < i; ++k) if (how->flags & nulls[k]) okf::close(fds[k]); + okf::close(static_cast(exe)); + return okf::fail(n); + } + fds[i] = static_cast(n); + } + + // THE VECTORS: the arguments, and the environment given or this program's, + // with the preference in KAL_RESOURCES where the start states one. + const char* pref = prefs == KAL_SPAWN_RESOURCES_PERFORMANCE ? "KAL_RESOURCES=performance" + : prefs == KAL_SPAWN_RESOURCES_ECONOMY ? "KAL_RESOURCES=economy" + : prefs == KAL_SPAWN_RESOURCES_BALANCED ? "KAL_RESOURCES=balanced" : nullptr; + kal_uintptr inherited = 0; + if (envc == 0 && !envp) for (char** e = okf::g_envp; e && *e; ++e) ++inherited; + const kal_uintptr av_n = argc + 1, ev_n = envc + inherited + 2; + auto** av = static_cast(kal_alloc(av_n * sizeof(char*), alignof(char*))); + auto** ev = static_cast(kal_alloc(ev_n * sizeof(char*), alignof(char*))); + kal_uintptr en = 0; + bool whole = av && ev; + if (av) okf::zero(av, av_n * sizeof(char*)); + if (ev) okf::zero(ev, ev_n * sizeof(char*)); + for (kal_uintptr i = 0; whole && i < argc; ++i) whole = (av[i] = copy_of(argv[i], argv_lens[i])) != nullptr; + for (kal_uintptr i = 0; whole && i < envc; ++i) { + if (pref && envp_lens[i] >= 14 && okf::same(envp[i], "KAL_RESOURCES=", 14)) continue; + whole = (ev[en++] = copy_of(envp[i], envp_lens[i])) != nullptr; + } + for (kal_uintptr i = 0; whole && i < inherited; ++i) { + const char* e = okf::g_envp[i]; + if (pref && okf::same(e, "KAL_RESOURCES=", 14)) continue; + whole = (ev[en++] = copy_of(e, okf::length(e))) != nullptr; + } + if (whole && pref) whole = (ev[en++] = copy_of(pref, okf::length(pref))) != nullptr; + auto release = [&] { + if (av) { for (kal_uintptr i = 0; i < argc; ++i) if (av[i]) kal_free(av[i], argv_lens[i] + 1, 1); kal_free(av, av_n * sizeof(char*), alignof(char*)); } + if (ev) { for (kal_uintptr i = 0; i < en; ++i) if (ev[i]) kal_free(ev[i], okf::length(ev[i]) + 1, 1); kal_free(ev, ev_n * sizeof(char*), alignof(char*)); } + for (int i = 0; i < 3; ++i) if (how->flags & nulls[i]) okf::close(fds[i]); + okf::close(static_cast(exe)); + }; + if (!whole) { release(); return kal_err_no_memory; } + + int report[2]; + const word pr = okf::pipe2(report, O_CLOEXEC); + if (okf::failed(pr)) { release(); return okf::fail(pr); } + const int join = how->job ? static_cast(how->job->h) : 0; // 0: a new unit + + int pd = -1; + const word pid = okf::pdfork(&pd, PD_DAEMON | PD_CLOEXEC); + if (pid == 0) { + // THE COPY: system calls only, until the program replaces it. + auto refused = [&](word r) { + const int why = static_cast(-r); + okf::write(report[1], &why, sizeof why); + okf::exit(127); + }; + if (how->job) okf::setpgid(0, join); + int saved[3] = { -1, -1, -1 }; + for (int i = 0; i < 3; ++i) { + if (fds[i] < 0) continue; + const word d = okf::fcntl(fds[i], F_DUPFD_CLOEXEC, 10); + if (okf::failed(d)) refused(d); + saved[i] = static_cast(d); + } + for (int i = 0; i < 3; ++i) { + if (saved[i] < 0) continue; + const word d = okf::dup2(saved[i], i); + if (okf::failed(d)) refused(d); + } + okf::ksigaction dfl{}; + dfl.sa_handler = SIG_DFL; + for (int s = 1; s < NSIG; ++s) if (s != SIGKILL && s != SIGSTOP) okf::sigaction(s, &dfl, nullptr); + okf::ksigset none{}; + okf::sigprocmask(SIG_SETMASK, &none, nullptr); + const word c = okf::fchdir(work); + if (okf::failed(c)) refused(c); + refused(okf::fexecve(static_cast(exe), av, ev)); + } + okf::close(report[1]); + release(); + if (okf::failed(pid)) { okf::close(report[0]); return okf::fail(pid); } + if (how->job) okf::setpgid(static_cast(pid), join); // both sides, as shells do + int e = 0; word r; + do r = okf::read(report[0], &e, sizeof e); while (r == -EINTR); + okf::close(report[0]); + if (r == sizeof e) { + int s = 0; + okf::wait6(P_PID, pid, &s, WEXITED, nullptr); + okf::close(pd); + return okf::translate(e); + } + if (how->job && how->job->h == 0) how->job->h = static_cast(pid); + started_keep(static_cast(pid), pd); + *out = kal_process{ started_handle(static_cast(pid)) }; + return kal_ok; +} + +int kal_process_channel(kal_stream* mine, kal_stream* theirs) { + if (!mine || !theirs) return kal_err_invalid; + int p[2]; + const word r = okf::pipe2(p, O_CLOEXEC); + if (okf::failed(r)) return okf::fail(r); + *mine = kal_stream{ okf::pack(p[0]) }; *theirs = kal_stream{ okf::pack(p[1]) }; + return kal_ok; +} +void kal_process_channel_close(kal_stream s) { const int fd = okf::unpack(s.h); if (fd >= 0) okf::close(fd); } + +int kal_process_wait(kal_process p, int* status, int* terminated) { + if (p.h == 0 || (p.h & kSelf)) return kal_err_invalid; + if (p.h & kOpened) return kal_err_not_supported; + bool done = false; + return reap(pid_of(p), 0, status, terminated, &done); +} +// A REQUEST TO END: SIGTERM, which the program may take over. +int kal_process_terminate(kal_process p) { + if (p.h == 0 || (p.h & kSelf)) return kal_err_invalid; + if (p.h & kOpened) return kal_err_not_supported; + if (ended_record(pid_of(p), nullptr)) return kal_ok; + const word r = okf::kill(pid_of(p), SIGTERM); + return okf::failed(r) ? okf::fail(r) : kal_ok; +} +void kal_process_close(kal_process p) { + if (p.h != 0 && !(p.h & (kOpened | kSelf))) { ended_forget(pid_of(p)); started_release(pid_of(p)); } +} +int kal_process_self(kal_process* out) { + if (!out) return kal_err_invalid; + *out = kal_process{ started_handle(static_cast(okf::getpid())) | kSelf }; + return kal_ok; +} +int kal_process_open_id(kal_u64 id, kal_process* out) { + if (!out || id == 0 || id > 0x7fffffffu) return kal_err_invalid; + if (okf::kill(static_cast(id), 0) == -ESRCH) return kal_err_not_found; + *out = kal_process{ started_handle(static_cast(id)) | kOpened }; + return kal_ok; +} +int kal_process_info(kal_process p, kal_u32 wanted, kal_program_info* out) { + if (!out || out->self_size < 8 || p.h == 0) return kal_err_invalid; + const kal_u32 cap = out->self_size; + kal_program_info i{}; i.self_size = cap; + const int pid = pid_of(p); + i.present = KAL_PINFO_ID; i.id = static_cast(pid); + if (p.h & kSelf) { + okf::krusage ru{}; + if (!okf::failed(okf::getrusage(RUSAGE_SELF, &ru))) { i.cpu_ns = cpu_of(ru); i.present |= KAL_PINFO_CPU; } + i.present |= KAL_PINFO_ENDED; i.ended = 0; + } else if (!(p.h & kOpened)) { + ended_program e{}; + i.present |= KAL_PINFO_ENDED; + if (ended_record(pid, &e)) { i.ended = 1; i.cpu_ns = e.cpu_ns; i.present |= KAL_PINFO_CPU; } + else i.ended = 0; + } + i.present &= wanted | KAL_PINFO_ID; + struct { kal_u32 bit; unsigned long end; } const fields[] = { + { KAL_PINFO_ID, __builtin_offsetof(kal_program_info, id) + 8 }, + { KAL_PINFO_CPU, __builtin_offsetof(kal_program_info, cpu_ns) + 8 }, + { KAL_PINFO_ENDED, __builtin_offsetof(kal_program_info, ended) + 4 }, + }; + for (const auto& f : fields) if (cap < f.end) i.present &= ~f.bit; + okf::copy(out, &i, cap < sizeof i ? cap : sizeof i); + return kal_ok; +} + +// UNITS ARE PROCESS GROUPS. +int kal_process_job_enter(kal_job* j) { + if (!j) return kal_err_invalid; + if (j->h == 0) { + const word r = okf::setpgid(0, 0); + if (okf::failed(r)) return okf::fail(r); + j->h = static_cast(okf::getpid()); + return kal_ok; + } + const word r = okf::setpgid(0, static_cast(j->h)); + return okf::failed(r) ? okf::fail(r) : kal_ok; +} +int kal_process_job_terminate(kal_job j) { + if (!j.h) return kal_err_invalid; + const word r = okf::kill(-static_cast(j.h), SIGKILL); + return okf::failed(r) ? okf::fail(r) : kal_ok; +} +void kal_process_job_close(kal_job) {} +// Declared units, usage and limits are not claimed (KAL_PROCESS_PROP_JOB_OBSERVE, +// _JOB_USAGE, _JOB_LIMIT_*), and the operations report so (clause 6.2). +int kal_process_job_create(kal_job*, char*, kal_uintptr, kal_uintptr*) { return kal_err_not_supported; } +int kal_process_job_open(const char*, kal_uintptr, kal_job*) { return kal_err_not_supported; } +int kal_process_job_observe(kal_job, const char*, kal_uintptr, kal_u32*) { return kal_err_not_supported; } +int kal_process_job_retire(kal_job, const char*, kal_uintptr, const kal_program_identity*, kal_u32*) { return kal_err_not_supported; } +int kal_process_job_usage(kal_job, kal_job_usage*) { return kal_err_not_supported; } +int kal_process_job_limit(kal_job, kal_uintptr kind, kal_u64) { + return kind == KAL_JOB_LIMIT_MEMORY ? kal_err_not_supported : kal_err_invalid; +} + +const kal_u32* kal_process_stop_requested(void) { + arm_stop(); + return &g_stop_word; +} +int kal_process_end_as_requested(void) { + const int sig = __atomic_load_n(&g_stop_signal, __ATOMIC_ACQUIRE); + if (sig == 0 || __atomic_load_n(&g_stop_word, __ATOMIC_ACQUIRE) == 0) return kal_err_invalid; + // The request's own disposition, restored, and the request made again. + okf::ksigaction act{}; + act.sa_handler = SIG_DFL; + okf::sigaction(sig, &act, nullptr); + okf::ksigset one{}; one.__bits[(sig - 1) / 32] = 1u << ((sig - 1) % 32); + okf::sigprocmask(SIG_UNBLOCK, &one, nullptr); + okf::kill(static_cast(okf::getpid()), sig); + return kal_err_invalid; +} + +} // extern "C" + +// Used by openkal.timeout. +namespace okf { +int reap_program(int pid, int opts, int* status, int* terminated, bool* done) { return reap(pid, opts, status, terminated, done); } +int started_pid(kal_process p, int* pid) { + if (p.h == 0 || (p.h & kSelf)) return kal_err_invalid; + if (p.h & kOpened) return kal_err_not_supported; + *pid = pid_of(p); + return kal_ok; +} +} // namespace okf diff --git a/src/start.cpp b/src/start.cpp new file mode 100644 index 0000000..473e359 --- /dev/null +++ b/src/start.cpp @@ -0,0 +1,106 @@ +// THE STANDALONE ARRANGEMENT'S BEGINNING (feature `standalone'): the program +// has no runtime, and this implementation receives control from the kernel. +// +// FreeBSD passes the address of the argument block --- argc, the arguments, a +// null, the environment, a null, the auxiliary vector --- in %rdi on amd64 and +// in x0 on arm64. The thread pointer is set before anything with thread-local +// state runs; then a C library's own hand-over is used where one is linked +// (`__libc_start_main', openkal-musl's), and otherwise the image's initialisers +// are run and `main' is called. +#include "common.h" + +#ifdef OKF_STANDALONE +#include "tls.h" + +// THE BRAND. The kernel recognises a FreeBSD image by this note (the ABI tag, +// "FreeBSD" and the __FreeBSD_version it was built for: 14.0-RELEASE). +__asm__( + ".section .note.tag,\"a\",%note\n" + ".p2align 2\n" + ".long 8\n" // the name's size, "FreeBSD" and its terminator + ".long 4\n" // the description's size + ".long 1\n" // NT_FREEBSD_ABI_TAG + ".asciz \"FreeBSD\"\n" + ".long 1400097\n" + ".previous\n"); + +extern "C" { +int main(int, char**, char**); +[[gnu::weak]] int __libc_start_main(int (*)(int, char**, char**), int, char**, + void (*)(), void (*)(), void (*)()); +[[noreturn]] void __okf_start_c(long* block); +} + +namespace { + +using initialiser = void (*)(int, char**, char**); +extern "C" { +[[gnu::weak]] extern initialiser __preinit_array_start[]; +[[gnu::weak]] extern initialiser __preinit_array_end[]; +[[gnu::weak]] extern initialiser __init_array_start[]; +[[gnu::weak]] extern initialiser __init_array_end[]; +} + +void* allocate(unsigned long n, unsigned long a) { return kal_alloc(n, a); } + +} // namespace + +#if defined(__x86_64__) +__asm__( +".text\n" +".globl _start\n" +".type _start,@function\n" +"_start:\n" +" xor %ebp,%ebp\n" +" and $-16,%rsp\n" +" call __okf_start_c\n" +" hlt\n" +".size _start,.-_start\n"); +#elif defined(__aarch64__) +__asm__( +".text\n" +".globl _start\n" +".type _start,%function\n" +"_start:\n" +" mov x29,#0\n" +" mov x30,#0\n" +" mov x1,sp\n" +" and x1,x1,#-16\n" +" mov sp,x1\n" +" bl __okf_start_c\n" +" brk #0\n" +".size _start,.-_start\n"); +#endif + +extern "C" [[noreturn]] void __okf_start_c(long* block) { + const int argc = static_cast(block[0]); + char** argv = reinterpret_cast(block + 1); + char** envp = argv + argc + 1; + char** e = envp; + while (*e) ++e; + okf::g_argc = argc; okf::g_argv = argv; okf::g_envp = envp; + okf::g_auxv = reinterpret_cast(e + 1); + + const okf::tls_block b = okf::make_tls(allocate); + if (b.tp != nullptr) okf::set_thread_pointer(b.tp); + + if (__libc_start_main != nullptr) { + __libc_start_main(main, argc, argv, nullptr, nullptr, nullptr); + kal_exit(127); // a C library's hand-over does not return + } + for (initialiser* p = __preinit_array_start; p != __preinit_array_end; ++p) (*p)(argc, argv, envp); + for (initialiser* p = __init_array_start; p != __init_array_end; ++p) (*p)(argc, argv, envp); + kal_exit(main(argc, argv, envp)); +} + +#endif // OKF_STANDALONE + +namespace okf { +unsigned long* g_auxv = nullptr; +// A value of the auxiliary vector, or zero. Recorded only where this +// implementation received control (standalone). +unsigned long auxval(unsigned long key) { + for (unsigned long* a = g_auxv; a && a[0] != 0; a += 2) if (a[0] == key) return a[1]; + return 0; +} +} // namespace okf diff --git a/src/sys.h b/src/sys.h new file mode 100644 index 0000000..f7da8b4 --- /dev/null +++ b/src/sys.h @@ -0,0 +1,393 @@ +// The system call interface of FreeBSD, as this implementation issues it. +// +// EVERY OPERATION BELOW IS A SYSTEM CALL, issued here; none is a function of the +// C library, and no header of a C library is read. The numbers, flags and +// structure layouts are the kernel's ABI, stated below as FreeBSD 14 states +// them in its headers. They are identical on amd64 and arm64, and were read +// from the base system's headers by compiling them for both (each structure's +// size is asserted). An implementation that is the whole of a program's +// environment, or that lies beneath a C library written above openkal, cannot +// take them from a C library's headers: those may describe another kernel. +// +// THE KERNEL'S ANSWER, AS ONE VALUE. FreeBSD reports a failure by setting the +// carry flag, with the error number in the first result register (%rax, x0); a +// success leaves the carry clear and the result there, and a second result, +// where there is one (fork, pdfork), in the next (%rdx, x1). `call6' folds the +// first two into one value: the result, or the error number negated, as the +// Linux implementation's system calls answer. +#pragma once + +using kal_ssize = long; + +namespace okf { + +using word = long; +using u8 = unsigned char; using u16 = unsigned short; using u32 = unsigned; using u64 = unsigned long long; +using i16 = short; using i32 = int; using i64 = long long; + +namespace abi { +constexpr int AT_FDCWD = -100; +constexpr int AT_REMOVEDIR = 2048; +constexpr int AT_SYMLINK_NOFOLLOW = 512; +constexpr int CLOCK_MONOTONIC = 4; +constexpr int CLOCK_REALTIME = 0; +constexpr int CTL_KERN = 1; +constexpr int EACCES = 13; +constexpr int EAGAIN = 35; +constexpr int EBADF = 9; +constexpr int ECONNRESET = 54; +constexpr int EDQUOT = 69; +constexpr int EEXIST = 17; +constexpr int EFAULT = 14; +constexpr int EINTR = 4; +constexpr int EINVAL = 22; +constexpr int EISDIR = 21; +constexpr int ELOOP = 62; +constexpr int ENAMETOOLONG = 63; +constexpr int ENODEV = 19; +constexpr int ENOENT = 2; +constexpr int ENOEXEC = 8; +constexpr int ENOMEM = 12; +constexpr int ENOSPC = 28; +constexpr int ENOSYS = 78; +constexpr int ENOTDIR = 20; +constexpr int ENOTEMPTY = 66; +constexpr int ENOTTY = 25; +constexpr int EOPNOTSUPP = 45; +constexpr int EPERM = 1; +constexpr int EPIPE = 32; +constexpr int EROFS = 30; +constexpr int ESRCH = 3; +constexpr int ETIMEDOUT = 60; +constexpr int EWOULDBLOCK = 35; +constexpr int EXDEV = 18; +constexpr int ECHILD = 10; +constexpr int F_DUPFD_CLOEXEC = 17; +constexpr int F_RDLCK = 1; +constexpr int F_SETLK = 12; +constexpr int F_SETLKW = 13; +constexpr int F_UNLCK = 2; +constexpr int F_WRLCK = 3; +constexpr int F_GETFD = 1; +constexpr int F_SETFD = 2; +constexpr int FD_CLOEXEC = 1; +constexpr int ICANON = 256; +constexpr int ICRNL = 256; +constexpr int IEXTEN = 1024; +constexpr int IGNCR = 128; +constexpr int INLCR = 64; +constexpr int ISIG = 128; +constexpr int ISTRIP = 32; +constexpr int IXON = 512; +constexpr int ECHO = 8; +constexpr int VMIN = 16; +constexpr int VTIME = 17; +constexpr int KERN_PROC = 14; +constexpr int KERN_PROC_PATHNAME = 12; +constexpr int LOCK_EX = 2; +constexpr int LOCK_NB = 4; +constexpr int LOCK_SH = 1; +constexpr int LOCK_UN = 8; +constexpr int MADV_FREE = 5; +constexpr int MADV_WILLNEED = 3; +constexpr int MAP_ANON = 4096; +constexpr int MAP_EXCL = 16384; +constexpr int MAP_FIXED = 16; +constexpr int MAP_PRIVATE = 2; +constexpr int MAP_SHARED = 1; +constexpr int MAP_STACK = 1024; +constexpr int MS_SYNC = 0; +constexpr int O_APPEND = 8; +constexpr int O_CLOEXEC = 1048576; +constexpr int O_CREAT = 512; +constexpr int O_DIRECTORY = 131072; +constexpr int O_EXCL = 2048; +constexpr int O_RDONLY = 0; +constexpr int O_RDWR = 2; +constexpr int O_TRUNC = 1024; +constexpr int O_WRONLY = 1; +constexpr int O_NOFOLLOW = 256; +constexpr int POLLIN = 1; +constexpr int POLLOUT = 4; +constexpr int PRIO_PROCESS = 0; +constexpr int PROT_EXEC = 4; +constexpr int PROT_NONE = 0; +constexpr int PROT_READ = 1; +constexpr int PROT_WRITE = 2; +constexpr int RUSAGE_SELF = 0; +constexpr int SA_RESTART = 2; +constexpr int SEEK_CUR = 1; +constexpr int SEEK_END = 2; +constexpr int SEEK_SET = 0; +constexpr int SIGABRT = 6; +constexpr int SIGHUP = 1; +constexpr int SIGINT = 2; +constexpr int SIGKILL = 9; +constexpr int SIGSTOP = 17; +constexpr int SIGTERM = 15; +constexpr int SIGPIPE = 13; +constexpr int SIG_SETMASK = 3; +constexpr int SIG_UNBLOCK = 2; +constexpr int SIG_BLOCK = 1; +constexpr int NSIG = 32; +constexpr unsigned long TIOCGETA = 1076655123ul; +constexpr unsigned long TIOCGWINSZ = 1074295912ul; +constexpr unsigned long TIOCSETAF = 2150396950ul; +constexpr int UMTX_OP_WAIT = 2; +constexpr int UMTX_OP_WAIT_UINT_PRIVATE = 15; +constexpr int UMTX_OP_WAKE_PRIVATE = 16; +constexpr int UMTX_OP_WAKE = 3; +constexpr int UTIME_OMIT = -2; +constexpr int WNOHANG = 1; +constexpr int P_PID = 0; +constexpr int PD_DAEMON = 1; +constexpr int PD_CLOEXEC = 2; +constexpr int AT_PHDR = 3; +constexpr int AT_PHENT = 4; +constexpr int AT_PHNUM = 5; +constexpr int AT_PAGESZ = 6; +constexpr int AT_USRSTACKBASE = 35; +constexpr int AT_USRSTACKLIM = 36; +constexpr int S_IFMT = 61440; +constexpr int S_IFDIR = 16384; +constexpr int S_IFREG = 32768; +constexpr int S_IFLNK = 40960; + +constexpr long SYS_read = 3; +constexpr long SYS_write = 4; +constexpr long SYS_close = 6; +constexpr long SYS_openat = 499; +constexpr long SYS_fstat = 551; +constexpr long SYS_fstatat = 552; +constexpr long SYS_mkdirat = 496; +constexpr long SYS_unlinkat = 503; +constexpr long SYS_renameat = 501; +constexpr long SYS_linkat = 495; +constexpr long SYS_symlinkat = 502; +constexpr long SYS_readlinkat = 500; +constexpr long SYS_utimensat = 547; +constexpr long SYS_futimens = 546; +constexpr long SYS_fchmodat = 490; +constexpr long SYS_fcntl = 92; +constexpr long SYS_ftruncate = 480; +constexpr long SYS_lseek = 478; +constexpr long SYS_pread = 475; +constexpr long SYS_pwrite = 476; +constexpr long SYS_fstatfs = 556; +constexpr long SYS_getdirentries = 554; +constexpr long SYS_mmap = 477; +constexpr long SYS_munmap = 73; +constexpr long SYS_mprotect = 74; +constexpr long SYS_madvise = 75; +constexpr long SYS_msync = 65; +constexpr long SYS_getpid = 20; +constexpr long SYS_kill = 37; +constexpr long SYS_wait4 = 7; +constexpr long SYS_wait6 = 532; +constexpr long SYS_execve = 59; +constexpr long SYS_fexecve = 492; +constexpr long SYS_fchdir = 13; +constexpr long SYS_dup2 = 90; +constexpr long SYS_pipe2 = 542; +constexpr long SYS_sigaction = 416; +constexpr long SYS_sigprocmask = 340; +constexpr long SYS_nanosleep = 240; +constexpr long SYS_clock_gettime = 232; +constexpr long SYS_clock_getres = 234; +constexpr long SYS_getrandom = 563; +constexpr long SYS_getrusage = 117; +constexpr long SYS_setpriority = 96; +constexpr long SYS_sched_yield = 331; +constexpr long SYS_ppoll = 545; +constexpr long SYS_ioctl = 54; +constexpr long SYS__umtx_op = 454; +constexpr long SYS_flock = 131; +constexpr long SYS___getcwd = 326; +constexpr long SYS_setpgid = 82; +constexpr long SYS___sysctl = 202; +constexpr long SYS___sysctlbyname = 570; +constexpr long SYS_exit = 1; +constexpr long SYS_fork = 2; +constexpr long SYS_pdfork = 518; +constexpr long SYS_thr_self = 432; +constexpr long SYS_thr_new = 455; +constexpr long SYS_thr_exit = 431; +constexpr long SYS_sysarch = 165; + +constexpr bool S_ISDIR(u32 m) { return (m & S_IFMT) == S_IFDIR; } +constexpr bool S_ISREG(u32 m) { return (m & S_IFMT) == S_IFREG; } +constexpr bool S_ISLNK(u32 m) { return (m & S_IFMT) == S_IFLNK; } +constexpr bool WIFEXITED(int s) { return (s & 0177) == 0; } +constexpr int WEXITSTATUS(int s) { return (s >> 8) & 0xff; } +constexpr bool WIFSIGNALED(int s) { return (s & 0177) != 0177 && (s & 0177) != 0; } +constexpr int WTERMSIG(int s) { return s & 0177; } +constexpr int AMD64_SET_FSBASE = 129; +constexpr int WEXITED = 16; +// A disposition is a function's address, or one of two values that are not. +inline void (*const SIG_DFL)(int) = nullptr; +inline void (*const SIG_IGN)(int) = reinterpret_cast(1); +constexpr int WTRAPPED = 32; +constexpr int NT_FREEBSD_ABI_TAG = 1; +} // namespace abi + +using namespace abi; + +struct ktimespec { i64 tv_sec; long tv_nsec; }; +struct ktimeval { i64 tv_sec; long tv_usec; }; +struct ksigset { u32 __bits[4]; }; +struct kstat { + u64 st_dev, st_ino, st_nlink; + u16 st_mode; i16 st_bsdflags; + u32 st_uid, st_gid; i32 st_padding1; + u64 st_rdev; + ktimespec st_atim, st_mtim, st_ctim, st_birthtim; + i64 st_size, st_blocks; + i32 st_blksize; u32 st_flags; + u64 st_gen; u64 st_spare[10]; +}; +struct kstatfs { + u32 f_version, f_type; u64 f_flags, f_bsize, f_iosize, f_blocks, f_bfree; i64 f_bavail; + unsigned char rest[2344 - 56]; +}; +struct kdirent { u64 d_fileno; i64 d_off; u16 d_reclen; u8 d_type, d_pad0; u16 d_namlen, d_pad1; char d_name[256]; }; +struct krusage { ktimeval ru_utime, ru_stime; long ru_rest[14]; }; +struct kwrusage { krusage wru_self, wru_children; }; +struct ksigaction { void (*sa_handler)(int); int sa_flags; ksigset sa_mask; }; +struct kflock { i64 l_start, l_len; i32 l_pid; i16 l_type, l_whence; i32 l_sysid; }; +struct ktermios { u32 c_iflag, c_oflag, c_cflag, c_lflag; u8 c_cc[20]; i32 c_ispeed, c_ospeed; }; +struct kwinsize { u16 ws_row, ws_col, ws_xpixel, ws_ypixel; }; +struct kpollfd { int fd; short events, revents; }; +struct kthr_param { + void (*start_func)(void*); void* arg; char* stack_base; unsigned long stack_size; + char* tls_base; unsigned long tls_size; long* child_tid; long* parent_tid; int flags; + void* rtp; void* spare[3]; +}; +static_assert(sizeof(kstat) == 224 && __builtin_offsetof(kstat, st_size) == 112 && __builtin_offsetof(kstat, st_mtim) == 64); +static_assert(sizeof(kstatfs) == 2344 && __builtin_offsetof(kstatfs, f_bavail) == 48); +static_assert(sizeof(kdirent) == 280 && __builtin_offsetof(kdirent, d_name) == 24); +static_assert(sizeof(krusage) == 144 && sizeof(kwrusage) == 288); +static_assert(sizeof(ksigaction) == 32 && __builtin_offsetof(ksigaction, sa_mask) == 12); +static_assert(sizeof(kflock) == 32 && __builtin_offsetof(kflock, l_type) == 20); +static_assert(sizeof(ktermios) == 44 && __builtin_offsetof(ktermios, c_cc) == 16); +static_assert(sizeof(kthr_param) == 104 && __builtin_offsetof(kthr_param, flags) == 64); + +struct answer { word value; word second; }; + +#if defined(__x86_64__) +inline answer call6(word n, word a = 0, word b = 0, word c = 0, word d = 0, word e = 0, word f = 0) { + register word r10 __asm__("r10") = d; + register word r8 __asm__("r8") = e; + register word r9 __asm__("r9") = f; + word rax = n, rdx = c; + unsigned char failed; + __asm__ __volatile__("syscall; setc %2" + : "+a"(rax), "+d"(rdx), "=r"(failed), "+r"(r10), "+r"(r8), "+r"(r9) + : "D"(a), "S"(b) + : "rcx", "r11", "memory", "cc"); + return answer{ failed ? -rax : rax, rdx }; +} +#elif defined(__aarch64__) +inline answer call6(word n, word a = 0, word b = 0, word c = 0, word d = 0, word e = 0, word f = 0) { + register word x8 __asm__("x8") = n; + register word x0 __asm__("x0") = a; + register word x1 __asm__("x1") = b; + register word x2 __asm__("x2") = c; + register word x3 __asm__("x3") = d; + register word x4 __asm__("x4") = e; + register word x5 __asm__("x5") = f; + word failed; + __asm__ __volatile__("svc #0\n\tcset %7, cs" + : "+r"(x0), "+r"(x1), "+r"(x2), "+r"(x3), "+r"(x4), "+r"(x5), "+r"(x8), "=r"(failed) + : + : "memory", "cc"); + return answer{ failed ? -x0 : x0, x1 }; +} +#else +#error "openkal-freebsd issues the system calls of amd64 and arm64" +#endif + +inline word sys(word n, word a = 0, word b = 0, word c = 0, word d = 0, word e = 0, word f = 0) { + return call6(n, a, b, c, d, e, f).value; +} + +template inline word w(T v) { return (word)(v); } + +// A value that is an error number, negated. +inline bool failed(word r) { return r < 0 && r > -4096; } + +// The operations, by the names the system gives them. +inline word read(int fd, void* b, unsigned long n) { return sys(SYS_read, fd, w(b), w(n)); } +inline word write(int fd, const void* b, unsigned long n) { return sys(SYS_write, fd, w(b), w(n)); } +inline word close(int fd) { return sys(SYS_close, fd); } +inline word openat(int dir, const char* p, int flags, int mode = 0) { return sys(SYS_openat, dir, w(p), flags, mode); } +inline word fstat(int fd, kstat* st) { return sys(SYS_fstat, fd, w(st)); } +inline word fstatat(int dir, const char* p, kstat* st, int flags) { return sys(SYS_fstatat, dir, w(p), w(st), flags); } +inline word mkdirat(int dir, const char* p, int mode) { return sys(SYS_mkdirat, dir, w(p), mode); } +inline word unlinkat(int dir, const char* p, int flags) { return sys(SYS_unlinkat, dir, w(p), flags); } +inline word renameat(int a, const char* x, int b, const char* y) { return sys(SYS_renameat, a, w(x), b, w(y)); } +inline word linkat(int a, const char* x, int b, const char* y, int flags) { return sys(SYS_linkat, a, w(x), b, w(y), flags); } +inline word symlinkat(const char* t, int dir, const char* p) { return sys(SYS_symlinkat, w(t), dir, w(p)); } +inline word readlinkat(int dir, const char* p, char* b, unsigned long n) { return sys(SYS_readlinkat, dir, w(p), w(b), w(n)); } +inline word utimensat(int dir, const char* p, const ktimespec* t, int flags) { return sys(SYS_utimensat, dir, w(p), w(t), flags); } +inline word futimens(int fd, const ktimespec* t) { return sys(SYS_futimens, fd, w(t)); } +inline word fchmodat(int dir, const char* p, int mode, int flags) { return sys(SYS_fchmodat, dir, w(p), mode, flags); } +inline word fcntl(int fd, int cmd, word arg = 0) { return sys(SYS_fcntl, fd, cmd, arg); } +inline word ftruncate(int fd, long long n) { return sys(SYS_ftruncate, fd, w(n)); } +inline word lseek(int fd, long long off, int whence) { return sys(SYS_lseek, fd, w(off), whence); } +inline word pread(int fd, void* b, unsigned long n, long long off) { return sys(SYS_pread, fd, w(b), w(n), w(off)); } +inline word pwrite(int fd, const void* b, unsigned long n, long long off) { return sys(SYS_pwrite, fd, w(b), w(n), w(off)); } +inline word fstatfs(int fd, kstatfs* s) { return sys(SYS_fstatfs, fd, w(s)); } +inline word getdirentries(int fd, char* b, unsigned long n, long long* base) { return sys(SYS_getdirentries, fd, w(b), w(n), w(base)); } +inline word mmap(void* a, unsigned long n, int prot, int flags, int fd, long long off) { return sys(SYS_mmap, w(a), w(n), prot, flags, fd, w(off)); } +inline word munmap(void* a, unsigned long n) { return sys(SYS_munmap, w(a), w(n)); } +inline word mprotect(void* a, unsigned long n, int prot) { return sys(SYS_mprotect, w(a), w(n), prot); } +inline word madvise(void* a, unsigned long n, int how) { return sys(SYS_madvise, w(a), w(n), how); } +inline word msync(void* a, unsigned long n, int flags) { return sys(SYS_msync, w(a), w(n), flags); } +inline word getpid() { return sys(SYS_getpid); } +inline word kill(int pid, int sig) { return sys(SYS_kill, pid, sig); } +inline word wait6(int idtype, long id, int* status, int options, kwrusage* ru) { + return sys(SYS_wait6, idtype, id, w(status), options, w(ru), 0); +} +inline word fexecve(int fd, char* const* av, char* const* ev) { return sys(SYS_fexecve, fd, w(av), w(ev)); } +inline word fchdir(int fd) { return sys(SYS_fchdir, fd); } +inline word dup2(int from, int to) { return sys(SYS_dup2, from, to); } +inline word pipe2(int fds[2], int flags) { return sys(SYS_pipe2, w(fds), flags); } +inline word sigaction(int sig, const ksigaction* a, ksigaction* old) { return sys(SYS_sigaction, sig, w(a), w(old)); } +inline word sigprocmask(int how, const ksigset* s, ksigset* old) { return sys(SYS_sigprocmask, how, w(s), w(old)); } +inline word nanosleep(const ktimespec* t, ktimespec* rest) { return sys(SYS_nanosleep, w(t), w(rest)); } +inline word clock_gettime(int clock, ktimespec* t) { return sys(SYS_clock_gettime, clock, w(t)); } +inline word clock_getres(int clock, ktimespec* t) { return sys(SYS_clock_getres, clock, w(t)); } +inline word getrandom(void* b, unsigned long n, unsigned flags) { return sys(SYS_getrandom, w(b), w(n), flags); } +inline word getrusage(int who, krusage* ru) { return sys(SYS_getrusage, who, w(ru)); } +inline word setpriority(int which, int who, int prio) { return sys(SYS_setpriority, which, who, prio); } +inline word sched_yield() { return sys(SYS_sched_yield); } +inline word ppoll(kpollfd* p, unsigned n, const ktimespec* t, const ksigset* m) { return sys(SYS_ppoll, w(p), w(n), w(t), w(m)); } +inline word ioctl(int fd, unsigned long req, void* arg) { return sys(SYS_ioctl, fd, w(req), w(arg)); } +inline word umtx(void* obj, int op, unsigned long val, void* a1, void* a2) { return sys(SYS__umtx_op, w(obj), op, w(val), w(a1), w(a2)); } +inline word flock(int fd, int how) { return sys(SYS_flock, fd, how); } +inline word getcwd(char* b, unsigned long n) { return sys(SYS___getcwd, w(b), w(n)); } +inline word setpgid(int pid, int pgid) { return sys(SYS_setpgid, pid, pgid); } +inline word sysctl(const int* name, unsigned n, void* old, unsigned long* oldlen, const void* nw = nullptr, unsigned long nwlen = 0) { + return sys(SYS___sysctl, w(name), n, w(old), w(oldlen), w(nw), w(nwlen)); +} +inline word sysctlbyname(const char* name, void* old, unsigned long* oldlen) { + unsigned long n = 0; while (name[n]) ++n; + return sys(SYS___sysctlbyname, w(name), w(n), w(old), w(oldlen), 0, 0); +} +inline word thr_self(long* id) { return sys(SYS_thr_self, w(id)); } +inline word thr_new(kthr_param* p) { return sys(SYS_thr_new, w(p), sizeof(kthr_param)); } +[[noreturn]] inline void thr_exit(long* state) { for (;;) sys(SYS_thr_exit, w(state)); } +[[noreturn]] inline void exit(int code) { for (;;) sys(SYS_exit, code); } + +// pdfork's two answers: the child is told by the second (1 there), and its +// first is then not zero. The descriptor of the started program is written to +// `*pd'; with PD_DAEMON, closing it does not end the program. +inline word pdfork(int* pd, int flags) { + const answer a = call6(SYS_pdfork, w(pd), flags); + if (failed(a.value)) return a.value; + return a.second ? 0 : a.value; +} + +} // namespace okf + +using namespace okf::abi; diff --git a/src/task.cpp b/src/task.cpp new file mode 100644 index 0000000..e18fcb7 --- /dev/null +++ b/src/task.cpp @@ -0,0 +1,219 @@ +// openkal.task: contexts, and a word waited upon through _umtx_op. +// +// TWO ARRANGEMENTS. Where the program carries a runtime that has received +// control and creates its threads (FreeBSD's libthr; the default), this +// implementation borrows both, as openkal-linux does above a C library: a +// context is the runtime's thread, and its stack is the one the runtime +// states. Where it carries none (feature `standalone'), a context is the +// kernel's thread (thr_new) on a stack and a thread-local block this +// implementation maps, and it ends with thr_exit, which sets a word and wakes +// its joiner. Every wait and wake is the kernel's own (_umtx_op on a private +// word), so a word is woken from a signal handler too. +#include "common.h" +#ifdef OKF_STANDALONE +#include "tls.h" +#else +#include +#include +#endif + +namespace { + +using okf::word; + +#ifndef OKF_STANDALONE +struct start_box { void (*entry)(void*); void* arg; }; + +void* trampoline(void* p) { + start_box b = *static_cast(p); + kal_free(p, sizeof(start_box), alignof(start_box)); + b.entry(b.arg); + return nullptr; +} +#else +// A CONTEXT OF THE KERNEL'S: its stack (above a guard), its thread-local +// block, and the word thr_exit sets when it ends. +constexpr unsigned long kGuard = 256u * 1024u; +struct context { + void (*entry)(void*); + void* arg; + void* map; unsigned long map_bytes; + void* stack; unsigned long stack_bytes; + okf::tls_block tls; + long ended; +}; +thread_local context* t_self = nullptr; + +void* allocate(unsigned long n, unsigned long a) { return kal_alloc(n, a); } + +void run(void* p) { + auto* c = static_cast(p); + t_self = c; + c->entry(c->arg); + okf::thr_exit(&c->ended); +} + +void release(context* c) { + if (c->map) okf::munmap(c->map, c->map_bytes); + if (c->tls.base) kal_free(c->tls.base, c->tls.bytes, c->tls.align); + kal_free(c, sizeof *c, alignof(context)); +} +#endif + +// THE SIZE A CONTEXT IS GIVEN WHEN IT ASKS FOR NONE: the host's own default, +// 8 MiB, which a request below it is raised to (kal_task_start_sized states a +// least size). +constexpr kal_uintptr kDefaultStack = 8u * 1024u * 1024u; + +// HOW MANY CONTEXTS WAIT, counted where they wait. The kernel's wake does not +// say how many it woke, and kal_task_wake reports it: each wait is counted in a +// bucket its word's address selects, and a wake reports at most that count. +unsigned g_waiting[64]; +unsigned* waiting_on(const kal_u32* w) { return &g_waiting[(reinterpret_cast(w) >> 2) % 64]; } + +} // namespace + +extern "C" { + +#ifdef OKF_STANDALONE +int kal_task_start_sized(void (*entry)(void*), void* arg, kal_uintptr size, kal_task* out) { + if (!entry || !out) return kal_err_invalid; + if (size > (kal_uintptr(1) << 46)) return kal_err_no_memory; + auto* c = static_cast(kal_alloc(sizeof(context), alignof(context))); + if (!c) return kal_err_no_memory; + okf::zero(c, sizeof *c); + c->entry = entry; c->arg = arg; + const kal_uintptr page = 4096; + const kal_uintptr want = size < kDefaultStack ? kDefaultStack : (size + page - 1) & ~(page - 1); + const okf::word m = okf::mmap(nullptr, want + kGuard, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON | MAP_STACK, -1, 0); + if (okf::failed(m)) { kal_free(c, sizeof *c, alignof(context)); return kal_err_no_memory; } + c->map = reinterpret_cast(m); c->map_bytes = want + kGuard; + okf::mprotect(c->map, kGuard, PROT_NONE); + c->stack = static_cast(c->map) + kGuard; c->stack_bytes = want; + c->tls = okf::make_tls(allocate); + if (!c->tls.tp) { release(c); return kal_err_no_memory; } + okf::kthr_param tp{}; + tp.start_func = run; tp.arg = c; + tp.stack_base = static_cast(c->stack); tp.stack_size = c->stack_bytes; + tp.tls_base = static_cast(c->tls.tp); tp.tls_size = c->tls.bytes; + long tid = 0; tp.child_tid = &tid; tp.parent_tid = &tid; + const okf::word r = okf::thr_new(&tp); + if (okf::failed(r)) { release(c); return r == -ENOMEM || r == -EAGAIN ? kal_err_no_memory : okf::fail(r); } + *out = kal_task{ reinterpret_cast(c) }; + return kal_ok; +} +#else +int kal_task_start_sized(void (*entry)(void*), void* arg, kal_uintptr size, kal_task* out) { + if (!entry || !out) return kal_err_invalid; + if (size > (kal_uintptr(1) << 46)) return kal_err_no_memory; + auto* b = static_cast(kal_alloc(sizeof(start_box), alignof(start_box))); + if (!b) return kal_err_no_memory; + b->entry = entry; b->arg = arg; + pthread_attr_t a; + pthread_attr_init(&a); + const kal_uintptr page = 4096; + kal_uintptr want = size < kDefaultStack ? kDefaultStack : (size + page - 1) & ~(page - 1); + pthread_attr_setstacksize(&a, want); + pthread_t t; + const int r = pthread_create(&t, &a, trampoline, b); + pthread_attr_destroy(&a); + if (r != 0) { + kal_free(b, sizeof(start_box), alignof(start_box)); + return (r == ENOMEM || r == EAGAIN) ? kal_err_no_memory : okf::translate(r); + } + *out = kal_task{ reinterpret_cast(t) }; + return kal_ok; +} +#endif +int kal_task_start(void (*entry)(void*), void* arg, kal_task* out) { return kal_task_start_sized(entry, arg, 0, out); } +#ifdef OKF_STANDALONE +int kal_task_join(kal_task t) { + if (t.h == 0) return kal_err_invalid; + auto* c = reinterpret_cast(t.h); + // thr_exit stores 1 and wakes the word as shared, so the wait is the shared one. + while (__atomic_load_n(&c->ended, __ATOMIC_ACQUIRE) == 0) + okf::umtx(&c->ended, UMTX_OP_WAIT, 0, nullptr, nullptr); + release(c); + return kal_ok; +} +#else +int kal_task_join(kal_task t) { + if (t.h == 0) return kal_err_invalid; + return pthread_join(reinterpret_cast(t.h), nullptr) == 0 ? kal_ok : kal_err_invalid; +} +#endif +void kal_task_yield(void) { okf::sched_yield(); } +// The context's identifier, the kernel's (thr_self). +kal_uintptr kal_task_current(void) { + long id = 0; + okf::sys(SYS_thr_self, okf::w(&id)); + return static_cast(id); +} +#ifdef OKF_STANDALONE +int kal_task_stack(void** base, kal_uintptr* size) { + if (!base || !size) return kal_err_invalid; + if (t_self) { *base = t_self->stack; *size = t_self->stack_bytes; return kal_ok; } + // The first context's stack, as the kernel states it: its top and the + // limit it may grow to (FreeBSD 14's auxiliary vector). + const unsigned long top = okf::auxval(AT_USRSTACKBASE), limit = okf::auxval(AT_USRSTACKLIM); + if (top == 0 || limit == 0) return kal_err_io; + *base = reinterpret_cast(top - limit); *size = limit; + return kal_ok; +} +#else +int kal_task_stack(void** base, kal_uintptr* size) { + if (!base || !size) return kal_err_invalid; + pthread_attr_t a; + pthread_attr_init(&a); + void* addr = nullptr; size_t sz = 0; + const bool ok = pthread_attr_get_np(pthread_self(), &a) == 0 && pthread_attr_getstack(&a, &addr, &sz) == 0; + pthread_attr_destroy(&a); + if (!ok) return kal_err_io; + *base = addr; *size = sz; + return kal_ok; +} +#endif +kal_uintptr kal_task_parallelism(void) { + int n = 0; unsigned long len = sizeof n; + return !okf::failed(okf::sysctlbyname("hw.ncpu", &n, &len)) && n > 0 ? static_cast(n) : 0; +} +kal_uintptr kal_task_cores(void) { return 0; } +int kal_task_set_priority(kal_uintptr pr) { + if (pr > KAL_TASK_PRIORITY_BACKGROUND) return kal_err_invalid; + const int nice_v = pr == KAL_TASK_PRIORITY_DEFAULT ? 0 : pr == KAL_TASK_PRIORITY_LOW ? 10 : 19; + const word r = okf::setpriority(PRIO_PROCESS, 0, nice_v); + return okf::failed(r) ? okf::fail(r) : kal_ok; +} +// THE KERNEL'S WAIT ON A WORD. It returns at once where the word no longer +// holds `expected'; a bound is relative, and its expiry is kal_err_again. +int kal_task_wait(const kal_u32* word_, kal_u32 expected, kal_u64 timeout_ns) { + if (!word_) return kal_err_invalid; + void* obj = const_cast(word_); + unsigned* count = waiting_on(word_); + __atomic_add_fetch(count, 1u, __ATOMIC_SEQ_CST); + word r; + if (timeout_ns == 0) { + r = okf::umtx(obj, UMTX_OP_WAIT_UINT_PRIVATE, expected, nullptr, nullptr); + } else { + okf::ktimespec t{ static_cast(timeout_ns / 1000000000ull), static_cast(timeout_ns % 1000000000ull) }; + r = okf::umtx(obj, UMTX_OP_WAIT_UINT_PRIVATE, expected, nullptr, &t); + } + __atomic_sub_fetch(count, 1u, __ATOMIC_SEQ_CST); + if (r == -ETIMEDOUT) return kal_err_again; + return kal_ok; // woken, changed, or interrupted: the caller re-examines +} +int kal_task_wake(const kal_u32* word_, kal_uintptr count, kal_uintptr* woken) { + if (!word_) return kal_err_invalid; + const int n = count > 0x7fffffff ? 0x7fffffff : static_cast(count); + const unsigned waiting = __atomic_load_n(waiting_on(word_), __ATOMIC_SEQ_CST); + const word r = n ? okf::umtx(const_cast(word_), UMTX_OP_WAKE_PRIVATE, static_cast(n), nullptr, nullptr) : 0; + if (okf::failed(r)) return okf::fail(r); + if (woken) *woken = count < waiting ? count : waiting; + return kal_ok; +} +kal_uintptr kal_task_props(void) { + return KAL_TASK_PROP_PREEMPTIVE | KAL_TASK_PROP_PARALLEL | KAL_TASK_PROP_WAIT_TIMEOUT + | KAL_TASK_PROP_THREAD_LOCAL | KAL_TASK_PROP_PRIORITY; +} + +} // extern "C" diff --git a/src/terminal_timeout.cpp b/src/terminal_timeout.cpp new file mode 100644 index 0000000..a2ac574 --- /dev/null +++ b/src/terminal_timeout.cpp @@ -0,0 +1,102 @@ +// openkal.terminal (the tty's ioctls) and openkal.timeout (ppoll, and a +// program awaited with a bound). +#include "common.h" + +namespace { +using okf::word; + +int ready(int fd, short ev, kal_u64 ns) { + okf::kpollfd p{ fd, ev, 0 }; + okf::ktimespec t{ static_cast(ns / 1000000000ull), static_cast(ns % 1000000000ull) }; + for (;;) { + const word r = okf::ppoll(&p, 1, ns == 0 ? nullptr : &t, nullptr); + if (r == -EINTR) continue; + if (okf::failed(r)) return okf::fail(r); + return r > 0 ? kal_ok : kal_err_again; + } +} +} // namespace + +extern "C" { + +// ================================================================ terminal +int kal_terminal_get_mode(kal_stream s, kal_uintptr* mode) { + if (!mode) return kal_err_invalid; + okf::ktermios t{}; + const word r = okf::ioctl(okf::unpack(s.h), TIOCGETA, &t); + if (okf::failed(r)) return r == -ENOTTY ? kal_err_not_supported : okf::fail(r); + kal_uintptr m = KAL_TERM_SEQUENCES; + if (t.c_lflag & ICANON) m |= KAL_TERM_LINE_EDIT; + if (t.c_lflag & ECHO) m |= KAL_TERM_ECHO; + if (!(t.c_lflag & ISIG) && !(t.c_iflag & IXON)) m |= KAL_TERM_PASS_CONTROL; + if (!(t.c_iflag & (ICRNL | INLCR | IGNCR | ISTRIP))) m |= KAL_TERM_PASS_INPUT; + *mode = m; + return kal_ok; +} +int kal_terminal_set_mode(kal_stream s, kal_uintptr m) { + const int fd = okf::unpack(s.h); + okf::ktermios t{}; + const word r = okf::ioctl(fd, TIOCGETA, &t); + if (okf::failed(r)) return r == -ENOTTY ? kal_err_not_supported : okf::fail(r); + auto setb = [](okf::u32& w, okf::u32 b, bool on) { w = on ? (w | b) : (w & ~b); }; + setb(t.c_lflag, ICANON, m & KAL_TERM_LINE_EDIT); + setb(t.c_lflag, ECHO, m & KAL_TERM_ECHO); + setb(t.c_lflag, ISIG | IEXTEN, !(m & KAL_TERM_PASS_CONTROL)); + setb(t.c_iflag, IXON, !(m & KAL_TERM_PASS_CONTROL)); + setb(t.c_iflag, ICRNL | INLCR | IGNCR | ISTRIP, !(m & KAL_TERM_PASS_INPUT)); + if (!(m & KAL_TERM_LINE_EDIT)) { t.c_cc[VMIN] = 1; t.c_cc[VTIME] = 0; } + const word w = okf::ioctl(fd, TIOCSETAF, &t); + return okf::failed(w) ? okf::fail(w) : kal_ok; +} +int kal_terminal_size(kal_stream s, kal_uintptr* cols, kal_uintptr* rows) { + okf::kwinsize w{}; + if (okf::failed(okf::ioctl(okf::unpack(s.h), TIOCGWINSZ, &w)) || w.ws_col == 0) return kal_err_not_supported; + if (cols) *cols = w.ws_col; + if (rows) *rows = w.ws_row; + return kal_ok; +} +// The mode where the stream is a terminal; its size where the terminal states +// one (a serial line states none). +kal_uintptr kal_terminal_props(kal_stream s) { + const int fd = okf::unpack(s.h); + okf::ktermios t{}; + if (fd < 0 || okf::failed(okf::ioctl(fd, TIOCGETA, &t))) return 0; + okf::kwinsize w{}; + const bool sized = !okf::failed(okf::ioctl(fd, TIOCGWINSZ, &w)) && w.ws_col != 0; + return KAL_TERM_PROP_MODE | (sized ? KAL_TERM_PROP_SIZE : 0); +} + +// ================================================================ timeout +kal_intptr kal_timeout_read(kal_stream s, void* buf, kal_uintptr len, kal_u64 ns) { + const int r = ready(okf::unpack(s.h), POLLIN, ns); + if (r != kal_ok) return -static_cast(r); + return kal_stream_read(s, buf, len); +} +kal_intptr kal_timeout_write(kal_stream s, const void* buf, kal_uintptr len, kal_u64 ns) { + const int r = ready(okf::unpack(s.h), POLLOUT, ns); + if (r != kal_ok) return -static_cast(r); + return kal_stream_write(s, buf, len); +} +int kal_timeout_wait_process(kal_process p, kal_u64 ns, int* status, int* terminated) { + int pid = 0; + const int e = okf::started_pid(p, &pid); + if (e != kal_ok) return e; + const kal_u64 end = kal_time_monotonic() + ns; + for (;;) { + bool done = false; + const int r = okf::reap_program(pid, WNOHANG, status, terminated, &done); + if (r != kal_ok || done) return r; + if (ns != 0 && kal_time_monotonic() >= end) return kal_err_again; + kal_time_sleep(1000000); // a millisecond, the granularity stated below + } +} +kal_u64 kal_timeout_granularity(void) { return 1000000; } +// The network's resources are not provided (openkal.net and openkal.datagram +// are absent), and timeout.h states that the operations of a resource type an +// implementation withholds report kal_err_not_supported. +int kal_timeout_accept(kal_net_listener, kal_u64, kal_net_conn*) { return kal_err_not_supported; } +kal_intptr kal_timeout_recv_from(kal_datagram, void*, kal_uintptr, kal_endpoint*, kal_u64) { + return -kal_err_not_supported; +} + +} // extern "C" diff --git a/src/tls.h b/src/tls.h new file mode 100644 index 0000000..15fb5e3 --- /dev/null +++ b/src/tls.h @@ -0,0 +1,77 @@ +// THE THREAD-LOCAL STORAGE OF A PROGRAM THAT HAS NO RUNTIME, in the standalone +// arrangement: the image's PT_TLS segment, laid out as FreeBSD lays it out +// (variant II on amd64: the thread pointer at the end of the block, pointing +// to itself; variant I on arm64: a 16-byte control block, then the block), and +// the thread pointer set (sysarch AMD64_SET_FSBASE; tpidr_el0). +#pragma once +#include "sys.h" + +namespace okf { + +unsigned long auxval(unsigned long key); + +struct tls_image { const unsigned char* data; unsigned long filesz, memsz, align; bool known; }; +inline tls_image& image() { static tls_image i{}; return i; } + +inline unsigned long round_up(unsigned long n, unsigned long to) { return (n + to - 1) & ~(to - 1); } + +inline void describe_tls() { + auto& im = image(); + if (im.known) return; + im.known = true; + struct elf_phdr { u32 type, flags; u64 offset, vaddr, paddr, filesz, memsz, align; }; + const unsigned long phdr = auxval(AT_PHDR), phent = auxval(AT_PHENT), phnum = auxval(AT_PHNUM); + if (phdr == 0 || phnum == 0) return; + for (unsigned long i = 0; i < phnum; ++i) { + const auto* p = reinterpret_cast(phdr + i * phent); + if (p->type != 7 /* PT_TLS */) continue; + im.data = reinterpret_cast(p->vaddr); + im.filesz = p->filesz; im.memsz = p->memsz; im.align = p->align; + return; + } +} + +struct tls_block { void* base; unsigned long bytes; unsigned long align; void* tp; }; + +inline tls_block make_tls(void* (*alloc)(unsigned long, unsigned long)) { + describe_tls(); + const auto& im = image(); + const unsigned long laid_out = im.align ? im.align : 1; + const unsigned long align = laid_out < 16 ? 16 : laid_out; + tls_block b{}; + b.align = align; +#if defined(__x86_64__) + // The thread pointer is the address of a word holding itself, whether or + // not the image has a segment: every amd64 program reads %fs:0. + const unsigned long size = round_up(im.memsz, laid_out); + b.bytes = size + 64; + b.base = alloc(b.bytes, align); + if (!b.base) return b; + auto* base = static_cast(b.base); + for (unsigned long i = 0; i < b.bytes; ++i) base[i] = 0; + if (im.data && im.filesz) for (unsigned long i = 0; i < im.filesz; ++i) base[i] = im.data[i]; + b.tp = base + size; + *reinterpret_cast(b.tp) = b.tp; +#elif defined(__aarch64__) + const unsigned long gap = round_up(16, align); + b.bytes = gap + round_up(im.memsz, laid_out) + 64; + b.base = alloc(b.bytes, align); + if (!b.base) return b; + auto* base = static_cast(b.base); + for (unsigned long i = 0; i < b.bytes; ++i) base[i] = 0; + if (im.data && im.filesz) for (unsigned long i = 0; i < im.filesz; ++i) base[gap + i] = im.data[i]; + b.tp = base; +#endif + return b; +} + +inline void set_thread_pointer(void* tp) { +#if defined(__x86_64__) + void* base = tp; + sys(SYS_sysarch, AMD64_SET_FSBASE, w(&base)); +#elif defined(__aarch64__) + __asm__ __volatile__("msr tpidr_el0, %0" :: "r"(tp)); +#endif +} + +} // namespace okf diff --git a/tools/check-standalone.sh b/tools/check-standalone.sh new file mode 100755 index 0000000..e6820b6 --- /dev/null +++ b/tools/check-standalone.sh @@ -0,0 +1,17 @@ +#!/usr/bin/env bash +# The standalone arrangement compiles for every architecture this +# implementation states, from its own sources and openkal's headers alone: no +# system header, no C library, `-ffreestanding'. The objects carry the brand. +# +# check-standalone.sh +set -euo pipefail +cxx="${1:?clang++}"; inc="${2:?the include directory of openkal}" +here="$(cd "$(dirname "$0")/.." && pwd)" +out="$(mktemp -d)"; trap 'rm -rf "$out"' EXIT +for arch in x86_64 aarch64; do + for f in "$here"/src/*.cpp; do + "$cxx" --no-default-config --target="$arch-unknown-freebsd14.0" -std=c++23 -ffreestanding -nostdinc++ \ + -fno-exceptions -fno-rtti -DOKF_STANDALONE -I "$inc" -c "$f" -o "$out/$arch-$(basename "$f" .cpp).o" + done + echo " ok standalone, $arch: every source compiles without a system header" +done diff --git a/tools/defer-runner b/tools/defer-runner new file mode 100755 index 0000000..3eae291 --- /dev/null +++ b/tools/defer-runner @@ -0,0 +1,24 @@ +#!/bin/sh +# The conformance suite's runner where the suite is built on one machine and +# run on another: the program and the variables it is to be run with are put in +# OKF_DEFERRED (a directory), with run.sh, which runs it there and answers as it +# does. CI builds on Linux and runs run.sh in a FreeBSD virtual machine. +set -eu +out="${OKF_DEFERRED:?the directory the run is put in}" +mkdir -p "$out" +cp "$1" "$out/" +program="./$(basename "$1")" +shift +{ + echo '#!/bin/sh' + echo 'cd "$(dirname "$0")"' + env | grep -E '^(OPENKAL|KAL)_[A-Z0-9_]*=' | while IFS= read -r line; do + name=${line%%=*}; value=${line#*=} + printf "export %s='%s'\n" "$name" "$(printf '%s' "$value" | sed "s/'/'\\\\''/g")" + done + printf 'exec %s' "$program" + for a in "$@"; do printf " '%s'" "$(printf '%s' "$a" | sed "s/'/'\\\\''/g")"; done + echo +} > "$out/run.sh" +chmod +x "$out/run.sh" "$out/$(basename "$program")" +echo "--- the suite is put in $out, to be run there by run.sh ---" diff --git a/tools/run-conformance-standalone.sh b/tools/run-conformance-standalone.sh new file mode 100755 index 0000000..894b672 --- /dev/null +++ b/tools/run-conformance-standalone.sh @@ -0,0 +1,61 @@ +#!/usr/bin/env bash +# THE CONFORMANCE SUITE IN THE STANDALONE ARRANGEMENT: built for +# `-freebsd-musl', openkal-musl over this implementation with the feature +# `standalone', from working trees --- the specification, openkal-musl, and +# this repository --- so that a change spanning them is examined as written. +# +# run-conformance-standalone.sh [] +# +# The suite is C++: its C++ runtime is openkal-llvm-runtime, built over +# openkal-musl for the row, from the given tree or else the index. +# +# The suite's runner is OPENKAL_CONFORMANCE_RUNNER, as for run-conformance.sh. +# The copies made here are in a temporary directory; nothing committed is +# rewritten. +set -euo pipefail +spec="$(cd "${1:?the specification}" && pwd)" +musl="$(cd "${2:?openkal-musl}" && pwd)" +arch="${3:?the architecture}" +features="${4:?the suite features}" +runtime="${5:-}" +[ -z "$runtime" ] || runtime="$(cd "$runtime" && pwd)" +here="$(cd "$(dirname "$0")/.." && pwd)" +work="$(mktemp -d)"; trap 'rm -rf "$work"' EXIT +cp -R "$spec/conformance" "$work/conformance" +cp -R "$musl" "$work/musl" +rm -rf "$work/conformance/target" "$work/musl/target" + +# openkal-musl names this implementation, standalone, by its tree, and the +# specification by its tree. +m="$work/musl/mcpp.toml" +sed -i -E "s|^openkal = .*$|openkal = { path = \"$spec\" }|" "$m" +if grep -qE '^openkal-freebsd = ' "$m"; then + sed -i -E "s|^openkal-freebsd = \\{ version = \"[^\"]*\"(.*)$|openkal-freebsd = { path = \"$here\"\\1|" "$m" +else + printf '\n[target.'"'"'cfg(os = "freebsd")'"'"'.dependencies]\nopenkal-freebsd = { path = "%s", features = ["standalone"] }\n\n[target.'"'"'cfg(os = "freebsd")'"'"'.build]\nldflags = ["-nostdlib", "-static", "-Wl,--no-dynamic-linker", "-Wl,--gc-sections", "-Lport/lib"]\n' "$here" >> "$m" +fi +grep -q "openkal-freebsd = { path = \"$here\"" "$m" || { echo "openkal-musl does not name this tree" >&2; exit 1; } + +# The suite: the specification by its tree, openkal-musl as its C library, +# and the row opted into with the toolchain the platform rows use. +c="$work/conformance/mcpp.toml" +sed -i -E "s|^openkal = .*$|openkal = { path = \"$spec\" }\nopenkal-musl = { path = \"$work/musl\" }|" "$c" +printf '\n[target.%s-freebsd-musl]\ntoolchain = "llvm@23.1.3"\n' "$arch" >> "$c" +if [ -n "$runtime" ]; then + rt="$work/runtime" + cp -R "$runtime" "$rt"; rm -rf "$rt/target" "$rt/.git" + sed -i -E "s|^openkal-musl = \\{ version = \"[^\"]*\"|openkal-musl = { path = \"$work/musl\"|" "$rt/mcpp.toml" + grep -q "openkal-musl = { path = \"$work/musl\"" "$rt/mcpp.toml" || { echo "openkal-llvm-runtime does not name this openkal-musl" >&2; exit 1; } + printf '\n[target.'"'"'cfg(os = "freebsd")'"'"'.dependencies]\nopenkal-llvm-runtime = { path = "%s" }\n' "$rt" >> "$c" +else + printf '\n[target.'"'"'cfg(os = "freebsd")'"'"'.dependencies]\nopenkal-llvm-runtime = "^0.20.1"\n' >> "$c" +fi + +cd "$work/conformance" +mcpp build --target "$arch-freebsd-musl" --features "$features" +binary="$(find target -type f -name openkal-conformance | head -1)" +[ -n "$binary" ] || { echo "the suite was not produced" >&2; exit 2; } +file "$binary" || true +export OPENKAL_CONFORMANCE_EMPTY="" +export OPENKAL_CONFORMANCE_PROVIDES="$(awk '/^[[:space:]]*provides-interfaces[[:space:]]*=/ {on=1} on {line=line $0; if ($0 ~ /\]/) on=0} END {gsub(/.*\[/,"",line); gsub(/\].*/,"",line); gsub(/[" \t]/,"",line); gsub(/,/,";",line); print line}' "$here/mcpp.toml")" +${OPENKAL_CONFORMANCE_RUNNER:-} "./$binary" diff --git a/tools/vm-run.py b/tools/vm-run.py new file mode 100755 index 0000000..c506fcd --- /dev/null +++ b/tools/vm-run.py @@ -0,0 +1,149 @@ +#!/usr/bin/env python3 +"""Runs a program in a FreeBSD virtual machine and answers as the program did. + + vm-run.py --image [--file ]... -- [arguments...] + +The machine is booted under qemu/KVM from (an overlay of FreeBSD's own +VM image; nothing is written to its backing file), the files are offered to it +on a read-only FAT drive and copied to /tmp/run, and the program is run there as +root on the serial console. What it writes is written here, and its status is +this program's status. Every argument is passed through single quotes, so the +command line is the one given. + +THE PROGRAM'S ENVIRONMENT. Variables named OPENKAL_* and KAL_* are passed to +the program. Where a test runner speaks the mtest protocol to it (MTEST_PROTOCOL +and MTEST_REPORT set), the program writes its events into a file of the machine, +which is appended to MTEST_REPORT here when it ends; MTEST_MODE and MTEST_SELECT +(a file, offered to the machine) are passed as well. + +It is how this repository's conformance suite is run on FreeBSD from a Linux +host: the suite's runner (OPENKAL_CONFORMANCE_RUNNER). The +image's kernel console is moved to the serial line at the loader prompt, as the +official image boots to the video console. +""" +import argparse, os, pty, re, select, shutil, subprocess, sys, tempfile, time + +ap = argparse.ArgumentParser() +ap.add_argument("--image", required=True) +ap.add_argument("--file", action="append", default=[]) +ap.add_argument("--memory", default="2048") +ap.add_argument("--timeout", type=int, default=1800) +ap.add_argument("command", nargs=argparse.REMAINDER) +args = ap.parse_args() +command = args.command[1:] if args.command[:1] == ["--"] else args.command +if not command: + ap.error("no program to run") + +share = tempfile.mkdtemp(prefix="vm-run-") +forward = {k: v for k, v in os.environ.items() if k.startswith("OPENKAL_") or k.startswith("KAL_")} +report = os.environ.get("MTEST_REPORT") if os.environ.get("MTEST_PROTOCOL") else None +if report: + forward["MTEST_PROTOCOL"] = os.environ["MTEST_PROTOCOL"] + forward["MTEST_REPORT"] = "/tmp/run/vm-run-report.ndjson" + if os.environ.get("MTEST_MODE"): + forward["MTEST_MODE"] = os.environ["MTEST_MODE"] + if os.environ.get("MTEST_SELECT") and os.path.isfile(os.environ["MTEST_SELECT"]): + shutil.copy2(os.environ["MTEST_SELECT"], os.path.join(share, "vm-run-select.txt")) + forward["MTEST_SELECT"] = "/tmp/run/vm-run-select.txt" +for f in args.file + [command[0]]: + if os.path.isfile(f): + shutil.copy2(f, os.path.join(share, os.path.basename(f))) +# A program offered from here is run from the copy; one named otherwise is the +# machine's own (`/usr/bin/lldb'). +program = "./" + os.path.basename(command[0]) if os.path.isfile(command[0]) else command[0] + +def quote(a): + return "'" + a.replace("'", "'\\''") + "'" + +line = " ".join(["env"] + [quote(f"{k}={v}") for k, v in sorted(forward.items())] + + [quote(program)] + [quote(a) for a in command[1:]]) +argv = ["qemu-system-x86_64", "-enable-kvm", "-m", args.memory, "-smp", "2", "-nographic", + "-drive", f"file={args.image},if=virtio,format=qcow2", + "-drive", f"file=fat:{share},if=virtio,format=raw,readonly=on", + "-nic", "none"] +pid, fd = pty.fork() +if pid == 0: + os.execvp(argv[0], argv) + +buf = b"" +def read_until(pattern, timeout): + global buf + end = time.time() + timeout + rx = re.compile(pattern) + while time.time() < end: + r, _, _ = select.select([fd], [], [], 0.2) + if r: + try: + chunk = os.read(fd, 65536) + except OSError: + return None + buf += chunk + m = rx.search(buf) + if m: + return m + return None + +def send(text): + for ch in text: + os.write(fd, ch.encode()) + time.sleep(0.02) + +def stop(status): + try: + os.kill(pid, 9) + except OSError: + pass + shutil.rmtree(share, ignore_errors=True) + sys.exit(status) + +if read_until(rb"Autoboot in", 180): + send(" "); time.sleep(0.5); send("3") + read_until(rb"OK ", 30); time.sleep(1); buf = b"" + send("set console=comconsole\r"); read_until(rb"OK ", 10); time.sleep(1); buf = b"" + send("boot\r") +if not read_until(rb"login: ", 300): + send("\r") + if not read_until(rb"login: ", 60): + sys.stderr.write("vm-run: the machine gave no login prompt\n") + stop(125) +buf = b"" +send("root\r") +read_until(rb"(# |Password:)", 30) +if b"Password:" in buf: + send("\r"); read_until(rb"# ", 30) + +# The program's output between two marks, and its status after the second. +buf = b"" +send("sh\r"); read_until(rb"\$ |# ", 10) +buf = b"" +send("mkdir -p /mnt/s /tmp/run && mount -t msdosfs -o ro /dev/vtbd1s1 /mnt/s && cp /mnt/s/* /tmp/run/ " + "&& chmod +x /tmp/run/* && cd /tmp/run && stty -echo; echo @@VM-RUN-BEGIN@@; " + + line + "; rc=$?; echo; echo @@VM-RUN-STATUS=$rc@@\r") +m = read_until(rb"@@VM-RUN-STATUS=(\d+)@@", args.timeout) +if not m: + sys.stderr.write("vm-run: the program did not finish within the time allowed\n") + stop(124) +text = buf.decode(errors="replace").replace("\r", "") +begin = text.rfind("@@VM-RUN-BEGIN@@\n") +body = text[begin + len("@@VM-RUN-BEGIN@@\n"):text.rfind("\n@@VM-RUN-STATUS=")] if begin >= 0 else text +# `echo' after the program added one line feed, so that the mark begins a line. +if body.endswith("\n"): + body = body[:-1] +sys.stdout.write(body + ("\n" if body and not body.endswith("\n") else "")) +sys.stdout.flush() +status = int(m.group(1)) +if report: + buf = b"" + send("echo @@VM-RUN-REPORT@@; cat /tmp/run/vm-run-report.ndjson 2>/dev/null; echo; echo @@VM-RUN-REPORT-END@@\r") + if read_until(rb"@@VM-RUN-REPORT-END@@\r?\n", 120): + text = buf.decode(errors="replace").replace("\r", "") + a = text.find("@@VM-RUN-REPORT@@\n") + b = text.rfind("@@VM-RUN-REPORT-END@@") + if a >= 0 and b > a: + events = [l for l in text[a + len("@@VM-RUN-REPORT@@\n"):b].split("\n") if l.startswith("{")] + with open(report, "a") as out: + for l in events: + out.write(l + "\n") +send("shutdown -p now\r") +read_until(rb"Uptime:", 60) +stop(status) diff --git a/tools/vm-runner b/tools/vm-runner new file mode 100755 index 0000000..8118188 --- /dev/null +++ b/tools/vm-runner @@ -0,0 +1,4 @@ +#!/bin/sh +# The conformance suite's runner on a Linux host: the program, run in the +# FreeBSD virtual machine whose disk OKF_VM_IMAGE names (tools/vm-run.py). +exec python3 "$(dirname "$0")/vm-run.py" --image "${OKF_VM_IMAGE:?the FreeBSD disk image}" -- "$@"