Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,10 @@ jobs:

- name: This package's own tests
run: |
# The run's own status decides as well: through `tee' without
# pipefail a program that failed to compile in a subdirectory of
# tests/ passed the step, since the list below names tests/*.cpp only.
set -o pipefail
mcpp test 2>&1 | tee tests.log
# A suite that discovered nothing reports success, so every suite is
# asserted to have run. The list is derived from the files present
Expand Down Expand Up @@ -468,15 +472,22 @@ jobs:
rm -f "$RUNNER_TEMP/android.bad"
echo "both Android ABIs build from this implementation unchanged, and name no C library symbol"

# A THIRTY-TWO BIT BUILD RUNS (openkal 0.25.1, clause 5.5). The same
# sources, standalone, for i386: built with the toolchain's clang and lld
# and RUN here, since an x86_64 kernel executes a static i386 program. A
# file past four gibibytes, a task with its own thread-local storage and a
# sleep through the sixty-four bit clock; the conformance suite runs for
# i686 where the family's C library and C++ runtime are built for it.
- name: An i386 build of this implementation runs
# THE ARCHITECTURES THIS MACHINE DOES NOT RUN NATIVELY RUN (tools/arch-check.sh).
# The same sources, standalone, built with the toolchain's clang and lld:
# i386 (openkal 0.25.1, clause 5.5) runs here, since an x86_64 kernel
# executes a static i386 program; riscv64 runs under qemu's user-mode
# translator. Each architecture's gate and numbers, the kernel's records, the
# thread pointer, the clock, a handler's record with the frame chain, and
# published instructions; the conformance suite runs for these rows where
# the family's C library and C++ runtime are built for them.
- name: The i386 and riscv64 builds of this implementation run
if: matrix.toolchain == 'llvm@22.1.8'
run: OPENKAL_INCLUDE="$PWD/.spec/include" bash tools/word32-check.sh
run: |
set -euo pipefail
sudo apt-get install -y -qq qemu-user > /dev/null
for arch in i386 riscv64; do
OPENKAL_INCLUDE="$PWD/.spec/include" bash tools/arch-check.sh "$arch"
done

# A checker is only useful if it fails when it should.
- name: The independence check detects a dependence
Expand Down
32 changes: 31 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,39 @@ for Linux, written on the kernel's own system-call interface.
openkal = "0.26.0"

[target.'cfg(os = "linux")'.dependencies]
openkal-linux = "0.27.0"
openkal-linux = "0.27.1"
```

## What 0.27.1 adds

**riscv64.** The kernel's riscv64 gate (`ecall`, the number in `a7`, the arguments in
`a0` to `a5`) beside aarch64's, over the one table of numbers both take from the
kernel's architecture-independent header; riscv64 has no `renameat`, and a rename
that replaces is `renameat2` without flags there (`okl::renameat`). The thread pointer
is `tp`, the storage beginning at it (variant I without a gap); `_start` sets the
global pointer before anything else; `clone` is the generic order. Its kernel's
records: the `O_DIRECTORY` and `O_NOFOLLOW` of the architecture-independent header,
which x86_64 and i386 take as well, and a handler's record without a restorer (the
kernel returns through its vDSO's `__vdso_rt_sigreturn`), which every unit builds
through `okl::handler_action`; the clock is the vDSO's `__vdso_clock_gettime`. A
sample and a fault report read the context's `pc`, `sp` and `s0`, and the frame chain
from the two words below each frame pointer. Published instructions are made visible
to the instruction path through `riscv_flush_icache`, the maintenance the kernel
offers as a call of its own (`kal_exec_publish`, `kal_exec_publish_part`).

**A frame chain without `process_vm_readv`.** A kernel built without
CROSS_MEMORY_ATTACH, and qemu's user-mode translator (measured with 8.2.2), answer
ENOSYS, and every sample and fault report then ended at its first frame. The words
of a frame pass through a pipe of the read's own instead, whose write answers EFAULT
for an address that cannot be read.

`tools/arch-check.sh` (formerly `tools/word32-check.sh`) builds the implementation
standalone for i386 and riscv64 and runs `tests/arch/runs.cpp`, i386 on the x86_64
machine and riscv64 under `qemu-riscv64`: a file past four gibibytes, a node's kind
and size, a directory made, opened and listed, a rename into it, a task with its own
thread-local storage, the clock, the processor time, a sample holding the frames
above the work, and published instructions run.

## What 0.27 changes (openkal 0.26)

**A start is bounded by the kernel alone** (clause 7.6). An argument vector or an
Expand Down
8 changes: 4 additions & 4 deletions mcpp.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
namespace = "mcpplibs"
name = "openkal-linux"
version = "0.27.0"
version = "0.27.1"
description = "The reference implementation of openkal for Linux, written on the kernel's own system-call interface so that it can be placed beneath a C library as well as above one."
license = "Apache-2.0"

Expand Down Expand Up @@ -95,10 +95,10 @@ flags = [
"-fno-asynchronous-unwind-tables"] },
]

# tests/word32/ is built standalone for i386 by tools/word32-check.sh, with the C library facilities
# it supplies itself; the host's test programs are the others.
# tests/arch/ is built standalone for i386 and riscv64 by tools/arch-check.sh, with the C library
# facilities it supplies itself; the host's test programs are the others.
[test]
discover = ["tests/**/*.cpp", "!tests/word32/**"]
discover = ["tests/**/*.cpp", "!tests/arch/**"]

[features]
default = []
Expand Down
3 changes: 2 additions & 1 deletion src/env.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ bool recover(char*** argv_out, int* argc_out, char*** envp_out) {
// started program the disposition this one was started with (process.cpp).
[[gnu::constructor(101)]] void quiet_the_signal_openkal_cannot_report() {
using okl::k_sigaction;
k_sigaction ignore{ reinterpret_cast<void*>(1) /* SIG_IGN */, 0, nullptr, 0 };
k_sigaction ignore{};
ignore.handler = reinterpret_cast<void*>(1); // SIG_IGN
k_sigaction found{};
if (!okl::failed(okl::sys(okl::nr_rt_sigaction, 13 /* SIGPIPE */,
reinterpret_cast<okl_long>(&ignore),
Expand Down
18 changes: 17 additions & 1 deletion src/exec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ bool branch_targets() {
return false;
#endif
}
// The instruction path made to observe the data path's writes, where the
// environment offers it as a call of its own (kal_exec_publish).
void sync_instructions(void* at, okl_uptr bytes) {
#if defined(__riscv)
const auto from = reinterpret_cast<okl_long>(at);
okl::sys(okl::nr_riscv_flush_icache, from, from + static_cast<okl_long>(bytes), 0);
#else
(void)at; (void)bytes;
#endif
}
} // namespace

extern "C" {
Expand Down Expand Up @@ -136,6 +146,10 @@ int kal_exec_publish(void* p, kal_uintptr size) {
//
// All three agree about what the PROGRAM must do, which is what the
// specification actually states.
//
// riscv64 is the case the rule names: its kernel offers the maintenance as
// a system call (`riscv_flush_icache'), so it is performed there.
sync_instructions(p, bytes);
return kal_ok;
}

Expand All @@ -150,7 +164,9 @@ int kal_exec_publish_part(void* region, kal_uintptr offset, kal_uintptr size) {
const okl_long r = okl::sys(okl::nr_mprotect,
reinterpret_cast<okl_long>(static_cast<char*>(region) + offset),
static_cast<okl_long>(size), okl::prot_read | okl::prot_exec);
return okl::failed(r) ? okl::translate(r) : kal_ok;
if (okl::failed(r)) return okl::translate(r);
sync_instructions(static_cast<char*>(region) + offset, size);
return kal_ok;
}

// SEALED READ-ONLY (0.23): the protection, and, for ever, `mseal' (Linux 6.10)
Expand Down
3 changes: 1 addition & 2 deletions src/fs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -826,8 +826,7 @@ int kal_fs_rename(kal_dir from, const char* a, kal_uintptr alen,
okl::terminated ta(a, alen), tb(b, blen);
if (!ta.ok || !tb.ok) return kal_err_invalid;
if (refused_write(f) || refused_write(t2)) return kal_err_permission;
const okl_long r = okl::sys(okl::nr_renameat, f, reinterpret_cast<okl_long>(ta.buf),
t2, reinterpret_cast<okl_long>(tb.buf));
const okl_long r = okl::renameat(f, ta.buf, t2, tb.buf);
if (okl::failed(r)) return okl::translate(r);
wrote(f, a, alen);
wrote(t2, b, blen);
Expand Down
58 changes: 46 additions & 12 deletions src/observe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,28 @@ bool peek(okl_uptr at, void* out, okl_uptr n) {
iov local{ out, n };
iov remote{ reinterpret_cast<void*>(at), n };
static okl_long self = 0;
static int readv_absent = 0;
if (self == 0) self = okl::sys(okl::nr_getpid);
const okl_long r = okl::sys(okl::nr_process_vm_readv, self, reinterpret_cast<okl_long>(&local), 1,
reinterpret_cast<okl_long>(&remote), 1, 0);
return r == static_cast<okl_long>(n);
if (!__atomic_load_n(&readv_absent, __ATOMIC_RELAXED)) {
const okl_long r = okl::sys(okl::nr_process_vm_readv, self, reinterpret_cast<okl_long>(&local), 1,
reinterpret_cast<okl_long>(&remote), 1, 0);
if (r != -okl::e_nosys) return r == static_cast<okl_long>(n);
__atomic_store_n(&readv_absent, 1, __ATOMIC_RELAXED);
}
// WITHOUT `process_vm_readv' (a kernel built without CROSS_MEMORY_ATTACH;
// qemu's user-mode translator answers ENOSYS, measured with 8.2.2 on
// riscv64, and every chain then ended at its first frame) the bytes pass
// through a pipe of this call's own: its write answers EFAULT for an address
// that cannot be read, and no other context shares it.
int fds[2] = { -1, -1 };
if (okl::failed(okl::sys(okl::nr_pipe2, reinterpret_cast<okl_long>(fds), okl::o_cloexec))) return false;
const bool ok = okl::sys(okl::nr_write, fds[1], static_cast<okl_long>(at), static_cast<okl_long>(n))
== static_cast<okl_long>(n)
&& okl::sys(okl::nr_read, fds[0], reinterpret_cast<okl_long>(out), static_cast<okl_long>(n))
== static_cast<okl_long>(n);
okl::sys(okl::nr_close, fds[0]);
okl::sys(okl::nr_close, fds[1]);
return ok;
}

// --- writing without a C library ------------------------------------------
Expand Down Expand Up @@ -244,10 +262,27 @@ frame_regs from_context(void* uc) {
okl::copy(&r.fp, b + mc + 8 + 29 * 8, 8);
okl::copy(&r.sp, b + mc + 8 + 31 * 8, 8);
okl::copy(&r.pc, b + mc + 8 + 32 * 8, 8);
#elif defined(__riscv)
// flags, link, stack (24), the signal mask with its reserve (128), then
// the sigcontext at a sixteen-byte boundary (its floating-point state is
// aligned so): pc, then x1..x31 --- ra, sp at x2, the frame pointer s0 at x8.
constexpr okl_uptr mc = 176;
okl::copy(&r.pc, b + mc, 8);
okl::copy(&r.sp, b + mc + 2 * 8, 8);
okl::copy(&r.fp, b + mc + 8 * 8, 8);
#endif
return r;
}

// Where a frame's record {the caller's frame pointer, the return address} lies
// relative to the frame pointer: at it on x86_64, i386 and aarch64; on riscv64
// the frame pointer is the frame's top, and the record is the two words below.
#if defined(__riscv)
constexpr okl_uptr frame_record_below = 2 * sizeof(okl_uptr);
#else
constexpr okl_uptr frame_record_below = 0;
#endif

// The return addresses of a chain of frame pointers.
int walk(frame_regs r, okl_uptr* pcs, int max) {
int n = 0;
Expand All @@ -256,7 +291,7 @@ int walk(frame_regs r, okl_uptr* pcs, int max) {
for (int i = 0; i < 256 && n < max; ++i) {
if (fp == 0 || (fp & (sizeof(okl_uptr) - 1)) != 0 || fp < r.sp) break;
okl_uptr pair[2];
if (!peek(fp, pair, sizeof pair)) break;
if (!peek(fp - frame_record_below, pair, sizeof pair)) break;
if (pair[1] == 0) break;
pcs[n++] = pair[1];
if (pair[0] <= fp) break; // a chain runs toward older frames
Expand Down Expand Up @@ -309,7 +344,7 @@ void report(int signo, void* info, void* uc) {


using okl::k_sigaction;
constexpr unsigned long sa_siginfo = 4, sa_onstack = 0x08000000, sa_restorer = 0x04000000,
constexpr unsigned long sa_siginfo = 4, sa_onstack = 0x08000000,
sa_resethand = 0x80000000, sa_restart = 0x10000000;

alignas(16) unsigned char g_alt_stack[256 * 1024];
Expand All @@ -331,9 +366,8 @@ alignas(16) unsigned char g_alt_stack[256 * 1024];
// A disposition the program was started with, other than the default,
// is the program's to keep.
if (old.handler != nullptr) continue;
k_sigaction act{ reinterpret_cast<void*>(&okl_fault_handler),
sa_siginfo | sa_onstack | sa_restorer,
reinterpret_cast<void*>(&okl_signal_return), 0 };
k_sigaction act = okl::handler_action(reinterpret_cast<void*>(&okl_fault_handler),
sa_siginfo | sa_onstack);
okl::sys(okl::nr_rt_sigaction, signo, reinterpret_cast<okl_long>(&act), 0, sizeof act.mask);
}
}
Expand Down Expand Up @@ -542,8 +576,8 @@ int kal_prof_sample_begin(kal_u32 per_second) {
g_ring.ready = reinterpret_cast<volatile okl_uptr*>(m + static_cast<okl_long>(cap * sizeof(kal_prof_sample)));
g_ring.cap = cap;
}
k_sigaction act{ reinterpret_cast<void*>(&okl_sample_handler), sa_siginfo | sa_restart | sa_restorer,
reinterpret_cast<void*>(&okl_signal_return), 0 };
k_sigaction act = okl::handler_action(reinterpret_cast<void*>(&okl_sample_handler),
sa_siginfo | sa_restart);
if (okl::failed(okl::sys(okl::nr_rt_sigaction, 27 /* SIGPROF */, reinterpret_cast<okl_long>(&act),
reinterpret_cast<okl_long>(&g_prof_before), sizeof act.mask)))
return kal_err_not_supported;
Expand Down Expand Up @@ -608,8 +642,8 @@ int kal_prof_touch_begin(const void* at, kal_uintptr size) {
if (now.handler != reinterpret_cast<void*>(&okl_fault_handler)) {
g_segv_before = now;
g_segv_replaced = true;
k_sigaction act{ reinterpret_cast<void*>(&okl_fault_handler), sa_siginfo | sa_onstack | sa_restorer,
reinterpret_cast<void*>(&okl_signal_return), 0 };
k_sigaction act = okl::handler_action(reinterpret_cast<void*>(&okl_fault_handler),
sa_siginfo | sa_onstack);
okl::sys(okl::nr_rt_sigaction, 11, reinterpret_cast<okl_long>(&act), 0, sizeof act.mask);
}
const int k = __atomic_load_n(&g_part_count, __ATOMIC_ACQUIRE);
Expand Down
2 changes: 1 addition & 1 deletion src/process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,7 @@ int start_image(void* arg) {
// SIGPIPE AS THIS PROGRAM WAS STARTED WITH, not as this implementation
// set it for its own writes (env.cpp): an ignore would outlive `execve'.
if (!okl::g_pipe_started_ignored) {
okl::k_sigaction standard{ nullptr /* SIG_DFL */, 0, nullptr, 0 };
okl::k_sigaction standard{}; // SIG_DFL
okl::sys(okl::nr_rt_sigaction, 13 /* SIGPIPE */,
reinterpret_cast<okl_long>(&standard), 0, sizeof standard.mask);
}
Expand Down
22 changes: 22 additions & 0 deletions src/start.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,28 @@ __asm__(
" brk #0\n"
".size _start,.-_start\n"
);
#elif defined(__riscv)
// The global pointer is established first, without relaxation: a relaxed
// `lla' would itself be addressed through the register it sets.
__asm__(
".text\n"
".globl _start\n"
".type _start,%function\n"
"_start:\n"
" .weak __global_pointer$\n"
" .hidden __global_pointer$\n"
" .option push\n"
" .option norelax\n"
" lla gp, __global_pointer$\n"
" .option pop\n"
" li fp,0\n"
" li ra,0\n"
" mv a0,sp\n"
" andi sp,sp,-16\n"
" call __okl_start_c\n"
" ebreak\n"
".size _start,.-_start\n"
);
#endif

extern "C" [[noreturn]] void __okl_start_c(long* sp) {
Expand Down
Loading
Loading