diff --git a/README.md b/README.md index 8a8cb8f..2a8a5dd 100644 --- a/README.md +++ b/README.md @@ -8,9 +8,35 @@ for Linux, written on the kernel's own system-call interface. openkal = "0.26.0" [target.'cfg(os = "linux")'.dependencies] -openkal-linux = "0.27.1" +openkal-linux = "0.27.2" ``` +## What 0.27.2 fixes + +**The riscv64 record of a signal's action is 32 bytes.** The kernel's record is +24 bytes (handler, flags, mask). qemu's user-mode translator (8.2, and 7.2) lays it +out with a restorer before the mask: it writes 32 bytes for the action it returns +and reads the mask at the fourth word. The eight bytes after a 24-byte record were +overwritten, measured as a started image whose reset of the signals' actions +returned to its first signal on every query and never reached the replacement. +The record carries a fourth word, the size musl gives it; no action this +implementation installs states a mask, so the kernel and the translator read the +same one. + +**An ended program's record is read only where it was written.** The usage of an +ended program not yet collected is the fifth argument of `waitid`, which Linux +writes whole when it reports the program. qemu's user-mode translator (8.2) takes +the call without the argument, and the zeros it left were read as a program with +no processor time and a peak of nothing. The peak is preset to a value the kernel +never writes, so a record that was not written is known as such. The program is +then not yet collected, and its entry answers what the entry keeps: the processor +time through the program's clock and the page faults from `/proc//stat`, as +for a running program. Its peak is in the record alone, and is reported as +unavailable (`KAL_PINFO_MEMORY` absent), not as zero. + +`tests/arch/runs.cpp` checks that a queried action leaves the word after its record +intact, starts a program and reads its status, and reads its peak only as stated. + ## What 0.27.1 adds **riscv64.** The kernel's riscv64 gate (`ecall`, the number in `a7`, the arguments in diff --git a/mcpp.toml b/mcpp.toml index 0c6340b..bf94f03 100644 --- a/mcpp.toml +++ b/mcpp.toml @@ -1,7 +1,7 @@ [package] namespace = "mcpplibs" name = "openkal-linux" -version = "0.27.1" +version = "0.27.2" 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" diff --git a/src/process.cpp b/src/process.cpp index f33693a..26602cd 100644 --- a/src/process.cpp +++ b/src/process.cpp @@ -1493,6 +1493,16 @@ bool cpu_ns(okl_long pid, bool self, okl_u64& out) { return true; } +// The page faults of a program from its entry (fields 10 and 12), which the +// kernel keeps until the program is collected. +bool entry_faults(okl_long pid, okl_u64& minor, okl_u64& major) { + char b[1024]; + const okl_long len = read_stat(pid, b); + bool a = false, c = false; + if (len > 0) { minor = stat_number(b, len, 10, a); major = stat_number(b, len, 12, c); } + return a && c; +} + // Whether a program has ended. A started one: `waitid' without collecting it // (WNOWAIT), and one already collected has ended too. One found by its // identifier: its descriptor is readable once it has ended. @@ -1616,11 +1626,7 @@ int kal_process_info(kal_process p, kal_u32 wanted, kal_program_info* out) { ok = !okl::failed(okl::sys(okl::nr_getrusage, 0 /* RUSAGE_SELF */, reinterpret_cast(&ru))); minor = static_cast(ru.rest[3]); major = static_cast(ru.rest[4]); } else if (!ended) { - char b[1024]; - const okl_long len = read_stat(pid, b); - bool a = false, c = false; - if (len > 0) { minor = stat_number(b, len, 10, a); major = stat_number(b, len, 12, c); } - ok = a && c; + ok = entry_faults(pid, minor, major); } else if (handle_kind(p) == kind_child) { ok = okl::child_faults(pid, minor, major); } @@ -1777,14 +1783,29 @@ bool resident(okl_long pid, okl_u64& now, okl_u64& peak) { } // An ended program not yet collected: its record, read without collecting it. -bool ended_usage(okl_long pid, k_rusage& ru) { +// Answers whether the program was reported; `written' states whether the record +// was. +// +// The record is the fifth argument of `waitid', which Linux adds to the call +// and writes whole when it reports a program. A translator may take the call +// and not the argument: qemu's user-mode translator (8.2) passes no record to +// the host and leaves this one as it was, and its zeros were read as a program +// that used no processor time and whose peak was nothing. The peak is set to a +// value the kernel never writes, so a record that was not written is known as +// such. A program that was reported is not yet collected, so its identifier is +// still its own and its entry answers what the entry keeps (child_cpu, +// child_faults). +bool ended_usage(okl_long pid, k_rusage& ru, bool& written) { alignas(8) unsigned char info[128] = {}; + ru = k_rusage{}; + ru.maxrss = -1; const okl_long r = okl::sys(okl::nr_waitid, 1 /* P_PID */, pid, reinterpret_cast(info), 4 | 1 | 0x01000000 /* WEXITED | WNOHANG | WNOWAIT */, reinterpret_cast(&ru)); if (okl::failed(r)) return false; int si_pid = 0; okl::copy(&si_pid, info + okl::si_pid_at, sizeof si_pid); + written = ru.maxrss >= 0; return si_pid != 0; } @@ -1923,8 +1944,11 @@ void job_forget(okl_long unit) { bool child_memory(okl_long pid, bool ended, okl_u64& now, okl_u64& peak) { if (!ended) return resident(pid, now, peak); + // An ended program has no memory left to read, so only the record states + // its peak. k_rusage ru{}; - if (!ended_usage(pid, ru)) return false; + bool written = false; + if (!ended_usage(pid, ru, written) || !written) return false; now = 0; peak = static_cast(ru.maxrss) * 1024; return true; @@ -1933,15 +1957,21 @@ bool child_memory(okl_long pid, bool ended, okl_u64& now, okl_u64& peak) { // The page faults of an ended program not yet collected (0.23). bool child_faults(okl_long pid, okl_u64& minor, okl_u64& major) { k_rusage ru{}; - if (!ended_usage(pid, ru)) return false; - minor = static_cast(ru.rest[3]); - major = static_cast(ru.rest[4]); - return true; + bool written = false; + if (!ended_usage(pid, ru, written)) return false; + if (written) { + minor = static_cast(ru.rest[3]); + major = static_cast(ru.rest[4]); + return true; + } + return entry_faults(pid, minor, major); } bool child_cpu(okl_long pid, okl_u64& ns) { k_rusage ru{}; - if (!ended_usage(pid, ru)) return false; + bool written = false; + if (!ended_usage(pid, ru, written)) return false; + if (!written) return cpu_ns(pid, false, ns); ns = static_cast(ru.utime_s + ru.stime_s) * 1000000000ull + static_cast(ru.utime_us + ru.stime_us) * 1000ull; return true; diff --git a/src/sys.h b/src/sys.h index 5f33a2c..5d35a99 100644 --- a/src/sys.h +++ b/src/sys.h @@ -717,8 +717,19 @@ constexpr okl_uptr si_status_at = si_fields_at + 8; // riscv64 has no SA_RESTORER: its record has no restorer field, and the kernel // returns from every handler through its vDSO's `__vdso_rt_sigreturn'. A // restorer written there would be read as the mask. +// +// The record is nevertheless 32 bytes and not the kernel's 24. qemu-user (8.2, +// and 7.2 that the platform's runner supplies) lays out the riscv64 record with +// a restorer before the mask: it writes 32 bytes for the action it returns and +// reads the mask at the fourth word. A record of 24 bytes had the eight bytes +// after it overwritten --- measured as the loop over the signals in a started +// image reset to its first signal on every query, so that the start never +// ended. The fourth word is the size musl gives the same record for the same +// reason. No action this implementation installs states a mask, so both +// readers find the same empty one. #if defined(__riscv) -struct k_sigaction { void* handler; okl_ulong flags; ksigset mask; }; +struct k_sigaction { void* handler; okl_ulong flags; ksigset mask; ksigset beyond; }; +static_assert(sizeof(k_sigaction) == 32); #else struct k_sigaction { void* handler; okl_ulong flags; void* restorer; ksigset mask; }; constexpr okl_ulong sa_restorer_flag = 0x04000000u; // SA_RESTORER diff --git a/tests/arch/runs.cpp b/tests/arch/runs.cpp index 46eee3b..db29b46 100644 --- a/tests/arch/runs.cpp +++ b/tests/arch/runs.cpp @@ -5,7 +5,8 @@ // gate and its numbers (a file past four gibibytes, a directory opened and listed, a rename), the // kernel's records (a node's kind and size), the thread pointer (a task with its own thread-local // storage), the clock, a handler's record and the frame chain (a sample of the program's own stack), -// and published instructions run. The program supplies the C library facilities the compiler calls. +// the record of a signal's action as the kernel and the translator each write it, a started +// program and its peak, and published instructions run. The program supplies the C library facilities the compiler calls. #include #include #include @@ -14,6 +15,7 @@ #include #include #include +#include "../../src/sys.h" #if defined(__i386__) #define OKL_ARCH "i386" @@ -122,6 +124,33 @@ extern "C" int main(int, char**, char**) { for (kal_intptr i = 0; i < got; ++i) deep = deep || (samples[i].depth >= 3 && samples[i].pc[0] != 0); check(got > 0 && deep, "a sample holds the work and the frames above it"); } + // The record of a signal's action is as long as what is written into it: the kernel writes its + // own record, and qemu's user-mode translator writes the riscv64 record with a restorer slot. + struct { okl::k_sigaction act; volatile okl_u64 guard; } probe{}; + probe.guard = 0x5a5a5a5a5a5a5a5aull; + const bool queried = !okl::failed(okl::sys(okl::nr_rt_sigaction, 1 /* SIGHUP */, 0, + reinterpret_cast(&probe.act), sizeof probe.act.mask)); + check(queried && probe.guard == 0x5a5a5a5a5a5a5a5aull, "an action's record holds what is written into it"); + // A started program: the started image queries and resets every signal's action through the + // kernel's record before it is replaced, and ends with the status it was given. The program + // is the host's shell, which the translator hands to the host kernel. + kal_dir root{}; + for (kal_uintptr i = 0; i < n; ++i) + if (kal_fs_preopen(i, &root, name, sizeof name, &name_len) == kal_ok && name_len == 1 && name[0] == '/') + break; + const char* argv[] = { "sh", "-c", "exit 3" }; + const kal_uintptr argv_lens[] = { 2, 2, 6 }; + kal_spawn how{}; how.base = root; how.work = work; + kal_process child{}; + int status = -1, terminated = 1; + check(kal_process_spawn(&how, "bin/sh", 6, argv, argv_lens, 3, nullptr, nullptr, 0, nullptr, &child) == kal_ok + && kal_process_wait(child, &status, &terminated) == kal_ok && status == 3 && terminated == 0, + "a started program ends with its own status"); + kal_program_info ended{}; ended.self_size = sizeof ended; + check(kal_process_info(child, KAL_PINFO_MEMORY, &ended) == kal_ok + && ((ended.present & KAL_PINFO_MEMORY) == 0 || ended.memory_peak_bytes > 0), + "an ended program's peak is stated only from a record the kernel wrote"); + kal_process_close(child); // Published instructions run. void* code = kal_exec_alloc(sizeof answer_code); bool ran = false;