From d5b8ec63caf03a9815805ff1218356fbf3b45e96 Mon Sep 17 00:00:00 2001 From: japabu Date: Wed, 4 Nov 2026 00:53:39 +0100 Subject: [PATCH 1/6] kernel: no kernel thread but the idle loop; the console UART is a userland driver's The small-kernel track's K4 and K6, by the owner's ruling that the kernel creates no thread but each CPU's idle loop. `klogd`, the one kernel thread, was the console's writer. It is gone, and so is every provision kernel threads had: `sched/kthread.rs` and its row table, `is_kernel_task` and `current_is_kernel_thread`, quiesce's exemption, the scheduler's zero baseline, `kernel_start`, `Start::Kernel`, a kernel stack without a user area, `Runs::Kernel`, the kernel address space object with its reserved PCID and ASID, `ElfInfo::none` and an `Option` on every task's image. The console UART - The kernel writes the UART only where no process can: inline as each record commits until `log::console::hand_over`, before userland runs; from the machine's stop, once userland is stopped; and on every fatal path. - Between them it is `/system/bin/uart`'s, which holds a `uart` claim (a new device class: COM1's ports and IRQ 4 on x86, as an `isa` row; the SPCR's PL011 frame and its level SPI on AArch64, which needed SPI routing to a row) and carries the log `logkeeper` feeds it on `log-wire`, as the virtio console's driver carries it on `log-carrier`: one carrier serving in logkeeper, two channels. The UART is quiet while the port carries, says so on itself, and is fed again when the port goes, stalls or the machine stops. - The kernel and logkeeper share one word: the newest stamp the console has spoken (`SYS_LOG_SPOKEN`, which replaces `SYS_CONSOLE_CARRIED`). The inline drain sets it; logkeeper advances it as a channel delivers whole rounds. The UART's driver starts after it, so the UART carries the boot once; the stop and a fatal path put every record after it on the UART. - The kernel's console object is gone with its queue: the supervisor makes the console's input a pipe, whose read end is every program's stdin and whose write end the UART's driver writes what the UART receives into. Its read waits on the pipe's readiness, not a 10 ms repoll. - Without a thread to post it, the log's readiness is owed by `emit` and posted at the next syscall's end or scheduler pass. Thread lifecycle - `SYS_YIELD`, and std's `yield_now` on it. - A detached thread's place goes as it ends (`proclife::leave_detached`), and `SYS_THREAD_EXIT`'s detached form releases its stack once the thread no longer runs on it; std's and libc's `REAPED` lists are gone. Deleted with the thread: `log/handoff.rs`, `kernel::carrier`, the sleep lock (its one user was `klogd`), the wire-holding actuators and their tests, the loom models of the handoff, the log waiter and the sleep lock, and their negative controls. Co-Authored-By: Claude Opus 5.5 --- Cargo.lock | 11 + Cargo.toml | 1 + build/build.rs | 13 +- build/ci.rs | 7 - build/kernelkeys.rs | 2 +- build/vmconsole.rs | 11 +- images/console.toml | 11 +- images/diag.toml | 8 +- images/system.toml | 18 +- ...its-on-the-keyboard-not-the-serial-line.md | 24 - ...ith-it-when-its-driver-is-queued-there.md} | 18 +- ...ts-last-userland-lines-miss-the-console.md | 5 +- ...n-reach-the-console-after-the-last-word.md | 49 -- ...uests-on-a-kernel-without-the-entry-cld.md | 5 +- ...chine-so-another-benchs-boot-carries-it.md | 2 +- ...-before-logkeeper-runs-says-why-nowhere.md | 28 + ...-wait-seconds-on-an-idle-cpu-under-hvf.md} | 31 +- ...-reaped-only-at-its-processs-next-spawn.md | 42 - .../guest-tests-a-loaded-host-can-turn-red.md | 2 +- ...ducer-and-a-kernel-that-waits-on-nobody.md | 11 +- .../std-yield-now-on-toyos-yields-nothing.md | 30 - ...oses-what-its-carrier-had-not-delivered.md | 75 +- ...-runs-only-what-no-cheaper-tier-reaches.md | 2 +- issues/the-kernel-still-creates-threads.md | 28 - issues/toyos-runs-on-arm64.md | 4 +- ...yield-is-not-yet-measured-against-linux.md | 23 + kernel/CLAUDE.md | 3 +- kernel/Cargo.toml | 23 +- kernel/loom/Cargo.toml | 22 - kernel/loom/src/lib.rs | 146 ---- kernel/loom/tests/console_handoff.rs | 40 - kernel/loom/tests/log_wake.rs | 129 --- kernel/loom/tests/sleep_lock.rs | 207 ----- kernel/proclife/src/teardown.rs | 32 + kernel/proclife/src/tree.rs | 2 +- kernel/pure/carrier.rs | 116 --- kernel/pure/lib.rs | 1 - kernel/src/actuator.rs | 14 - kernel/src/arch/aarch64/boot.rs | 3 +- kernel/src/arch/aarch64/console_uart.rs | 63 +- kernel/src/arch/aarch64/entry.rs | 25 +- kernel/src/arch/aarch64/irqchip.rs | 54 +- kernel/src/arch/aarch64/paging.rs | 33 +- kernel/src/arch/aarch64/pio.rs | 49 +- kernel/src/arch/aarch64/trap.rs | 10 +- kernel/src/arch/x86_64/boot.rs | 1 + kernel/src/arch/x86_64/console_uart.rs | 35 +- kernel/src/arch/x86_64/entry.rs | 23 +- kernel/src/arch/x86_64/hw.rs | 12 +- kernel/src/arch/x86_64/idt/exceptions.rs | 12 - kernel/src/arch/x86_64/idt/isa.rs | 1 + kernel/src/arch/x86_64/idt/mod.rs | 3 +- kernel/src/arch/x86_64/paging.rs | 36 +- kernel/src/arch/x86_64/pio.rs | 23 +- kernel/src/device.rs | 22 +- kernel/src/drivers/serial.rs | 197 +---- kernel/src/iommu/fault.rs | 5 - kernel/src/isa.rs | 9 +- kernel/src/loader/mod.rs | 15 +- kernel/src/loader/start.rs | 95 +-- kernel/src/log/console.rs | 763 +++--------------- kernel/src/log/handoff.rs | 46 -- kernel/src/log/mod.rs | 20 +- kernel/src/log/read.rs | 30 - kernel/src/log/shard.rs | 39 +- kernel/src/log/user.rs | 26 +- kernel/src/main.rs | 8 +- kernel/src/mm/mod.rs | 1 - kernel/src/mm/space.rs | 31 - kernel/src/object/device.rs | 72 +- kernel/src/object/handle.rs | 4 - kernel/src/object/mod.rs | 2 - kernel/src/object/ops.rs | 70 +- kernel/src/panic.rs | 5 +- kernel/src/power.rs | 8 +- kernel/src/process.rs | 75 +- kernel/src/quiesce.rs | 12 - kernel/src/sched/driver.rs | 51 +- kernel/src/sched/kthread.rs | 151 ---- kernel/src/sched/mod.rs | 1 - kernel/src/sched/payload.rs | 10 +- kernel/src/scheduler.rs | 46 +- kernel/src/sleeplock.rs | 189 ----- kernel/src/smp.rs | 4 +- kernel/src/syscall/device.rs | 2 +- kernel/src/syscall/dispatch.rs | 13 +- kernel/src/syscall/io.rs | 50 +- kernel/src/syscall/machine.rs | 38 +- kernel/src/syscall/proc.rs | 25 +- kernel/src/syscall/vm.rs | 19 +- kernel/src/watch.rs | 39 +- lib/logkeeper-api/src/lib.rs | 16 +- lib/logstream/src/lib.rs | 39 +- sdk/abi/src/handle.rs | 26 +- sdk/abi/src/lib.rs | 1 + sdk/abi/src/syscall.rs | 70 +- sdk/abi/src/uart.rs | 46 ++ sdk/libc/src/posix_io.rs | 1 - sdk/libc/src/pthread.rs | 20 +- sdk/libc/src/stdio.rs | 15 +- sdk/std/sys/thread.rs | 48 +- sdk/toyos/src/device.rs | 34 + sdk/toyos/src/endow.rs | 2 +- sdk/toyos/src/lib.rs | 31 +- sdk/toyos/src/log/mod.rs | 7 + system/logkeeper/src/carrier.rs | 436 ++++++---- system/logkeeper/src/main.rs | 205 ++--- system/logkeeper/src/serve.rs | 226 ++++-- system/supervisor/src/main.rs | 55 +- system/uart/Cargo.toml | 15 + system/uart/src/hw.rs | 137 ++++ system/uart/src/line.rs | 227 ++++++ system/uart/src/main.rs | 138 ++++ system/virtio-console/Cargo.toml | 1 + system/virtio-console/src/console.rs | 3 +- tests/common/power.rs | 110 --- tests/common/qemu.rs | 15 +- tests/common/serial.rs | 3 - tests/common/vmconsole.rs | 18 +- tests/guest/kernelprobe/src/arch/aarch64.rs | 3 + tests/guest/osbench/src/time.rs | 10 +- .../guest/rust-tests/src/bin/acpi_mediated.rs | 12 +- tests/guest/rust-tests/src/bin/std_threads.rs | 54 +- tests/images/acpicase.toml | 8 +- tests/images/benchcase.toml | 10 +- tests/images/clipboardcase.toml | 9 +- tests/images/designatecase.toml | 9 +- tests/images/drivescase.toml | 10 +- tests/images/floorcase.toml | 10 +- tests/images/hostedclangcase.toml | 9 +- tests/images/hotplugcase.toml | 10 +- tests/images/jobcase.toml | 10 +- tests/images/latencycase.toml | 9 +- tests/images/launchcase.toml | 9 +- tests/images/libccase.toml | 10 +- tests/images/linkcase.toml | 9 +- tests/images/metalcase.toml | 10 +- tests/images/metaldevicecase.toml | 8 +- tests/images/netcase.toml | 10 +- tests/images/panelcase.toml | 9 +- tests/images/proctreecase.toml | 10 +- tests/images/scalecase.toml | 9 +- tests/images/sessioncase.toml | 10 +- tests/images/testcases.toml | 10 +- tests/images/undeclaredcase.toml | 8 +- tests/images/virtclaimcase.toml | 9 +- tests/images/virtjobcase.toml | 8 +- tests/images/virtpaniccase.toml | 8 +- tests/images/virtrebootcase.toml | 8 +- tests/images/virtsmpcase.toml | 8 +- tests/toyos.rs | 102 ++- 151 files changed, 2432 insertions(+), 3713 deletions(-) delete mode 100644 issues/a-console-watch-waits-on-the-keyboard-not-the-serial-line.md rename issues/{a-cpu-that-stops-passing-takes-the-console-with-it.md => a-cpu-that-stops-passing-takes-the-console-with-it-when-its-driver-is-queued-there.md} (61%) delete mode 100644 issues/a-holders-queued-line-can-reach-the-console-after-the-last-word.md create mode 100644 issues/a-supervisor-that-fails-before-logkeeper-runs-says-why-nowhere.md rename issues/{a-woken-klogd-can-wait-seconds-on-an-idle-cpu-under-hvf.md => a-woken-thread-can-wait-seconds-on-an-idle-cpu-under-hvf.md} (70%) delete mode 100644 issues/an-ended-detached-thread-is-reaped-only-at-its-processs-next-spawn.md delete mode 100644 issues/std-yield-now-on-toyos-yields-nothing.md delete mode 100644 issues/the-kernel-still-creates-threads.md create mode 100644 issues/toyos-yield-is-not-yet-measured-against-linux.md delete mode 100644 kernel/loom/tests/console_handoff.rs delete mode 100644 kernel/loom/tests/log_wake.rs delete mode 100644 kernel/loom/tests/sleep_lock.rs delete mode 100644 kernel/pure/carrier.rs delete mode 100644 kernel/src/log/handoff.rs delete mode 100644 kernel/src/sched/kthread.rs delete mode 100644 kernel/src/sleeplock.rs create mode 100644 sdk/abi/src/uart.rs create mode 100644 system/uart/Cargo.toml create mode 100644 system/uart/src/hw.rs create mode 100644 system/uart/src/line.rs create mode 100644 system/uart/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index f9fac62ed17..90e34792a48 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4325,6 +4325,16 @@ version = "1.20.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6f5e870be6c3b371b77fe0ee0bafb859fa4964b4404c27de1d380043c4dda20" +[[package]] +name = "uart" +version = "0.1.0" +dependencies = [ + "logkeeper-api", + "toyos", + "toyos-abi", + "toyos-logstream", +] + [[package]] name = "unicode-ident" version = "1.0.24" @@ -4474,6 +4484,7 @@ dependencies = [ "toyos", "toyos-abi", "toyos-device-memory", + "toyos-logstream", "toyos-manifest", "toyos-pci-claim", "toyos-vdagent", diff --git a/Cargo.toml b/Cargo.toml index 5a057469b0f..dc9842af659 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -148,6 +148,7 @@ members = [ "system/toybox", "system/toybox/copies", "system/trace", + "system/uart", "system/update", "system/update/copies", "system/usbd", diff --git a/build/build.rs b/build/build.rs index fe248ffc2f2..864b10eea5b 100644 --- a/build/build.rs +++ b/build/build.rs @@ -677,7 +677,8 @@ const SUPERVISOR_SERVED: &[&str] = &[toyos_swap::PORT, "power", toyos_manifest:: /// drawn at, the session-admin port, whose holder signs its user's sessions /// out, the wifi port, whose holder joins networks and hands over their /// passphrases, the radio port, whose holder is sent a network's keys, the -/// log's carrier port, for whose holder the console UART goes quiet, and the +/// log's carrier port, for whose holder the console UART goes quiet, the +/// log's wire port, whose holder puts the log on the console UART, and the /// clipboard agent's port, whose holder speaks to the agent as the host. const HELD_ALONE: &[(&str, &[&str])] = &[ (toyos_swap::PORT, &[toyos_swap::HOLDER]), @@ -690,6 +691,7 @@ const HELD_ALONE: &[(&str, &[&str])] = &[ (toyos_wifi::PORT, toyos_wifi::HOLDERS), (toyos_radio::PORT, toyos_radio::HOLDERS), (toyos_logstream::CARRIER, &[toyos_logstream::CARRIER_HOLDER]), + (toyos_logstream::WIRE, &[toyos_logstream::WIRE_HOLDER]), (toyos_vdagent::endpoint::SERVICE, &[toyos_vdagent::endpoint::SERVICE_HOLDER]), ]; @@ -3197,6 +3199,15 @@ mod tests { parsed.programs.contains_key("logkeeper"), "{config} starts `logkeeper` and has no `[programs.logkeeper]` row to say what it holds", ); + // The UART carries nothing past the kernel's hand-over but what + // its driver puts on it. + let wire = toyos_logstream::WIRE_HOLDER; + assert!( + parsed.boot.start.iter().any(|p| p == wire) + && parsed.programs.get(wire).is_some_and(|row| row.devices.iter().any(|d| d == "uart")), + "{config} runs `logkeeper` and does not start `{wire}` holding the `uart` claim, so its console \ + UART says nothing from the kernel's hand-over to the machine's stop", + ); for (name, program) in &parsed.programs { let holds = program.syscap.iter().any(|s| s == "logread"); if name == "logkeeper" { diff --git a/build/ci.rs b/build/ci.rs index 1e1a8e0343d..0d36155122b 100644 --- a/build/ci.rs +++ b/build/ci.rs @@ -525,9 +525,6 @@ const REGISTERED: &[Verdict] = &[ /// Every negative control a model crate declares; `build/build.rs`'s /// `every_model_control_is_run` holds this against the manifests. pub(crate) const CONTROLS: &[Control] = &[ - red(KERNEL_LOOM, "wake-fence-off", Some("log_wake"), &[ - Fails("a_commit_and_an_arm_cannot_both_miss"), - ]), red(KERNEL_LOOM, "lock-acquire-off", Some("ticket_lock"), &[ Fails("try_lock_observes_the_previous_owners_writes"), ]), @@ -590,10 +587,6 @@ pub(crate) const CONTROLS: &[Control] = &[ Fails("a_post_and_a_recheck_answer_a_poll_once"), Fails("a_withdrawal_and_a_post_never_both_take_a_poll"), ]), - red(KERNEL_LOOM, "sleeplock-acquire-off", Some("sleep_lock"), &[ - Fails("a_parking_contender_observes_the_holders_writes"), - Fails("two_holders_never_overlap"), - ]), red(KERNEL_LOOM, "device-irq-lossy", Some("device_irq"), &[ Fails("every_message_is_counted_once"), ]), diff --git a/build/kernelkeys.rs b/build/kernelkeys.rs index 75e2203fa00..79a7deed80c 100644 --- a/build/kernelkeys.rs +++ b/build/kernelkeys.rs @@ -47,7 +47,7 @@ pub const DECLARED: &[Declared] = &[ file: "kernel/src/process.rs", ty: "HashMap", keys: "the pid of the entry's `ProcessObject`, which only `toyos_proclife::Pids::take` issues, \ - at a spawn's admission or a kernel thread's start", + at a spawn's admission", }, ]; diff --git a/build/vmconsole.rs b/build/vmconsole.rs index ef662c20d43..8fe472b21be 100644 --- a/build/vmconsole.rs +++ b/build/vmconsole.rs @@ -21,7 +21,7 @@ use std::process::Child; use std::time::{Duration, Instant}; use toyos_logstream::death::{died, Died}; -use toyos_logstream::Showing; +use toyos_logstream::{Showing, CARRIED_ELSEWHERE}; use toyos_virtio_serial::{CONSOLE_PORT, MAX_PORTS, PORT_ID}; /// The port's QEMU device id, which a test unplugs it by. @@ -78,11 +78,6 @@ impl Painter { } } -/// What the kernel puts on the UART as the console's carrier takes the log -/// (`kernel/src/log/console.rs`): every UART line before it the console port -/// carries too, and the ones after it are the UART's alone. -pub const CARRIED_ELSEWHERE: &str = "console: the log is carried elsewhere from here"; - /// Which of a virtio machine's UART lines are shown beside its console port: /// the loader, the early boot, and the lines the port cannot carry — the /// stop's, a fatal path's, and the whole log again once the console's carrier @@ -481,7 +476,7 @@ mod tests { assert!(shown == want, "the terminal was shown other bytes than the {length} painted"); } - const QUIET: &str = "[ 6.459 cpu0 kernel] console: the log is carried elsewhere from here; the UART takes the stop's and a fatal path's lines\n"; + const QUIET: &str = "[ 6.459 logkeeper] console: the log is carried elsewhere from here; the UART takes the stop's and a fatal path's lines\n"; /// The UART's lines before the quiet line are the port's too and are kept /// back; the quiet line itself says nothing the operator needs, and every @@ -489,7 +484,7 @@ mod tests { #[test] fn the_uart_is_shown_from_the_carriers_word_on() { let early = "[ 0.001 cpu0 kernel] boot: early\n[ 4.000 supervisor] supervisor: started logkeeper\n"; - let late = "[ 9.000 cpu0 kernel] console: the UART carries the log again\n[ 9.100 supervisor] supervisor: x ended\n"; + let late = "[ 9.000 logkeeper] console: the UART carries the log again\n[ 9.100 supervisor] supervisor: x ended\n"; let stream = format!("{early}{QUIET}{late}"); for cut in [1, 7, stream.len()] { let mut uart = Uart::default(); diff --git a/images/console.toml b/images/console.toml index 5237fe1f6cb..16e2d3bfa2b 100644 --- a/images/console.toml +++ b/images/console.toml @@ -21,7 +21,7 @@ assets = ["assets"] memory-floor-mib = 96 [boot] -start = ["logkeeper", "diskserver", "fileserver", "console", "acpiserver", "ps2server"] +start = ["logkeeper", "uart", "diskserver", "fileserver", "console", "acpiserver", "ps2server"] # `/system/bin/console` owns the screen and provides the surface its shell and the # shell's children inherit. No compositor here, so nothing receives one. @@ -34,12 +34,18 @@ start = ["logkeeper", "diskserver", "fileserver", "console", "acpiserver", "ps2s [programs.logkeeper] service = true syscap = ["logread"] -serves = ["log"] +serves = ["log", "log-wire"] + +[programs.uart] +devices = ["uart"] +receives = ["log-wire"] +restart = true # The machine's ACPI fixed hardware, as `images/system.toml` serves it: `shutdown` # powers this machine off with the sleep type this program reads out of the # firmware's tables and hands the kernel, which refuses a power-off without # one. The claim hands it the firmware's own access, as there. + [programs.acpiserver] service = true devices = ["acpi"] @@ -70,7 +76,6 @@ receives = ["input"] devices = ["isa:0060,0064:1,12"] memory-floor-mib = 24 - # `power` because `bin/shutdown` below is this binary, and on the machine this # image exists for — no serial port, a prompt on the panel — there is no other # way to turn it off: it asks the supervisor, which has `logkeeper` make the log whole first. diff --git a/images/diag.toml b/images/diag.toml index 0f0ed32589a..bcb051c28da 100644 --- a/images/diag.toml +++ b/images/diag.toml @@ -11,7 +11,7 @@ # guarantee is that this config declares no `devices`, which # `no_diag_program_claims_the_screen` checks. [boot] -start = ["logkeeper", "fileserver", "toybox"] +start = ["logkeeper", "uart", "fileserver", "toybox"] # **Every image that carries a `TOYOS-LOG` partition runs this**, and every # image does. The kernel keeps the record ring and the console and writes no @@ -23,6 +23,12 @@ start = ["logkeeper", "fileserver", "toybox"] [programs.logkeeper] service = true syscap = ["logread"] +serves = ["log-wire"] + +[programs.uart] +devices = ["uart"] +receives = ["log-wire"] +restart = true [programs.toybox] args = ["pwd"] diff --git a/images/system.toml b/images/system.toml index d768708118a..2230341804f 100644 --- a/images/system.toml +++ b/images/system.toml @@ -29,7 +29,7 @@ memory-floor-mib = 96 # while the compositor had no way to ask for a program's *declared* authority — # the launcher gives it one, and starting a service from `[boot] start` is not # the thing that needed fixing. -start = ["logkeeper", "diskserver", "fileserver", "compositor", "soundserver", "netstack", "filepicker", "acpiserver", "ps2server", "wlan", "vdagent"] +start = ["logkeeper", "uart", "diskserver", "fileserver", "compositor", "soundserver", "netstack", "filepicker", "acpiserver", "ps2server", "wlan", "vdagent"] # What every program launched out of `/apps` holds, whatever its own directory # says. A package is unpacked by `/system/bin/pkg` into a writable directory, so @@ -58,15 +58,25 @@ folder = "read-write" pkg = { state = true } # **Every image that carries a `TOYOS-LOG` partition runs this**, and every -# image does. The kernel keeps the record ring and the console and writes no +# image does. The kernel keeps the record ring, writes the console only before +# userland runs and from the stop, and writes no # file at all, so a boot config without `logkeeper` is a boot whose `/log` is empty — # `every_boot_config_runs_logkeeper` is what refuses one. It claims no device, and # `log` is the port it answers `inspect` on. `log-carrier` is where the VM's -# console asks to carry the log, which quiets the UART while it does. +# console asks to carry the log, which quiets the UART while it does, and +# `log-wire` where the UART's driver asks. [programs.logkeeper] service = true syscap = ["logread"] -serves = ["log", "log-carrier"] +serves = ["log", "log-carrier", "log-wire"] + +# The console UART's driver: the log `logkeeper` hands it over `log-wire` onto +# the UART, from the kernel's hand-over to the machine's stop, and what the UART +# receives into the console's input. A machine with no console UART exits it 0. +[programs.uart] +devices = ["uart"] +receives = ["log-wire"] +restart = true # The machine's ACPI fixed hardware: the kernel puts the machine in ACPI mode # when it mints the `acpi` claim and back when the claim goes, so the SCI, diff --git a/issues/a-console-watch-waits-on-the-keyboard-not-the-serial-line.md b/issues/a-console-watch-waits-on-the-keyboard-not-the-serial-line.md deleted file mode 100644 index 038224d4196..00000000000 --- a/issues/a-console-watch-waits-on-the-keyboard-not-the-serial-line.md +++ /dev/null @@ -1,24 +0,0 @@ ---- -status: open -kind: defect -opened: 2026-09-25 ---- - -# A console's watch waits on the keyboard, and its readiness is the serial line - -A console's readiness is the serial line's (`drivers::serial::has_data`), -and nothing on the serial line posts a wake. A blocking read of a console -(`sys_read`, `kernel/src/syscall/io.rs`) parks on its own thread's watch and is -woken only by its own 10 ms repoll (`CONSOLE_REPOLL`). A watch through an -inbox (`Poller::watch` of a console handle) has no source at all. Its one -source was the kernel's keyboard watch, which went with the kernel's input -streams (the small-kernel track's step 10), so a console has no readable -watch at all now: registering one is refused, and libc's `poll` reads every -completed token as ready, a refusal included (`sdk/libc/src/posix_io.rs`), so a -program that polls its console for input spins rather than waits. - -## Exit condition - -A console's poll source and its readiness are the same source, a blocking read -waits on it with no repoll, and a test that watches a console handle through a -poller and types on the serial line sees the watch complete. diff --git a/issues/a-cpu-that-stops-passing-takes-the-console-with-it.md b/issues/a-cpu-that-stops-passing-takes-the-console-with-it-when-its-driver-is-queued-there.md similarity index 61% rename from issues/a-cpu-that-stops-passing-takes-the-console-with-it.md rename to issues/a-cpu-that-stops-passing-takes-the-console-with-it-when-its-driver-is-queued-there.md index a0899cb4fbd..2409bf7ba03 100644 --- a/issues/a-cpu-that-stops-passing-takes-the-console-with-it.md +++ b/issues/a-cpu-that-stops-passing-takes-the-console-with-it-when-its-driver-is-queued-there.md @@ -4,7 +4,7 @@ kind: finding opened: 2026-09-18 --- -# A CPU that stops passing takes the console with it when `klogd` is queued there +# A CPU that stops passing takes the console with it when its driver is queued there Seen while bounding `nmi_gate`'s hold (PR #470), under a mutation that leaves the storm's CPU spinning inside its idle-loop hook forever, dev host, TCG, @@ -21,13 +21,15 @@ though the dead CPU had logged `syscall-window-nmi: held cpu=…` and another CP logged after it. Userland's own lines kept arriving, so the machine and the UART were alive. -The reading, not yet separated from others: `log::console`'s only drainer once -boot is over is the `klogd` thread, a commit wakes it with `wake_direct`, and a -task queued on a CPU leaves it only when that CPU answers a steal request in a -scheduler pass. A CPU that never passes again answers nothing, so `klogd` woken -onto it stays there and every later record commits to its shard and reaches no -wire. The reviewer's run of the same mutation got the `held cpu=` line out on -one boot of two, which fits: it depends on where `klogd` was queued. +The reading, not yet separated from others: the console's drainer once boot +was over was the `klogd` thread, and a task queued on a CPU leaves it only +when that CPU answers a steal request in a scheduler pass. A CPU that never +passes again answers nothing, so `klogd` woken onto it stayed there and every +later record reached no wire. `klogd` is deleted; the console's writers are +now `logkeeper` and the UART's driver, both threads, and the same holds for +either queued on such a CPU. The reviewer's run of the same mutation got the +`held cpu=` line out on one boot of two, which fits: it depends on where the +writer was queued. What this costs is the evidence of the failure it coincides with: a wedged CPU is the case a reader most wants the console for, and diff --git a/issues/a-forced-reboots-last-userland-lines-miss-the-console.md b/issues/a-forced-reboots-last-userland-lines-miss-the-console.md index 98f74d3fe7d..d82caed801a 100644 --- a/issues/a-forced-reboots-last-userland-lines-miss-the-console.md +++ b/issues/a-forced-reboots-last-userland-lines-miss-the-console.md @@ -25,8 +25,9 @@ Faster connections shorten the time from the terminate to the stop, so the window opens more often, but it is there without them. Where the lines go between a program's log ring and the wire at a stop -(`logkeeper`'s flush answers the supervisor before the stop goes on; -`console: klogd let the wire go to the stop`) is not yet traced. +(`logkeeper`'s flush answers the supervisor before the stop goes on, and the +UART's driver is stopped with the rest of userland) is not yet traced; both +runs above were of the kernel's console thread, since deleted. **Exit**: every userland line written before a forced stop's request reaches the console before the machine resets, and `session_lifecycle` is green 20 diff --git a/issues/a-holders-queued-line-can-reach-the-console-after-the-last-word.md b/issues/a-holders-queued-line-can-reach-the-console-after-the-last-word.md deleted file mode 100644 index 512769f11aa..00000000000 --- a/issues/a-holders-queued-line-can-reach-the-console-after-the-last-word.md +++ /dev/null @@ -1,49 +0,0 @@ ---- -status: open -kind: defect -opened: 2026-09-27 ---- - -# A console holder's queued line can reach the console after the boot's last word - -`quiesce_stops_the_machine` was red once, wide, at 618e68e2 (`wt/toyos-logtrack`, -PR #527), in a fast tier run as load beside twenty `log_stream_stalled_reader` -runs, 1-minute load average 28–97; green alone: - -``` -FAIL quiesce_stops_the_machine: 3 line(s) reached the console after the boot's last word: - {5.024 tid=2 pid=6 test-runner} quiesce-writer: 1 4 - {5.123 tid=3 pid=6 test-runner} quiesce-writer: 2 4 - {5.292 tid=5 pid=6 test-runner} quiesce-writer: 4 3 -``` - -The lines are stamped before the stop and reached the wire after `Rebooting.`. -The mechanism read off the code: `logd` (now `logkeeper`) puts a program's line in -`kernel/src/log/console.rs`'s queue, and `klogd` drains it a chunk of records -and then a chunk of queued lines per hold of the wire, whatever their age. The -stop stops every userland thread and writes its last word as a record, which -`drain_inline` or `klogd`'s next pass puts on the wire; a line still in the -queue then goes after it, inside `quiesce-late-word`'s window. - -## Exit condition - -No holder's line can follow the last word by construction, and a test that -leaves a line in the queue at the stop goes red without that and green with it. - -## The stop now drains the queue - -The stop drains the queue on the wire right after -`quiesce::stop()`, before `Syncing filesystems...` -(`log::console::take_for_the_stop`). -- The drain disabled as a checked patch: `cargo test --test toyos-build -- - --nightly quiesce_stops_the_machine` EXIT=1, `1 line(s) reached the - console after the boot's last word: console: a holder's line, queued once - the stop had stopped every holder`, wide and alone. The power-off's - `serial::flush_final` wrote it. -- With the drain: EXIT=0. - -What is left of "by construction" is a stop that did not stop every holder, -which it reports at alert level. A holder that still runs after the drain -can queue a line that follows the last word. - -`quiesce_stops_the_machine` is deleted; `issues/a-quiesce-writers-first-pass-outlasts-the-jobs-five-second-spin-up.md` records the commit that restores it. diff --git a/issues/a-log-inside-a-pass-wedged-four-of-twelve-guests-on-a-kernel-without-the-entry-cld.md b/issues/a-log-inside-a-pass-wedged-four-of-twelve-guests-on-a-kernel-without-the-entry-cld.md index b567f1cb10b..d221a5506f0 100644 --- a/issues/a-log-inside-a-pass-wedged-four-of-twelve-guests-on-a-kernel-without-the-entry-cld.md +++ b/issues/a-log-inside-a-pass-wedged-four-of-twelve-guests-on-a-kernel-without-the-entry-cld.md @@ -16,8 +16,9 @@ Twelve-wide TCG boot storm, six minutes: four guests wedged, every one in the spawn burst with the previously logged rung as its last line, all recorded as hangs. The landed version, the same `sched-tripwire heap-tripwire` kernel without the log, ran 7,205 boots with 0 deaths. Its commit -blamed "the log's readiness path", which `log::emit` does not reach, then or now -(`log::user::post_readiness` has one caller, `klogd`'s loop). No capture of the +blamed "the log's readiness path", which `log::emit` did not reach then +(`log::user::post_readiness` had one caller, `klogd`'s loop); it reaches +`log::user::owe_readiness` now, one store. No capture of the four guests' registers was kept. That kernel had no `cld` on any Ring 0 entry and `IA32_FMASK` was `0x40200`. diff --git a/issues/a-metal-row-names-no-machine-so-another-benchs-boot-carries-it.md b/issues/a-metal-row-names-no-machine-so-another-benchs-boot-carries-it.md index e495d477631..7f011a44742 100644 --- a/issues/a-metal-row-names-no-machine-so-another-benchs-boot-carries-it.md +++ b/issues/a-metal-row-names-no-machine-so-another-benchs-boot-carries-it.md @@ -12,7 +12,7 @@ arms ride that boot, whichever bench the run is on. On 2026-10-22, `amdvi_usbd_drives_its_own_entry`, an AMD-Vi row, rode the T14's `testcases` boot. Its deliberate DMA fault (`test_rs_usbd_spare_fault`, aimed at `0x50200000`) then failed every judge of that boot that requires a clean -kernel log, `klogd_hosted` among them. The AMD row's own judge, which accounts +kernel log, `klogd_hosted` (since `console_uart_driven`) among them. The AMD row's own judge, which accounts for that fault, never ran there. The stick's controller is no longer pinned per machine (the `@boot` place), so diff --git a/issues/a-supervisor-that-fails-before-logkeeper-runs-says-why-nowhere.md b/issues/a-supervisor-that-fails-before-logkeeper-runs-says-why-nowhere.md new file mode 100644 index 00000000000..987a7e8f937 --- /dev/null +++ b/issues/a-supervisor-that-fails-before-logkeeper-runs-says-why-nowhere.md @@ -0,0 +1,28 @@ +--- +status: open +kind: defect +opened: 2026-11-04 +--- + +# A supervisor that fails before logkeeper runs says why nowhere + +The supervisor's boot ends by name when `/system`'s file server does not +start, or `/system` cannot be read (`bootstrap_fails`, +`system/supervisor/src/main.rs`). Until the kernel's console thread was +deleted it wrote that line on the kernel console the kernel spawned it with, +which the thread put on the UART. That console is gone with the thread: the +UART is its userland driver's, which `logkeeper` feeds, and `logkeeper` is a +program on `/system`. The line now goes to the supervisor's own ring, which +nothing reads before `logkeeper` runs, and then the supervisor panics. + +What is left is the kernel's: the supervisor's `exit:` record, and the +deadline's report, which put both on the UART and in the black box. Neither +says why. + +**Evidence:** the code; no test stages a broken `/system`. + +**Owner:** the supervisor's. + +**Exit:** a boot whose `/system` will not serve says why on a channel an +operator reads — the black box, or the UART at the fatal path that follows — +shown by a boot staged with a `/system` that does not serve. diff --git a/issues/a-woken-klogd-can-wait-seconds-on-an-idle-cpu-under-hvf.md b/issues/a-woken-thread-can-wait-seconds-on-an-idle-cpu-under-hvf.md similarity index 70% rename from issues/a-woken-klogd-can-wait-seconds-on-an-idle-cpu-under-hvf.md rename to issues/a-woken-thread-can-wait-seconds-on-an-idle-cpu-under-hvf.md index 42f37d16752..626f5236e76 100644 --- a/issues/a-woken-klogd-can-wait-seconds-on-an-idle-cpu-under-hvf.md +++ b/issues/a-woken-thread-can-wait-seconds-on-an-idle-cpu-under-hvf.md @@ -4,10 +4,14 @@ kind: defect opened: 2026-10-09 --- -# A woken `klogd` can wait seconds on an idle CPU under HVF +# A woken thread can wait seconds on an idle CPU under HVF -A wake that makes `klogd` runnable on an AArch64 CPU under HVF can leave it -undispatched for seconds, until something else wakes that CPU. +A wake that makes a thread runnable on an AArch64 CPU under HVF can leave it +undispatched for seconds, until something else wakes that CPU. Every +measurement below is of `klogd`, the kernel's console thread, which is +deleted: no thread of the kernel's is left to wait so, and the stop waits on +none. The delay itself is unexplained, and any userland thread woken onto an +idle CPU under HVF may pay it. **What it cost before the stop held the wire.** A boot asked to stop could power off without `Shutting down.` on its console. The last word's drain @@ -40,28 +44,19 @@ was parked the whole time. `klogd` ran 5 ms after the stop's deadline, when a probe on that path kicked every CPU. That staging's wait timed out in 6 of 10 runs of that test. -**What it costs now.** The stop holds the console's wire from right after it -stops userland to the machine's end (`log::console::take_for_the_stop`), so -no record waits on `klogd` past it. A `klogd` not dispatched when the stop -asks costs the stop `log::console::LET_GO`, 5 s, and an alert, which reds -every test (`tests/common/serial.rs`, `NEVER_CLEAN`); where it never lets -go, the lines console holders had queued are not on the console, which the -alert says. - **Until it is fixed** these stay emulated (`tests/toyos.rs`): -`virt_mask_windows`, `virt_off_names_the_cpus_left_on`, `virt_reboot`, -`virt_reboot_wire_held` and `virt_reboot_wire_kept` at EL2, and -`virt_el1_smp` on `Profile::VirtTcg`. +`virt_mask_windows`, `virt_off_names_the_cpus_left_on` and `virt_reboot` at +EL2, and `virt_el1_smp` on `Profile::VirtTcg`. **Not known.** Whether the wake reached the idle CPU, whether its sleep handshake missed it, or whether HVF delivered the kick late: no register capture of the idle CPU exists. **Exit**: the cause named from a capture of the idle CPU, and fixed; then -`virt_el1_smp`, `virt_mask_windows`, `virt_off_names_the_cpus_left_on`, -`virt_reboot` and the two `virt_reboot_wire_` tests under `Profile::Virt` on -an Apple host, 300 side-by-side runs at load 30 or more with no red, the -harness's PSCI trace allowed under HVF; they then move to `Profile::Virt`, +`virt_el1_smp`, `virt_mask_windows`, `virt_off_names_the_cpus_left_on` and +`virt_reboot` under `Profile::Virt` on an Apple host, 300 side-by-side runs +at load 30 or more with no red, the harness's PSCI trace allowed under HVF; +they then move to `Profile::Virt`, `virt_reboot` keeping an EL2 row of its own for `SYSTEM_RESET` through the SMC conduit, and `Profile::VirtTcg` is deleted. diff --git a/issues/an-ended-detached-thread-is-reaped-only-at-its-processs-next-spawn.md b/issues/an-ended-detached-thread-is-reaped-only-at-its-processs-next-spawn.md deleted file mode 100644 index 5d325bcb14a..00000000000 --- a/issues/an-ended-detached-thread-is-reaped-only-at-its-processs-next-spawn.md +++ /dev/null @@ -1,42 +0,0 @@ ---- -status: open -kind: defect -opened: 2026-10-22 ---- - -# An ended detached thread is reaped only at its process's next spawn - -The kernel releases a thread only when another thread joins it -(`SYS_THREAD_JOIN`). Nothing can be told that no thread ever will. A thread std -detaches can't free the stack it runs on, so as it ends it pushes its block -onto `REAPED` (`sdk/std/sys/thread.rs`). The next `Thread::new` in the same -process joins it, which frees its 2 MiB stack and its `MAX_THREADS` place. - -Until that spawn, the ended thread stays in its process's table. `ps` shows it -as `Z`, and its stack stays allocated. A process that detaches threads and then -never spawns another keeps all of them for its whole life: any program that -drops a `JoinHandle` and starts no thread after it. The cost is bounded by the threads detached since -the last spawn, not by the number of spawns. #987 made it bounded; before #987 -nothing reaped these threads at all. - -Measured on QEMU's x86-64 machine booting `tests/images/testcases.toml`, with -toybox `ps` after the boot: - -- At `ff9b4b1c5`, which has neither #987 nor a join, the supervisor's two - `bootstrap` reader threads, which the supervisor had detached, show `Z` for - the rest of the boot. -- At `25d787482`, which has #987 but not that commit's join (a scratch revert), - no `bootstrap` thread is listed. The supervisor's later thread spawns had - reaped them. - -The case this record is about, a process that never spawns again, was not -measured. It follows from `Thread::new` being the only reaper. - -*Exit:* the kernel releases an ended thread that nobody will join. That means -a detach call, or a flag at thread creation, so the thread's place goes when -it ends. std's detach then frees the stack at once: by the kernel unmapping it -as part of the thread's end, or by std freeing it from a thread that is still -alive. `REAPED` is deleted. libc's `pthread_detach` (`sdk/libc/src/pthread.rs`) -uses the same pattern and moves with it. - -Owner: the orchestrator. diff --git a/issues/guest-tests-a-loaded-host-can-turn-red.md b/issues/guest-tests-a-loaded-host-can-turn-red.md index a06cdecfb8b..4487f75f71b 100644 --- a/issues/guest-tests-a-loaded-host-can-turn-red.md +++ b/issues/guest-tests-a-loaded-host-can-turn-red.md @@ -30,7 +30,7 @@ the fastest boot the run has seen, so load that arrives after it is not in it. one-minute load averages of 82.78 and 69.67 on 14 cores as their suites began; the first had its ceilings priced at 1.00x. The stop's wall-clock budget is owned by `issues/timing-verdicts-ruled-off-qemu-have-no-metal-arm.md` and -`issues/a-woken-klogd-can-wait-seconds-on-an-idle-cpu-under-hvf.md`. +`issues/a-woken-thread-can-wait-seconds-on-an-idle-cpu-under-hvf.md`. Owner: the orchestrator, which dispatches the reshape. diff --git a/issues/logging-records-from-every-producer-and-a-kernel-that-waits-on-nobody.md b/issues/logging-records-from-every-producer-and-a-kernel-that-waits-on-nobody.md index aa8f2cc8180..3adf9c0204f 100644 --- a/issues/logging-records-from-every-producer-and-a-kernel-that-waits-on-nobody.md +++ b/issues/logging-records-from-every-producer-and-a-kernel-that-waits-on-nobody.md @@ -10,9 +10,10 @@ What is built: every program init starts or launches writes records into a log ring of its own that init makes and names to `logd` (`sdk/toyos/src/log/`); writing never waits, allocates or makes a syscall, a full ring is a count `logd` says, and soundd's mix thread writes a lane of its own wait-free. The -clock is a page (`sdk/abi/src/clock.rs`). `klogd` is the console's one -writer, with interrupts on, and a program's line reaches the console only -through `logd`, under its name. The kernel waits on `logd` neither at a +clock is a page (`sdk/abi/src/clock.rs`). Between the kernel's boot and its +stop the console UART is its userland driver's, which `logd` feeds the log it +writes, so a program's line reaches the console only through `logd`, under +its name. The kernel waits on `logd` neither at a panic, which halts the other CPUs first and seals its tail in the black box, nor at a stop, which init sequences through `logd`'s flush. `logd` syncs on an alert or an interval, keeps every boot's first part, and holds each program @@ -75,7 +76,7 @@ Constraints a reader would otherwise re-derive: slots and that outlives it writes into a ring nobody reads. - A shared-memory region is a 2 MiB page, so a ring costs 2 MiB per program — what the pipe it replaced cost once written to. -- The console queue `klogd` drains is 64 lines; `logd` holds up to 1 MiB of - program lines behind it and counts what does not fit. +- The UART's driver takes the log at the UART's own rate, and `logd` holds + what it has not taken in its replay, as for every reader. - A raw `SYS_REBOOT` or `SYS_SHUTDOWN` loses whatever a ring holds unread: the one caller that has `logd` flush first is init's `power` port. diff --git a/issues/std-yield-now-on-toyos-yields-nothing.md b/issues/std-yield-now-on-toyos-yields-nothing.md deleted file mode 100644 index 9b44422dcad..00000000000 --- a/issues/std-yield-now-on-toyos-yields-nothing.md +++ /dev/null @@ -1,30 +0,0 @@ ---- -status: open -kind: defect -opened: 2026-10-28 ---- - -# std's `yield_now` on ToyOS yields nothing - -ToyOS has no yield syscall, and its std's `thread::yield_now` is a spin-loop -hint (`sdk/std/sys/thread.rs`). A Rust program that yields to let another -runnable thread go keeps its CPU instead, and spin-wait loops written against -`yield_now` behave unlike they do on every other platform. `osbench`'s -`time.yield` therefore reports `unsupported` on ToyOS rather than time a hint -against Linux's `sched_yield`. - -From the 2026-10-28 scoreboard (evidence branch `scoreboard-2026-10-28`): -ToyOS at 069cbbeb8, four boots off the internal NVMe, against five runs of -each bench's Linux as installed, interleaved. A cell is the p50 with its -run-to-run range. - -| row | T14 Linux | AMD Linux | ToyOS | -|---|---|---|---| -| `time.yield` | 190,677 ps (0.6%) | 552,167 ps (1.9%) | unsupported, all 4 boots on both benches | - -**Exit**: a yield that gives the CPU to another runnable thread of the -caller's band, std's `yield_now` on it, and `time.yield` measured on -ToyOS within Linux's on both benches. - -**Owner**: the scheduler's syscall surface (`kernel/src/sched/`, `sdk/abi`) -and ToyOS's std (`sdk/std/sys/thread.rs`). diff --git a/issues/the-console-loses-what-its-carrier-had-not-delivered.md b/issues/the-console-loses-what-its-carrier-had-not-delivered.md index 81b0a91b37e..34b3c83ae59 100644 --- a/issues/the-console-loses-what-its-carrier-had-not-delivered.md +++ b/issues/the-console-loses-what-its-carrier-had-not-delivered.md @@ -6,51 +6,42 @@ opened: 2026-10-24 # The console loses what its carrier had not delivered when it went -While `/system/bin/virtio-console` carries the log, the UART takes none of -it (`system/logkeeper/src/carrier.rs`, `kernel/src/log/console.rs`). When the -carrier goes, two runs of lines reach neither the console port nor the UART: - -- the bytes logkeeper sent the carrier that its channel had not taken, - `[delivered, sent)` in `Carrier`'s state; -- the kernel records `klogd` advanced past between the carrier's end and - logkeeper's word that the UART carries the log again — among them the - kernel's own record of a driver that crashed, the line that explains the - event. - -The same holds across logkeeper's own death, whose console's last handle -gives the log back: what it had read and not yet written, and the records -`klogd` advanced past before that handle went. `/log` keeps the first run -where logkeeper lived to write it, and the kernel's ring and black box the -second. +Each of the console's channels — the VM's console port and the console UART — +is a userland driver `logkeeper` feeds (`system/logkeeper/src/carrier.rs`). +While the port carries the log, the UART is fed none of it. Two runs of +lines can reach neither channel: + +- the bytes logkeeper sent a carrier that its channel had not taken when the + carrier went, `[delivered, sent)` in its `Carriage`; the UART's carrier is + fed from the moment the port's goes, not from what the port had not taken; +- at the machine's stop, the program lines in the UART's pipe that the UART + had not taken when userland stopped. The kernel's stop and every fatal path + put the kernel's own records on the UART from the newest stamp a channel + delivered (`log::console::spoken`), so no kernel record is lost this way. + +The same holds across logkeeper's own death: its carriers' pipes end, and +the UART carries nothing until the kernel's stop or a fatal path. `/log` +keeps what logkeeper lived to write, and the kernel's ring and black box the +kernel's records. **A carrier that is stuck but alive** — a driver that stops reading its pipe, or a device that never completes a transmit — no longer leaves the UART -silent: once the channel has taken nothing it is owed for `STALL` (5 s), -logkeeper tells the kernel the UART carries the log again and keeps feeding -the carrier, and quiets it once the channel takes more -(`Carriage::tick`). What the channel had not taken when the stall was -declared is on neither channel, as above, and while the UART speaks beside a -carrier that recovers, lines can reach both. - -**Residual: logkeeper itself stuck but alive.** A logkeeper that deadlocks, -or whose stall thread cannot run, still holds its console, so the kernel -keeps the UART quiet and `klogd` advances past every record until the stop -or a fatal path. Nothing in the kernel watches whether the holder still -reads the log, and the kernel keeps no per-reader cursor to watch. The -proposed exit for this part is a lease: a carry expires unless its holder -renews it within a bound, and logkeeper's main loop renews it as it reads the -ring, so a logkeeper that stops reading gives the UART the log back by the -lease's end. - -**Evidence:** the code; `the_uart_carries_the_log_again_once_the_console_carrier_goes` -shows the restore itself (PR #1062). +silent: once the port's channel has taken nothing it is owed for `STALL` +(5 s), the UART is fed the log again beside it, and quiet once the channel +takes more (`Carriage::tick`). What the channel had not taken when the stall +was declared is on neither channel, as above, and while the UART speaks +beside a carrier that recovers, lines can reach both. + +**Residual: logkeeper itself stuck but alive.** A logkeeper that deadlocks +feeds neither channel, and nothing watches whether it still reads the ring. + +**Evidence:** the code. **Owner:** unheld; whoever next changes `carrier.rs`. -**Exit:** logkeeper's own stall covered by the lease above, and a carrier -that goes mid-stream leaves every line on one of the two channels: -logkeeper, which holds the replay and knows `delivered`, puts the -undelivered tail on the UART as it gives the log back, and the records -`klogd` advanced past since the carrier last delivered reach the UART too. -Shown by a guest test that unplugs the port with lines in flight and finds -each of them once on the merged capture. +**Exit:** a carrier that goes mid-stream leaves every line on one of the two +channels: logkeeper, which holds the replay and knows `delivered`, feeds the +UART from the port's undelivered tail as it gives the log back; and the stop +waits, bounded, for the UART to take what it was fed. Shown by a guest test +that unplugs the port with lines in flight and finds each of them once on the +merged capture. diff --git a/issues/the-guest-suite-runs-only-what-no-cheaper-tier-reaches.md b/issues/the-guest-suite-runs-only-what-no-cheaper-tier-reaches.md index 9d4c2f62f21..22378516bc3 100644 --- a/issues/the-guest-suite-runs-only-what-no-cheaper-tier-reaches.md +++ b/issues/the-guest-suite-runs-only-what-no-cheaper-tier-reaches.md @@ -63,7 +63,7 @@ console's renderer. Exit: as stage A. - metal `panic_outlives_the_deadline`: a panic crosses the deadline's reset as a panic report. - metal `hang_bounded_by_the_stick`: a hanging image costs one boot and never traps the machine. - metal `double_fault_stack`, `syscall_panic_halts`, `syscall_fault_halts`, - `heap_over_ceiling_halts`, `klogd_fault_halts`: each fatal path halts with its own report. + `heap_over_ceiling_halts`: each fatal path halts with its own report. - metal `reentry_names_the_first_panic`, `double_panic_names_the_fault`, `nested_fault_is_recursive`: a fault inside the report names the first. - metal `pre_idle_wedge_speaks`: a boot stopped in phase 3 says where. diff --git a/issues/the-kernel-still-creates-threads.md b/issues/the-kernel-still-creates-threads.md deleted file mode 100644 index 1e632929219..00000000000 --- a/issues/the-kernel-still-creates-threads.md +++ /dev/null @@ -1,28 +0,0 @@ ---- -status: open -kind: track -opened: 2026-09-27 ---- - -# The kernel still creates threads - -The owner's ruling: the kernel creates no thread but the per-CPU idle loop. -Kernel work runs, bounded, on the thread or interrupt that caused it and is -charged to it; long-running work with no owner is a userland server's. The -ruling holds only if the whole kernel-thread machinery is deleted — -`sched/kthread.rs`, `kthread::spawn`, the row table and every policy a row -carries, `is_kernel_task`/`current_is_kernel_thread`, and every special case that exists -only for kernel threads. Stopping new uses is not enough. - -**Exit condition:** `kernel/src/sched/kthread.rs` does not exist, and no kernel -code creates a schedulable task other than the per-CPU idle loop. - -**Evidence:** `git grep -n 'kthread::spawn(' -- kernel/src`. - -**Stages:** - -- **K4:** `klogd` goes: the console moves to logd, and this track owns that move. The boot before logd runs - and the panic path write the console wire (`log::console::drain_inline`, - `serial::panic_flush`), so one wire's driver stays in the kernel whatever - K4 moves. -- **K6:** delete the machinery named above. Blocked on K4. diff --git a/issues/toyos-runs-on-arm64.md b/issues/toyos-runs-on-arm64.md index 2291280dd63..9c615737fba 100644 --- a/issues/toyos-runs-on-arm64.md +++ b/issues/toyos-runs-on-arm64.md @@ -293,8 +293,8 @@ Each stage names its exit; "measured" means a number from a run. answers from a virtio-rng (`kernel/src/random.rs`); `virt_el2_drop`, `virt_smp` and `virt_jobs_at_el2`, one boot of the job case with the timer and FP jobs in it, stay emulated at EL2, which HVF gives no guest, and - `virt_reboot` and five more stay emulated until - `issues/a-woken-klogd-can-wait-seconds-on-an-idle-cpu-under-hvf.md` + `virt_reboot` and three more stay emulated until + `issues/a-woken-thread-can-wait-seconds-on-an-idle-cpu-under-hvf.md` is fixed. Each judges an event, never a rate: no QEMU test measures time. **Accepted with the move to HVF:** `virt_user_mode`, `virt_irq_storm`, `virt_timer_floor`, `virt_failed_ap_leaves_no_hole` and diff --git a/issues/toyos-yield-is-not-yet-measured-against-linux.md b/issues/toyos-yield-is-not-yet-measured-against-linux.md new file mode 100644 index 00000000000..851ec31944a --- /dev/null +++ b/issues/toyos-yield-is-not-yet-measured-against-linux.md @@ -0,0 +1,23 @@ +--- +status: open +kind: tooling +opened: 2026-10-28 +--- + +# ToyOS's yield is not yet measured against Linux's + +std's `thread::yield_now` on ToyOS is `SYS_YIELD` (`sdk/std/sys/thread.rs`), +which gives the CPU to another runnable thread of the caller's band, and +`osbench`'s `time.yield` times it on ToyOS as on Linux +(`tests/guest/osbench/src/time.rs`). Until it did, std's `yield_now` was a +spin-loop hint, and the 2026-10-28 scoreboard (evidence branch +`scoreboard-2026-10-28`) recorded the row unsupported: + +| row | T14 Linux | AMD Linux | ToyOS | +|---|---|---|---| +| `time.yield` | 190,677 ps (0.6%) | 552,167 ps (1.9%) | unsupported, all 4 boots on both benches | + +**Exit**: `time.yield` measured on ToyOS on both benches beside their +Linux, and within it, or the difference named. + +**Owner**: the orchestrator's next scoreboard run. diff --git a/kernel/CLAUDE.md b/kernel/CLAUDE.md index 5aad337590e..29601312e81 100644 --- a/kernel/CLAUDE.md +++ b/kernel/CLAUDE.md @@ -8,11 +8,10 @@ The module header at the site owns its subsystem — read it before changing a m - **Anything added to the idle loop is an audio change** — housekeeping runs before `pass()`, so a woken CPU is late by what it costs; and on a machine with nothing to run the idle loop does not run, so a diagnostic there reports nothing exactly when it is needed. - **The idle loop may not take a global lock unconditionally** — the crash report reads the process table through a `try_lock` it must never block on. `sched/reap_gate.rs` is the pattern: a relaxed-load gate in front of the lock. - **Every x86 Ring 0 entry clears the direction flag, and every interrupt or trap gate `AC`** — no hardware gate does either; `memmove` sets `DF` across `rep` operations with interrupts on, and Ring 3 sets `AC`, under which SMAP lets Ring 0 reach user pages. `arch::entry::ring3_naked_asm` is where they live; `SYSCALL` clears `AC` through `IA32_FMASK`. -- **`BackendGuard` masks interrupts for its whole life**, so anything written under it is an interrupt latency; a new holder bounds itself as the console drain and the userland `write` flush do. +- **`BackendGuard` masks interrupts for its whole life**, so anything written under it is an interrupt latency; a new holder bounds itself as the console drain does. - **A syscall that can block resolves its handle and clones the object out before it blocks** — a `with_object`/`with_process_data` guard held across a park is a runtime panic no compile check catches; the `SYS_FSYNC` arm in `syscall/dispatch.rs` is the pattern. - **A decision the process table makes lives in `kernel/proclife/`, never in `process.rs`** — its defects are interleavings and its host tests are the only machine that can enumerate one. - **A task holds at most one watch registration** — a standing registration across a loop must not call anything that registers again. A double registration panics only at attempt ≥ 2, so the contention depth is the coverage. -- **A console is per holder, minted at spawn** — the object *is* the line buffer. - **`ops::close` cancels a poll only for a source its object really ends** — `Watch::cancel_polls` answers every ring's poll on that watch; `ops::close_ends_polls` is where a new object kind answers. - **A page shared with userland is never reached through a Rust reference** — the words a protocol shares are `&AtomicU32` one at a time, everything else is a volatile copy of the whole value, and a page is laid out *before* it is mapped (`SharedMemObject::write` before `AddressSpace::map`). The kernel, `toyos-abi` and the SDK each hold one end of this rule. - **A device a process drives reaches only the memory its grants map** — the domain is the gate, not the descriptor; a device address outside a grant is a `DMA FAULT` record and never a crash. diff --git a/kernel/Cargo.toml b/kernel/Cargo.toml index 8e806e40fce..8bc6f0afd3c 100644 --- a/kernel/Cargo.toml +++ b/kernel/Cargo.toml @@ -14,16 +14,11 @@ license = "MIT OR Apache-2.0" # is declared only so `cfg` checking knows the name. loom = [] # Declared, never enabled here, and never enabled by any *kernel* build: -# `kernel-loom` turns it on to remove the two `SeqCst` fences, and -# `kernel/loom/tests/log_wake.rs` must red when it does. A model that has never -# failed proves nothing, and no guest test can stand in for it — on x86 a build -# with either fence removed behaves identically, which is the whole reason the -# obligation is a model. -wake-fence-off = [] -# The same for the panic console's published descriptor: `kernel-loom` turns it on -# to drop the publisher's `Release` fence from `seqlock.rs`, -# and `kernel/loom/tests/panic_console_publish.rs` must red — a reader keeping a -# descriptor that is half one publication and half the next. +# `kernel-loom` turns it on for the panic console's published descriptor, to +# drop the publisher's `Release` fence from `seqlock.rs`, and +# `kernel/loom/tests/panic_console_publish.rs` must red — a reader keeping a +# descriptor that is half one publication and half the next. A model that has +# never failed proves nothing, and no guest test can stand in for it. seqlock-writer-fence-off = [] # The same for the console backend's lock: `kernel-loom` turns it on to build # `drivers/serial_lock.rs`'s `try_lock` with `then_some`, whose lost exchange @@ -67,14 +62,6 @@ shard-publish-relaxed = [] # `device_irq` reds. They *are* the record's design, so turning them off reverts # the whole of what the model claims. device-irq-lossy = [] -# The same arrangement for the sleep lock's acquire edge, and it is the first -# *contended*-acquire model this tree has: `src/sleeplock.rs`'s two loads of -# `now` — the ones that decide whose turn it is — go `Relaxed`, so the previous -# holder's writes are unordered against the next holder's reads, and -# `kernel/loom/tests/sleep_lock.rs` reds. `Lock::lock`'s spin is unreachable by -# loom (`src/sync.rs`'s `ACQUIRED` says so at the site); a parking acquire has -# no unbounded branch, which is what makes this one modellable at all. -sleeplock-acquire-off = [] # The same arrangement for the CPU roster's publication edge: `src/smp_roster.rs`'s # `commit` publishes the count with a `Relaxed` store instead of the release the # slot store needs, so a reader can see a count over an unfilled slot, and diff --git a/kernel/loom/Cargo.toml b/kernel/loom/Cargo.toml index bd49f0bdda5..8b33bc9b2ea 100644 --- a/kernel/loom/Cargo.toml +++ b/kernel/loom/Cargo.toml @@ -22,16 +22,6 @@ publish = false [features] default = ["loom"] loom = [] -# The negative control for W3. It removes the two `SeqCst` fences from -# `shard.rs`'s wake pair, and `log_wake.rs` must red under it — the one command -# that proves the model has teeth: -# -# cargo test --manifest-path kernel/loom/Cargo.toml --features wake-fence-off \ -# --test log_wake -# -# Never on by default and never reachable from a kernel build, which declares -# the same name only so `cfg` checking knows it. -wake-fence-off = [] # The negative control for a poll ring's one-shot answer. It splits # `inbox/once.rs`'s exchange into a load and a store, so a post and the # registrant's own recheck can both answer one poll, and `poll_once.rs` must red: @@ -142,18 +132,6 @@ log-commit-release-off = [] # Never on by default and never reachable from a kernel build, which declares # the same name only so `cfg` checking knows it. shard-publish-relaxed = [] -# The negative control for the sleep lock's acquire edge. It makes -# `sleeplock.rs`'s two loads of `now` — the ones that decide whose turn it is — -# `Relaxed`, so the previous holder's writes are unordered against the next -# holder's reads, and `sleep_lock.rs` must red with loom's own words, -# `Causality violation: Concurrent write accesses to UnsafeCell`: -# -# cargo test --manifest-path kernel/loom/Cargo.toml --features sleeplock-acquire-off \ -# --test sleep_lock -# -# Never on by default and never reachable from a kernel build, which declares -# the same name only so `cfg` checking knows it. -sleeplock-acquire-off = [] # The negative control for a claimed PCI function's interrupt record. The # reader's `swap` of the count becomes a load and a store, which is the whole of # what this record's design is, and `device_irq.rs` must red: diff --git a/kernel/loom/src/lib.rs b/kernel/loom/src/lib.rs index 85483bbc364..5615ea0f991 100644 --- a/kernel/loom/src/lib.rs +++ b/kernel/loom/src/lib.rs @@ -245,152 +245,6 @@ extern crate alloc; #[path = "../../src/inbox/polls.rs"] pub mod inbox_polls; -/// What `sleeplock.rs` names of the kernel's watch, and nothing more. -/// -/// **The park is shimmed, and that is the scope statement for -/// `tests/sleep_lock.rs`.** Loom has no scheduler, so there is nothing here to -/// park *on*; [`watch::wait_uncancellable_until`] yields instead, which is what -/// lets the model drive the real contended acquire at all. What the model -/// therefore proves is the ticket arithmetic and the acquire/release edge — -/// mutual exclusion, ordering, and the order contenders are served in. What it -/// does **not** prove is the wake handshake: that a post reaches a waiter -/// registered on a watch is the scheduler core's, modelled in -/// `tests/loom_watch.rs` against the real watch and park. -/// -/// `Watch` is a stand-in for the same reason: nothing registers here, so -/// [`watch::Watch::post_n`] has nobody to tell and answers zero. -pub mod watch { - use crate::scheduler::Parkable; - - /// What a waitable object holds — a stand-in, because the park above it - /// is shimmed and nothing ever registers. - pub struct Watch; - - impl Watch { - pub const fn new() -> Self { - Self - } - - /// Nobody is registered here, so nobody is told. The kernel's answer - /// is how many parked waiters this post woke; the models never read it. - pub fn post_n(&self, _token: u64, _limit: usize) -> usize { - 0 - } - } - - impl Default for Watch { - fn default() -> Self { - Self::new() - } - } - - /// The shimmed park. `yield_now` is loom's own way of saying "another - /// thread may run here", which is exactly what a park is from the schedule - /// explorer's point of view — and unlike `core::hint::spin_loop`, which is - /// what makes `Lock::lock`'s spin unmodellable, it is a branch loom can - /// bound. - pub fn wait_uncancellable_until( - _p: &Parkable, - _watch: &Watch, - _token: u64, - ready: impl Fn() -> bool, - ) { - while !ready() { - #[cfg(feature = "loom")] - loom::thread::yield_now(); - #[cfg(not(feature = "loom"))] - std::hint::spin_loop(); - } - } -} - -/// What `sleeplock.rs` names of the scheduler: the right to park, and who is -/// asking. -/// -/// Four items, and the arithmetic in [`TaskId`] is the whole of what is -/// restated here rather than compiled — `kernel/src/scheduler.rs` names the -/// process table, the scheduler core's driver and half the kernel besides, so it -/// cannot be a `#[path]` module the way `sync.rs` and `inbox/once.rs` are. -pub mod scheduler { - /// The right to give the CPU back. In the kernel its constructor asserts - /// the context's baseline preempt depth; here there is no preempt count and - /// no context, so the type carries only the fact that a caller had one. - pub struct Parkable(()); - - impl Parkable { - pub fn at_entry() -> Self { - Self(()) - } - } - - /// Process-scoped thread identity, as the two halves of the word the lock - /// stores. - #[derive(Clone, Copy, PartialEq, Eq, Debug)] - pub struct TaskId(pub u32, pub u32); - - impl TaskId { - pub fn pack(self) -> u64 { - u64::from(self.1) | (u64::from(self.0) << 32) - } - - pub fn unpack(value: u64) -> Self { - Self((value >> 32) as u32, value as u32) - } - } - - impl core::fmt::Display for TaskId { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - write!(f, "{}:{}", self.0, self.1) - } - } - - // Who this thread is, so the lock's holder word and its self-deadlock - // refusal mean something. The model says; the kernel reads the same fact - // out of two per-CPU words. A `//` comment and not a doc one: rustdoc - // documents no item a macro invocation produces, and `///` here is a - // warning rather than documentation. - // - // **`loom::thread_local!` under loom and `std::thread_local!` without it**, - // and the split is not cosmetic: loom's threads are coroutines on one OS - // thread, so a `std` slot would be one cell shared by all of them and every - // task in the model would be the last one to speak. - #[cfg(feature = "loom")] - loom::thread_local! { - static WHO: core::cell::Cell> = core::cell::Cell::new(None); - } - #[cfg(not(feature = "loom"))] - std::thread_local! { - static WHO: core::cell::Cell> = const { core::cell::Cell::new(None) }; - } - - /// Say who the current thread is. No kernel counterpart: there, the answer - /// is whatever the CPU is running. - pub fn become_task(id: TaskId) { - WHO.with(|who| who.set(Some(id))); - } - - pub fn current_task() -> Option { - WHO.with(|who| who.get()) - } - - /// Whether the scheduler exists. Always, in a model: the one lock taken - /// before it does is the kernel's boot console, on one CPU with no task. - pub fn started() -> bool { - true - } -} - -/// The sleep lock, compiled a second time against loom's atomics. Its -/// dependency surface is the two shim modules above and nothing else — the same -/// narrowness `inbox/once.rs` carries, and for the same reason. -#[path = "../../src/sleeplock.rs"] -pub mod sleeplock; - -/// The stop's claim on the console wire and `klogd`'s answer, driven over the -/// sleep lock above by `tests/console_handoff.rs`. Atomics and nothing else. -#[path = "../../src/log/handoff.rs"] -pub mod log_handoff; - /// The panic snapshot's owner and access state, driven together by /// `tests/panic_capture.rs`. #[path = "../../src/drivers/panic_console/latch.rs"] diff --git a/kernel/loom/tests/console_handoff.rs b/kernel/loom/tests/console_handoff.rs deleted file mode 100644 index c48479b8f7f..00000000000 --- a/kernel/loom/tests/console_handoff.rs +++ /dev/null @@ -1,40 +0,0 @@ -//! Loom: the stop's claim on the console wire against a holder's letting go -//! (`kernel/src/log/handoff.rs`), over the real `SleepLock`. -//! -//! The holder, `klogd` here, lets the wire go and reads whether the stop -//! asked; the stop asks, and tries the wire. One of them must see the other: -//! the stop's try finds the wire free, or the holder sees the ask, and so -//! posts the release the stop parks on. Where neither does, the stop waits out its whole budget -//! on a wire nobody holds and writes over it. Removing either `SeqCst` fence -//! from `handoff.rs` reds this model with exactly that outcome. - -#![cfg(feature = "loom")] - -use kernel_loom::log_handoff::Handoff; -use kernel_loom::scheduler::{become_task, TaskId}; -use kernel_loom::sleeplock::SleepLock; - -const KLOGD: TaskId = TaskId(1, 0); -const STOP: TaskId = TaskId(2, 1); - -#[test] -fn the_stop_finds_the_wire_free_or_klogd_sees_the_ask() { - loom::model(|| { - let wire: &'static SleepLock<()> = Box::leak(Box::new(SleepLock::new(()))); - let handoff: &'static Handoff = Box::leak(Box::new(Handoff::new())); - - become_task(KLOGD); - let held = wire.try_lock().expect("nobody else has the wire yet"); - let klogd = loom::thread::spawn(move || { - become_task(KLOGD); - drop(held); - handoff.asked() - }); - - become_task(STOP); - handoff.ask(); - let took = wire.try_lock().is_some(); - let saw = klogd.join().unwrap(); - assert!(took || saw, "the stop's try found the wire held and klogd never saw the ask: nobody posts"); - }); -} diff --git a/kernel/loom/tests/log_wake.rs b/kernel/loom/tests/log_wake.rs deleted file mode 100644 index b41bacf726d..00000000000 --- a/kernel/loom/tests/log_wake.rs +++ /dev/null @@ -1,129 +0,0 @@ -//! Loom: **W3 — the wake edge**, and the one obligation the wake's own -//! correctness rests on. -//! -//! The invariant, in one sentence: **after a commit and an arm, at least one -//! side has seen the other.** Either the producer finds the flag and owns a -//! post, or the reader's rescan finds the record and does not park. Never -//! neither — that is a committed record left under a parked reader, which is a -//! console that has gone quiet with something still to say. -//! -//! Both halves are a store followed by a load of a *different* location, which -//! is the one reordering x86 TSO permits — so on the machine this kernel ships -//! for, a build with either `SeqCst` fence removed behaves identically to one -//! with both. **No guest test can fail here, on any hardware this tree -//! targets**, which is why the obligation is a model and why the model ships in -//! the same chunk as the code. -//! -//! **What this model is not about.** The park itself is the scheduler core's -//! two-phase commit: a producer that wins the flag writes `klogd`'s word — -//! claiming it if it is committing or parked, flagging it if it is running — -//! and the task's own next commit refuses to park over either. That handshake -//! has its own models in that crate, and re-modelling it here would only -//! re-derive it badly — the first draft of this file did, with a plain `parked` -//! word, and reported a lost wake that the rendezvous CAS makes unreachable. What is left here is exactly the pair of -//! fences, which nothing else models. -//! -//! The negative case is a cargo feature rather than a comment: -//! -//! ```text -//! cargo test --manifest-path kernel/loom/Cargo.toml --features wake-fence-off \ -//! --test log_wake -//! ``` -//! -//! removes both fences from `shard.rs` and this file must red. A model that has -//! never failed proves nothing. - -#![cfg(feature = "loom")] - -use kernel_loom::arch::IrqGuard; -use kernel_loom::log_shard::{arm_waiter, signal_after_commit, waiter, Shard, FIRST_SEQ}; -use loom::sync::atomic::{AtomicBool, Ordering}; -use loom::sync::Arc; -use toyos_abi::log::LogRecord; - -struct Machine { - shard: Shard, - waiter: AtomicBool, - /// Set by the producer when `signal_after_commit` says it owns the post. - /// The post itself is `park::notify`'s write of the word and is not this - /// model's subject; that it was *reached* is. - posted: AtomicBool, -} - -fn record(seq: u64) -> LogRecord { - LogRecord { seq, at_ns: seq, len: 0, ..LogRecord::EMPTY } -} - -/// **The invariant.** One producer commits one record; one reader arms and -/// re-scans. Whatever the schedule, the run may not end with the producer -/// having posted nothing *and* the reader having decided to park. -/// -/// Remove either fence and it can: the producer's flag load is allowed to move -/// ahead of its commit store and the reader's rescan ahead of its flag store, -/// so both sides look at the other's previous value. -#[test] -fn a_commit_and_an_arm_cannot_both_miss() { - loom::model(|| { - let m = Arc::new(Machine { - shard: Shard::new(), - waiter: waiter(), - posted: AtomicBool::new(false), - }); - - let producer = m.clone(); - let p = loom::thread::spawn(move || { - let guard = IrqGuard::close(); - // SAFETY: one producer, and the guard is the model's stand-in for - // the kernel's sole-writer bracket (`percpu_fetch_add`'s shim - // argument in `lib.rs` is the other half). - let seq = unsafe { producer.shard.reserve(&guard) }; - unsafe { producer.shard.commit(seq, &record(seq), &guard) }; - // The kernel's `IrqGuard` has a `Drop` that reopens interrupts - // here; the model's stand-in has nothing to restore, so this reads - // as a no-op drop. It stays because the bracket closing before the - // wake signal is the edge this model is about. - #[allow(clippy::drop_non_drop)] - drop(guard); - if signal_after_commit(&producer.waiter) { - producer.posted.store(true, Ordering::SeqCst); - } - }); - - // `klogd`'s park decision, and nothing else of its body: `true` is "do - // not park". - let dont_park = arm_waiter(&m.waiter, || m.shard.stamp(FIRST_SEQ).is_some()); - - p.join().unwrap(); - - assert!( - dont_park || m.posted.load(Ordering::SeqCst), - "the producer posted nothing and the reader decided to park: a committed record \ - is left with a parked reader, and only a missing fence can produce it" - ); - }); -} - -/// Two producers, one flag: exactly one of them owns the post. -/// -/// This is what the `swap` in `signal_after_commit` buys, and it is why the -/// flag is *loaded* first — a producer that finds it clear pays no -/// read-modify-write at all, which is the whole reason the record path has none -/// on it: one locked RMW per log line was measured at 350 ms of boot under TCG. -#[test] -fn exactly_one_producer_owns_a_park() { - loom::model(|| { - let flag = Arc::new(waiter()); - flag.store(true, Ordering::SeqCst); - - let a = flag.clone(); - let ta = loom::thread::spawn(move || u32::from(signal_after_commit(&a))); - let owned_b = u32::from(signal_after_commit(&flag)); - let owned_a = ta.join().unwrap(); - - assert_eq!( - owned_a + owned_b, - 1, - "the swap admits one poster per park: {owned_a} + {owned_b}" - ); - }); -} diff --git a/kernel/loom/tests/sleep_lock.rs b/kernel/loom/tests/sleep_lock.rs deleted file mode 100644 index af5c77a445b..00000000000 --- a/kernel/loom/tests/sleep_lock.rs +++ /dev/null @@ -1,207 +0,0 @@ -//! Loom: `SleepLock`'s acquire edges, and the first *contended* acquire this -//! tree has ever modelled. -//! -//! `Lock::lock`'s spin is unreachable by loom — the model checker explores a -//! `core::hint::spin_loop` as an unbounded branch and gives up, which this -//! crate's own scope note and `kernel::sync`'s `ACQUIRED` both state, so -//! `ticket_lock.rs` can only drive `try_lock`. A parking acquire has no -//! unbounded branch: the contender gives the CPU back and loom's own -//! `yield_now` is a branch it can bound. So the path `ticket_lock.rs` cannot -//! reach is exactly the path this file is about. -//! -//! **What this proves and what it does not.** Loom has no scheduler, so the -//! park itself is shimmed -//! (`kernel_loom::watch::wait_uncancellable_until` yields). What is -//! therefore under test is the lock: the ticket arithmetic, mutual exclusion, -//! and the release-to-acquire edge that hands the next holder the previous -//! one's writes. What is **not** under test is the wake handshake — the -//! registration and the notified word that make a post reach a *parked* -//! waiter, which is `loom_watch.rs`'s model. -//! -//! Service order is FIFO by the arithmetic rather than by anything this file -//! observes: `now` is advanced exactly once per release and a contender waits -//! for the ticket it took, so ticket order *is* service order. There is no -//! interleaving that makes that false and none that makes it visible, so it is -//! stated here rather than asserted below. -//! -//! On x86 every load is an acquire, so a build with the edge relaxed behaves -//! identically to this one and no guest test can fail here. The negative case -//! is a cargo feature rather than a comment: -//! -//! ```text -//! cargo test --manifest-path kernel/loom/Cargo.toml --features sleeplock-acquire-off \ -//! --test sleep_lock -//! ``` -//! -//! makes `sleeplock.rs`'s two loads of `now` relaxed and this file must red — -//! loom answers `Causality violation: Concurrent write accesses to UnsafeCell`, -//! which is a lock handing out data it did not synchronize, stated exactly. -//! Verified 2026-08-19, both ways round: all four models red under the feature -//! and all four pass without it. `build/ci.rs`'s `CONTROLS` runs it and demands -//! `a_parking_contender_observes_the_holders_writes` and -//! `two_holders_never_overlap` by name — the contended park and mutual -//! exclusion, the two the *ordering* breaks rather than the arithmetic. - -use std::sync::atomic::{AtomicBool, Ordering as StdOrdering}; - -use kernel_loom::scheduler::{become_task, Parkable, TaskId}; -use kernel_loom::sleeplock::SleepLock; -use loom::sync::Arc; - -/// The two identities the models use. Distinct, because the holder word and the -/// self-deadlock refusal are about *which* task holds the lock. -const ONE: TaskId = TaskId(1, 1); -const TWO: TaskId = TaskId(1, 2); - -/// A `try_lock` that succeeds must see every write the previous holder made. -/// -/// The mirror of `ticket_lock.rs`'s first model, on the type that replaces it -/// where the holder may be descheduled. Neither thread parks, so what this one -/// isolates is the edge alone. -#[test] -fn try_lock_observes_the_previous_holders_writes() { - static FOLLOWED: AtomicBool = AtomicBool::new(false); - - loom::model(|| { - let lock = Arc::new(SleepLock::new(0u32)); - - let writer = { - let lock = lock.clone(); - loom::thread::spawn(move || { - become_task(TWO); - if let Some(mut guard) = lock.try_lock() { - *guard = 42; - } - }) - }; - - become_task(ONE); - if let Some(guard) = lock.try_lock() { - let seen = *guard; - assert!( - seen == 0 || seen == 42, - "try_lock handed out a value nobody wrote: {seen}", - ); - if seen == 42 { - FOLLOWED.store(true, StdOrdering::Relaxed); - } - } - - writer.join().unwrap(); - }); - - assert!( - FOLLOWED.load(StdOrdering::Relaxed), - "no try_lock followed the writer's release, so the release edge was never checked", - ); -} - -/// **The model that did not exist before this type did.** A contender that had -/// to queue — take a ticket, arm, give the CPU back — must see the holder's -/// writes when its turn comes. -/// -/// Both threads acquire with `lock`, so on every schedule where they collide -/// one of them goes down the queued path and the release edge is what hands it -/// the data. This is the model `sleeplock-acquire-off` reds. -#[test] -fn a_parking_contender_observes_the_holders_writes() { - loom::model(|| { - let lock = Arc::new(SleepLock::new(0u32)); - - let other = { - let lock = lock.clone(); - loom::thread::spawn(move || { - become_task(TWO); - let parkable = Parkable::at_entry(); - let mut guard = lock.lock(&parkable); - // Read-modify-write *through the guard*: a lock that failed to - // synchronize hands this side a stale value, which is the - // defect, and loom sees the unordered pair either way. - *guard += 1; - }) - }; - - become_task(ONE); - let parkable = Parkable::at_entry(); - { - let mut guard = lock.lock(&parkable); - *guard += 1; - } - - other.join().unwrap(); - - let guard = lock - .try_lock() - .expect("both holders released, so the lock is free"); - assert_eq!(*guard, 2, "a holder's increment went missing"); - }); -} - -/// Two acquires never overlap, however they got in. -/// -/// The counter above would survive an overlap on a machine where the two -/// increments happened to interleave harmlessly; this one cannot. Each holder -/// claims the slot on entry and gives it back on exit, so a second holder -/// arriving while the first is inside finds it taken. -#[test] -fn two_holders_never_overlap() { - loom::model(|| { - let lock = Arc::new(SleepLock::new(false)); - - let other = { - let lock = lock.clone(); - loom::thread::spawn(move || { - become_task(TWO); - let parkable = Parkable::at_entry(); - let mut guard = lock.lock(&parkable); - assert!(!*guard, "two tasks held the lock at once"); - *guard = true; - *guard = false; - }) - }; - - become_task(ONE); - let parkable = Parkable::at_entry(); - { - let mut guard = lock.lock(&parkable); - assert!(!*guard, "two tasks held the lock at once"); - *guard = true; - *guard = false; - } - - other.join().unwrap(); - }); -} - -/// A contender that queued is always served, and `holder` names it while it is -/// inside. -/// -/// Termination is half the claim — every schedule loom explores runs to the end, -/// so no ticket is ever left unserved — and the other half is that the word the -/// self-deadlock refusal reads agrees with who is actually holding it. -#[test] -fn a_queued_contender_is_served_and_named() { - loom::model(|| { - let lock = Arc::new(SleepLock::new(0u32)); - - let other = { - let lock = lock.clone(); - loom::thread::spawn(move || { - become_task(TWO); - let parkable = Parkable::at_entry(); - let _guard = lock.lock(&parkable); - assert_eq!(lock.holder(), Some(TWO), "the holder word names nobody"); - }) - }; - - become_task(ONE); - let parkable = Parkable::at_entry(); - { - let _guard = lock.lock(&parkable); - assert_eq!(lock.holder(), Some(ONE), "the holder word names nobody"); - } - - other.join().unwrap(); - assert_eq!(lock.holder(), None, "a free lock still names a holder"); - }); -} diff --git a/kernel/proclife/src/teardown.rs b/kernel/proclife/src/teardown.rs index 988c1f3a232..f741195d780 100644 --- a/kernel/proclife/src/teardown.rs +++ b/kernel/proclife/src/teardown.rs @@ -109,6 +109,18 @@ pub fn leave(table: &mut T, pid: Pid, tid: Tid, chosen: Option(table: &mut T, pid: Pid, tid: Tid, code: i32) -> Leave { + let left = leave(table, pid, tid, Some(code)); + let proc = table.get_mut(pid).expect("leave_detached: a thread's process is not in the table"); + if left == Leave::NotLast && !proc.tearing_down() { + proc.forget_thread(tid); + } + left +} + /// The last one out has torn its process down: it is dead with `mark`. pub fn torn_down(table: &mut T, pid: Pid, tid: Tid, mark: i32) { let proc = table.get_mut(pid).expect("torn_down: a process is in the table until its exit is published"); @@ -231,6 +243,26 @@ mod tests { assert_eq!(proc.location(t2), Some(ThreadLocation::Zombie(5))); } + /// A detached thread's place goes as it leaves, with no join to take it; + /// in a process being torn down it stays for the teardown, as a joinable + /// one does, and the last one out still tears the process down. + #[test] + fn a_detached_thread_leaves_no_zombie_unless_its_process_is_being_torn_down() { + let mut world = World::new(); + let pid = world.spawn_process(); + let main = world.main_tid(pid); + let t1 = world.spawn_thread(pid); + let t2 = world.spawn_thread(pid); + assert_eq!(leave_detached(&mut world, pid, t1, 0), Leave::NotLast); + assert_eq!(world.get(pid).unwrap().location(t1), None, "its place went with it"); + assert_eq!(crate::join::collect_zombie(&mut world, pid, t1), Err(crate::join::JoinRefused::NoSuchThread)); + + assert!(claim_teardown(&mut world, pid, End::Exited(3))); + assert_eq!(leave_detached(&mut world, pid, t2, 7), Leave::NotLast); + assert_eq!(world.get(pid).unwrap().location(t2), Some(ThreadLocation::Zombie(7)), "the teardown takes it"); + assert_eq!(leave(&mut world, pid, main, None), Leave::Last { end: End::Exited(3), mark: 3 }); + } + #[test] #[should_panic(expected = "is not a thread still in its process")] fn a_thread_leaves_once() { diff --git a/kernel/proclife/src/tree.rs b/kernel/proclife/src/tree.rs index 3e18da063e2..9a23ce614a3 100644 --- a/kernel/proclife/src/tree.rs +++ b/kernel/proclife/src/tree.rs @@ -44,7 +44,7 @@ pub struct Node { } impl Node { - /// A process placed under none: the supervisor at depth 0, and a kernel thread's. + /// A process placed under none: the supervisor, at depth 0. pub fn root() -> Self { Self { parent: None, depth: 0, children: Vec::new(), holds: 1 } } diff --git a/kernel/pure/carrier.rs b/kernel/pure/carrier.rs deleted file mode 100644 index 728978acece..00000000000 --- a/kernel/pure/carrier.rs +++ /dev/null @@ -1,116 +0,0 @@ -//! Who carries the machine's log off it: the console UART, or the console -//! holder that says another channel does. One word, which the console's one -//! writer reads before each record it would put on the UART. -//! -//! **A holder's end is its release**, so a holder that dies cannot leave the -//! UART quiet; and a fatal path or the stop takes the log back to the UART for -//! good, which nothing undoes. - -use core::sync::atomic::{AtomicUsize, Ordering}; - -const UART: usize = 0; -const FOR_GOOD: usize = 1; - -pub struct Carrier(AtomicUsize); - -/// Why a holder may not carry the log. -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub enum Refused { - /// Another holder carries it. - Another, - /// A fatal path or the stop gave it to the UART for good. - ForGood, -} - -impl Carrier { - pub const fn new() -> Self { - Self(AtomicUsize::new(UART)) - } - - /// `holder` carries the log from now; answered `Ok` again for the holder - /// that already does. A holder is an address, which is never 0 or 1. - pub fn carry(&self, holder: usize) -> Result<(), Refused> { - assert!(holder > FOR_GOOD, "carrier: {holder:#x} is no holder's address"); - match self.0.compare_exchange(UART, holder, Ordering::SeqCst, Ordering::SeqCst) { - Ok(_) => Ok(()), - Err(now) if now == holder => Ok(()), - Err(FOR_GOOD) => Err(Refused::ForGood), - Err(_) => Err(Refused::Another), - } - } - - /// `holder` no longer carries the log, by its word or by its end: whether - /// it did. - pub fn release(&self, holder: usize) -> bool { - self.0.compare_exchange(holder, UART, Ordering::SeqCst, Ordering::SeqCst).is_ok() - } - - /// The UART carries everything from here to the machine's end. - pub fn for_good(&self) { - self.0.store(FOR_GOOD, Ordering::SeqCst); - } - - /// Whether a holder carries the log, so the UART takes none of it. - pub fn elsewhere(&self) -> bool { - !matches!(self.0.load(Ordering::SeqCst), UART | FOR_GOOD) - } -} - -impl Default for Carrier { - fn default() -> Self { - Self::new() - } -} - -#[cfg(test)] -mod tests { - use super::*; - - const LOGKEEPER: usize = 0x1000; - const OTHER: usize = 0x2000; - - #[test] - fn a_holder_carries_until_it_lets_go() { - let carrier = Carrier::new(); - assert!(!carrier.elsewhere()); - assert_eq!(carrier.carry(LOGKEEPER), Ok(())); - assert_eq!(carrier.carry(LOGKEEPER), Ok(())); - assert!(carrier.elsewhere()); - assert!(carrier.release(LOGKEEPER)); - assert!(!carrier.elsewhere()); - assert!(!carrier.release(LOGKEEPER)); - } - - /// The holder's console goes with its process, which is what a crash of - /// the holder is: its end releases it, and nothing else need run. - #[test] - fn a_holders_end_gives_the_log_back_to_the_uart() { - let carrier = Carrier::new(); - carrier.carry(LOGKEEPER).expect("the first holder carries"); - // The end of another holder is not this one's. - assert!(!carrier.release(OTHER)); - assert!(carrier.elsewhere()); - assert!(carrier.release(LOGKEEPER)); - assert!(!carrier.elsewhere()); - assert_eq!(carrier.carry(OTHER), Ok(()), "a later holder may carry it again"); - } - - #[test] - fn a_second_holder_is_refused() { - let carrier = Carrier::new(); - carrier.carry(LOGKEEPER).expect("the first holder carries"); - assert_eq!(carrier.carry(OTHER), Err(Refused::Another)); - assert!(carrier.release(LOGKEEPER)); - } - - #[test] - fn for_good_is_never_undone() { - let carrier = Carrier::new(); - carrier.carry(LOGKEEPER).expect("the first holder carries"); - carrier.for_good(); - assert!(!carrier.elsewhere()); - assert!(!carrier.release(LOGKEEPER), "the holder no longer carries it to let go"); - assert_eq!(carrier.carry(LOGKEEPER), Err(Refused::ForGood)); - assert!(!carrier.elsewhere()); - } -} diff --git a/kernel/pure/lib.rs b/kernel/pure/lib.rs index 23ebcd9b04d..98c3c03e9b6 100644 --- a/kernel/pure/lib.rs +++ b/kernel/pure/lib.rs @@ -10,7 +10,6 @@ extern crate alloc; extern crate std; pub mod bootwrite; -pub mod carrier; pub mod clock; pub mod cpu; pub mod dirnodes; diff --git a/kernel/src/actuator.rs b/kernel/src/actuator.rs index 6d1ad9a691f..c7fe1f59b7c 100644 --- a/kernel/src/actuator.rs +++ b/kernel/src/actuator.rs @@ -61,19 +61,6 @@ actuators! { /// it outlasts the stop. Judged by `machine_shutdown_short_stop`. stop_budget_spent = "stop-budget-spent"; - /// Have `klogd` take the console's wire as the stop begins and keep it - /// until the stop asks for it. Judged by `machine_shutdown_wire_held`. - wire_held_across_the_stop = "wire-held-across-the-stop"; - - /// The same, with `klogd` keeping the wire through the stop's whole - /// `LET_GO`. Judged by `machine_shutdown_wire_kept`. - wire_kept_through_the_stop = "wire-kept-through-the-stop"; - - /// Have `klogd` hold the console's wire from the boot's last word until the - /// stop has taken the seal, where the stop does not hold it itself. Judged - /// by `machine_shutdown_wire_at_the_seal`. - wire_held_at_the_last_word = "wire-held-at-the-last-word"; - /// Wedge one CPU with interrupts off, spinning on a lock another CPU holds /// and never gives back: the negative control on `crate::hardlockup`, and a /// machine nothing else in this tree ends. Where CPUID states no @@ -217,7 +204,6 @@ const IMPLIES: &[(&str, &[&str])] = &[ // so the control that ends a machine no other bound ends is staged over the // one that stops this machine ending itself. ("hard-lockup-probe", &["wedge-before-reset"]), - ("wire-kept-through-the-stop", &["wire-held-across-the-stop"]), ]; #[cfg(feature = "boot-actuators")] diff --git a/kernel/src/arch/aarch64/boot.rs b/kernel/src/arch/aarch64/boot.rs index fd1aaeb91ea..8b7d087ce53 100644 --- a/kernel/src/arch/aarch64/boot.rs +++ b/kernel/src/arch/aarch64/boot.rs @@ -370,9 +370,10 @@ pub fn timer() { } /// The platform's own devices that are not PCI functions: on an ACPI Arm -/// machine, the `acpi` claim's row alone. +/// machine, the `acpi` claim's row and the console UART's. pub fn platform_devices(_platform: &Platform, rsdp_addr: u64) { super::acpi_mode::init(rsdp_addr); + super::console_uart::fill_row(); } /// Every other CPU, running. diff --git a/kernel/src/arch/aarch64/console_uart.rs b/kernel/src/arch/aarch64/console_uart.rs index 91bf79de17b..23971fee575 100644 --- a/kernel/src/arch/aarch64/console_uart.rs +++ b/kernel/src/arch/aarch64/console_uart.rs @@ -3,21 +3,26 @@ //! describes that configuration, so this file programs nothing and only moves //! bytes: a PL011's `UARTDR` and `UARTFR`, which the SBSA generic UART keeps //! at the same offsets (Arm PL011 TRM r1p5, §3.3; Arm BSA 1.0, Appendix B). +//! Its claim ([`claim`]) hands the holder the register frame and the SPI. -use core::sync::atomic::{AtomicU64, Ordering}; +use core::sync::atomic::{AtomicU32, AtomicU64, Ordering}; +use toyos_abi::uart::{UartInfo, PL011}; use toyos_acpi::{SerialInterface, GAS_SYSTEM_MEMORY}; +use crate::device::ClaimError; use crate::drivers::acpi::direct_phys; use crate::drivers::serial::Registers; use crate::log; +use crate::mm::policy::CachePolicy; use crate::mm::{DirectMap, Mmio}; +use crate::object::device::DeviceInfo; +use crate::object::shm::SharedMemObject; /// `UARTDR`, the data register, and `UARTFR`, the flag register. const DR: u64 = 0x000; const FR: u64 = 0x018; -/// `UARTFR.RXFE`: the receive FIFO is empty. `UARTFR.TXFF`: the transmit FIFO is full. -const FR_RXFE: u32 = 1 << 4; +/// `UARTFR.TXFF`: the transmit FIFO is full. const FR_TXFF: u32 = 1 << 5; /// Bytes the transmitter takes once [`tx_ready`] says so: `TXFF` clear promises /// room for one, whatever the FIFO's depth. @@ -28,6 +33,12 @@ const FRAME: u64 = 0x1000; /// The register frame's physical address; zero until [`init`] found one. static BASE: AtomicU64 = AtomicU64::new(0); +/// The UART's SPI, as the SPCR names it; zero for none. +static GSIV: AtomicU32 = AtomicU32::new(0); + +/// `isa`'s row for the console UART. +const UART_ROW: usize = 2; + /// The register frame's physical address, once [`init`] found one: what the /// kernel's own tables map before they replace the loader's. pub fn frame() -> Option { @@ -65,18 +76,52 @@ pub fn init(_: &mut Registers, rsdp_addr: u64) -> bool { return false; } BASE.store(spcr.base.address, Ordering::Relaxed); + GSIV.store(spcr.gsiv, Ordering::Relaxed); log!("serial: {kind} at {:#x} (SPCR, GSIV {})", spcr.base.address, spcr.gsiv); true } -/// Whether a received byte waits. -pub fn rx_ready(_: &mut Registers) -> bool { - regs().read_u32(FR) & FR_RXFE == 0 +/// Fill the console UART's row, where the boot found one: no port, and its +/// interrupt, which a PL011 raises active-high and holds until its cause is +/// cleared (Arm PL011 TRM r1p5, "Interrupts"), so a level SPI. Only its +/// class claims it. +pub fn fill_row() { + let gsiv = GSIV.load(Ordering::Relaxed); + if frame().is_none() { + return; + } + if gsiv < 32 { + return log!("isa: no console UART row — the SPCR names its interrupt GSIV {gsiv}, which is no SPI"); + } + crate::isa::fill( + UART_ROW, + crate::isa::Function { + name: "the console UART", + runs: alloc::vec![], + irqs: alloc::vec![], + wires: alloc::vec![super::pio::Wire { intid: gsiv, level: true }], + }, + ); } -/// The received byte; only after [`rx_ready`] said one waits. -pub fn read_byte(_: &mut Registers) -> u8 { - regs().read_u32(DR) as u8 +/// The claim on the UART's row: its register frame, mapped uncached, and its +/// SPI. +pub fn claim() -> Result<(crate::isa::Row, DeviceInfo), ClaimError> { + let base = frame().ok_or(ClaimError::Absent)?; + // A frame that does not start a page is one no mapping hands over alone. + if base % FRAME != 0 { + return Err(ClaimError::Unusable); + } + let row = crate::isa::claim_row(UART_ROW)?; + let frame = SharedMemObject::aperture(base, FRAME, CachePolicy::Uncacheable)?; + let info = UartInfo { + kind: PL011, + port: 0, + frame: toyos_abi::HANDLE_INVALID, + frame_len: FRAME as u32, + level: 1, + }; + Ok((row, DeviceInfo::Uart(info, Some(frame)))) } /// Whether the transmitter will take a byte. diff --git a/kernel/src/arch/aarch64/entry.rs b/kernel/src/arch/aarch64/entry.rs index 31a0cdf09bf..2e5a16639a7 100644 --- a/kernel/src/arch/aarch64/entry.rs +++ b/kernel/src/arch/aarch64/entry.rs @@ -1,6 +1,6 @@ //! Where a new context starts: the frame [`super::switch::context_switch`] -//! restores it from, and the trampolines its return lands in — to EL0 for the -//! first time, or into a kernel thread's body. +//! restores it from, and the trampolines its return lands in, to EL0 for the +//! first time. //! //! A trampoline to EL0 leaves nothing of the kernel in a register: every //! general register but the argument is zeroed, and the FP/SIMD state is the @@ -47,27 +47,6 @@ pub(crate) extern "C" fn process_start() { /// A thread's first entry is a process's, with its argument in `x0`. pub(crate) use process_start as thread_start; -/// Entry point for a kernel thread: `x19` = body, `x21` = argument. Never -/// reaches EL0; unmasks interrupts once `trampoline_entry`, which requires -/// them masked, is done. -#[unsafe(naked)] -pub(crate) extern "C" fn kernel_start() { - core::arch::naked_asm!( - "bl {unlock}", - "msr daifclr, #3", - "mov x0, x21", - "blr x19", - "bl {returned}", - unlock = sym crate::sched::driver::trampoline_entry, - returned = sym kernel_thread_returned, - ); -} - -/// What [`kernel_start`] calls when a kernel thread's body returns: panics rather than halting silently. -extern "C" fn kernel_thread_returned() -> ! { - panic!("a kernel thread's body returned; nothing runs on this stack now"); -} - /// Lay out, just below `top`, the frame `context_switch` restores a new /// context from, and answer the stack pointer that names it: `trampoline` is /// where its return lands, with the entry, the stack and the argument where diff --git a/kernel/src/arch/aarch64/irqchip.rs b/kernel/src/arch/aarch64/irqchip.rs index 0e18dcfae63..93d02aac662 100644 --- a/kernel/src/arch/aarch64/irqchip.rs +++ b/kernel/src/arch/aarch64/irqchip.rs @@ -5,9 +5,10 @@ //! GTDT names. //! //! **What this kernel takes is SGIs, the timer's PPI, the IOMMU's event SPI -//! ([`route_iommu_events`]) and one LPI per claimed function ([`its`]), and -//! nothing else.** No other SPI is routed: a device's interrupt is a message the ITS -//! translates, and only a claimed function's is. +//! ([`route_iommu_events`]), the SPI of a claimed `isa` row's function +//! ([`route_spi`]) and one LPI per claimed function ([`its`]), and nothing +//! else.** A PCI function's interrupt is a message the ITS translates, and +//! only a claimed function's is. //! //! **The virtual timer, not the physical**: it is the one EL1 owns outright — //! under a hypervisor the physical one traps — and with `CNTVOFF_EL2` written @@ -312,28 +313,61 @@ pub(super) fn timer_intid() -> u32 { /// (IHI 0070 H.a §3.18.2, §12.4), and the IORT names no trigger. Refused /// with the distributor's limit where it takes no such SPI. pub(super) fn route_iommu_events(intid: u32) -> Result<(), u32> { - let gicd = Mmio::new(DirectMap::from_phys(DISTRIBUTOR.load(Relaxed)), FRAME); + route_spi(intid, false)?; + IOMMU_EVENTS.store(intid, Relaxed); + set_spi_masked(intid, false); + Ok(()) +} + +/// Route SPI `intid` to this CPU at [`PRIORITY`], level- or edge-triggered, +/// masked and with nothing pending; [`set_spi_masked`] lets it in. Refused +/// with the distributor's limit where it takes no such SPI. +pub(super) fn route_spi(intid: u32, level: bool) -> Result<(), u32> { + let gicd = distributor(); let limit = (32 * ((gicd.read_u32(GICD_TYPER) & 0x1F) + 1)).min(1020); if !(32..limit).contains(&intid) { return Err(limit); } - let (word, bit) = (u64::from(intid / 32) * 4, 1 << (intid % 32)); - gicd.write_u32(GICD_ICENABLER + word, bit); - settles(100, "GICD_CTLR after disabling an SPI", || gicd.read_u32(GICD_CTLR) & RWP == 0); + let (word, bit) = spi_bit(intid); + set_spi_masked(intid, true); gicd.write_u32(GICD_IGROUPR + word, gicd.read_u32(GICD_IGROUPR + word) | bit); gicd.write_u32(GICD_IGRPMODR + word, gicd.read_u32(GICD_IGRPMODR + word) & !bit); let priority = GICD_IPRIORITYR + u64::from(intid & !3); let shift = 8 * (intid % 4); gicd.write_u32(priority, gicd.read_u32(priority) & !(0xFF << shift) | u32::from(PRIORITY) << shift); + // `GICD_ICFGR`'s upper bit of the pair: edge-triggered where set. let config = GICD_ICFGR + u64::from(intid / 16) * 4; - gicd.write_u32(config, gicd.read_u32(config) | 0b10 << (2 * (intid % 16))); + let edge = 0b10 << (2 * (intid % 16)); + let configured = gicd.read_u32(config); + gicd.write_u32(config, if level { configured & !edge } else { configured | edge }); gicd.write_u64(GICD_IROUTER + 8 * u64::from(intid), toyos_gicv3::unpacked_affinity(cpu::hardware_id())); gicd.write_u32(GICD_ICPENDR + word, bit); - IOMMU_EVENTS.store(intid, Relaxed); - gicd.write_u32(GICD_ISENABLER + word, bit); Ok(()) } +/// Stop or start taking SPI `intid`; a mask waits for the distributor to +/// have applied it (IHI 0069H §12.9.4, `GICD_CTLR.RWP`). Callable from the +/// SPI's own handler. +pub(super) fn set_spi_masked(intid: u32, masked: bool) { + let gicd = distributor(); + let (word, bit) = spi_bit(intid); + if masked { + gicd.write_u32(GICD_ICENABLER + word, bit); + settles(100, "GICD_CTLR after disabling an SPI", || gicd.read_u32(GICD_CTLR) & RWP == 0); + } else { + gicd.write_u32(GICD_ISENABLER + word, bit); + } +} + +fn distributor() -> Mmio { + Mmio::new(DirectMap::from_phys(DISTRIBUTOR.load(Relaxed)), FRAME) +} + +/// SPI `intid`'s word offset and bit in the distributor's word-per-32 registers. +fn spi_bit(intid: u32) -> (u64, u32) { + (u64::from(intid / 32) * 4, 1 << (intid % 32)) +} + /// Pend the IOMMU's event SPI again, for records its handler left behind: the /// unit raises none for them. Taken once the handler's `end` deactivates it: /// the write makes an active SPI active and pending, which is never signalled, diff --git a/kernel/src/arch/aarch64/paging.rs b/kernel/src/arch/aarch64/paging.rs index 150be97b867..b937a1d8c4d 100644 --- a/kernel/src/arch/aarch64/paging.rs +++ b/kernel/src/arch/aarch64/paging.rs @@ -348,21 +348,6 @@ impl Drop for AsidGuard { } } -enum Asid { - /// ASID 0, the kernel space's, whose user half is empty. - Kernel, - User(AsidGuard), -} - -impl Asid { - fn value(&self) -> u16 { - match self { - Self::Kernel => kernel::pcid::KERNEL_PCID, - Self::User(guard) => guard.0.get(), - } - } -} - /// `None` when every user ASID is held by a live space. fn alloc_asid() -> Option { let mut pool = ASIDS.lock(); @@ -579,7 +564,8 @@ impl Unlinked { /// invalid, and the caller's [`shoot_down`](Self::shoot_down) otherwise. pub struct UserTables { tables: Tables, - asid: Asid, + /// Owned for this space's life: dropping it returns the ASID. + asid: AsidGuard, /// The job its tables are counted to. job: Arc, } @@ -593,19 +579,11 @@ impl UserTables { free_table(job, root); return None; }; - Some(Self { tables: Tables { root }, asid: Asid::User(asid), job: Arc::clone(job) }) - } - - /// The kernel's own space, whose user half is empty: what a kernel thread - /// and an idle CPU run under. Never dropped. - pub fn kernel() -> Self { - let root = KERNEL_TTBR0.load(core::sync::atomic::Ordering::Acquire) & ADDR; - assert!(root != 0, "paging: the kernel's space before `init`"); - Self { tables: Tables { root }, asid: Asid::Kernel, job: Arc::clone(crate::mm::charge::boot()) } + Some(Self { tables: Tables { root }, asid, job: Arc::clone(job) }) } pub fn root(&self) -> Root { - Root(u64::from(self.asid.value()) << 48 | self.tables.root) + Root(u64::from(self.asid.0.get()) << 48 | self.tables.root) } /// The table of leaves over `va`, if one is built. @@ -814,7 +792,7 @@ impl UserTables { /// the invalidations broadcast in hardware. pub fn shoot_down(&self, start: u64, end: u64, unlinked: &Unlinked) -> bool { const BY_PAGE: u64 = 64; - let asid = self.asid.value(); + let asid = self.asid.0.get(); if !unlinked.is_empty() || (end - start) / PAGE_4K > BY_PAGE { tlb::asid(asid); } else { @@ -830,7 +808,6 @@ impl UserTables { /// issued again. impl Drop for UserTables { fn drop(&mut self) { - assert!(matches!(self.asid, Asid::User(_)), "paging: the kernel's address space dropped"); let all = self.unlink(0, toyos_userbound::USER_TOP, |_, _| false); all.free(&self.job); free_table(&self.job, self.tables.root); diff --git a/kernel/src/arch/aarch64/pio.rs b/kernel/src/arch/aarch64/pio.rs index adddcd26e5b..7f4ed32ab6b 100644 --- a/kernel/src/arch/aarch64/pio.rs +++ b/kernel/src/arch/aarch64/pio.rs @@ -1,36 +1,55 @@ -//! The I/O port space: AArch64 has none, so no port to declare or open, and -//! no line a row raises; the `acpi` claim's row names neither. +//! The I/O port space, which AArch64 has none of, and the lines a row's +//! function raises: an SPI each, routed to the boot CPU and taken by +//! `crate::isa`'s handler for the row ([`row_of`]). -use core::convert::Infallible; +use core::sync::atomic::{AtomicU32, Ordering::Relaxed}; use toyos_userbound::Ports; use crate::process::Pid; -/// No row raises a line, so no wire is resolved. -pub type Wire = Infallible; +/// A line a row's function raises: an SPI, and whether it is level-triggered. +#[derive(Clone, Copy)] +pub struct Wire { + pub intid: u32, + pub level: bool, +} + +/// A routed line. +pub type Line = Wire; -/// No line is routed where no row raises one. -pub type Line = Infallible; +/// Each row's SPI once routed, zero until then: what the interrupt's handler +/// finds its row by. +static ROUTED: [AtomicU32; crate::isa::MAX_ROWS] = [const { AtomicU32::new(0) }; crate::isa::MAX_ROWS]; + +/// Route `wire` to the boot CPU for `row`, masked. +pub fn route(row: usize, wire: Wire) -> Result { + super::irqchip::route_spi(wire.intid, wire.level) + .map_err(|limit| alloc::format!("the distributor takes no SPI {} (its limit is {limit})", wire.intid))?; + ROUTED[row].store(wire.intid, Relaxed); + Ok(wire) +} -pub fn route(_row: usize, wire: Wire) -> Result { - match wire {} +/// The row whose routed line `intid` is. +pub fn row_of(intid: u32) -> Option { + (intid != 0).then(|| ROUTED.iter().position(|routed| routed.load(Relaxed) == intid)).flatten() } pub fn level(line: Line) -> bool { - match line {} + line.level } pub fn describe(wire: Wire) -> alloc::string::String { - match wire {} + alloc::format!("SPI {} {}", wire.intid, if wire.level { "level" } else { "edge" }) } -pub fn same(a: Wire, _b: Wire) -> bool { - match a {} +pub fn same(a: Wire, b: Wire) -> bool { + a.intid == b.intid } -pub fn set_masked(line: Line, _masked: bool) { - match line {} +/// Callable from the line's own handler. +pub fn set_masked(line: Line, masked: bool) { + super::irqchip::set_spi_masked(line.intid, masked); } /// Nothing is declared where there are no ports. diff --git a/kernel/src/arch/aarch64/trap.rs b/kernel/src/arch/aarch64/trap.rs index 1aa14ff2599..3637895faec 100644 --- a/kernel/src/arch/aarch64/trap.rs +++ b/kernel/src/arch/aarch64/trap.rs @@ -137,7 +137,6 @@ extern "C" fn dispatch(frame: &mut Frame, entry: u64) { /// EL1, or an FIQ or SError from anywhere. The panic handler's branch puts it /// on the console and the panel. fn exception(frame: &Frame, entry: u64) -> ! { - crate::log::console::on_the_uart_for_good(); let entry = Entry(entry); log!("KERNEL PANIC: {entry}: {} (ESR={:#010x})", class_name(frame.esr), frame.esr); log!(" elr={:#018x} far={:#018x} spsr={:#010x}", frame.elr, frame.far, frame.spsr); @@ -229,6 +228,11 @@ fn irq(frame: &Frame, from_el0: bool) -> bool { crate::iommu::fault_interrupt(); false } + intid if let Some(row) = super::pio::row_of(intid) => { + percpu::irq_took(Source::UserDev); + crate::isa::isr(row); + true + } _ => match irqchip::its::function_of(intid) { Some(function) => { percpu::irq_took(Source::UserDev); @@ -338,10 +342,6 @@ fn user_abort(frame: &mut Frame) { fn user_fatal(frame: &Frame) -> ! { let prev = percpu::swap_fault_state(CpuFaultState::Fatal); let recursive = matches!(prev, CpuFaultState::Fatal | CpuFaultState::Panic); - // Before the first record of a report that halts the machine. - if recursive { - crate::log::console::on_the_uart_for_good(); - } // First: a panic anywhere below reaches the panic handler as DOUBLE // PANIC, which can only report what was captured here. crate::panic::record_fault(class_name(frame.esr), frame.elr, frame.far, frame.esr); diff --git a/kernel/src/arch/x86_64/boot.rs b/kernel/src/arch/x86_64/boot.rs index b9503a86560..62c853c6a5b 100644 --- a/kernel/src/arch/x86_64/boot.rs +++ b/kernel/src/arch/x86_64/boot.rs @@ -147,6 +147,7 @@ pub fn timer() { /// The platform's own devices that are not PCI functions. pub fn platform_devices(platform: &Platform, rsdp_addr: u64) { super::pio::fill_i8042_row(); + super::pio::fill_uart_row(); super::acpi_mode::init(rsdp_addr, platform.facs); if let Err(why) = super::rtc::arm_update_edges() { log!("clock: no RTC edges time the TSC: {why}"); diff --git a/kernel/src/arch/x86_64/console_uart.rs b/kernel/src/arch/x86_64/console_uart.rs index 7c275c28653..a22d916f725 100644 --- a/kernel/src/arch/x86_64/console_uart.rs +++ b/kernel/src/arch/x86_64/console_uart.rs @@ -1,20 +1,21 @@ //! The console UART: the 16550 at COM1's I/O ports, where every PC this -//! kernel boots puts one if it has one. +//! kernel boots puts one if it has one, and its claim ([`claim`]). use super::cpu::{inb, outb}; -use super::pio::{Port, COM1}; +use super::pio::{Port, COM1, UART_ROW}; +use crate::device::ClaimError; use crate::drivers::serial::Registers; use crate::log; +use crate::object::device::DeviceInfo; +use toyos_abi::uart::{UartInfo, NS16550}; /// COM1's register `n`. const fn reg(n: u16) -> Port { COM1.port(n) } -/// Line status register bits: a received byte waits, and the transmitter -/// holding register is empty. +/// Line status register bit: the transmitter holding register is empty. const LSR: Port = reg(5); -const LSR_DATA_READY: u8 = 0x01; const LSR_THR_EMPTY: u8 = 0x20; /// Bytes the transmitter takes once [`tx_ready`] says so: in FIFO mode @@ -56,16 +57,6 @@ pub fn init(_: &mut Registers, _rsdp_addr: u64) -> bool { loopback == 0xAE } -/// Whether a received byte waits. -pub fn rx_ready(_: &mut Registers) -> bool { - inb(LSR) & LSR_DATA_READY != 0 -} - -/// The received byte; only after [`rx_ready`] said one waits. -pub fn read_byte(_: &mut Registers) -> u8 { - inb(reg(0)) -} - /// Whether the transmitter will take a byte. pub fn tx_ready(_: &mut Registers) -> bool { inb(LSR) & LSR_THR_EMPTY != 0 @@ -78,3 +69,17 @@ pub fn write_byte(_: &mut Registers, byte: u8) { // console output only. unsafe { outb(reg(0), byte) }; } + +/// The claim on the UART's row: COM1's ports, from its first read, and IRQ 4, +/// an ISA line and so an edge. +pub fn claim() -> Result<(crate::isa::Row, DeviceInfo), ClaimError> { + let row = crate::isa::claim_row(UART_ROW)?; + let info = UartInfo { + kind: NS16550, + port: u32::from(COM1.ports().first()), + frame: toyos_abi::HANDLE_INVALID, + frame_len: 0, + level: 0, + }; + Ok((row, DeviceInfo::Uart(info, None))) +} diff --git a/kernel/src/arch/x86_64/entry.rs b/kernel/src/arch/x86_64/entry.rs index f3c653b20e3..294255bc108 100644 --- a/kernel/src/arch/x86_64/entry.rs +++ b/kernel/src/arch/x86_64/entry.rs @@ -5,8 +5,7 @@ //! stays in the registers across every entry, and `arch::fpu` moves it only //! where a switch takes a thread off its CPU. A thread's trampoline loads the //! declared state before anything that can switch, so every save of its area -//! follows a restore on that CPU (`arch::fpu`'s invariant); a kernel thread's -//! loads none, having no user state. +//! follows a restore on that CPU (`arch::fpu`'s invariant). /// `cld`, which every Ring 0 entry runs first: no gate clears `DF`, and this /// kernel's forward-only `memmove`/`memcpy` write backward if a tick @@ -102,26 +101,6 @@ pub(crate) extern "C" fn thread_start() { first_entry!("sub r13, 8") } -/// Entry point for a kernel thread: r12 = body, r14 = argument. Never reaches Ring 3. -// The `sti` is load-bearing: `alloc_kernel_stack` leaves `IF` clear, and `trampoline_entry` requires it clear on entry. -#[unsafe(naked)] -pub(crate) extern "C" fn kernel_start() { - core::arch::naked_asm!( - "call {unlock}", - "sti", - "mov rdi, r14", - "call r12", - "call {returned}", - unlock = sym crate::sched::driver::trampoline_entry, - returned = sym kernel_thread_returned, - ); -} - -/// What [`kernel_start`] calls when a kernel thread's body returns: panics rather than halting silently. -extern "C" fn kernel_thread_returned() -> ! { - panic!("a kernel thread's body returned; nothing runs on this stack now"); -} - /// Lay out, just below `top`, the frame `context_switch` restores a new /// context from, and answer the stack pointer that names it: `trampoline` is diff --git a/kernel/src/arch/x86_64/hw.rs b/kernel/src/arch/x86_64/hw.rs index 691f8e255de..b198a6990b9 100644 --- a/kernel/src/arch/x86_64/hw.rs +++ b/kernel/src/arch/x86_64/hw.rs @@ -268,7 +268,7 @@ pub fn sysret_ss_probe(parkable: &crate::scheduler::Parkable) { let before = read_ss(); write_ss(0); let deadline = Deadline::at(crate::clock::now() + Duration::from_millis(2)); - let _ = watch::wait(parkable, &armed, deadline); + watch::wait_uncancellable(parkable, &armed, deadline); let after = read_ss(); write_ss(percpu::KERNEL_DS); if after == percpu::KERNEL_DS { @@ -294,13 +294,13 @@ impl Hw for KernelHw { let _masked = crate::arch::IrqGuard::close(); // SAFETY: `save`/`restore` are live Box-backed contexts from `SchedPass::finish`, freed only by a later pass; `incoming.thread_pointer` is this kernel's own canonical value for the thread being installed; `area` is the outgoing task's own, saved as it leaves this CPU and restored when it returns into this frame. unsafe { - // A thread's user state area lies above its stack's top (`fpu`); a - // kernel thread and the idle context have none, and switch with - // neither a save nor a restore. The thread resumes in this frame, - // so the restore below reads the area its own save wrote. + // A thread's user state area lies above its stack's top (`fpu`); + // the idle context has none, and switches with neither a save nor a + // restore. The thread resumes in this frame, so the restore below + // reads the area its own save wrote. let area = match (*save).runs { Runs::User(_) => Some((*save).kernel_stack_top), - Runs::Kernel(_) | Runs::Idle => None, + Runs::Idle => None, }; (*save).thread_pointer = cpu::read_fs_base(); (*save).preempt = crate::preempt::count(); diff --git a/kernel/src/arch/x86_64/idt/exceptions.rs b/kernel/src/arch/x86_64/idt/exceptions.rs index 684a6334e5d..56b73fee86b 100644 --- a/kernel/src/arch/x86_64/idt/exceptions.rs +++ b/kernel/src/arch/x86_64/idt/exceptions.rs @@ -362,7 +362,6 @@ fn crash_report_panic(info: &core::panic::PanicInfo, rbp: u64) { /// Double fault handler — runs on IST1. Always from kernel. Never returns. pub(super) fn double_fault_handler(frame: &TrapFrame) -> ! { - crate::log::console::on_the_uart_for_good(); let cr2 = cpu::read_cr2(); let cpu_id = percpu::cpu_id(); let tid = percpu::current_tid(); @@ -449,7 +448,6 @@ pub(super) fn double_fault_handler(frame: &TrapFrame) -> ! { // Untested: firmware leaves CR4.MCE set, so a machine check reaches here, but // nothing in the suite can stage one. pub(super) fn machine_check_handler(frame: &TrapFrame) -> ! { - crate::log::console::on_the_uart_for_good(); log!("MACHINE CHECK on CPU {}", percpu::cpu_id()); let ctx = ExceptionContext { frame, cr2: 0 }; crash_report(&CrashInfo::Exception(&ctx)); @@ -470,11 +468,6 @@ pub(super) fn page_fault_handler(frame: &TrapFrame) { } let fault_addr = cpu::read_cr2(); - // A Ring 0 fault halts below: before its first record. - if !Ring::of_cs(frame.cs).is_user() { - crate::log::console::on_the_uart_for_good(); - } - if frame.error_code & PF_PRESENT != 0 && !Ring::of_cs(frame.cs).is_user() && mm::is_kernel_addr(fault_addr) { @@ -524,11 +517,6 @@ pub(super) fn exception_handler(frame: &TrapFrame) -> ! { fn fatal_exception(ctx: &ExceptionContext) -> ! { let prev = percpu::swap_fault_state(CpuFaultState::Fatal); let recursive = prev == CpuFaultState::Fatal || prev == CpuFaultState::Panic; - // Before the first record of a report that halts the machine. - if recursive || !ctx.ring().is_user() { - crate::log::console::on_the_uart_for_good(); - } - // Must run first: a panic anywhere below reaches the panic handler as // DOUBLE PANIC, which can only report what was captured here. crate::panic::record_fault( diff --git a/kernel/src/arch/x86_64/idt/isa.rs b/kernel/src/arch/x86_64/idt/isa.rs index 121d222b860..f8c85209154 100644 --- a/kernel/src/arch/x86_64/idt/isa.rs +++ b/kernel/src/arch/x86_64/idt/isa.rs @@ -21,3 +21,4 @@ macro_rules! isa_row { isa_row!(isa0_handler, isa0_entry, 0); isa_row!(isa1_handler, isa1_entry, 1); +isa_row!(isa2_handler, isa2_entry, 2); diff --git a/kernel/src/arch/x86_64/idt/mod.rs b/kernel/src/arch/x86_64/idt/mod.rs index a986e9b23e5..908d42e3ab5 100644 --- a/kernel/src/arch/x86_64/idt/mod.rs +++ b/kernel/src/arch/x86_64/idt/mod.rs @@ -32,7 +32,7 @@ pub const KICK_VECTOR: u8 = Vector::Kick as u8; /// The vector each `isa` row's lines are routed to, by row: the vector is /// how the kernel knows whose record an interrupt belongs to. -pub const ISA_VECTORS: [u8; crate::isa::MAX_ROWS] = [Vector::Isa0 as u8, Vector::Isa1 as u8]; +pub const ISA_VECTORS: [u8; crate::isa::MAX_ROWS] = [Vector::Isa0 as u8, Vector::Isa1 as u8, Vector::Isa2 as u8]; /// The vector an IOMMU writes into its own `FEDATA`. pub const DMA_FAULT_VECTOR: u8 = Vector::DmaFault as u8; @@ -249,6 +249,7 @@ idt_vectors! { Rtc = 0x26, rtc::rtc_entry; Isa0 = 0x2C, isa::isa0_entry; Isa1 = 0x2D, isa::isa1_entry; + Isa2 = 0x2E, isa::isa2_entry; Kick = 0xFC, kick::kick_entry; // Never returns: `cli; hlt` forever. HaltAll = 0xFD, stub_halt_all; diff --git a/kernel/src/arch/x86_64/paging.rs b/kernel/src/arch/x86_64/paging.rs index 3bbf2daa10b..10c4027627f 100644 --- a/kernel/src/arch/x86_64/paging.rs +++ b/kernel/src/arch/x86_64/paging.rs @@ -425,23 +425,6 @@ impl Drop for PcidGuard { } } -/// Which tag an address space carries, and who returns it. -enum PcidHandle { - /// The reserved tag 0; the kernel space is leaked, so it is never returned. - Kernel, - /// A user tag, returned to the pool when this handle drops. - User(PcidGuard), -} - -impl PcidHandle { - fn value(&self) -> u16 { - match self { - Self::Kernel => kernel::pcid::KERNEL_PCID, - Self::User(g) => g.0.get(), - } - } -} - /// `None` when all 4095 tags are held by live spaces. The pool lock is held /// across the reclaim shootdown — safe under it because a CPU spinning for the /// lock polls shootdowns, and so that one CPU reclaims at a time. @@ -806,9 +789,9 @@ impl Unlinked { /// otherwise. pub struct UserTables { root: u64, - /// Owned for this space's life: dropping it returns a user tag, so two live + /// Owned for this space's life: dropping it returns the tag, so two live /// spaces can never share one. - pcid: PcidHandle, + pcid: PcidGuard, /// The job its tables are counted to. job: Arc, } @@ -833,19 +816,11 @@ impl UserTables { ); pml4.init_entry(slot, entry); } - Some(Self { root: pml4.phys(), pcid: PcidHandle::User(pcid), job: Arc::clone(job) }) - } - - /// The kernel's own space, whose user half is empty: what a kernel thread - /// and an idle CPU run under. Never dropped. - pub fn kernel() -> Self { - let root = KERNEL.lock().root; - assert!(root != 0, "paging: the kernel's space before `init`"); - Self { root, pcid: PcidHandle::Kernel, job: Arc::clone(crate::mm::charge::boot()) } + Some(Self { root: pml4.phys(), pcid, job: Arc::clone(job) }) } pub fn root(&self) -> Cr3 { - Cr3(self.root | self.pcid.value() as u64) + Cr3(self.root | self.pcid.0.get() as u64) } fn root_table(&self) -> &PageTablePage { @@ -1047,7 +1022,7 @@ impl UserTables { // A table taken away is cached apart from the leaves: by page is not // enough for it. let (start, end) = if unlinked.is_empty() { (start, end) } else { (0, u64::MAX) }; - crate::arch::tlb::user_change(crate::invalidation::Origin::Unmap, self.pcid.value(), start, end) + crate::arch::tlb::user_change(crate::invalidation::Origin::Unmap, self.pcid.0.get(), start, end) } } @@ -1058,7 +1033,6 @@ impl UserTables { /// again. impl Drop for UserTables { fn drop(&mut self) { - assert!(matches!(self.pcid, PcidHandle::User(_)), "paging: the kernel's address space dropped"); let all = self.unlink(0, (ROOT_HIGH_HALF as u64) << 39, |_, _| false); all.free(&self.job); free_table(&self.job, self.root); diff --git a/kernel/src/arch/x86_64/pio.rs b/kernel/src/arch/x86_64/pio.rs index 3727f6edbcb..95a90310da5 100644 --- a/kernel/src/arch/x86_64/pio.rs +++ b/kernel/src/arch/x86_64/pio.rs @@ -7,7 +7,9 @@ //! reaches a declared port**: [`FIXED`] holds what every machine has, and //! [`declare`] what a probe or a firmware table names at boot, each refused //! where it shares a port with a run declared before it. `crate::isa` asks -//! [`holder`] before it opens a row. +//! [`holder`] before it opens a row. The one exception is COM1, the console +//! UART's row ([`fill_uart_row`]), which the kernel writes only where no +//! process holds it (`log::console`). //! //! **Every declaration says what its ports answer the `acpi` claim's holder** //! ([`Mediated`]), whose AML may name any port: [`standing`] is what @@ -74,7 +76,6 @@ pub const CMOS: Declared = Declared::fixed(0x70, 2); /// round every claim. `CONFIG_ADDRESS` is a dword at 0xCF8, so its first port /// alone keeps it refused, and 0xCF9 free for a reset register. const FIXED: &[(&str, Declared, Mediated)] = &[ - ("COM1", COM1, Mediated::Kept), ("the 8259 pair", PIC_PRIMARY, Mediated::Kept), ("the 8259 pair", PIC_SECONDARY, Mediated::Kept), // A write nothing decodes costs the kernel nothing whoever makes it. @@ -240,6 +241,24 @@ pub fn refused_port(access: PortAccess) -> Option { /// `isa`'s row for the i8042. pub const I8042_ROW: usize = 0; +/// `isa`'s row for the console UART. +pub const UART_ROW: usize = 2; + +/// Fill the console UART's row, where the boot found one: COM1's ports and +/// its line, IRQ 4. Only its class claims it. +pub fn fill_uart_row() { + if !crate::drivers::serial::has_console() { + return; + } + let Some(wire) = ioapic::gsi_for_isa_irq(4) else { + return log!("isa: no console UART row — no I/O APIC carries its line"); + }; + crate::isa::fill( + UART_ROW, + crate::isa::Function { name: "the console UART", runs: alloc::vec![COM1.ports()], irqs: alloc::vec![], wires: alloc::vec![wire] }, + ); +} + /// Fill the i8042's row: its data and command ports, its keyboard line and /// its aux line, as `isa:0060,0064:1,12` names them. pub fn fill_i8042_row() { diff --git a/kernel/src/device.rs b/kernel/src/device.rs index 573707d1162..bf580b36ebc 100644 --- a/kernel/src/device.rs +++ b/kernel/src/device.rs @@ -39,8 +39,8 @@ pub struct Claim { /// What one claim holds. A class is at most one device on this machine and a /// per-class flag says whether it is taken; a PCI function is one of several, -/// so what it gives back is its `pcidev` slot; an ISA function's and the ACPI -/// fixed hardware's exclusivity is its `isa` row's. +/// so what it gives back is its `pcidev` slot; an ISA function's, the ACPI +/// fixed hardware's and the console UART's exclusivity is its `isa` row's. /// /// The slot and the row are held as what their modules' calls take, and the /// drop of each is its release: a call borrows one from the claim's `Held`, so @@ -50,6 +50,7 @@ enum Claimed { PciFunction(crate::pcidev::Binding), Isa(crate::isa::Row), Acpi(crate::isa::Row), + Uart(crate::isa::Row), } impl Claim { @@ -72,15 +73,16 @@ impl Claim { pub(crate) fn binding(&self) -> Option<&crate::pcidev::Binding> { match &self.what { Claimed::PciFunction(binding) => Some(binding), - Claimed::Class(_) | Claimed::Isa(_) | Claimed::Acpi(_) => None, + Claimed::Class(_) | Claimed::Isa(_) | Claimed::Acpi(_) | Claimed::Uart(_) => None, } } /// What `isa`'s calls on this claim are made with, or `None` for a claim - /// on anything but an ISA function or the ACPI fixed hardware. + /// on anything but an ISA function, the ACPI fixed hardware or the + /// console UART. pub(crate) fn row(&self) -> Option<&crate::isa::Row> { match &self.what { - Claimed::Isa(row) | Claimed::Acpi(row) => Some(row), + Claimed::Isa(row) | Claimed::Acpi(row) | Claimed::Uart(row) => Some(row), Claimed::Class(_) | Claimed::PciFunction(_) => None, } } @@ -89,7 +91,7 @@ impl Claim { pub(crate) fn add_poll(&self, entry: crate::inbox::PollEntry) { match &self.what { Claimed::PciFunction(binding) => crate::pcidev::add_poll(binding, entry), - Claimed::Isa(row) | Claimed::Acpi(row) => crate::isa::add_poll(row, entry), + Claimed::Isa(row) | Claimed::Acpi(row) | Claimed::Uart(row) => crate::isa::add_poll(row, entry), Claimed::Class(_) => unreachable!("a poll registered through a claim on no function or row"), } } @@ -104,7 +106,7 @@ impl Drop for Claim { // process reaches it. Claimed::PciFunction(_) => {} // The row's drop is its release. - Claimed::Isa(_) => {} + Claimed::Isa(_) | Claimed::Uart(_) => {} // Back to the mode the firmware handed over, and then the row's // drop; and no declaration of the root bridges' memory will // come where its holder made none. @@ -192,6 +194,12 @@ pub fn try_claim(class: DeviceType, selector: [u64; 2]) -> Result { + // The row's own guard, taken inside. + let (row, info) = crate::arch::console_uart::claim()?; + let claim = Claim { what: Claimed::Uart(row) }; + Ok(DeviceClaim::new(class, info, claim)?) + } } } diff --git a/kernel/src/drivers/serial.rs b/kernel/src/drivers/serial.rs index c00c670db6f..eee485863ad 100644 --- a/kernel/src/drivers/serial.rs +++ b/kernel/src/drivers/serial.rs @@ -1,18 +1,17 @@ -//! The console UART and the two locks that serialise it. Where the UART is -//! and what it is are the architecture's (`arch::console_uart`). +//! The console UART, as the kernel writes it (`log::console`), and the lock +//! that serialises the kernel's writers. Where the UART is and what it is are +//! the architecture's (`arch::console_uart`). //! //! [`BackendGuard`] is the registers' lock, held with interrupts off for one -//! burst: what the UART's transmitter takes at once, or a byte read. The -//! panic path takes the registers alone, as a [`PanicUart`], and bypasses them -//! once they stay held. Nothing that holds a kernel lock formats here. +//! burst: what the UART's transmitter takes at once. The panic path takes the +//! registers alone, as a [`PanicUart`], and bypasses them once they stay held. +//! Nothing that holds a kernel lock formats here. use core::cell::Cell; use core::sync::atomic::{AtomicBool, Ordering}; use crate::arch::IrqGuard; use crate::log; use super::serial_lock::{BackendLock, Held, Seized}; -use crate::scheduler::Parkable; -use crate::sleeplock::{Holder, SleepGuard, SleepLock}; use crate::time::{Budget, Duration}; use crate::arch::console_uart as uart; @@ -62,24 +61,6 @@ impl BackendGuard { pub fn write_raw(&mut self, bytes: &[u8]) { uart_write_bytes(&mut self.registers, bytes); } - - pub fn has_data(&mut self) -> bool { - has_console() && uart::rx_ready(&mut self.registers) - } - - pub fn try_read_byte(&mut self) -> Option { - self.has_data().then(|| uart::read_byte(&mut self.registers)) - } -} - -pub fn has_data() -> bool { - let mut g = BackendGuard::lock(); - g.has_data() -} - -pub fn try_read_byte() -> Option { - let mut g = BackendGuard::lock(); - g.try_read_byte() } /// How long, on the clock, a fatal path waits for another holder to let the @@ -91,7 +72,7 @@ const PANIC_LOCK_WAIT: Budget = Budget::of( ); /// The console UART's registers for a fatal path, from [`panic_registers`]. -/// A `log!` under one before `klogd` runs may drain inline on this CPU, as a +/// A `log!` under one before the hand-over may drain inline on this CPU, as a /// burst that `BackendLock::lock` refuses while this CPU's fatal path holds /// the registers. pub struct PanicUart(Hold); @@ -122,7 +103,6 @@ const DRAINED_RAW: &[u8] = /// The registers for a fatal path: waited for while another holder may still /// let them go, and had at once where this CPU's fatal path holds them. pub fn panic_registers() -> PanicUart { - crate::log::console::on_the_uart_for_good(); let irq = IrqGuard::close(); let seized = Cell::new(None); // Uncalibrated, this asks once: only the boot CPU runs, so a holder is beneath this frame and never lets go. @@ -214,62 +194,14 @@ pub unsafe fn panic_flush() { unsafe { crate::log::console::drain_bypassed(&mut uart) }; } -/// The registers for a stop whose wire `klogd` kept, to write its records -/// over the holder through: a line of the holder's still in flight may -/// interleave with them. Never bypasses: every CPU is still live here. -pub fn over_the_wire(write: impl FnOnce(&mut BackendGuard)) { - if !has_console() { - return; - } - let mut uart = panic_registers(); - match &mut uart.0 { - Hold::Held(guard) => write(guard), - Hold::Reentered(_) | Hold::Expired(_) => uart.write(NOT_DRAINED), - } -} - -/// What a stop whose registers stayed held too writes in place of its records. -const NOT_DRAINED: &[u8] = - b"\n[serial] the stop's records are not on the console: its wire and its registers stayed held\n"; - -/// Who may put a line on the wire: `klogd`, the drains that stand in for it -/// before it runs, and the stop, which takes it from `klogd` for good. **Held with -/// interrupts on and preemption allowed**, for a whole line, so a line is one -/// holder's; [`BackendGuard`] is taken inside it once per burst, which is the -/// only interrupts-off window the console costs. -static WIRE: SleepLock<()> = SleepLock::new(()); - -/// The wire, for a task that may park until it is free. -pub fn wire(parkable: &Parkable) -> SleepGuard<'_, ()> { - WIRE.lock(parkable) -} - -/// The wire, if it is free, from any context — the boot before per-CPU state -/// exists included, which has no task to hold it as; and who holds it where -/// it is not. -pub fn try_wire() -> Result, Holder> { - let taken = if crate::log::PERCPU_READY.load(Ordering::Acquire) { - WIRE.try_lock() - } else { - WIRE.try_lock_untasked() - }; - taken.ok_or_else(|| WIRE.holder_name()) -} - -/// Whether the running task holds the wire, for a staging that asks who holds -/// it at the boot's last word. -#[cfg(feature = "boot-actuators")] -pub fn wire_is_mine() -> bool { - WIRE.holder().is_some_and(|holder| Some(holder) == crate::scheduler::current_task()) -} - /// Whether the UART has refused a write, which is said once. static UART_REFUSED: AtomicBool = AtomicBool::new(false); -/// `bytes` onto the wire the caller holds, in bursts of what the UART's -/// transmitter takes at once: each burst waits for the transmitter with -/// interrupts on, and takes the register lock only to ask it and fill it. -pub fn write_wire(_wire: &SleepGuard<'_, ()>, bytes: &[u8]) { +/// `bytes` onto the UART, in bursts of what its transmitter takes at once: +/// each burst waits for the transmitter with interrupts on, and takes the +/// register lock only to ask it and fill it. The boot's and the stop's, +/// whose drains are the UART's one writer while they write. +pub fn write(bytes: &[u8]) { if !has_console() { return; } @@ -287,7 +219,7 @@ pub fn write_wire(_wire: &SleepGuard<'_, ()>, bytes: &[u8]) { drop(burst); asked += 1; // A UART that never empties its FIFO takes the rest of this write - // with it rather than holding the wire for ever, and is said once: + // with it rather than holding the boot for ever, and is said once: // the saying is itself a write it would drop. if asked == THRE_SPIN_LIMIT { if !UART_REFUSED.swap(true, Ordering::Relaxed) { @@ -303,109 +235,6 @@ pub fn write_wire(_wire: &SleepGuard<'_, ()>, bytes: &[u8]) { } } -/// One console holder's partly-written line. Must live per holder, never -/// shared: one buffer used by two processes splices their output. -/// -/// **A console holder does not write the wire.** A whole line goes to -/// `klogd`'s queue ([`crate::log::console::queue`]), and `klogd` puts it on -/// the wire between its own records — so the kernel has one console writer, -/// and a write here never waits on a device. The bytes go as they came: the -/// only holder that writes is `/system/bin/logkeeper`, which renders every control -/// byte a program wrote as text before it gets here. -/// -/// **A write takes the whole lines the queue has room for and no more**, and -/// says how many bytes that was, so a writer that is ahead of the console is -/// told so rather than losing lines it cannot see; its poll for `WRITABLE` is -/// answered when `klogd` frees room. -pub struct ConsoleLine { - buf: [u8; MAX_CONSOLE_LINE], - len: usize, - /// `buf` is a whole piece of a line the queue had no room for, which goes - /// before anything after it; the holder still has the rest of the line. - held: bool, -} - -impl ConsoleLine { - pub const fn new() -> Self { - Self { buf: [0; MAX_CONSOLE_LINE], len: 0, held: false } - } - - /// Take as much of a userland write as ends in lines the queue has room - /// for, and a trailing partial line; answer how many bytes were taken. - /// - /// **A line that does not fit is not taken**: its bytes in this write go - /// back to the holder, which writes them again once it has room. Taken and - /// held here, the last line of a holder with nothing more to say would - /// wait for a write that never comes. - pub fn write(&mut self, src: &crate::user_ptr::UserBytes) -> usize { - if self.held && !self.piece() { - return 0; - } - let mut chunk = [0u8; STRIP_CHUNK]; - let mut off = 0; - // Where this write's part of the line `buf` holds begins. - let mut line = 0; - while off < src.len() { - let n = chunk.len().min(src.len() - off); - src.read_at(off, &mut chunk[..n]); - for (i, &b) in chunk[..n].iter().enumerate() { - let at = off + i; - if self.len == MAX_CONSOLE_LINE { - self.held = true; - if !self.piece() { - return at; - } - line = at; - } - self.buf[self.len] = b; - self.len += 1; - if b == b'\n' { - if !crate::log::console::queue(&self.buf[..self.len], false) { - self.len -= at + 1 - line; - return line; - } - self.len = 0; - line = at + 1; - } - } - off += n; - } - src.len() - } - - /// Queue the whole piece `buf` holds, which the next goes on from. - fn piece(&mut self) -> bool { - if !crate::log::console::queue(&self.buf[..self.len], true) { - return false; - } - self.len = 0; - self.held = false; - true - } - - /// Queues whatever is held, whether or not a newline came, as the holder - /// goes. With the queue full it is counted unshown: nothing waits here. - pub fn finish(&mut self) { - if self.len > 0 && !crate::log::console::queue(&self.buf[..self.len], false) { - crate::log::console::unshown(); - } - self.len = 0; - self.held = false; - } -} - -impl Default for ConsoleLine { - fn default() -> Self { - Self::new() - } -} - -/// Size of one copy out of user memory. -const STRIP_CHUNK: usize = 256; - -/// The longest piece of a line one queue entry carries. -pub const MAX_CONSOLE_LINE: usize = 1024; - /// Bounded, not belt-and-braces: a UART wedged with THRE clear would spin /// forever here, on `panic_flush`'s bypass path where nothing else can help. const THRE_SPIN_LIMIT: u32 = 100_000; diff --git a/kernel/src/iommu/fault.rs b/kernel/src/iommu/fault.rs index 5c6d40aa0ac..58a5ae6fec7 100644 --- a/kernel/src/iommu/fault.rs +++ b/kernel/src/iommu/fault.rs @@ -205,11 +205,6 @@ pub fn report(unit: usize, count: &AtomicU32, fault: Fault) -> bool { (None, Who::Function(stream)) if crate::pcidev::note_fault(stream.requester()) => Owner::Process, (None, _) => Owner::Nobody, }; - // A fault this kernel's own function made halts the machine: before its - // line, as every fatal path's first record. - if matches!(owner, Owner::Kernel) { - crate::log::console::on_the_uart_for_good(); - } let count = count.fetch_add(1, Ordering::Relaxed) + 1; // A stray's line is bounded by its count, never by the device: the next // line it writes says how many were read in between. diff --git a/kernel/src/isa.rs b/kernel/src/isa.rs index e270962c1d3..b8b65c38f48 100644 --- a/kernel/src/isa.rs +++ b/kernel/src/isa.rs @@ -4,8 +4,8 @@ //! //! **What can be claimed is a row the boot filled, matched exactly.** A row is //! filled once, before userland runs, and never changes: the i8042's, named by -//! an `isa:` selector, and the firmware's ACPI fixed hardware, named by its -//! class. A port no row names is never opened, a set that is not a whole row +//! an `isa:` selector, and the firmware's ACPI fixed hardware and the console +//! UART, each named by its class. A port no row names is never opened, a set that is not a whole row //! is refused rather than trimmed to one, and a row with a port this kernel //! declared (`arch::pio::holder`) is refused naming who holds it. //! @@ -50,8 +50,9 @@ use crate::process::Pid; use crate::sync::Lock; use crate::watch::IrqWatch; -/// Every row any architecture fills: the i8042's and the ACPI fixed hardware's. -pub const MAX_ROWS: usize = 2; +/// Every row any architecture fills: the i8042's, the ACPI fixed hardware's +/// and the console UART's. +pub const MAX_ROWS: usize = 3; /// One function a process may be handed whole. pub struct Function { diff --git a/kernel/src/loader/mod.rs b/kernel/src/loader/mod.rs index 974c78e2f4a..5b3511b9e89 100644 --- a/kernel/src/loader/mod.rs +++ b/kernel/src/loader/mod.rs @@ -418,7 +418,7 @@ pub fn spawn( address_space: child_pt.clone(), thread_pointer, job, - image: Some(image), + image, // The spawning thread's, as a new thread's is. mask: crate::sched::driver::current_mask(), }); @@ -456,17 +456,6 @@ pub fn spawn_supervisor(images: crate::bootimage::Objects) -> Pid { let mut handles = HandleTable::new(&job); let mut entries = Vec::new(); let mut labels = Vec::new(); - // One console, the supervisor's descriptors 0 to 2. - let console = KObjectRef::Console(crate::object::device::ConsoleObject::new(&job).expect("spawn_supervisor: no heap for its console")); - for fd in ["0", "1", "2"] { - let entry = crate::object::HandleEntry::new(console.clone(), ops::initial_rights(&console)); - let handle = handles.install(entry).expect("spawn_supervisor: three handles cannot exhaust an empty table"); - let label_off = labels.len() as u32; - labels.extend_from_slice(toyos_abi::syscall::FD_PREFIX.as_bytes()); - labels.extend_from_slice(fd.as_bytes()); - let label_len = labels.len() as u32 - label_off; - entries.push(toyos_abi::syscall::EndowEntry { label_off, label_len, handle, _pad: 0 }); - } let cap = KObjectRef::SysCap(crate::object::syscap::SysCap::new().expect("spawn_supervisor: no heap for the system capability")); // Every machine-wide authority the system has: rights only shrink from // here, so a bit absent here is a bit no manifest can ever name. LOG and @@ -506,7 +495,7 @@ pub fn spawn_supervisor(images: crate::bootimage::Objects) -> Pid { let rights = ops::initial_rights(&object); let handle = handles .install(crate::object::HandleEntry::new(object, rights)) - .expect("spawn_supervisor: a table of five handles refused a boot image"); + .expect("spawn_supervisor: a table of three handles refused a boot image"); entries.push(toyos_abi::syscall::EndowEntry { label_off: labels.len() as u32, label_len: label.len() as u32, diff --git a/kernel/src/loader/start.rs b/kernel/src/loader/start.rs index 8847cb1e5a7..885c259c694 100644 --- a/kernel/src/loader/start.rs +++ b/kernel/src/loader/start.rs @@ -30,29 +30,26 @@ pub(crate) enum Start { Process { entry: toyos_userbound::Entry, sp: u64 }, /// A thread's, on a stack its process chose, with its argument. Thread { entry: toyos_userbound::Entry, sp: u64, arg: u64 }, - /// A kernel thread's body, which never returns to userland. - Kernel { body: extern "C" fn(u64) -> !, arg: u64 }, } -/// A task's kernel stack, which only [`alloc_kernel_stack`] builds. A thread -/// of a process has above its top the area its user machine state is kept in -/// while it is switched out (`arch::fpu`), and a kernel thread has none: a -/// switch's save above [`top`](Self::top) lands inside this allocation. +/// A task's kernel stack, which only [`alloc_kernel_stack`] builds, and above +/// its top the area its thread's user machine state is kept in while it is +/// switched out (`arch::fpu`): a switch's save above [`top`](Self::top) lands +/// inside this allocation. pub(crate) struct KernelStack { alloc: OwnedAlloc, - user: bool, /// The stack, and the records its task keeps for as long as it does. _charge: Charge, } impl KernelStack { /// The heap a thread takes: its stack, its scheduler records, its place - /// on the one watch it may wait on, whose list doubles, and, for a thread - /// of a process, its place in the process's thread table, its own data, - /// and what one of its calls copies in and may hold while it is preempted - /// — two strings at their bound, as a rename's paths, and a page besides, - /// as a namespace's names. A spawn charges what it copies past that. - fn heap(user: bool) -> usize { + /// on the one watch it may wait on, whose list doubles, its place in its + /// process's thread table, its own data, and what one of its calls copies + /// in and may hold while it is preempted — two strings at their bound, as + /// a rename's paths, and a page besides, as a namespace's names. A spawn + /// charges what it copies past that. + fn heap() -> usize { let waiter = 2 * core::mem::size_of::<(usize, u64)>(); let call = 2 * crate::user_ptr::MAX_USER_STR as usize + crate::mm::PAGE_BYTES; let records = kernel::sched::task::TaskBuilder::::allocations().into_iter().map(charge::heap).sum::() @@ -61,13 +58,12 @@ impl KernelStack { let process = charge::map_entry::() + charge::arc::>() + call; - charge::heap(Self::layout_for(user)) + records + if user { process } else { 0 } + charge::heap(Self::layout()) + records + process } - /// What one is allocated as, with the area or without it. - fn layout_for(user: bool) -> core::alloc::Layout { - let area = if user { crate::arch::fpu::area_bytes() } else { 0 }; - core::alloc::Layout::from_size_align(KERNEL_STACK_SIZE + area, crate::mm::PAGE_BYTES) + /// What one is allocated as. + pub(crate) fn layout() -> core::alloc::Layout { + core::alloc::Layout::from_size_align(KERNEL_STACK_SIZE + crate::arch::fpu::area_bytes(), crate::mm::PAGE_BYTES) .expect("a kernel stack's layout") } @@ -81,32 +77,21 @@ impl KernelStack { self.alloc.ptr() as u64 + KERNEL_STACK_SIZE as u64 } - /// Whether its task is a thread of a process, which has the area. - pub(crate) fn is_user(&self) -> bool { - self.user - } - - /// The layout it was allocated with. - #[cfg(feature = "heap-tripwire")] - pub(crate) fn layout(&self) -> core::alloc::Layout { - Self::layout_for(self.user) - } } /// Allocate a kernel stack, charged to the job its task runs in, and lay out /// the frame `context_switch` will restore. pub(crate) fn alloc_kernel_stack(job: &Arc, start: Start) -> Option<(KernelStack, u64)> { - use crate::arch::entry::{kernel_start, process_start, thread_start}; - let (trampoline, user_entry, user_sp, arg, user): (unsafe extern "C" fn(), _, _, _, _) = match start { - Start::Process { entry, sp } => (process_start, entry.addr(), sp, 0, true), - Start::Thread { entry, sp, arg } => (thread_start, entry.addr(), sp, arg, true), - Start::Kernel { body, arg } => (kernel_start, body as usize as u64, 0, arg, false), + use crate::arch::entry::{process_start, thread_start}; + let (trampoline, user_entry, user_sp, arg): (unsafe extern "C" fn(), _, _, _) = match start { + Start::Process { entry, sp } => (process_start, entry.addr(), sp, 0), + Start::Thread { entry, sp, arg } => (thread_start, entry.addr(), sp, arg), }; - let charge = Charge::new(job, KernelStack::heap(user)).ok()?; - let layout = KernelStack::layout_for(user); + let charge = Charge::new(job, KernelStack::heap()).ok()?; + let layout = KernelStack::layout(); let alloc = OwnedAlloc::new(layout.size(), layout.align())?; scheduler::write_stack_canary(&alloc); - let stack = KernelStack { alloc, user, _charge: charge }; + let stack = KernelStack { alloc, _charge: charge }; // SAFETY: `stack` is fresh and exclusively owned, and its `top` ends the stack. let frame = unsafe { crate::arch::entry::initial_frame(stack.top(), trampoline, user_entry, user_sp, arg) }; Some((stack, frame)) @@ -215,26 +200,7 @@ impl Commit for PendingHandles { if !table.make_room(moving.len() + Own::LABELS.len()) || !data.handles.make_room(1) { return Err(SyscallError::ResourceExhausted.into()); } - // The consoles this spawn endowed, by the parent's object, each with - // the child's own: minted before any removal too, since one can be - // refused. A console is minted, not moved: it is its own line buffer, - // and one shared would splice two processes' half-lines. Two - // endowments of one parent console are one child console, or aliased - // stdout and stderr would split in two. - let mut minted: Vec<(usize, KObjectRef)> = Vec::new(); - for (_, handle) in &moving { - if let KObjectRef::Console(parent_console) = data.handles.get_ref(*handle, Rights::NONE)? { - let key = Arc::as_ptr(parent_console) as usize; - if !minted.iter().any(|(seen, _)| *seen == key) { - let console = crate::object::device::ConsoleObject::new(&job).map_err(SyscallError::from)?; - minted.push((key, KObjectRef::Console(console))); - } - } - } - let mut entries = Vec::with_capacity(moving.len()); - // Moved-out console handles, dropped once the parent's lock is let go. - let mut displaced = Vec::new(); // What each device claim moving into the child handed out, taken off // it under the caller's lock and revoked once that lock is let go. let mut handed = Vec::new(); @@ -243,21 +209,9 @@ impl Commit for PendingHandles { .handles .remove(parent_handle) .expect("an endowed handle verified under this lock stopped resolving"); - let moved = match moved.object() { - KObjectRef::Console(parent_console) => { - let key = Arc::as_ptr(parent_console) as usize; - let (_, object) = minted.iter().find(|(seen, _)| *seen == key).expect("each endowed console was minted"); - let object = object.clone(); - let rights = moved.rights(); - displaced.push(moved); - HandleEntry::new(object, rights) - } - KObjectRef::Device(claim) => { - handed.extend(claim.take_mappings()); - moved - } - _ => moved, - }; + if let KObjectRef::Device(claim) = moved.object() { + handed.extend(claim.take_mappings()); + } entry.handle = table .install(moved) .expect("a child table with verified room refused an endowment"); @@ -267,7 +221,6 @@ impl Commit for PendingHandles { let held = ops::install(&mut data.handles, own.process.object().clone()) .expect("a caller's table with verified room refused its child"); drop(data); - drop(displaced); for handed in handed { handed.revoke(); } diff --git a/kernel/src/log/console.rs b/kernel/src/log/console.rs index 97525599ccb..471f8ac1b65 100644 --- a/kernel/src/log/console.rs +++ b/kernel/src/log/console.rs @@ -1,431 +1,172 @@ -//! The kernel's console sink: the one writer of the console wire, `klogd`, -//! and its two drain phases (inline at boot, then the thread). +//! The kernel's console wire, the UART, which the kernel writes only where no +//! process can: inline, as each record commits, until [`hand_over`] gives it +//! to its claim's holder before userland runs; from the moment the stop has +//! stopped userland to the machine's end ([`StopWire`]); and on a fatal path +//! ([`drain_locked`], [`drain_bypassed`]). Between the hand-over and either +//! end the UART is the holder's, which `logkeeper` feeds, and no record of +//! the kernel's touches it. //! -//! **One writer.** `klogd` puts the kernel's records on the wire, and between -//! them the lines console holders write ([`queue`]) — which on this machine is -//! `/system/bin/logkeeper` alone, rendering each program's line with its tag. -//! It holds the wire ([`serial::wire`]) with interrupts on and -//! preemption allowed, and the registers only for one burst at a time. +//! **The console has spoken every record stamped at or before [`spoken`]**: +//! what the inline drain put on the UART, and from the hand-over what +//! `logkeeper` says the console's channels have delivered ([`speak`]). The +//! stop and a fatal path put every record stamped after it on the UART, so +//! they repeat at most what a channel delivered and had not said yet, and +//! lose none of the kernel's. //! -//! [`Drain::Inline`] and [`Drain::Thread`] are phases, not fallbacks: exactly -//! one is active, and `Drain::Inline` *is* [`KLOGD`] being null. -//! -//! **The stop takes the wire from `klogd` for good** ([`take_for_the_stop`]): -//! asked, `klogd` lets it go between two writes, posts the stop and never -//! takes it again, and the stop holds it to the machine's end, so every -//! record the stop commits reaches the wire before a CPU is taken down. A -//! holder that keeps it through [`LET_GO`] has the stop write over it, through -//! the registers, from a position the stop alone walks. -//! -//! **A console holder may carry the log elsewhere** ([`carried_by`]): `klogd` -//! says so on the wire and advances past what it would have written, until -//! the holder lets go or its console ends. A fatal path and the stop take the -//! log back to the UART for good ([`on_the_uart_for_good`]). - -use core::sync::atomic::{AtomicPtr, AtomicU64, Ordering}; +//! **One writer at a time**: the boot's, whose drains take turns through a +//! claim on the wire; then the holder's; then the stop's, once userland is +//! stopped, or a fatal path's, once every other CPU is. A holder the stop did +//! not stop within its budget, which the stop's record names, may still write +//! over it. -use alloc::sync::Arc; +use core::sync::atomic::{AtomicBool, AtomicU64, Ordering}; use toyos_abi::log::LogRecord; -use kernel::sched::task::{WaitClass, WakeCause, WakeReason}; -use kernel::sched::park::notify; -use crate::drivers::serial::{self, BackendGuard, PanicUart, MAX_CONSOLE_LINE}; -use crate::hw::HW; -use crate::sched::driver::{cpus, irq_off}; -use crate::sched::kthread; -use crate::sleeplock::SleepGuard; -use crate::time::{Budget, Deadline, Duration, DEAF_CPU}; -use crate::watch; -use crate::sched::payload::KShared; -use crate::scheduler::{self, Parkable}; -use crate::sync::Lock; +use crate::drivers::serial::{self, BackendGuard, PanicUart}; -use super::handoff::Handoff; use super::read::{drain_ordered, Published, RecordSink, Shards}; -use super::shard; - -const NAME: &str = "klogd"; - -// `emit` finds `klogd` through this, not the process table: the lookup takes a lock, and `emit` runs inside IRQ handlers and every syscall's locked region. -static KLOGD: AtomicPtr> = AtomicPtr::new(core::ptr::null_mut()); -/// Who puts a committed record on the wire. -pub enum Drain { - /// The producer itself, inline immediately after committing. - /// Nothing else runs yet: no thread exists before `klogd`'s spawn, and no CPU takes a scheduler pass this early. - Inline, - /// `klogd`, woken at the commit of the record it will drain. - /// Only a commit or a queued line wakes it — no idle loop, no timer. - Thread, -} - -/// Which phase this machine is in. -pub fn mode() -> Drain { - if KLOGD.load(Ordering::Acquire).is_null() { - Drain::Inline - } else { - Drain::Thread - } -} +/// Whether the UART is its claim's holder's; set once, at [`hand_over`]. +static HANDED: AtomicBool = AtomicBool::new(false); /// One position for every drain context, so a record never reaches the wire -/// twice. Moved only by the holder of the wire, or by the panic path. +/// twice: moved by the inline drain, then by the stop and the fatal paths. static DRAINED: Published = Published::new(); -/// [`StopWire::Kept`]'s position, walked by the stop alone. -static KEPT: Published = Published::new(); +/// The newest stamp the console has spoken: see the module's header. +static SPOKEN: AtomicU64 = AtomicU64::new(0); + +/// A boot drain's claim on the wire, which a drain that finds it held leaves +/// to its holder: that drain walks to the newest commit before letting go. +static WIRE: AtomicBool = AtomicBool::new(false); /// Positions for each of the `cpus` CPUs' shards. pub fn size(cpus: usize) { DRAINED.size(cpus); - KEPT.size(cpus); } -/// Start the thread. Called once, from `kernel_main`, before the scheduler starts. -/// Placement matters: APs spin until the machine is released, so an earlier spawn could not run while the machine has no console. -pub fn start() { - let sched = kthread::spawn(NAME, body, 0); - // Leaked: `klogd` never exits, and a producer reading this pointer under lock may not touch a refcount. - let shared: &'static Arc = alloc::boxed::Box::leak(alloc::boxed::Box::new(sched.shared)); - KLOGD.store(shared as *const _ as *mut _, Ordering::Release); +/// Whether a record's producer drains it to the UART: before the hand-over. +pub fn inline() -> bool { + !HANDED.load(Ordering::Acquire) } -/// Post the wake this producer owns; called from `emit` after its publication bracket has closed, and from [`queue`]. -pub fn post_wake() { - let ptr = KLOGD.load(Ordering::Acquire); - if ptr.is_null() { +/// Put every committed record this boot has not yet spoken on the UART. +/// Declining loses nothing: the record stays committed, and the drain that +/// holds the wire takes it too. +pub fn drain_inline() { + if !inline() || !serial::has_console() { return; } - // SAFETY: leaked once from a `Box`, never cleared; live for the machine's life. - let shared = unsafe { &*ptr }; - // One read-modify-write of `klogd`'s own word and no lock: it claims a parked or committing `klogd` - // and flags a running one, whose next commit refuses — the post a producer holding any lock may make. - irq_off(|guard| { - notify(shared, WakeCause::new(WakeReason::Woken), cpus(), &HW, guard); - }); -} - -/// Put every committed record this machine has not yet spoken on the wire, -/// where the wire is free: the boot before `klogd` runs, and the UART's -/// arrival. Declining loses nothing: the record stays committed, and whoever -/// holds the wire drains it too. -pub fn drain_inline() { - if !serial::has_console() { + if WIRE.swap(true, Ordering::Acquire) { return; } - let Ok(wire) = serial::try_wire() else { return }; - drain_records(&wire, u64::MAX, Yield::Never); + drain_records(); + WIRE.store(false, Ordering::Release); } -/// Whether a drain lets the wire go to the stop that asks for it. -#[derive(Clone, Copy, PartialEq, Eq)] -enum Yield { - /// `klogd`'s: it stops between two writes once the stop asks. - ToTheStop, - Never, +/// Give the UART to its claim's holder, having spoken every record committed +/// so far: the boot's last inline drain. The boot CPU's, once, before +/// userland can claim the UART. +pub fn hand_over() { + drain_inline(); + HANDED.store(true, Ordering::Release); } -/// The stop's claim on the wire, which `klogd` answers. -static HANDOFF: Handoff = Handoff::new(); - -/// Posted by `klogd` as it lets the wire go once the stop has asked for it. -static RELEASED: watch::Watch = watch::Watch::new(); - -/// Let the wire go, and answer whether the stop has asked for it, posting the -/// stop where it has: the stop's next try finds it free or this read sees -/// the ask (`log/handoff.rs`). -fn let_wire_go(wire: SleepGuard<'_, ()>) -> bool { - drop(wire); - let asked = HANDOFF.asked(); - if asked { - RELEASED.post(); +/// Every committed record onto the UART, through its registers a burst at a +/// time, and the newest spoken stamp moved past each. +fn drain_records() { + struct Wire; + impl RecordSink for Wire { + fn put(&mut self, record: &LogRecord) -> bool { + write_line(record, serial::write); + SPOKEN.fetch_max(record.at_ns, Ordering::Relaxed); + true + } } - asked + drain_ordered(&Shards, &mut &DRAINED, &mut Wire); } -/// How long the stop waits for `klogd` to let the wire go once asked. -/// -/// `klogd` lets go between two writes, so what it owes is the write it is in: -/// one rendered line, at most [`LINE_BYTES`], which the slowest wire this -/// kernel programs, the 16550 at 38400 baud, takes in 300 ms. [`DEAF_CPU`]'s -/// span is far past that and is the span after which a CPU that has not run -/// what it was given is a wedged one: a `klogd` that has not let go by then is -/// on one, or holds the wire through a defect. -pub(crate) const LET_GO: Budget = Budget::of( - Duration::from_nanos(DEAF_CPU.nanos()), - "the stop says who kept the wire, and writes every record over it through the registers", -); +/// The newest stamp the console has spoken. +pub fn spoken() -> u64 { + SPOKEN.load(Ordering::Relaxed) +} -/// The console as the stop holds it, from the moment it has stopped every -/// console holder to the machine's end. Never dropped: what holds one ends -/// the machine. -pub enum StopWire { - /// The wire, which `klogd` let go of and never takes again. - Held(SleepGuard<'static, ()>), - /// Its holder kept the wire through [`LET_GO`]; the stop writes over it - /// from [`KEPT`], a copy of the position taken once, so a holder still - /// inside a drain cannot move it back under the stop. - Kept, +/// The console's channels have delivered every record stamped at or before +/// `at_ns`, as `logkeeper` counts them; answers the newest spoken stamp, +/// which never moves back, nor past the clock: no record committed after the +/// word, a fatal path's among them, is kept off the UART by it. +pub fn speak(at_ns: u64) -> u64 { + let at_ns = at_ns.min(crate::clock::stamp().map_or(0, |now| now.nanos())); + SPOKEN.fetch_max(at_ns, Ordering::Relaxed).max(at_ns) } +/// The console as the stop holds it, from the moment userland has stopped to +/// the machine's end. Never dropped: what holds one ends the machine. +pub struct StopWire(()); + impl StopWire { - /// Every committed record onto the console, and every line console - /// holders queued where the wire is held. + /// Every committed record the console has not spoken onto the UART. pub fn drain(&mut self) { - match self { - Self::Held(wire) => { - drain_records(wire, u64::MAX, Yield::Never); - drain_queue(wire, usize::MAX, Yield::Never); - } - Self::Kept => serial::over_the_wire(|guard| { - drain_ordered(&Shards, &mut &KEPT, &mut Registers { out: guard }); - }), + if !serial::has_console() { + return; } + let mut sink = Unspoken { spoken: Some(spoken()), out: serial::write }; + drain_ordered(&Shards, &mut &DRAINED, &mut sink); } } -/// The wire, for the stop once it has stopped every console holder, held from -/// here to the machine's end; and every queued line onto it, so what a holder -/// queued before it was stopped goes on the wire under the boot's last word -/// and never after it. -/// -/// Waits on the holder's letting go, bounded by [`LET_GO`]: past it the stop -/// alerts by name and writes over the holder (a line of the holder's still in -/// flight may interleave, and the lines console holders queued are lost, -/// which the alert says). The record backlog stays undrained here, so the -/// sync behind this is not spent behind a slow wire: the boot's last word -/// drains it. -pub fn take_for_the_stop(parkable: &Parkable) -> StopWire { - on_the_uart_for_good(); - // Armed before the ask: a release posted between the ask and the park is - // one the park returns on at once. - let armed = watch::arm(&RELEASED, 0, WaitClass::Other).expect("console: the stop holds no task to park"); - HANDOFF.ask(); - // A shipping `klogd` never parks holding the wire; a staged one does, and - // reads the ask only once woken. - #[cfg(feature = "boot-actuators")] - if crate::actuator::wire_held_across_the_stop() { - post_wake(); - } - let deadline = Deadline::at(crate::clock::now() + LET_GO.duration()); - let mut refused = None; - loop { - let late = deadline.reached(crate::clock::now()); - match serial::try_wire() { - Ok(wire) => { - if let (true, Some(holder)) = (late, &refused) { - crate::alert!("console: {holder} held the wire through the stop's {LET_GO}, and let it go only after"); - } - drain_queue(&wire, usize::MAX, Yield::Never); - return StopWire::Held(wire); - } - Err(holder) if late => { - crate::alert!( - "console: {holder} kept the wire through the stop's {LET_GO}; the lines console holders queued \ - are not on it" - ); - KEPT.copy_from(&DRAINED); - return StopWire::Kept; - } - Err(holder) => refused = Some(holder), - } - watch::wait_uncancellable(parkable, &armed, deadline); +/// The UART, for the stop once it has stopped userland: the holder's last +/// line ended, so the stop's first is a line of its own. +pub fn take_for_the_stop() -> StopWire { + if !inline() { + serial::write(b"\n"); } + StopWire(()) } -/// Records and queued lines `klogd` takes per hold of the wire, so a console -/// holder's line is never behind the whole backlog of records, nor the other -/// way round. -const CHUNK: u64 = 8; - -/// The whole record backlog through the registers the panic path holds. -/// Unbounded because interrupt latency doesn't matter while halting, and the report should be whole. +/// Every record the console has not spoken through the registers the panic +/// path holds. Unbounded because interrupt latency doesn't matter while +/// halting, and the report should be whole. pub fn drain_locked(guard: &mut BackendGuard) { - let mut sink = Registers { out: guard }; - drain_ordered(&Shards, &mut &DRAINED, &mut sink); -} - -/// Advances the position with no UART, or while a holder carries the log -/// (`carried`), until it no longer does: standing still, an armed waiter -/// would find the same record on every rescan and spin. Safe to advance: -/// shards keep every record for the panel and the log's reader regardless. -fn discard_pending(carried: bool) { - let mut sink = Discard { carried }; - drain_ordered(&Shards, &mut &DRAINED, &mut sink); + let spoken = after_the_holder(|bytes| guard.write_raw(bytes)); + drain_ordered(&Shards, &mut &DRAINED, &mut Unspoken { spoken, out: |bytes: &[u8]| guard.write_raw(bytes) }); } -/// At most `budget` records onto the wire the caller holds. Returns how many went. -fn drain_records(wire: &SleepGuard<'_, ()>, budget: u64, yields: Yield) -> u64 { - let mut sink = Wire { wire, records: 0, budget, yields }; - drain_ordered(&Shards, &mut &DRAINED, &mut sink); - sink.records -} - -/// Drain straight to the 16550, over whoever holds its registers. +/// Drain straight to the UART, over whoever holds its registers. /// /// # Safety /// Panic path only, once `serial::panic_flush` found no clean handoff; the position is unsynchronised and a record may reach the wire twice. pub unsafe fn drain_bypassed(uart: &mut PanicUart) { - let mut sink = Raw { uart }; - drain_ordered(&Shards, &mut &DRAINED, &mut sink); -} - -/// Who carries the log off this machine ([`kernel::carrier`]). -static CARRIER: kernel::carrier::Carrier = kernel::carrier::Carrier::new(); - -/// What `klogd` puts on the wire as a holder takes the log off it, and as the -/// log comes back to it: the UART's account of where its lines went. -pub const CARRIED_ELSEWHERE: &str = - "console: the log is carried elsewhere from here; the UART takes the stop's and a fatal path's lines"; -pub const BACK_ON_THE_UART: &str = "console: the UART carries the log again"; - -/// The console holder `holder` carries the log from now, and `klogd` advances -/// past every record and queued line without putting it on the UART. -pub fn carried_by(holder: usize) -> Result<(), kernel::carrier::Refused> { - CARRIER.carry(holder)?; - post_wake(); - Ok(()) -} - -/// `holder` no longer carries the log, by its word or by its console's end. -pub fn released_by(holder: usize) { - if CARRIER.release(holder) { - post_wake(); - } + let spoken = after_the_holder(|bytes| uart.write(bytes)); + drain_ordered(&Shards, &mut &DRAINED, &mut Unspoken { spoken, out: |bytes: &[u8]| uart.write(bytes) }); } -/// Every record from here goes on the UART, whoever carried the log: a fatal -/// path's, before its first record, and the stop's. -pub fn on_the_uart_for_good() { - CARRIER.for_good(); -} - -/// Lines console holders queued for the wire. Fixed, because a console -/// holder's write may neither allocate nor wait: a line that finds it full is -/// counted in [`UNSHOWN`] and said by `klogd`. -const QUEUED_LINES: usize = 64; - -struct Queue { - lines: [[u8; MAX_CONSOLE_LINE]; QUEUED_LINES], - lens: [u16; QUEUED_LINES], - /// Whether the entry is a piece of a line the next entry goes on with. - continues: [bool; QUEUED_LINES], - head: usize, - len: usize, -} - -static QUEUE: Lock = Lock::new(Queue { - lines: [[0; MAX_CONSOLE_LINE]; QUEUED_LINES], - lens: [0; QUEUED_LINES], - continues: [false; QUEUED_LINES], - head: 0, - len: 0, -}); - -/// Whether the wire's last bytes are a queued line's piece its holder goes on -/// with, so a record put on the wire first ends that line. Read and written -/// only by the holder of the wire. -static MID_LINE: core::sync::atomic::AtomicBool = core::sync::atomic::AtomicBool::new(false); - -/// End a queued line left mid-way on the wire, before anything else goes on it. -fn end_the_line(wire: &SleepGuard<'_, ()>) { - if MID_LINE.swap(false, Ordering::Relaxed) { - serial::write_wire(wire, b"\n"); - } -} - -/// Lines refused a full [`QUEUE`] since `klogd` last said so. -static UNSHOWN: AtomicU64 = AtomicU64::new(0); - -/// One console holder's line, or a piece of one it `continues` in the next, -/// for `klogd` to put on the wire; `false` is a full queue, which the holder's -/// write answers as bytes it did not take. Never waits. -pub fn queue(line: &[u8], continues: bool) -> bool { - let line = &line[..line.len().min(MAX_CONSOLE_LINE)]; - { - let mut queue = QUEUE.lock(); - if queue.len == QUEUED_LINES { - return false; - } - let at = (queue.head + queue.len) % QUEUED_LINES; - queue.lines[at][..line.len()].copy_from_slice(line); - queue.lens[at] = line.len() as u16; - queue.continues[at] = continues; - queue.len += 1; - } - // The same wake a committed record takes: the store above precedes the - // fence `signal_after_commit` runs, which `klogd`'s re-scan pairs with. - if shard::signal_after_commit(shard::log_waiter()) { - post_wake(); +/// What a fatal path starts with: a line of its own, where a holder may have +/// left one unended, and the newest stamp spoken; before the hand-over the +/// position alone says what is spoken, an untimed boot's records among it. +fn after_the_holder(mut write: impl FnMut(&[u8])) -> Option { + if inline() { + return None; } - true + write(b"\n"); + Some(spoken()) } -/// A line a holder let go of with no room for it: the one kind of console -/// loss, counted and said by `klogd`. -pub fn unshown() { - UNSHOWN.fetch_add(1, Ordering::Relaxed); +/// Records stamped after `spoken` onto `out`; the rest advance the position +/// with no byte built. +struct Unspoken { + spoken: Option, + out: F, } -/// Whether a console holder's next line would be taken. -pub fn has_room() -> bool { - QUEUE.lock().len < QUEUED_LINES -} - -/// Room in the queue: what a console holder that found it full polls for -/// `WRITABLE` on, posted by `klogd` as it frees room. -pub static SPACE: watch::Watch = watch::Watch::new(); - -/// At most `budget` queued lines onto the wire the caller holds, each ended -/// with a newline, so a line a holder left unended is not joined to the next. -/// A line's pieces go on together, whatever the budget: they are one line, -/// and a queue's worth past it at most. Answers whether it freed room. -fn drain_queue(wire: &SleepGuard<'_, ()>, budget: usize, yields: Yield) -> bool { - let mut line = [0u8; MAX_CONSOLE_LINE + 1]; - let mut freed = false; - let (mut ended, mut taken) = (0, 0); - // A holder that never ends its line gets a queue's worth past the budget. - while (ended < budget || MID_LINE.load(Ordering::Relaxed)) && taken < budget.saturating_add(QUEUED_LINES) { - // A line left mid-way is the stop's to end: `MID_LINE` goes with the wire. - if yields == Yield::ToTheStop && HANDOFF.asked() { - break; - } - taken += 1; - let (len, continues) = { - let mut queue = QUEUE.lock(); - if queue.len == 0 { - break; - } - let at = queue.head; - let len = queue.lens[at] as usize; - line[..len].copy_from_slice(&queue.lines[at][..len]); - queue.head = (at + 1) % QUEUED_LINES; - queue.len -= 1; - (len, queue.continues[at]) - }; - freed = true; - let end = if continues || line[..len].ends_with(b"\n") { - len - } else { - line[len] = b'\n'; - len + 1 - }; - serial::write_wire(wire, &line[..end]); - MID_LINE.store(continues, Ordering::Relaxed); - if !continues { - ended += 1; +impl RecordSink for Unspoken { + fn put(&mut self, record: &LogRecord) -> bool { + if self.spoken.is_none_or(|spoken| record.at_ns > spoken) { + write_line(record, &mut self.out); } + true } - freed -} - -/// Empty the queue with no backend to put it on; whether it freed room. -fn discard_queue() -> bool { - let mut queue = QUEUE.lock(); - let dropped = queue.len; - queue.len = 0; - dropped > 0 } /// Sized for the tag, the ABI's widest bracket, the message, and the elision note. @@ -482,289 +223,3 @@ pub fn write_line(record: &LogRecord, emit: impl FnMut(&[u8])) { let _ = write!(line, "{record}"); line.finish(); } - -/// Records onto a held wire, up to a budget; `put` returns false before the refused record, so the next hold starts there. -struct Wire<'w, 'g> { - wire: &'w SleepGuard<'g, ()>, - records: u64, - budget: u64, - yields: Yield, -} - -impl RecordSink for Wire<'_, '_> { - fn put(&mut self, record: &LogRecord) -> bool { - if self.records >= self.budget || (self.yields == Yield::ToTheStop && HANDOFF.asked()) { - return false; - } - end_the_line(self.wire); - write_line(record, |bytes| serial::write_wire(self.wire, bytes)); - self.records += 1; - true - } -} - -/// Records through the registers the panic path holds. -struct Registers<'a> { - out: &'a mut BackendGuard, -} - -impl RecordSink for Registers<'_> { - fn put(&mut self, record: &LogRecord) -> bool { - write_line(record, |bytes| self.out.write_raw(bytes)); - true - } -} - -/// Advances the position; no bytes are built. Read after its record, so a -/// fatal path's records, which follow its [`on_the_uart_for_good`], are -/// never advanced past while a holder carried the log. -struct Discard { - carried: bool, -} - -impl RecordSink for Discard { - fn put(&mut self, _record: &LogRecord) -> bool { - !self.carried || CARRIER.elsewhere() - } -} - -/// Records straight to the 16550, for the bypass. Bounded per byte. -struct Raw<'a> { - uart: &'a mut PanicUart, -} - -impl RecordSink for Raw<'_> { - fn put(&mut self, record: &LogRecord) -> bool { - write_line(record, |bytes| self.uart.write(bytes)); - true - } -} - -extern "C" fn body(_arg: u64) -> ! { - - let parkable = scheduler::Parkable::at_entry(); - let handle = crate::sched::driver::current_handle().expect("klogd runs as a task"); - // Whether the wire last said the log is carried elsewhere. - let mut said_elsewhere = false; - loop { - let (freed, asked) = if serial::has_console() { - // A chunk of each per hold, with interrupts on throughout. - let wire = serial::wire(&parkable); - #[cfg(feature = "boot-actuators")] - staged::hold_if_staged(&parkable, &handle); - let elsewhere = CARRIER.elsewhere(); - if elsewhere != said_elsewhere { - said_elsewhere = elsewhere; - crate::log!("{}", if elsewhere { CARRIED_ELSEWHERE } else { BACK_ON_THE_UART }); - // The change's own line is on the wire before the UART goes - // quiet, so what the UART carried ends at it. - if elsewhere { - drain_records(&wire, u64::MAX, Yield::ToTheStop); - } - } - if elsewhere { - discard_pending(true); - (discard_queue(), let_wire_go(wire)) - } else { - drain_records(&wire, CHUNK, Yield::ToTheStop); - let freed = drain_queue(&wire, CHUNK as usize, Yield::ToTheStop); - (freed, let_wire_go(wire)) - } - } else { - discard_pending(false); - (discard_queue(), HANDOFF.asked()) - }; - if asked { - let_go(&parkable, &handle); - } - // A holder that found the queue full waits on room; this is the room. - if freed { - SPACE.post(); - } - let unshown = UNSHOWN.swap(0, Ordering::Relaxed); - if unshown > 0 { - crate::log!( - "console: {unshown} line(s) a console holder wrote went unshown: they came faster \ - than this console takes them, and the log has them" - ); - } - - // The one point with committed records just observed that may take a lock; `emit` may not. - // Outside `drain_inline`: that function's other callers (a producer mid-`emit`, the panic path) may not take a watch's lock. - super::user::post_readiness(); - - // Registered each pass, after the drain and before the recheck: the - // wire's lock may park this thread on its own watch, and a task waits - // on one watch at a time. `post_wake` notifies this thread's word - // directly; nothing else posts this watch but the thread's own end. - let armed = watch::arm(handle.watch(), 0, WaitClass::Other).expect("klogd runs as a task"); - // Safe with no UART, or with the log carried elsewhere, because `discard_pending` still advances the position each pass. - if shard::arm_waiter(shard::log_waiter(), || { - // The registration above forgot the staging's wake if it came - // while this thread ran: the staging's own word is the recheck. - #[cfg(feature = "boot-actuators")] - if staged::pending() { - return true; - } - // Under the lock `queue` stores under, ahead of the fence its wake takes. - DRAINED.any_pending() || QUEUE.lock().len > 0 - }) { - continue; - } - // No deadline: a spurious wake costs a re-drain; a missing one is what W3's fences prevent. - // `klogd` is never killed, so this cancel arm is unreachable. - let _ = watch::wait(&parkable, &armed, crate::time::Deadline::never()); - } -} - -/// `klogd`'s end: the wire is the stop's, and this thread parks for the -/// machine's life, a commit's wake answered by parking again. -fn let_go(parkable: &Parkable, handle: &crate::sched::payload::TaskHandle) -> ! { - // The backlog this leaves reaches the wire at the boot's last word. - crate::log!("console: klogd let the wire go to the stop"); - loop { - let armed = watch::arm(handle.watch(), 0, WaitClass::Other).expect("klogd runs as a task"); - let _ = watch::wait(parkable, &armed, crate::time::Deadline::never()); - } -} - -#[cfg(feature = "boot-actuators")] -pub use staged::{hold_across_the_stop, hold_at_the_last_word, sealed}; - -/// `klogd` inside its hold of the wire across the stop, which only an -/// actuator stages. -#[cfg(feature = "boot-actuators")] -mod staged { - use core::sync::atomic::{AtomicBool, AtomicU8, Ordering}; - - use kernel::sched::task::WaitClass; - use crate::sched::payload::TaskHandle; - - use crate::drivers::serial; - use crate::scheduler::{Parkable, TaskId}; - use crate::time::{Deadline, Duration, DEAF_CPU}; - use crate::watch; - - /// What the stop staged `klogd`'s next hold of the wire to be. - static STAGED: AtomicU8 = AtomicU8::new(NONE); - const NONE: u8 = 0; - /// Held until the stop asks for the wire, or, with - /// `wire-kept-through-the-stop`, for good. - const ACROSS_THE_STOP: u8 = 1; - /// Held from the boot's last word until the stop has taken the seal. - const AT_THE_LAST_WORD: u8 = 2; - - /// Set by `klogd` once it holds the wire, and posted. - static HOLDING: AtomicBool = AtomicBool::new(false); - static HELD: watch::Watch = watch::Watch::new(); - /// Set by the stop once it has taken the seal. - static SEALED: AtomicBool = AtomicBool::new(false); - - /// The stop's, before it takes anything down: `klogd` holds the wire, and - /// lets it go when the stop asks for it, as a shipping one does. - pub fn hold_across_the_stop(parkable: &Parkable) { - stage(parkable, ACROSS_THE_STOP); - } - - /// The stop's, just before the boot's last word: where the wire is not - /// the stop's, `klogd` holds it, parked inside its hold until the stop - /// has taken the seal, and then lets it go the moment it runs. - pub fn hold_at_the_last_word(parkable: &Parkable) { - if serial::wire_is_mine() { - crate::log!("console: the stop holds the wire at the boot's last word, staged"); - return; - } - stage(parkable, AT_THE_LAST_WORD); - } - - /// The stop's, once it has taken the seal: a `klogd` staged at the last - /// word is runnable from here, on whatever CPU will run it. - pub fn sealed() { - SEALED.store(true, Ordering::Release); - super::post_wake(); - } - - fn stage(parkable: &Parkable, what: u8) { - let armed = watch::arm(&HELD, 0, WaitClass::Other).expect("the stop holds a task"); - STAGED.store(what, Ordering::Release); - super::post_wake(); - // No bound in guest time, which runs on while a host starves the - // guest: the harness's ceiling, scaled by the host, is this wait's. - // `DEAF_CPU` into it, what the scheduler holds is said once. - let mut deadline = Some(Deadline::at(crate::clock::now() + Duration::from_nanos(DEAF_CPU.nanos()))); - while !HOLDING.load(Ordering::Acquire) { - watch::wait_uncancellable(parkable, &armed, deadline.unwrap_or(Deadline::never())); - if deadline.is_some_and(|at| at.reached(crate::clock::now())) && !HOLDING.load(Ordering::Acquire) { - say_klogd_unrun(); - deadline = None; - } - } - } - - /// What the judge reds on: `klogd`'s state and this CPU's run queue, - /// `DEAF_CPU` into a wait for a woken `klogd` to take the wire. - fn say_klogd_unrun() { - let ptr = super::KLOGD.load(Ordering::Acquire); - // SAFETY: leaked once from a `Box`, never cleared; set before userland, which asked for this stop, ran. - let state = unsafe { &*ptr }.state(); - let mut ready = [None::; 8]; - let mut queued = 0usize; - let read = crate::sched::driver::irq_off(|_| { - crate::sched::driver::for_each_ready(|id| { - if let Some(slot) = ready.get_mut(queued) { - *slot = Some(id); - } - queued += 1; - }) - }); - crate::log!( - "console: the staged klogd has not taken the wire {DEAF_CPU} into the stop's wait: klogd is {state:?}, \ - and the stop's {:?} has its run queue {}read, {queued} ready", - crate::sched::driver::current_cpu(), - if read { "" } else { "not " }, - ); - for id in ready.iter().flatten() { - crate::log!("console: ready behind the staged klogd's wait: {id}"); - } - } - - /// Whether the stop has staged a hold `klogd` has not yet taken: stored - /// before the stage's wake, so `klogd`'s park, which a wake made before its - /// registration does not end, reads it after registering. - pub fn pending() -> bool { - STAGED.load(Ordering::Acquire) != NONE - } - - /// `klogd`'s, inside its hold of the wire: a staged hold parks here, the - /// wire held, until what the stop staged it for has passed; then `body` - /// drains and lets the wire go as a shipping `klogd` does. - pub fn hold_if_staged(parkable: &Parkable, handle: &TaskHandle) { - let what = STAGED.swap(NONE, Ordering::Acquire); - if what == NONE { - return; - } - // `klogd`'s own: the wake this commit owes is spent on `klogd`'s next - // park, so the stop's commits after it wake nothing, and only the - // stop's ask can end the hold. - match what { - ACROSS_THE_STOP => crate::log!("console: klogd holds the wire as the stop begins, staged"), - AT_THE_LAST_WORD => crate::log!("console: klogd holds the wire at the boot's last word, staged"), - _ => unreachable!("console: a staged hold of {what}"), - } - HOLDING.store(true, Ordering::Release); - HELD.post(); - let kept = crate::actuator::wire_kept_through_the_stop(); - loop { - let armed = watch::arm(handle.watch(), 0, WaitClass::Other).expect("klogd runs as a task"); - let over = match what { - ACROSS_THE_STOP => !kept && super::HANDOFF.asked(), - AT_THE_LAST_WORD => SEALED.load(Ordering::Acquire), - _ => unreachable!("console: a staged hold of {what}"), - }; - if over { - return; - } - let _ = watch::wait(parkable, &armed, Deadline::never()); - } - } -} diff --git a/kernel/src/log/handoff.rs b/kernel/src/log/handoff.rs deleted file mode 100644 index 8a133694426..00000000000 --- a/kernel/src/log/handoff.rs +++ /dev/null @@ -1,46 +0,0 @@ -//! The stop's claim on the console wire, and its holder's answer to it. -//! -//! The stop [`Handoff::ask`]s and then tries the wire; every holder lets the -//! wire go and then reads [`Handoff::asked`], and posts that it let go when it -//! was. Each side's write and its later read are split by a `SeqCst` fence, so -//! at least one of them sees the other's: the stop's try finds the wire free, -//! or the holder sees the ask and posts. Without them both can read stale, and the -//! stop waits out its whole budget on a wire nobody holds. -//! Compiled a second time by `kernel-loom`, whose `console_handoff` drives -//! both sides over the real `SleepLock`. - -#[cfg(not(feature = "loom"))] -use core::sync::atomic::{fence, AtomicBool, Ordering}; - -#[cfg(feature = "loom")] -use loom::sync::atomic::{fence, AtomicBool, Ordering}; - -pub struct Handoff { - asked: AtomicBool, -} - -impl Handoff { - /// Must stay `const`: the console's hand-off is a `static`. - #[cfg(not(feature = "loom"))] - pub const fn new() -> Self { - Self { asked: AtomicBool::new(false) } - } - - #[allow(clippy::new_without_default)] - #[cfg(feature = "loom")] - pub fn new() -> Self { - Self { asked: AtomicBool::new(false) } - } - - /// The stop's, once, before it first tries the wire. - pub fn ask(&self) { - self.asked.store(true, Ordering::Relaxed); - fence(Ordering::SeqCst); - } - - /// `klogd`'s, after it let the wire go, and between two writes it makes. - pub fn asked(&self) -> bool { - fence(Ordering::SeqCst); - self.asked.load(Ordering::Relaxed) - } -} diff --git a/kernel/src/log/mod.rs b/kernel/src/log/mod.rs index fecbfb3d0bc..1eb732d3a52 100644 --- a/kernel/src/log/mod.rs +++ b/kernel/src/log/mod.rs @@ -6,7 +6,6 @@ #![warn(clippy::undocumented_unsafe_blocks)] pub mod console; -mod handoff; pub mod read; pub mod registry; pub mod shard; @@ -223,21 +222,12 @@ pub fn emit(severity: Severity, args: core::fmt::Arguments) { unsafe { origin.shard.commit(seq, &record, &guard) }; drop(guard); - // The two `Drain` modes are boot phases, not interchangeable fallbacks, - // and `console::mode` is the single word read for it rather than a flag - // kept beside it. - match console::mode() { - // Nothing else can run yet: the producer is the drainer, which is - // what makes a boot that wedges before the idle loop say everything - // it had logged. - console::Drain::Inline => console::drain_inline(), - // `emit` may take no lock, so the wake is a fence-guarded post, not a queue wake. - console::Drain::Thread => { - if shard::signal_after_commit(shard::log_waiter()) { - console::post_wake(); - } - } + // Until the hand-over the producer is the drainer, which is what makes a + // boot that wedges before the idle loop say everything it had logged. + if console::inline() { + console::drain_inline(); } + user::owe_readiness(); // After the commit, so the record this call made is the one on the panel. crate::drivers::panic_console::early_checkpoint(); diff --git a/kernel/src/log/read.rs b/kernel/src/log/read.rs index 55b1f94a89b..38e1503239f 100644 --- a/kernel/src/log/read.rs +++ b/kernel/src/log/read.rs @@ -138,25 +138,6 @@ impl Published { Some(ap) => &self.aps[ap], } } - - /// Records the walks over this position counted lost. - pub fn lost(&self) -> u64 { - self.lost.load(Ordering::Relaxed) - } - - /// Take `other`'s place: what a walk from here reads, `other`'s walkers no - /// longer move. - pub fn copy_from(&self, other: &Published) { - for ring in 0..self.count() { - self.word(ring).store(other.word(ring).load(Ordering::Relaxed), Ordering::Relaxed); - } - self.lost.store(other.lost(), Ordering::Relaxed); - } - - /// Is there a committed record this position has not taken? Lock-free. - pub fn any_pending(&self) -> bool { - any_committed(self) - } } impl Positions for &Published { @@ -277,17 +258,6 @@ pub fn newest_committed() -> LogStamp { LogStamp::since_zero(newest) } -/// Is there a committed record `at` has not taken, without taking it? -pub fn any_committed(at: &Published) -> bool { - (0..super::shard_count().min(at.count())).any(|i| match super::shard(i) { - Some(shard) => { - let want = (&at).next(i).max(FIRST_SEQ).max(shard.oldest_readable()); - shard.stamp(want).is_some() - } - None => false, - }) -} - /// Every committed record stamped in `from..=to`, newest first merged by /// `at_ns` until `out` is full, walking with `scratch`'s positions, which the /// caller holds alone; returns nothing, since no caller counts. diff --git a/kernel/src/log/shard.rs b/kernel/src/log/shard.rs index c07ac96ef6c..c4a68b69ddf 100644 --- a/kernel/src/log/shard.rs +++ b/kernel/src/log/shard.rs @@ -7,10 +7,10 @@ //! Compiled a second time by `kernel-loom`, which shims only the atomics and `arch::percpu_fetch_add`. #[cfg(not(feature = "loom"))] -use core::sync::atomic::{fence, AtomicBool, AtomicU64, Ordering}; +use core::sync::atomic::{fence, AtomicU64, Ordering}; #[cfg(feature = "loom")] -use loom::sync::atomic::{fence, AtomicBool, AtomicU64, Ordering}; +use loom::sync::atomic::{fence, AtomicU64, Ordering}; use toyos_abi::log::{LogRecord, MAX_RECORD_MESSAGE}; #[cfg(not(feature = "loom"))] @@ -254,38 +254,3 @@ impl Shard { } // `Ring` is `Sync` by auto-derivation (every word is `AtomicU64`); no hand-written `unsafe impl` needed. - -/// Is a reader parked on this machine's records? Kept as one flag so the producer's fast path avoids a locked read-modify-write. -#[cfg(not(feature = "loom"))] -static LOG_WAITER: AtomicBool = AtomicBool::new(false); - -/// The machine's one waiter flag. -#[cfg(not(feature = "loom"))] -pub fn log_waiter() -> &'static AtomicBool { - &LOG_WAITER -} - -#[cfg(feature = "loom")] -pub fn waiter() -> AtomicBool { - AtomicBool::new(false) -} - -/// Read the waiter flag after the guarded commit store; `true` means this caller owns the post. -pub fn signal_after_commit(waiter: &AtomicBool) -> bool { - // Load-bearing on x86 too: without this fence a committed record can leave a parked reader unposted. - #[cfg(not(feature = "wake-fence-off"))] - fence(Ordering::SeqCst); - if !waiter.load(Ordering::Relaxed) { - return false; - } - waiter.swap(false, Ordering::AcqRel) -} - -/// Arm the flag, fence, and re-scan; `true` means do not park. -/// `committed_record_waiting` must test for a commit, not for `head` moving: `head` counts reservations, so that would busy-wait on a writer still mid-publish. -pub fn arm_waiter(waiter: &AtomicBool, committed_record_waiting: impl Fn() -> bool) -> bool { - waiter.store(true, Ordering::Relaxed); - #[cfg(not(feature = "wake-fence-off"))] - fence(Ordering::SeqCst); - committed_record_waiting() -} diff --git a/kernel/src/log/user.rs b/kernel/src/log/user.rs index d66acd0e7d4..fb7dd7059a7 100644 --- a/kernel/src/log/user.rs +++ b/kernel/src/log/user.rs @@ -4,6 +4,8 @@ //! //! [`Rights::LOG`]: toyos_abi::handle::Rights::LOG +use core::sync::atomic::{AtomicBool, Ordering}; + use toyos_abi::syscall::SyscallError; use toyos_abi::UserSafe; @@ -16,11 +18,25 @@ use super::shard::Ring; /// What a log poll waits on: edge-triggered, since a reader's position is its own cursor and the kernel holds none. pub static WATCH: Watch = Watch::new(); -/// Tell every ring watching the log that records have moved. -// Posted only by `klogd` after a drain batch — `emit` runs under sync.rs/IRQ/scheduler locks and may not lock. -// Edge, not level: whether a caller has unread records is a property of its cursor, which the kernel does not hold. -pub fn post_readiness() { - WATCH.post(); +/// A record committed since [`WATCH`] was last posted. +static OWED: AtomicBool = AtomicBool::new(false); + +/// A record committed: [`post_owed`] posts [`WATCH`] for it. `emit`'s, which +/// runs under any lock, and so may post nothing itself. +pub fn owe_readiness() { + OWED.store(true, Ordering::Release); +} + +/// Tell every ring watching the log that records have moved, where any were +/// committed since the last post: at every syscall's end and every scheduler +/// pass, which hold no lock a post takes. Edge, not level: whether a caller +/// has unread records is a property of its cursor, which the kernel does not +/// hold. +pub fn post_owed() { + // Relaxed first: no read-modify-write on a pass with nothing owed. + if OWED.load(Ordering::Relaxed) && OWED.swap(false, Ordering::Acquire) { + WATCH.post(); + } } // Fixed stride, never packed: the caller indexes by shift, so the kernel does no length arithmetic. diff --git a/kernel/src/main.rs b/kernel/src/main.rs index 21cca3e7650..9158d2157ed 100644 --- a/kernel/src/main.rs +++ b/kernel/src/main.rs @@ -18,7 +18,6 @@ mod shootdown; mod tlb_tags; mod cpu_local; mod cpu_slice; -mod sleeplock; mod smp; mod smp_roster; mod sync; @@ -109,8 +108,6 @@ use toyos_rootimage::handoff::{held, Descriptor}; #[panic_handler] fn panic(info: &core::panic::PanicInfo) -> ! { cpu::disable_interrupts(); - // Before the first record of the report: no holder carries it off the UART. - log::console::on_the_uart_for_good(); // A handler's own panic reports through locks like any other. #[cfg(feature = "mask-windows")] windows::stand_down(); @@ -533,8 +530,9 @@ pub(crate) unsafe extern "C" fn kernel_main(loader_args: &mut KernelArgs) -> ! { late_panic::Nest>>>>>>>>>::on_screen_console_check(); } - // Last thing before enter_idle_loop: nothing can run before it, and a klogd spawned earlier would idle through phases 5-7 with no drainer. - log::console::start(); + // Last thing before the release: from here the UART is its claim's + // holder's, and nothing earlier can claim it. + log::console::hand_over(); smp::set_ready(); diff --git a/kernel/src/mm/mod.rs b/kernel/src/mm/mod.rs index c5de10f548a..35350a7cad9 100644 --- a/kernel/src/mm/mod.rs +++ b/kernel/src/mm/mod.rs @@ -182,7 +182,6 @@ pub fn init(memory_map: &'static [MemoryMapEntry], reserved: &[Region], image: & alloc::init(); charge::init(); paging::seal_kernel_half(); - space::init_kernel(); } /// Walk the kernel's tables and stop the boot on a leaf that is writable and diff --git a/kernel/src/mm/space.rs b/kernel/src/mm/space.rs index 519b10f4cf6..d7792a78452 100644 --- a/kernel/src/mm/space.rs +++ b/kernel/src/mm/space.rs @@ -166,7 +166,6 @@ impl Space { inventory: Inventory::new(FLOOR, TOP), tables: UserTables::new(job)?, usage: Arc::clone(&usage), - kernel: false, ranges: Charge::none(job), job: Arc::clone(job), }; @@ -193,39 +192,10 @@ impl Space { } } -/// The kernel's own space: what a kernel thread and an idle CPU run under, -/// holding nothing in its user half. Made once, after the kernel's tables. -static KERNEL: core::sync::atomic::AtomicPtr = core::sync::atomic::AtomicPtr::new(core::ptr::null_mut()); - -/// Make the kernel's space; `mm::init` calls it once its tables are built. -pub(super) fn init_kernel() { - let usage = Arc::new(Usage::default()); - let space = AddressSpace { - inventory: Inventory::new(FLOOR, TOP), - tables: UserTables::kernel(), - usage: Arc::clone(&usage), - kernel: true, - ranges: Charge::none(charge::boot()), - job: Arc::clone(charge::boot()), - }; - // Leaked: it outlives every task, and its tables are the kernel's. - let space: &'static PageTables = alloc::boxed::Box::leak(alloc::boxed::Box::new(Arc::new(Space { inner: Lock::new(space), usage }))); - KERNEL.store(core::ptr::from_ref(space).cast_mut(), core::sync::atomic::Ordering::Release); -} - -pub fn kernel() -> PageTables { - let space = KERNEL.load(core::sync::atomic::Ordering::Acquire); - assert!(!space.is_null(), "space: the kernel's before `mm::init`"); - // SAFETY: `init_kernel` stored a leaked box's pointer, which is never - // cleared or freed. - Arc::clone(unsafe { &*space }) -} - pub struct AddressSpace { inventory: Inventory, tables: UserTables, usage: Arc, - kernel: bool, /// What its ranges take of the heap, in grains of [`RANGE_GRAIN`]. ranges: Charge, /// The job its memory is counted to: its process's. @@ -950,7 +920,6 @@ fn copy_in(source: &dyn CopySource, offset: u64, len: u64, phys: u64) { /// futex waiter is keyed on its frames and no copy pins them. impl Drop for AddressSpace { fn drop(&mut self) { - assert!(!self.kernel, "space: the kernel's dropped"); let committed = self.committed_pages(FLOOR, TOP); self.tables.edit(FLOOR, TOP, |_, leaf| matches!(leaf, Leaf::Mapped(Mapping { owned: false, .. })).then_some(Leaf::Empty)); let mut freed = 0; diff --git a/kernel/src/object/device.rs b/kernel/src/object/device.rs index 755376fcc8a..72be419d11e 100644 --- a/kernel/src/object/device.rs +++ b/kernel/src/object/device.rs @@ -1,4 +1,4 @@ -//! A device claim, and the one console every kernel-spawned process starts on. +//! A device claim. use alloc::boxed::Box; use alloc::sync::Arc; @@ -27,6 +27,9 @@ pub enum DeviceInfo { Isa(toyos_abi::syscall::IsaId), /// The fixed hardware's blocks. Acpi(toyos_abi::acpi::AcpiInfo), + /// The console UART, and its register frame where the architecture maps + /// one, whose handle the mint installs. + Uart(toyos_abi::uart::UartInfo, Option>), } // MAP and WRITE draw into it; DUP and TRANSFER let a daemon hand the buffer to another process. @@ -54,6 +57,15 @@ impl DeviceInfo { Self::PciFunction(info) => bytes(info).into(), Self::Isa(set) => set.wire().iter().flat_map(|word| word.to_ne_bytes()).collect(), Self::Acpi(info) => bytes(info).into(), + Self::Uart(info, frame) => { + let mut info = *info; + if let Some(frame) = frame { + // The holder's alone, as the row's ports are: no right moves it on. + let entry = HandleEntry::new(KObjectRef::SharedMem(Arc::clone(frame)), Rights::MAP.union(Rights::WRITE)); + info.frame = table.install(entry).map_err(|_| SyscallError::ResourceExhausted)?; + } + bytes(&info).into() + } }) } } @@ -166,61 +178,3 @@ impl ZeroHandles for DeviceClaim { self.reference.release(); } } - -/// One holder's view of the machine's serial console — each holder gets its own, never a shared handle. -pub struct ConsoleObject { - pub(super) core: ObjectCore, - // Lock order process_data -> line -> the console queue; taking them in reverse deadlocks. - line: crate::sync::Lock, -} - -impl ConsoleObject { - /// A console of its own for a holder in `job`, charged to it. - pub fn new(job: &Arc) -> Result, Exhausted> { - Ok(Arc::new(Self { - core: Self::new_core_for(job, 0)?, - line: crate::sync::Lock::new(crate::drivers::serial::ConsoleLine::new()), - })) - } - - /// Take a userland write, queueing every whole line it completes that the - /// console has room for; answers the bytes taken. - pub fn write(&self, buf: &crate::user_ptr::UserBytes) -> usize { - self.line.lock().write(buf) - } - - /// Whether this holder carries the log off the machine, so the UART does - /// not (`log::console::carried_by`); its last handle lets go of it. - pub fn carry(&self, elsewhere: bool) -> Result<(), kernel::carrier::Refused> { - match elsewhere { - true => { - crate::log::console::carried_by(self.holder())?; - // A last handle closed beside this call ran its release - // before the carry landed: the carry is given back here. - core::sync::atomic::fence(Ordering::SeqCst); - if self.core.retired() { - crate::log::console::released_by(self.holder()); - } - Ok(()) - } - false => { - crate::log::console::released_by(self.holder()); - Ok(()) - } - } - } - - fn holder(&self) -> usize { - self as *const Self as usize - } -} - -// Flushes the partial line and gives the log back to the UART if this holder -// carried it, on last handle: a dying holder's releases run before its end is -// published, so its end never leaves the UART quiet. -impl ZeroHandles for ConsoleObject { - fn on_zero_handles(&self) { - self.line.lock().finish(); - crate::log::console::released_by(self.holder()); - } -} diff --git a/kernel/src/object/handle.rs b/kernel/src/object/handle.rs index fb76692f0f5..989f804fc4f 100644 --- a/kernel/src/object/handle.rs +++ b/kernel/src/object/handle.rs @@ -125,10 +125,6 @@ impl HandleEntry { &self.object } - pub fn rights(&self) -> Rights { - self.rights - } - /// A second handle to the same object, carrying no more than this one. pub fn duplicate(&self, rights: Rights) -> Result { if !self.rights.contains(Rights::DUP) { diff --git a/kernel/src/object/mod.rs b/kernel/src/object/mod.rs index 68cf93eabc6..9e7f786ebfe 100644 --- a/kernel/src/object/mod.rs +++ b/kernel/src/object/mod.rs @@ -276,8 +276,6 @@ kobject! { immediate Namespace => namespace::Namespace, // A file's flush and cache reference ride the last `Arc`; `read`/`write` on a file never park. immediate File => file::FileObject, - // Its last handle lets go of the log's carrier, which a stranded `Arc` must not hold. - deferred Console => device::ConsoleObject, // The authority is the rights on the handle; a handle going away *is* the whole event. immediate SysCap => syscap::SysCap, // The last handle's loss is the loss of the *ability to wait*, not proof the process should stop. diff --git a/kernel/src/object/ops.rs b/kernel/src/object/ops.rs index e52685958d6..47d71e8c2a1 100644 --- a/kernel/src/object/ops.rs +++ b/kernel/src/object/ops.rs @@ -9,7 +9,6 @@ use alloc::sync::Arc; use toyos_abi::handle::{RawHandle, Rights}; use toyos_abi::syscall::{FileType, OpenFlags, SeekFrom, SyscallError, MAX_WHOLE_PIPE_WRITE}; -use crate::drivers::serial; use crate::file_cache; use crate::pipe::{self, PipeRef}; use kernel::pipe::End; @@ -37,7 +36,6 @@ pub fn initial_rights(object: &KObjectRef) -> Rights { KObjectRef::Device(_) => { Rights::TRANSFER.union(Rights::WAIT).union(Rights::READ).union(Rights::WRITE) } - KObjectRef::Console(_) => BASE.union(Rights::READ).union(Rights::WRITE), KObjectRef::Acceptor(_) => BASE.union(Rights::READ), KObjectRef::Inbox(_) => { BASE.union(Rights::READ).union(Rights::WRITE).union(Rights::MAP) @@ -220,7 +218,7 @@ pub fn pipe_read(object: &KObjectRef) -> Option<&PipeRef> { match object { KObjectRef::PipeRead(r) => Some(r.pipe()), KObjectRef::Connection(_) | KObjectRef::PipeWrite(_) | KObjectRef::File(_) | KObjectRef::Device(_) - | KObjectRef::Console(_) | KObjectRef::Acceptor(_) | KObjectRef::Inbox(_) + | KObjectRef::Acceptor(_) | KObjectRef::Inbox(_) | KObjectRef::SysCap(_) | KObjectRef::Connector(_) | KObjectRef::WakeLatency(_) | KObjectRef::EventSignal(_) | KObjectRef::EventWait(_) | KObjectRef::Pager(_) | KObjectRef::Paged(_) | KObjectRef::Namespace(_) | KObjectRef::SharedMem(_) | KObjectRef::Process(_) | KObjectRef::Job(_) | KObjectRef::Null(_) @@ -233,7 +231,7 @@ pub fn pipe_write(object: &KObjectRef) -> Option<&PipeRef> { match object { KObjectRef::PipeWrite(w) => Some(w.pipe()), KObjectRef::Connection(_) | KObjectRef::PipeRead(_) | KObjectRef::File(_) | KObjectRef::Device(_) - | KObjectRef::Console(_) | KObjectRef::Acceptor(_) | KObjectRef::Inbox(_) + | KObjectRef::Acceptor(_) | KObjectRef::Inbox(_) | KObjectRef::SysCap(_) | KObjectRef::Connector(_) | KObjectRef::WakeLatency(_) | KObjectRef::EventSignal(_) | KObjectRef::EventWait(_) | KObjectRef::Pager(_) | KObjectRef::Paged(_) | KObjectRef::Namespace(_) | KObjectRef::SharedMem(_) | KObjectRef::Process(_) | KObjectRef::Job(_) | KObjectRef::Null(_) @@ -288,13 +286,11 @@ pub fn read_watch(object: &KObjectRef) -> Option { KObjectRef::EventWait(e) => Some(WatchRef::Shared(e.watch().clone())), KObjectRef::Pager(p) => Some(WatchRef::Shared(p.watch().clone())), KObjectRef::Job(_) => Some(WatchRef::InPlace(crate::mm::pmm::returned())), - // Nothing posts for a serial byte, so a console's readable poll has - // no source (`issues/a-console-watch-waits-on-the-keyboard-not-the-serial-line.md`). - KObjectRef::Console(_) => None, KObjectRef::Device(d) => match d.class() { device_registry::DeviceType::PciFunction | device_registry::DeviceType::Isa - | device_registry::DeviceType::Acpi => Some(WatchRef::Claim(d.clone())), + | device_registry::DeviceType::Acpi + | device_registry::DeviceType::Uart => Some(WatchRef::Claim(d.clone())), device_registry::DeviceType::Framebuffer => None, }, // Named unconditionally: the watch alone cannot enforce rights. @@ -312,7 +308,7 @@ pub fn watched_readable(object: &KObjectRef) { KObjectRef::Notice(n) => n.listen(), KObjectRef::PipeRead(_) | KObjectRef::PipeWrite(_) | KObjectRef::Connection(_) | KObjectRef::Device(_) | KObjectRef::Acceptor(_) | KObjectRef::Inbox(_) | KObjectRef::SharedMem(_) | KObjectRef::Connector(_) | KObjectRef::WakeLatency(_) | KObjectRef::EventSignal(_) | KObjectRef::EventWait(_) | KObjectRef::Pager(_) | KObjectRef::Paged(_) - | KObjectRef::Namespace(_) | KObjectRef::File(_) | KObjectRef::Console(_) | KObjectRef::SysCap(_) + | KObjectRef::Namespace(_) | KObjectRef::File(_) | KObjectRef::SysCap(_) | KObjectRef::Process(_) | KObjectRef::Null(_) | KObjectRef::Job(_) | KObjectRef::DeviceSet(_) => {} } } @@ -322,7 +318,6 @@ pub fn write_watch(object: &KObjectRef) -> Option { match object { KObjectRef::PipeWrite(w) => Some(WatchRef::Shared(w.pipe().write_watch().clone())), KObjectRef::Connection(c) => Some(WatchRef::Shared(c.tx().write_watch().clone())), - KObjectRef::Console(_) => Some(WatchRef::Static(&crate::log::console::SPACE)), KObjectRef::PipeRead(_) | KObjectRef::File(_) | KObjectRef::Device(_) | KObjectRef::Acceptor(_) | KObjectRef::Inbox(_) | KObjectRef::SysCap(_) @@ -340,7 +335,7 @@ pub fn pipe_end_watch(object: &KObjectRef) -> Option { KObjectRef::PipeWrite(w) => Some(WatchRef::Shared(w.pipe().left_watch(End::Write).clone())), KObjectRef::Connection(_) | KObjectRef::Acceptor(_) | KObjectRef::Process(_) | KObjectRef::Job(_) | KObjectRef::Notice(_) - | KObjectRef::Console(_) | KObjectRef::Device(_) | KObjectRef::SysCap(_) + | KObjectRef::Device(_) | KObjectRef::SysCap(_) | KObjectRef::File(_) | KObjectRef::Inbox(_) | KObjectRef::Connector(_) | KObjectRef::WakeLatency(_) | KObjectRef::EventSignal(_) | KObjectRef::EventWait(_) | KObjectRef::Pager(_) | KObjectRef::Paged(_) | KObjectRef::Namespace(_) | KObjectRef::SharedMem(_) | KObjectRef::Null(_) | KObjectRef::DeviceSet(_) => None, } @@ -354,7 +349,7 @@ pub fn other_end_gone(object: &KObjectRef) -> bool { KObjectRef::PipeWrite(w) => w.pipe().other_end_gone(End::Write), KObjectRef::Connection(_) | KObjectRef::Acceptor(_) | KObjectRef::Process(_) | KObjectRef::Job(_) | KObjectRef::Notice(_) - | KObjectRef::Console(_) | KObjectRef::Device(_) | KObjectRef::SysCap(_) + | KObjectRef::Device(_) | KObjectRef::SysCap(_) | KObjectRef::File(_) | KObjectRef::Inbox(_) | KObjectRef::Connector(_) | KObjectRef::WakeLatency(_) | KObjectRef::EventSignal(_) | KObjectRef::EventWait(_) | KObjectRef::Pager(_) | KObjectRef::Paged(_) | KObjectRef::Namespace(_) | KObjectRef::SharedMem(_) | KObjectRef::Null(_) | KObjectRef::DeviceSet(_) => false, } @@ -370,14 +365,14 @@ pub fn other_end_gone(object: &KObjectRef) -> bool { fn close_ends_polls(object: &KObjectRef) -> bool { match object { KObjectRef::SysCap(_) => false, - KObjectRef::Console(_) => false, KObjectRef::Process(_) | KObjectRef::Job(_) => false, // The set's watch is every handle's: one handle's close ends none. KObjectRef::DeviceSet(_) => false, KObjectRef::Device(d) => match d.class() { device_registry::DeviceType::PciFunction | device_registry::DeviceType::Isa - | device_registry::DeviceType::Acpi => false, + | device_registry::DeviceType::Acpi + | device_registry::DeviceType::Uart => false, device_registry::DeviceType::Framebuffer => true, }, KObjectRef::PipeRead(_) | KObjectRef::PipeWrite(_) | KObjectRef::Connection(_) @@ -501,7 +496,7 @@ pub fn read_device( } // The PCI shape, but the description is what binds the ports to the // reader, and nothing after it answers any other process. - device_registry::DeviceType::Isa | device_registry::DeviceType::Acpi => { + device_registry::DeviceType::Isa | device_registry::DeviceType::Acpi | device_registry::DeviceType::Uart => { let pid = crate::process::current_process(); if !claim.info_read() { if claim.isa(|row| crate::isa::bind(row, pid)).is_none() { @@ -554,23 +549,6 @@ pub fn try_read(object: &KObjectRef, buf: &mut UserBytesMut) -> Option { KObjectRef::File(f) => read_file(f, buf), KObjectRef::PipeRead(r) => r.pipe().try_read(buf).map(|n| n as u64), KObjectRef::Device(_) => unreachable!("a device claim is read by `read_device`"), - KObjectRef::Console(_) => { - let mut count = 0usize; - while count < buf.len() { - if let Some(b) = serial::try_read_byte() { - buf.write_at(count, &[b]); - count += 1; - if b == b'\n' || b == b'\r' { - break; - } - } else if count > 0 { - break; - } else { - return None; - } - } - Some(count as u64) - } KObjectRef::Null(_) => Some(0), // An empty read listens and takes nothing: the reason would have nowhere to go. KObjectRef::Notice(n) if buf.is_empty() => { @@ -608,9 +586,6 @@ pub fn try_write(object: &KObjectRef, buf: &UserBytes) -> Option { Some(answer) }), KObjectRef::PipeWrite(_) => write_pipe(object, buf), - // The whole lines `klogd`'s queue has room for, and a trailing partial one: - // a short count is a writer ahead of the console, told so. - KObjectRef::Console(c) => Some(c.write(buf) as u64), KObjectRef::Device(d) => Some(write_device(d, buf)), KObjectRef::Null(_) => Some(buf.len() as u64), // A connection carries whole messages, which `SYS_MESSAGE_SEND` sends. @@ -673,7 +648,6 @@ pub fn fstat(object: &KObjectRef) -> Stat { plain(if w.is_tty() { FileType::Tty } else { FileType::Pipe }) } KObjectRef::Connection(_) => plain(FileType::Socket), - KObjectRef::Console(_) => plain(FileType::Serial), KObjectRef::Null(_) => plain(FileType::Null), KObjectRef::Acceptor(_) => plain(FileType::Pipe), KObjectRef::SharedMem(m) => Stat { @@ -697,7 +671,8 @@ pub fn fstat(object: &KObjectRef) -> Stat { device_registry::DeviceType::Framebuffer => FileType::Framebuffer, device_registry::DeviceType::PciFunction | device_registry::DeviceType::Isa - | device_registry::DeviceType::Acpi => FileType::Unknown, + | device_registry::DeviceType::Acpi + | device_registry::DeviceType::Uart => FileType::Unknown, }), } } @@ -733,7 +708,6 @@ pub fn has_data(object: &KObjectRef) -> bool { match object { KObjectRef::PipeRead(r) => r.pipe().has_data(), KObjectRef::Connection(c) => c.rx().readable(), - KObjectRef::Console(_) => serial::has_data(), KObjectRef::Acceptor(a) => a.has_pending(), KObjectRef::Process(p) => p.finished(), KObjectRef::DeviceSet(set) => set.changed(), @@ -748,9 +722,9 @@ pub fn has_data(object: &KObjectRef) -> bool { device_registry::DeviceType::PciFunction => { !d.info_read() || d.pci(crate::pcidev::has_irq).unwrap_or(false) } - device_registry::DeviceType::Isa | device_registry::DeviceType::Acpi => { - !d.info_read() || d.isa(crate::isa::has_irq).unwrap_or(false) - } + device_registry::DeviceType::Isa + | device_registry::DeviceType::Acpi + | device_registry::DeviceType::Uart => !d.info_read() || d.isa(crate::isa::has_irq).unwrap_or(false), device_registry::DeviceType::Framebuffer => true, }, KObjectRef::PipeWrite(_) | KObjectRef::Inbox(_) | KObjectRef::SysCap(_) @@ -765,7 +739,7 @@ pub fn has_data(object: &KObjectRef) -> bool { pub fn read_posts_are_readiness(object: &KObjectRef) -> bool { match object { KObjectRef::SysCap(_) => true, - KObjectRef::Console(_) | KObjectRef::PipeRead(_) | KObjectRef::PipeWrite(_) | KObjectRef::Connection(_) + KObjectRef::PipeRead(_) | KObjectRef::PipeWrite(_) | KObjectRef::Connection(_) | KObjectRef::Acceptor(_) | KObjectRef::File(_) | KObjectRef::Device(_) | KObjectRef::Inbox(_) | KObjectRef::Connector(_) | KObjectRef::WakeLatency(_) | KObjectRef::EventSignal(_) | KObjectRef::EventWait(_) | KObjectRef::Pager(_) | KObjectRef::Paged(_) | KObjectRef::Namespace(_) | KObjectRef::SharedMem(_) | KObjectRef::Process(_) | KObjectRef::Job(_) | KObjectRef::Null(_) @@ -780,7 +754,6 @@ pub fn has_space(object: &KObjectRef) -> bool { KObjectRef::PipeWrite(w) => w.pipe().has_space(MAX_WHOLE_PIPE_WRITE), KObjectRef::Connection(c) => c.tx().writable(), KObjectRef::File(_) | KObjectRef::Null(_) => true, - KObjectRef::Console(_) => crate::log::console::has_room(), KObjectRef::PipeRead(_) | KObjectRef::Device(_) | KObjectRef::Acceptor(_) | KObjectRef::Inbox(_) | KObjectRef::SysCap(_) | KObjectRef::Connector(_) | KObjectRef::WakeLatency(_) | KObjectRef::EventSignal(_) | KObjectRef::EventWait(_) | KObjectRef::Pager(_) | KObjectRef::Paged(_) | KObjectRef::Namespace(_) @@ -800,7 +773,7 @@ pub fn mark_tty(object: &KObjectRef) -> u64 { 0 } KObjectRef::Connection(_) | KObjectRef::File(_) | KObjectRef::Device(_) - | KObjectRef::Console(_) | KObjectRef::Acceptor(_) | KObjectRef::Inbox(_) + | KObjectRef::Acceptor(_) | KObjectRef::Inbox(_) | KObjectRef::SysCap(_) | KObjectRef::SharedMem(_) | KObjectRef::Connector(_) | KObjectRef::WakeLatency(_) | KObjectRef::EventSignal(_) | KObjectRef::EventWait(_) | KObjectRef::Pager(_) | KObjectRef::Paged(_) | KObjectRef::Namespace(_) | KObjectRef::Process(_) | KObjectRef::Job(_) | KObjectRef::Null(_) | KObjectRef::Notice(_) | KObjectRef::DeviceSet(_) => { @@ -813,19 +786,20 @@ pub fn mark_tty(object: &KObjectRef) -> u64 { /// ports are bound to, which unmasks the row's level lines. Nothing else /// writes to a claim. fn write_device(claim: &DeviceClaim, buf: &UserBytes) -> u64 { - match claim.class() { - device_registry::DeviceType::Isa | device_registry::DeviceType::Acpi => {} + let ack = match claim.class() { + device_registry::DeviceType::Isa | device_registry::DeviceType::Acpi => toyos_abi::acpi::ACK, + device_registry::DeviceType::Uart => toyos_abi::uart::ACK, device_registry::DeviceType::Framebuffer | device_registry::DeviceType::PciFunction => { return SyscallError::PermissionDenied.to_u64(); } - } + }; if !claim.info_read() { return SyscallError::PermissionDenied.to_u64(); } let mut word = [0u8; 4]; let acknowledges = buf.len() == word.len() && { buf.read_at(0, &mut word); - u32::from_ne_bytes(word) == toyos_abi::acpi::ACK + u32::from_ne_bytes(word) == ack }; let pid = crate::process::current_process(); // The holder's check and the unmask are one borrow of the row. diff --git a/kernel/src/panic.rs b/kernel/src/panic.rs index 115804e8808..b373267dff2 100644 --- a/kernel/src/panic.rs +++ b/kernel/src/panic.rs @@ -268,8 +268,8 @@ pub fn last_words( }; let second_message = second.message().as_str().unwrap_or(NOT_CAPTURED); - // Let go of before the record below: before `klogd` runs, its drain may be - // a burst on this CPU, which this hold would refuse. + // Let go of before the record below: before the hand-over, its drain may + // be a burst on this CPU, which this hold would refuse. { let mut uart = serial::panic_registers(); uart.write(b"\n!!! "); @@ -346,7 +346,6 @@ pub fn last_words( /// path's one funnel. // panic_flush bypasses the log-ring and serial locks — once the others are stopped a wedged holder never releases them, so taking them normally could deadlock. pub fn halt_all_cpus() -> ! { - crate::log::console::on_the_uart_for_good(); // Before the halt and the panel: from here this machine holds a report for // whoever is in front of it, with interrupts masked and under a bound of its // own — which to a hard-lockup sample or a deadline poll is diff --git a/kernel/src/power.rs b/kernel/src/power.rs index 54ff58f0da0..fbce0a7a8a1 100644 --- a/kernel/src/power.rs +++ b/kernel/src/power.rs @@ -11,9 +11,9 @@ //! power loss — are outside it and always will be. //! //! **Nothing an end says is logged after the console's last drain.** The -//! stop holds the console's wire (`log::console::StopWire`), or writes over -//! the holder that kept it, and its drain here is the log ring's last reader -//! for the console: a record committed past it reaches no wire. What an end says +//! stop holds the console's UART (`log::console::StopWire`), and its drain +//! here is the log ring's last reader for the console: a record committed +//! past it reaches no wire. What an end says //! through the ring it says above the drain (`arch::power::settle`); //! `arch::power::off` and `arch::power::reset` log nothing, and a panic in //! either drains for itself. The black box's page is another reader and an @@ -43,7 +43,7 @@ pub fn reboot(mut wire: StopWire) -> ! { /// Reset the machine and do nothing else. /// /// **[`reboot`] is not reachable from a wedge**, which is why this exists -/// beside it: that path is the stop's, which waits on `klogd` and the +/// beside it: that path is the stop's, which waits on userland's stop and the /// console's registers, and a `BackendGuard` masks interrupts for its whole /// life — so a CPU stuck inside one holds what the stop would wait for, on /// exactly the boots `crate::deadline` exists for. This takes no lock. diff --git a/kernel/src/process.rs b/kernel/src/process.rs index 8e5ee1e8625..a1c84e29ced 100644 --- a/kernel/src/process.rs +++ b/kernel/src/process.rs @@ -154,7 +154,7 @@ pub struct ProcessEntry { object: Arc, name: [u8; THREAD_NAME_LEN], process_data: Arc>, - /// No `Lock`: written once by the loader, then read-only — a crash report reaches it through the task it is reporting on, never through this table. `None` for a kernel thread's. + /// No `Lock`: written once by the loader, then read-only — a crash report reaches it through the task it is reporting on, never through this table. `None` once the process is torn down. image: Option>, main_tid: Tid, threads: crate::id_map::IdMap, @@ -290,25 +290,6 @@ pub struct ElfInfo { // The ledger stops at `MAX_LIBRARIES`, and a doubling from below it stays inside one heap allocation. const _: () = assert!(2 * MAX_LIBRARIES * core::mem::size_of::() <= crate::mm::MAX_HEAP_ALLOC); -impl ElfInfo { - /// The state of a process with no ELF at all (a kernel thread). Not `Default`: `next_tls_module_id`'s only honest default is 1, not 0; written here, not at the one call site, so a field added to [`ElfInfo`] stops this build too. - pub fn none() -> Self { - Self { - elf_alloc: None, - tls_modules: Vec::new(), - tls: toyos_elf::tls::Static::empty(crate::loader::TLS_VARIANT), - next_tls_module_id: 1, - dynamic_tls_blocks: alloc::collections::BTreeMap::new(), - loaded_libs: Vec::new(), - elf_base: UserAddr::new(0), - exe_eh_frame_hdr: (0, 0), - exe_vaddr_max: 0, - exe_phdrs: (0, 0), - lib_paths: Vec::new(), - } - } -} - /// Process-level data shared across all threads via `Arc>`. pub struct ProcessData { /// Every kernel object this process can name; stdio is handles `0`, `1`, `2`. @@ -387,10 +368,6 @@ pub struct Endowments { } impl Endowments { - pub fn empty() -> Self { - Self { entries: alloc::boxed::Box::new([]), labels: alloc::boxed::Box::new([]) } - } - pub fn new(entries: Vec, labels: Vec) -> Self { Self { entries: entries.into_boxed_slice(), labels: labels.into_boxed_slice() } } @@ -713,7 +690,7 @@ pub fn spawn_thread( let proc = table.get_mut(parent_process) .expect("spawn_thread: the entry the insert admission just answered for"); // Every thread carries the same image, so a crash report never asks this table. - let image = proc.image.clone(); + let image = proc.image.clone()?; let tid = proc.threads.insert(ThreadEntry::new(thread_data)); // Before the thread's first instruction, which is the enqueue below: the // thread reads its own id here without a syscall (`toyos_abi::TCB_TID`). @@ -899,19 +876,39 @@ fn teardown(pid: Pid, tid: Tid, end: End, mark: i32, process_data: &Arc) { +fn leave_as(leaving: Leaving) { let (pid, tid) = (current_process(), current_tid()); crate::mm::paging::activate_kernel(); let process_data = process_data(); scheduler::flush_current_stats(&mut process_data.lock().accounting); let out = { let mut guard = PROCESS_TABLE.lock(); - proclife::leave(guard.as_mut().unwrap(), pid, tid, chosen) + let table = guard.as_mut().unwrap(); + match leaving { + Leaving::Ended => proclife::leave(table, pid, tid, None), + Leaving::Joinable(code) => proclife::leave(table, pid, tid, Some(code)), + Leaving::Detached(code) => proclife::leave_detached(table, pid, tid, code), + } }; match out { - // The table says zombie now, which a sweep counts as nothing left to stop. + // The table says zombie, or has it no more, which a sweep counts as nothing left to stop. proclife::Leave::NotLast => crate::quiesce::note_progress(), proclife::Leave::Last { end, mark } => teardown(pid, tid, end, mark, &process_data), } @@ -959,12 +956,20 @@ pub fn exit(end: End) -> ! { scheduler::post_retire(&sched); } walk(owed); - leave(None); + leave(); scheduler::exit_current(); } +/// Whether anyone will join an exiting thread. +pub enum Joined { + /// Its code waits in the table for a join. + Joinable, + /// Nobody will: its place goes as it leaves. + Detached, +} + /// Exit the current thread. If this is the main thread, it is the process's exit. For a child thread, frees its own mappings and leaves. -pub fn thread_exit(code: i32) -> ! { +pub fn thread_exit(code: i32, joined: Joined) -> ! { let process_pid = current_process(); let tid = current_tid(); let route = { @@ -978,7 +983,10 @@ pub fn thread_exit(code: i32) -> ! { }; release_thread(tid); - leave(Some(code)); + match joined { + Joined::Joinable => leave_as(Leaving::Joinable(code)), + Joined::Detached => leave_as(Leaving::Detached(code)), + } // Whoever joined this thread armed on it; post before the exit pass — after it this thread never runs again. if let Some(handle) = crate::sched::driver::current_handle() { // Held together by assertion, not shared state: the decision names the subject, this performs it via the CPU's own handle. @@ -1221,10 +1229,7 @@ fn with_current_image(f: impl FnOnce(&UserImage) -> bool) -> FrameRecord { FrameRecord::NoTask }; }; - match image { - Some(image) if f(&image) => FrameRecord::Recorded, - _ => FrameRecord::Outside, - } + if f(&image) { FrameRecord::Recorded } else { FrameRecord::Outside } } /// Kill the process an object names, and everything below it. diff --git a/kernel/src/quiesce.rs b/kernel/src/quiesce.rs index 17cf1676726..caf5d4b54f2 100644 --- a/kernel/src/quiesce.rs +++ b/kernel/src/quiesce.rs @@ -23,9 +23,6 @@ //! the boot's last word needs. Freezing CPUs inside a pass instead would strand //! whatever lock the thread on that CPU was holding. //! -//! Kernel threads are exempt by identity, not by accident: -//! [`crate::sched::kthread::is_kernel_task`] is what tells them apart. -//! //! # What the stop waits on //! //! [`PROGRESS`], posted by [`note_progress`] from the three transitions that @@ -45,7 +42,6 @@ use kernel::sched::watch::Gate; use crate::arch::percpu; use crate::watch::{self, Watch}; use crate::process; -use crate::scheduler::TaskId; use crate::time::{Budget, Deadline, Duration}; /// How long the machine gets to stop. @@ -97,11 +93,6 @@ fn stops(stage: u32) -> bool { let (Some(pid), Some(tid)) = (percpu::current_pid(), percpu::current_tid()) else { return false; }; - // A kernel thread reaches this boundary on its first dispatch and has no - // Ring 3 to be stopped from. - if crate::sched::kthread::is_kernel_task(TaskId(pid, tid)) { - return false; - } must_stop(ThreadId { pid: pid.raw(), tid: tid.raw() }, caller()) } @@ -208,9 +199,6 @@ fn sweep(caller: ThreadId) -> Sweep { if matches!(thread.state(), process::ThreadLocation::Zombie(_)) { continue; } - if crate::sched::kthread::is_kernel_task(TaskId(pid, tid)) { - continue; - } let sched = thread .sched() .expect("quiesce::sweep: a live thread in the table with no task"); diff --git a/kernel/src/sched/driver.rs b/kernel/src/sched/driver.rs index adc95d2098d..8a618928b46 100644 --- a/kernel/src/sched/driver.rs +++ b/kernel/src/sched/driver.rs @@ -45,23 +45,11 @@ pub fn preempt_off(f: impl FnOnce(&PreemptOff) -> R) -> R { result } -/// The same proof, bought with `cli`. -pub struct IrqOff(()); - -// SAFETY: `do_preempt` needs `IF` set or a voluntary pass; `irq_off` masks `IF` and calls nothing that yields one. -unsafe impl PreemptGuard for IrqOff {} - -/// Run `f` with interrupts masked, holding [`IrqOff`] for exactly that region. -pub fn irq_off(f: impl FnOnce(&IrqOff) -> R) -> R { - let _guard = crate::arch::IrqGuard::close(); - f(&IrqOff(())) -} - static CPUS: AtomicPtr> = AtomicPtr::new(ptr::null_mut()); static NEXT_KEY: AtomicU64 = AtomicU64::new(1); -/// Nanoseconds between two pass-cost reports — wall-clock, not per-pass, since `klogd`'s own wake is itself a pass. +/// Nanoseconds between two pass-cost reports — wall-clock, not per-pass, since the report's own record owes a pass. #[cfg(feature = "sched-check")] const PASS_COST_REPORT_EVERY_NS: u64 = 200_000_000; @@ -138,11 +126,6 @@ pub(crate) fn spin_for(ns: u64) { } } -/// Whether [`init`] has run: before it there is no task, so nothing can wait. -pub fn started() -> bool { - !CPUS.load(Ordering::Acquire).is_null() -} - pub fn cpus() -> &'static CpuHandles { let ptr = CPUS.load(Ordering::Acquire); assert!(!ptr.is_null(), "scheduler used before sched::init"); @@ -408,9 +391,7 @@ fn placement(now: Nanos, mask: CpuMask) -> CpuId { cpus().place(start, now, mask) } -/// Everything a new thread needs. `entry_sp` points at the trampoline frame `alloc_kernel_stack` built; -/// `address_space` is not `Option` — every kernel thread uses the kernel address space, so one declaration -/// decides `cr3`. +/// Everything a new thread needs. `entry_sp` points at the trampoline frame `alloc_kernel_stack` built. pub struct NewTask { pub id: TaskId, pub kernel_stack: KernelStack, @@ -419,24 +400,21 @@ pub struct NewTask { pub thread_pointer: u64, /// The job the thread's time is charged to. pub job: Arc, - /// The process's image; `None` for a kernel thread. - pub image: Option>, + /// The process's image. + pub image: Arc, /// The CPUs it may run on. pub mask: CpuMask, } /// Place a new task by message — never by reaching into the destination's queue. pub fn spawn(new: NewTask) -> (ThreadSched, CpuId) { - // A kernel thread's is the kernel address space, the one every CPU - // sits in between user threads — why `idle_ctx` names the same `cr3`. - // Nothing is released at teardown: this `Arc` clones a leaked, permanent kernel mapping. let root = new.address_space.lock().root(); let ctx = KernelCtx { sp: new.entry_sp, root, thread_pointer: new.thread_pointer, kernel_stack_top: new.kernel_stack.top(), - runs: if new.kernel_stack.is_user() { Runs::User(new.id) } else { Runs::Kernel(new.id) }, + runs: Runs::User(new.id), // The one level `trampoline_entry` discharges before the first `iretq`. preempt: 1, }; @@ -521,6 +499,7 @@ pub fn pass(dispose: Dispose) { // Before the pass picks, so a wake this posts is in the run queue by the // time the pass chooses. crate::object::drain_zero_handles(); + crate::log::user::post_owed(); let now = HW.now(); crate::arch::watchdog::feed(now.0); crate::arch::clocksource::check(now.0); @@ -733,13 +712,13 @@ pub fn current_handle() -> Option> { try_with_cpu(|cpu| cpu.running().map(|t| t.ext().handle.clone())).flatten() } -/// The image of the task this CPU is running, `None` inside for a kernel thread. +/// The image of the task this CPU is running. /// /// Takes no lock: the image is immutable and the `Arc` outlives teardown. `None`: a pass already holds this /// record, or none is running. /// /// No pass can start underneath the read: a fault handler runs with preemption declined. -pub fn current_image() -> Option>> { +pub fn current_image() -> Option> { try_with_cpu(|cpu| cpu.running().map(|t| t.ext().image.clone())).flatten() } @@ -819,18 +798,6 @@ pub fn current_is_rt() -> bool { try_with_cpu(|cpu| cpu.running().is_some_and(|t| t.rt().is_rt())).unwrap_or(false) } -/// Every thread on this CPU's run queue, in pick order, for a staging that -/// says why a woken thread has not run. `false` means a pass owns the state. -#[cfg(feature = "boot-actuators")] -pub fn for_each_ready(mut f: impl FnMut(TaskId)) -> bool { - try_with_cpu(|cpu| { - for task in cpu.rq().tasks() { - f(task.ext().id); - } - }) - .is_some() -} - /// Tail of the first switch into a fresh task. No lock to release, no outgoing task to park: only the /// preempt-count bracket's other half is owed. pub extern "C" fn trampoline_entry() { @@ -933,7 +900,7 @@ fn stack_depth(payload: &KernelPayload) { let bottom = payload.kernel_stack.bottom(); // SAFETY: the running task's own stack, allocated with exactly its // `layout()`; running on this CPU, so nothing is freeing it. - unsafe { crate::mm::check_heap_bands(bottom, payload.kernel_stack.layout(), "kernel stack") }; + unsafe { crate::mm::check_heap_bands(bottom, KernelStack::layout(), "kernel stack") }; let mut used = 0; for rung in DEPTH_RUNGS { // SAFETY: `rung < KERNEL_STACK_SIZE`, so this reads a word of that diff --git a/kernel/src/sched/kthread.rs b/kernel/src/sched/kthread.rs deleted file mode 100644 index 67308278b4a..00000000000 --- a/kernel/src/sched/kthread.rs +++ /dev/null @@ -1,151 +0,0 @@ -//! Kernel threads: ordinary tasks that name `mm::paging::kernel` as their -//! address space, enter through `arch::entry::kernel_start`, and hold a process-table -//! entry. One is preempted or stolen only at a preemption point its body reaches, -//! and a Ring 0 loop reaches none. [`ROWS`] holds every one. - -use alloc::string::String; -use alloc::sync::Arc; -use alloc::vec::Vec; -use core::sync::atomic::{AtomicU64, Ordering}; - -use crate::process::{ - ElfInfo, Endowments, ProcessAccounting, ProcessData, ProcessEntry, ThreadData, - ThreadEntry, PROCESS_TABLE, THREAD_NAME_LEN, -}; -use crate::scheduler::{self, TaskId}; -use crate::sync::Lock; -use toyos_proclife::Processes; - -use super::payload::ThreadSched; - -/// `klogd`. -const MAX_KERNEL_TASKS: usize = 1; - -/// Collides with no packed id: neither id map issues `u32::MAX`. -const NO_TASK: u64 = u64::MAX; - -/// A reserved row whose identity is not yet known; collides with no packed id. -const CLAIMING: u64 = u64::MAX - 1; - -/// A row reserved before the table lock and published before `enqueue_new`. -struct Claim(&'static AtomicU64); - -impl Claim { - /// Reserve a row, or panic naming the thread. - fn take(name: &str) -> Self { - for row in &ROWS { - if row - .compare_exchange(NO_TASK, CLAIMING, Ordering::Relaxed, Ordering::Relaxed) - .is_ok() - { - return Self(row); - } - } - panic!("kthread: {name} is the {}th kernel thread and there is room for {MAX_KERNEL_TASKS}", MAX_KERNEL_TASKS + 1); - } - - fn publish(self, id: TaskId) { - self.0.store(id.pack(), Ordering::Relaxed); - } -} - -/// Registered at spawn and never cleared. -static ROWS: [AtomicU64; MAX_KERNEL_TASKS] = [const { AtomicU64::new(NO_TASK) }; MAX_KERNEL_TASKS]; - -/// Is the task this CPU is running a kernel thread? Lock-free and fault-free, -/// so it may run with any lock held or preemption on. -pub fn current_is_kernel_thread() -> bool { - let (Some(pid), Some(tid)) = ( - crate::arch::percpu::current_pid(), - crate::arch::percpu::current_tid(), - ) else { - return false; - }; - is_kernel_task(TaskId(pid, tid)) -} - -/// Is `id` a kernel thread? -// No lock: the stop asks this on a machine already suspected of being stuck, -// where taking one could hang diagnostics. -pub fn is_kernel_task(id: TaskId) -> bool { - let packed = id.pack(); - ROWS.iter().any(|row| row.load(Ordering::Relaxed) == packed) -} - -/// Start a kernel thread running `body(arg)` on its own kernel stack and return its scheduler faces. -pub fn spawn(name: &str, body: extern "C" fn(u64) -> !, arg: u64) -> ThreadSched { - // A job of its own: a kernel thread is nobody's helper. - let job = crate::object::job::new_job(); - let (stack, entry_sp) = crate::loader::alloc_kernel_stack(&job, crate::loader::Start::Kernel { body, arg }) - .unwrap_or_else(|| panic!("kthread: no kernel stack for {name}")); - - // Before the table lock: a panic holding the process table hangs the machine. - let claim = Claim::take(name); - let taken = PROCESS_TABLE.lock().as_mut().expect("kthread: spawned before process::init").pids().take(); - let pid = taken.unwrap_or_else(|| panic!("kthread: {name} found every pid issued")); - - let mut short = [0u8; THREAD_NAME_LEN]; - let len = name.len().min(THREAD_NAME_LEN - 1); - short[..len].copy_from_slice(&name.as_bytes()[..len]); - // One hold across insert and place: a visible pid already has its thread scheduled. - let mut guard = PROCESS_TABLE.lock(); - let table = guard.as_mut().expect("kthread: spawned before process::init"); - table.insert(ProcessEntry::new( - crate::object::process::ProcessObject::new(pid).expect("kthread: no heap for its process object"), - short, - Arc::new(Lock::new(kernel_process_data(name, job.clone()))), - // No user image: its frames resolve through `symbols::resolve_kernel`. - None, - ThreadEntry::new(Arc::new(Lock::new(kernel_thread_data()))), - toyos_proclife::Node::root(), - crate::object::notice::NoticeObject::new().expect("kthread: no heap for its notice"), - )); - let tid = table.get(pid).expect("kthread: the entry just inserted is gone").main_tid(); - claim.publish(TaskId(pid, tid)); - // The kernel address space, named so one declaration decides every task's `cr3`. - let (sched, _dst) = scheduler::enqueue_new(scheduler::NewTask { - id: TaskId(pid, tid), - kernel_stack: stack, - entry_sp, - address_space: crate::mm::space::kernel(), - thread_pointer: 0, - job, - image: None, - mask: kernel::sched::affinity::CpuMask::ANY, - }); - table - .get_mut(pid) - .and_then(|p| p.threads_mut().get_mut(tid)) - .expect("kthread: the thread just inserted is gone") - .set_sched(sched.clone()); - drop(guard); - - crate::log!("kthread: {name} pid={pid} tid={tid} runs in the kernel address space"); - sched -} - -/// Every field is the empty value but its job: a kernel thread has no user half. -fn kernel_process_data(name: &str, job: Arc) -> ProcessData { - ProcessData { - handles: crate::object::HandleTable::new(&job), - cwd: String::from("/"), - env: Vec::new(), - elf: ElfInfo::none(), - space: crate::mm::space::kernel(), - exe_path: String::from(name), - spawn_ns: crate::clock::nanos_since_boot(), - accounting: ProcessAccounting::default(), - endowments: Endowments::empty(), - charge: crate::mm::charge::Charge::none(&job), - pager: crate::process::PagerRole::None, - job, - } -} - -fn kernel_thread_data() -> ThreadData { - ThreadData { - tls: None, - user_stack_base: crate::mm::UserAddr::new(0), - user_stack_size: 0, - } -} diff --git a/kernel/src/sched/mod.rs b/kernel/src/sched/mod.rs index b3d3db63842..b752a8471a6 100644 --- a/kernel/src/sched/mod.rs +++ b/kernel/src/sched/mod.rs @@ -3,7 +3,6 @@ #![warn(clippy::undocumented_unsafe_blocks)] pub mod driver; -pub mod kthread; pub mod payload; pub mod reap_gate; pub mod futex; diff --git a/kernel/src/sched/payload.rs b/kernel/src/sched/payload.rs index 18ddf352571..6a23da63c44 100644 --- a/kernel/src/sched/payload.rs +++ b/kernel/src/sched/payload.rs @@ -56,8 +56,6 @@ pub struct KernelCtx { pub enum Runs { /// This CPU's idle loop. Idle, - /// A kernel thread, which never enters Ring 3. - Kernel(TaskId), /// A thread of a process. User(TaskId), } @@ -67,7 +65,7 @@ impl Runs { pub fn id(self) -> Option { match self { Self::Idle => None, - Self::Kernel(id) | Self::User(id) => Some(id), + Self::User(id) => Some(id), } } } @@ -76,12 +74,12 @@ impl Runs { pub struct KernelPayload { pub id: TaskId, pub kernel_stack: KernelStack, - /// The address space this task runs in; never `Option` — a kernel thread runs in the kernel's own. + /// The address space this task runs in. pub address_space: PageTables, /// The cross-CPU-readable face of this task; a `CpuSched` is `!Sync` and cannot be walked remotely. pub handle: Arc, - /// This task's process's image, as a crash report records a frame in it; kept here, not looked up via the process table, so a crash report never takes that lock. `None` for a kernel thread. - pub image: Option>, + /// This task's process's image, as a crash report records a frame in it; kept here, not looked up via the process table, so a crash report never takes that lock. + pub image: Arc, } impl SchedPayload for KernelPayload { diff --git a/kernel/src/scheduler.rs b/kernel/src/scheduler.rs index 6537717900c..fb2faae9a34 100644 --- a/kernel/src/scheduler.rs +++ b/kernel/src/scheduler.rs @@ -20,7 +20,7 @@ use crate::time::Deadline; use crate::DirectMap; pub use crate::sched::driver::{ - current_address_space, enter_idle_loop, in_pass as in_schedule_self, started, total_cpu_ns, + current_address_space, enter_idle_loop, in_pass as in_schedule_self, total_cpu_ns, write_stack_canary, NewTask, Ticket, }; @@ -43,23 +43,14 @@ const BASELINE_TRAP: u32 = 1; /// are past the trap entry level. const BASELINE_IRQ_EXIT: u32 = 0; -/// Read from `sched::kthread`'s rows rather than the `CpuSched`, which a -/// preempting pass may be holding `&mut` at this point. -fn blocking_baseline() -> u32 { - if crate::sched::kthread::current_is_kernel_thread() { - 0 - } else { - BASELINE_TRAP - } -} /// Proof that the calling context may park; threaded by reference rather /// than stored, with no public constructor beside [`Parkable::at_entry`]. pub struct Parkable(()); impl Parkable { - /// Asserts this context is a trap entry or a kernel thread's body, and - /// nothing below one, then mints the proof. + /// Asserts this context is a trap entry, and nothing below one, then + /// mints the proof. #[track_caller] pub fn at_entry() -> Parkable { Parkable::mint() @@ -67,7 +58,7 @@ impl Parkable { #[track_caller] fn mint() -> Parkable { - assert_baseline(blocking_baseline()); + assert_baseline(BASELINE_TRAP); Parkable(()) } } @@ -80,18 +71,6 @@ impl TaskId { pub fn pack(self) -> u64 { self.1.raw() as u64 | (self.0.raw() as u64) << 32 } - pub fn unpack(v: u64) -> Self { - Self(Pid::from_raw((v >> 32) as u32), Tid::from_raw(v as u32)) - } -} - -/// The running task, or `None` for boot and an idle CPU. No lock: called -/// with preemption still on, aliasing a preempting pass's `&mut CpuSched`. -pub fn current_task() -> Option { - match (percpu::current_pid(), percpu::current_tid()) { - (Some(pid), Some(tid)) => Some(TaskId(pid, tid)), - _ => None, - } } impl core::fmt::Display for TaskId { @@ -124,19 +103,17 @@ pub fn prepare_wait(cancel: Cancel, class: WaitClass) -> Result /// reaches the machine without phase 1 behind it is the lost-wake window. #[track_caller] pub fn block_on(ticket: Ticket, deadline: Deadline) { - // One level above the calling context's baseline: the ticket has held the - // registration window's own level since `prepare_wait`. - assert_baseline(blocking_baseline() + 1); + // One level above the trap's: the ticket has held the registration + // window's own level since `prepare_wait`. + assert_baseline(BASELINE_TRAP + 1); driver::pass_block(ticket, (!deadline.is_never()).then(|| Nanos(deadline.nanos()))); } /// Give the CPU up voluntarily, keeping the claim on it: the pass decides -/// whether anything else deserves the quantum. Asserts the calling context's -/// own baseline, not a flat trap level, since a kernel thread yields at zero -/// and a flat assert would panic it. +/// whether anything else deserves the quantum. #[track_caller] pub fn yield_now() { - assert_baseline(blocking_baseline()); + assert_baseline(BASELINE_TRAP); driver::pass(Dispose::Yield); } @@ -182,7 +159,7 @@ pub fn leave_user_if_due() { // closes and address-space drop are no interrupt latency. The // depth stays this boundary's, which is `do_preempt`'s own. crate::arch::cpu::enable_interrupts(); - process::leave(None); + process::leave(); crate::arch::cpu::disable_interrupts(); driver::pass(Dispose::Exit); unreachable!("leave_user_if_due: returned from the exit pass"); @@ -202,8 +179,7 @@ pub fn exit_to_user() { leave_user_if_due(); // `do_preempt` owns clearing `need_resched`; this function never clears it itself. if !crate::preempt::need_resched() { - // Every caller's next unmask is the return to user mode, or a - // kernel thread's first. + // Every caller's next unmask is the return to user mode. #[cfg(feature = "mask-windows")] crate::windows::irqs_unmasking(); return; diff --git a/kernel/src/sleeplock.rs b/kernel/src/sleeplock.rs deleted file mode 100644 index 73e52397e7f..00000000000 --- a/kernel/src/sleeplock.rs +++ /dev/null @@ -1,189 +0,0 @@ -//! The lock a contender parks on. -//! A contender takes a ticket, arms on the lock's own watch, and gives the -//! CPU back; the release posts to the ticket whose turn it now is. Unlike a -//! [`crate::sync::Lock`] guard, a [`SleepGuard`] does not raise the preempt -//! count. Compiled a second time by `kernel-loom`, which drives the real -//! acquire. The console's wire (`drivers::serial`) is one. - -#[cfg(not(feature = "loom"))] -use core::cell::UnsafeCell; -#[cfg(not(feature = "loom"))] -use core::sync::atomic::{AtomicU64, Ordering}; - -#[cfg(feature = "loom")] -use crate::cell::UnsafeCell; -#[cfg(feature = "loom")] -use loom::sync::atomic::{AtomicU64, Ordering}; - -use core::ops::{Deref, DerefMut}; - -use crate::watch::{self, Watch}; -use crate::scheduler::{current_task, started, Parkable, TaskId}; -use crate::sync::{AtomicTicket, Ticket}; - -/// Acquire: orders the previous holder's writes before this read; `sleeplock-acquire-off` flips it to `Relaxed` so `kernel-loom` can prove the gap is real. -#[cfg(not(feature = "sleeplock-acquire-off"))] -const TURN: Ordering = Ordering::Acquire; -#[cfg(feature = "sleeplock-acquire-off")] -const TURN: Ordering = Ordering::Relaxed; - -/// `u32::MAX` in the pid half, which `id_map` never issues. -const FREE: u64 = u64::MAX; - -/// Held by a context with no task — boot, or a caller through [`SleepLock::try_lock`]; distinct from [`FREE`], though [`SleepLock::holder`] answers `None` to both. -const NOT_A_TASK: u64 = u64::MAX - 1; - -fn word_of(task: Option) -> u64 { - match task { - Some(id) => id.pack(), - None => NOT_A_TASK, - } -} - -/// A lock whose contended acquire parks. -pub struct SleepLock { - /// Next ticket to hand out; an uncontended acquire CASes it from [`Self::now`]. - ticket: AtomicTicket, - /// Whose turn it is; the release publishes through this and [`TURN`] loads it. - now: AtomicTicket, - /// [`FREE`], [`NOT_A_TASK`], or the holder's packed [`TaskId`]; not part of the ticket pair's exclusion. - holder: AtomicU64, - /// Contenders arm here with their own ticket as token. - watch: Watch, - data: UnsafeCell, -} - -// SAFETY: exclusivity comes from the ticket pair, not from `Sync`; `T: Send` because any task may become the holder. -unsafe impl Sync for SleepLock {} - -impl SleepLock { - /// Must stay `const`: every converted lock in the kernel is a `static`; loom's atomics have no const constructor, hence the second arm. - #[cfg(not(feature = "loom"))] - pub const fn new(value: T) -> Self { - Self { - ticket: AtomicTicket::new(Ticket::ZERO), - now: AtomicTicket::new(Ticket::ZERO), - holder: AtomicU64::new(FREE), - watch: Watch::new(), - data: UnsafeCell::new(value), - } - } - - #[cfg(feature = "loom")] - pub fn new(value: T) -> Self { - Self { - ticket: AtomicTicket::new(Ticket::ZERO), - now: AtomicTicket::new(Ticket::ZERO), - holder: AtomicU64::new(FREE), - watch: Watch::new(), - data: UnsafeCell::new(value), - } - } - - /// Must not be called while a [`crate::sync::Lock`] is held — use [`Self::try_lock`] there instead. - #[track_caller] - pub fn lock<'p>(&'p self, p: &'p Parkable) -> SleepGuard<'p, T> { - let me = current_task(); - // RT6: a second acquire by the holder would take a ticket nothing will ever serve. - assert!( - me.is_none() || self.holder() != me, - "sleeplock: {} already holds this lock", - Holder(word_of(me)), - ); - let owner = word_of(me); - if let Some(guard) = self.take(owner) { - return guard; - } - // Arms before re-reading `now`, so a release landing in between is not lost. - let mine = self.ticket.fetch_advance(Ordering::Relaxed); - // Uncancellable: a killed holder still releases via `Drop` on unwind, so the wait is bounded. - watch::wait_uncancellable_until( - p, - &self.watch, - u64::from(mine.raw()), - || self.now.load(TURN) == mine, - ); - self.holder.store(owner, Ordering::Relaxed); - SleepGuard { lock: self } - } - - /// Takes the lock if free, from any context — an interrupt handler, the panic path, boot, or a caller already holding a [`crate::sync::Lock`]. - pub fn try_lock(&self) -> Option> { - self.take(word_of(current_task())) - } - - /// [`Self::try_lock`] for a context that cannot ask which task it is: the - /// boot before per-CPU state exists, where there is no task to be. - pub fn try_lock_untasked(&self) -> Option> { - self.take(NOT_A_TASK) - } - - /// Fails whenever anyone is queued, because `ticket` has already moved past `now`. - fn take(&self, owner: u64) -> Option> { - let turn = self.now.load(TURN); - self.ticket - .compare_advance(turn, Ordering::Relaxed, Ordering::Relaxed) - .ok()?; - self.holder.store(owner, Ordering::Relaxed); - Some(SleepGuard { lock: self }) - } - - /// `None` means "not a task" (free, or acquired via [`Self::try_lock`]), never "held by task 0". - pub fn holder(&self) -> Option { - match self.holder.load(Ordering::Relaxed) { - FREE | NOT_A_TASK => None, - word => Some(TaskId::unpack(word)), - } - } - - /// Who holds the lock as of this read, for a message and never a - /// decision: it may have moved by the time it is said. - pub fn holder_name(&self) -> Holder { - Holder(self.holder.load(Ordering::Relaxed)) - } -} - -/// A lock's holder by name: a task, a context with no task, or nobody. -pub struct Holder(u64); - -impl core::fmt::Display for Holder { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - match self.0 { - FREE => write!(f, "nobody"), - NOT_A_TASK => write!(f, "a context with no task"), - word => write!(f, "{}", TaskId::unpack(word)), - } - } -} - -pub struct SleepGuard<'a, T> { - lock: &'a SleepLock, -} - -impl Deref for SleepGuard<'_, T> { - type Target = T; - fn deref(&self) -> &T { - // SAFETY: the ticket pair grants this caller the lock until the release below. - unsafe { &*self.lock.data.get() } - } -} - -impl DerefMut for SleepGuard<'_, T> { - fn deref_mut(&mut self) -> &mut T { - // SAFETY: as `deref`; `&mut self` gives the exclusive borrow. - unsafe { &mut *self.lock.data.get() } - } -} - -impl Drop for SleepGuard<'_, T> { - // `holder` is cleared before the release: after the release another CPU may already hold the lock and have overwritten it. - fn drop(&mut self) { - self.lock.holder.store(FREE, Ordering::Relaxed); - let next = self.lock.now.fetch_advance(Ordering::Release).succ(); - // Before the scheduler exists no task can have queued, and a post - // needs the scheduler: the boot's console takes this lock that early. - if started() { - let _ = self.lock.watch.post_n(u64::from(next.raw()), 1); - } - } -} diff --git a/kernel/src/smp.rs b/kernel/src/smp.rs index 8d22249db74..3eac39f4b72 100644 --- a/kernel/src/smp.rs +++ b/kernel/src/smp.rs @@ -68,8 +68,8 @@ pub fn set_ready() { /// Whether [`set_ready`] has run: the machine's own word for "the scheduler is /// what runs now". `kernel_main` calls it immediately before -/// `scheduler::enter_idle_loop`, so a `false` here means no task and no -/// kernel thread can make progress, and the panic path waits for none of them. +/// `scheduler::enter_idle_loop`, so a `false` here means no task can make +/// progress, and the panic path waits for none of them. pub fn is_ready() -> bool { ROSTER.released() } diff --git a/kernel/src/syscall/device.rs b/kernel/src/syscall/device.rs index 910c1153fde..d442b395e56 100644 --- a/kernel/src/syscall/device.rs +++ b/kernel/src/syscall/device.rs @@ -145,7 +145,7 @@ pub(super) fn sys_device_claim(syscap: RawHandle, class: u64, selector: [u64; 2] // A PCI function's second word is the firmware windows its claim is // granted at the mint (`toyos_abi::pci::FirmwareWindows`). let read = match class { - device::DeviceType::Framebuffer | device::DeviceType::Acpi => 0, + device::DeviceType::Framebuffer | device::DeviceType::Acpi | device::DeviceType::Uart => 0, device::DeviceType::PciFunction => 2, device::DeviceType::Isa => 2, }; diff --git a/kernel/src/syscall/dispatch.rs b/kernel/src/syscall/dispatch.rs index dcbb536176f..67fd6cb45b4 100644 --- a/kernel/src/syscall/dispatch.rs +++ b/kernel/src/syscall/dispatch.rs @@ -33,7 +33,7 @@ use super::fs::{ }; use super::handles::{sys_close, sys_handle_dup, with_object, with_object_ref}; use super::io::{ - sys_console_carried, sys_null, sys_random, sys_read, sys_read_nonblock, sys_write, sys_write_nonblock, + sys_null, sys_random, sys_read, sys_read_nonblock, sys_write, sys_write_nonblock, }; use super::ipc::{ Incoming, Outgoing, @@ -42,7 +42,7 @@ use super::ipc::{ sys_port_badge, sys_port_create, sys_port_mint, sys_shm_create, sys_shm_map, sys_shm_sub, }; use super::machine::{ - sys_boot_sources, sys_counters, sys_device_inventory, sys_device_set, sys_device_set_read, sys_log_read, + sys_boot_sources, sys_counters, sys_device_inventory, sys_device_set, sys_device_set_read, sys_log_read, sys_log_spoken, sys_machine_stops, sys_reboot, sys_shutdown, sys_sysinfo, sys_trace_read, MAX_INVENTORY_RECORDS, }; @@ -51,7 +51,7 @@ use super::machine::lower_sysinfo_bound; use super::paged::{sys_paged_create, sys_paged_holder, sys_paged_notices, sys_paged_op, sys_paged_read, sys_pager_create}; use super::proc::{ spawn_pager, spawn_place, sys_endowments, sys_exit, sys_nanosleep, sys_process_stats, - sys_account_info, sys_job_create, sys_job_floor, sys_job_own, sys_process_quit, sys_process_wait, sys_rt_enter, sys_spawn, sys_thread_exit, sys_thread_get_affinity, sys_thread_join, sys_thread_set_affinity, sys_thread_spawn, sys_wake_latency_hold, + sys_account_info, sys_job_create, sys_job_floor, sys_job_own, sys_process_quit, sys_process_wait, sys_rt_enter, sys_spawn, sys_thread_exit, sys_thread_get_affinity, sys_thread_join, sys_thread_set_affinity, sys_thread_spawn, sys_wake_latency_hold, sys_yield, }; use super::vm::{ shared_image, sys_dlopen, sys_dlsym, sys_query_modules, sys_tls_alloc_block, sys_vm_decommit, sys_vm_discard, sys_vm_remap, @@ -90,7 +90,7 @@ pub(crate) fn syscall_dispatch(num: u64, a1: u64, a2: u64, a3: u64, a4: u64) -> let Some(mut buf) = ctx.user_bytes_mut(UserAddr::new(a2), a3) else { return bad_addr }; sys_read(RawHandle(a1 as u32), &mut buf) } - SYS_THREAD_EXIT => sys_thread_exit(a1 as i32), + SYS_THREAD_EXIT => sys_thread_exit(a1 as i32, a2, a3, a4), SYS_RANDOM => { let Some(mut buf) = ctx.user_bytes_mut(UserAddr::new(a1), a2) else { return bad_addr }; sys_random(&mut buf) @@ -257,6 +257,7 @@ pub(crate) fn syscall_dispatch(num: u64, a1: u64, a2: u64, a3: u64, a4: u64) -> SYS_MARK_TTY => with_object(RawHandle(a1 as u32), Rights::NONE, ops::mark_tty), SYS_THREAD_SPAWN => sys_thread_spawn(a1, a2, a3, a4), SYS_THREAD_JOIN => sys_thread_join(a1), + SYS_YIELD => sys_yield(), // Answers from the boot-time anchor, never the CMOS: NotSupported beats a // 1970-epoch number a caller cannot tell from real time. SYS_CLOCK_EPOCH => { @@ -486,7 +487,7 @@ pub(crate) fn syscall_dispatch(num: u64, a1: u64, a2: u64, a3: u64, a4: u64) -> SYS_SHM_CREATE => sys_shm_create(a1, a2, RawHandle(a3 as u32)), SYS_SHM_MAP => sys_shm_map(RawHandle(a1 as u32), a2), SYS_SHM_SUB => sys_shm_sub(RawHandle(a1 as u32), a2, a3), - SYS_CONSOLE_CARRIED => sys_console_carried(RawHandle(a1 as u32), a2), + SYS_LOG_SPOKEN => sys_log_spoken(RawHandle(a1 as u32), a2), SYS_PORT_CREATE => sys_port_create(), SYS_PORT_MINT => { // Copied in before any handle is looked up: the kernel holds the badge, never the caller's page. @@ -550,7 +551,6 @@ pub(crate) fn syscall_dispatch(num: u64, a1: u64, a2: u64, a3: u64, a4: u64) -> 0 } DA::FATAL_HALT => { - crate::log::console::on_the_uart_for_good(); log!("{}", FATAL_HALT_NONCE); crate::panic::halt_all_cpus(); } @@ -720,6 +720,7 @@ pub(crate) fn syscall_dispatch(num: u64, a1: u64, a2: u64, a3: u64, a4: u64) -> // queued it: a hook must not run under whatever guard the syscall held when the // last handle went. crate::object::drain_zero_handles(); + crate::log::user::post_owed(); // Wall-clock, not CPU time: elapsed includes any preemption. let elapsed = crate::arch::cpu::counter().saturating_sub(entered); diff --git a/kernel/src/syscall/io.rs b/kernel/src/syscall/io.rs index c35fa6e7acd..6d4ba13dd89 100644 --- a/kernel/src/syscall/io.rs +++ b/kernel/src/syscall/io.rs @@ -9,7 +9,7 @@ use crate::watch; use crate::object::null::NullObject; use crate::object::{ops, KObjectRef}; -use crate::time::{Cadence, Deadline, Duration}; +use crate::time::Deadline; use crate::user_ptr::{UserBytes, UserBytesMut}; use crate::{pipe, process}; @@ -35,9 +35,6 @@ enum ReadBlock { Pipe(pipe::PipeRef), /// A notice with no reason in it, until a quit puts one there. Notice(alloc::sync::Arc), - /// A console, which reads the serial line and nothing else: nothing posts - /// a serial byte, so it parks on its own thread's watch until its re-poll. - Console(Deadline), /// Nothing to wait for: the answer is this word. Refused(u64), /// Carried out of the process's lock: `HandleError::refuse` may take the @@ -83,14 +80,6 @@ fn read_block(object: &KObjectRef) -> ReadBlock { match object { KObjectRef::Device(_) => unreachable!("a device claim blocks via `read_block_device`"), KObjectRef::Notice(n) => ReadBlock::Notice(n.clone()), - KObjectRef::Console(_) => { - // Nothing posts a serial-console key, so the timer alone wakes it. - const CONSOLE_REPOLL: Cadence = Cadence::every( - Duration::from_millis(10), - "nothing posts a serial-console key, so this rate is the whole of the wake", - ); - ReadBlock::Console(Deadline::at(crate::clock::now() + CONSOLE_REPOLL.duration())) - } _ => match ops::pipe_read(object).map(|pipe| ReadBlock::Pipe(pipe.clone())) { Some(block) => block, None => ReadBlock::Refused(SyscallError::NotFound.to_u64()), @@ -152,22 +141,6 @@ pub(super) fn sys_read(h: RawHandle, buf: &mut UserBytesMut) -> u64 { return cancelled(); } } - Err(ReadBlock::Console(deadline)) => { - let parkable = crate::scheduler::Parkable::at_entry(); - let Some(handle) = crate::sched::driver::current_handle() else { return cancelled() }; - if watch::wait_until( - &parkable, - handle.watch(), - 0, - WaitClass::Io, - deadline, - crate::drivers::serial::has_data, - ) - .is_err() - { - return cancelled(); - } - } Err(ReadBlock::Refused(word)) => return word, Err(ReadBlock::BadHandle(e)) => return e.refuse(), } @@ -235,24 +208,3 @@ pub(super) fn sys_random(out: &mut UserBytesMut) -> u64 { crate::random::fill_user(out); 0 } - -/// `SYS_CONSOLE_CARRIED`: whether the holder of the console `h` carries the -/// log off the machine ([`crate::object::device::ConsoleObject::carry`]). -pub(super) fn sys_console_carried(h: RawHandle, elsewhere: u64) -> u64 { - let elsewhere = match elsewhere { - 0 => false, - 1 => true, - _ => return SyscallError::InvalidArgument.to_u64(), - }; - let console = match process::with_process_data(|data| { - data.handles.get::(h, Rights::WRITE) - }) { - Ok(console) => console, - Err(e) => return e.refuse(), - }; - match console.carry(elsewhere) { - Ok(()) => 0, - Err(kernel::carrier::Refused::Another) => SyscallError::AlreadyExists.to_u64(), - Err(kernel::carrier::Refused::ForGood) => SyscallError::NotSupported.to_u64(), - } -} diff --git a/kernel/src/syscall/machine.rs b/kernel/src/syscall/machine.rs index db7cfdf3439..115c4b95f1b 100644 --- a/kernel/src/syscall/machine.rs +++ b/kernel/src/syscall/machine.rs @@ -28,6 +28,17 @@ pub(super) fn sys_log_read( read_on_cursor(ctx, syscap, Rights::LOG, cursor, log::shard_count(), |next| log::user::read(next, out, capacity)) } +/// The console's channels have delivered every record stamped at or before +/// `at_ns`; answers the newest stamp the console has spoken. Requires a +/// `SysCap` carrying [`Rights::LOG`], whose holder carries the log to the +/// console (`log::console`). +pub(super) fn sys_log_spoken(syscap: RawHandle, at_ns: u64) -> u64 { + if let Err(e) = demand_syscap(syscap, Rights::LOG) { + return e.refuse(); + } + log::console::speak(at_ns) +} + /// Copies the diary's records into the caller's buffer; requires a `SysCap` carrying [`Rights::TRACE`]. pub(super) fn sys_trace_read( ctx: &SyscallContext, @@ -104,24 +115,17 @@ fn quiesce(last: &str) -> Result<(crate::quiesce::Stopping, StopWire), SyscallEr if crate::actuator::wedge_before_reset() { crate::deadline::stage_a_wedge(); } - let parkable = crate::scheduler::Parkable::at_entry(); - // The console's writer holding the wire as the stop begins, which a - // shipping `klogd` does at any moment. - #[cfg(feature = "boot-actuators")] - if crate::actuator::wire_held_across_the_stop() { - crate::log::console::hold_across_the_stop(&parkable); - } // First: what follows outlasts a feed cadence, and no pass runs to feed again. crate::arch::watchdog::disarm(); - // Every userland thread stops here, the log's writer with the rest: - // the supervisor had it flush before it asked for this stop. + // Every userland thread stops here, the log's writer and the UART's holder + // with the rest: the supervisor had the log flush before it asked for this stop. let (stopped, stopping) = crate::quiesce::stop(); // From here on nothing carries a record to a file: the seal below takes // every one stamped after this one, the newest the stop found. let stop_began = crate::log::read::newest_committed(); - // Held to the machine's end: every record below reaches the wire before a - // CPU is taken down, whoever was writing it when the stop began. - let mut wire = crate::log::console::take_for_the_stop(&parkable); + // Held to the machine's end: every record below reaches the UART before a + // CPU is taken down. + let mut wire = crate::log::console::take_for_the_stop(); // The machine's census, which no process's start or end takes. crate::census::log(); #[cfg(feature = "mask-windows")] @@ -133,12 +137,6 @@ fn quiesce(last: &str) -> Result<(crate::quiesce::Stopping, StopWire), SyscallEr } else { crate::alert!("{stopped}"); } - // `klogd` inside its hold of the wire as the last word is logged, which a - // stop that did not hold the wire would leave it in at the seal below. - #[cfg(feature = "boot-actuators")] - if crate::actuator::wire_held_at_the_last_word() { - crate::log::console::hold_at_the_last_word(&parkable); - } log!("{last}"); // Order is load-bearing: the console drain, the seal, then the caller's // non-returning call. @@ -153,10 +151,6 @@ fn quiesce(last: &str) -> Result<(crate::quiesce::Stopping, StopWire), SyscallEr // census and the last word among them. Nothing wrote them to `/log`, // and on a machine with no serial port this is their only reader. crate::log::seal_tail(stop_began); - #[cfg(feature = "boot-actuators")] - if crate::actuator::wire_held_at_the_last_word() { - crate::log::console::sealed(); - } Ok((stopping, wire)) } diff --git a/kernel/src/syscall/proc.rs b/kernel/src/syscall/proc.rs index 12dfd91da5d..11775baad6f 100644 --- a/kernel/src/syscall/proc.rs +++ b/kernel/src/syscall/proc.rs @@ -20,8 +20,29 @@ use kernel::sched::task::WaitClass; use super::cancelled; use super::handles::demand_syscap; -pub(super) fn sys_thread_exit(code: i32) -> u64 { - process::thread_exit(code); +/// End the calling thread. A detached one first gives back the range it +/// names, its own stack, which it no longer runs on here; a range refused is +/// answered and the thread runs on. +pub(super) fn sys_thread_exit(code: i32, flags: u64, at: u64, len: u64) -> u64 { + let joined = match flags { + 0 if at == 0 && len == 0 => process::Joined::Joinable, + THREAD_EXIT_DETACHED => { + if len != 0 { + if let Err(e) = super::vm::release(at, len) { + return e.to_u64(); + } + } + process::Joined::Detached + } + _ => return SyscallError::InvalidArgument.to_u64(), + }; + process::thread_exit(code, joined); +} + +/// Let another runnable thread of the caller's band have this CPU. +pub(super) fn sys_yield() -> u64 { + crate::scheduler::yield_now(); + 0 } pub(super) fn sys_exit(code: i32) -> u64 { diff --git a/kernel/src/syscall/vm.rs b/kernel/src/syscall/vm.rs index 30365cfc39f..3c166aed0b4 100644 --- a/kernel/src/syscall/vm.rs +++ b/kernel/src/syscall/vm.rs @@ -64,13 +64,18 @@ pub(super) fn sys_vm_reserve(len: u64, prot: u64, place: u64, kind: u64) -> u64 /// Take every page of `[addr, addr + len)` away, as POSIX `munmap` does. pub(super) fn sys_vm_release(addr: u64, len: u64) -> u64 { - answer(range(addr, len).and_then(|(start, len)| { - let space = process::current_address_space(); - let mut space = space.lock(); - space.room(start.raw(), start.raw() + len, 0)?; - space.release(start, len); - Ok(()) - })) + answer(release(addr, len)) +} + +/// Take `len` bytes at `addr` out of the caller's space, as `SYS_VM_RELEASE` +/// and a detached thread's exit do. +pub(super) fn release(addr: u64, len: u64) -> Result<(), SyscallError> { + let (start, len) = range(addr, len)?; + let space = process::current_address_space(); + let mut space = space.lock(); + space.room(start.raw(), start.raw() + len, 0)?; + space.release(start, len); + Ok(()) } pub(super) fn sys_vm_protect(addr: u64, len: u64, prot: u64) -> u64 { diff --git a/kernel/src/watch.rs b/kernel/src/watch.rs index 8d34310feb5..59e2dff7a1d 100644 --- a/kernel/src/watch.rs +++ b/kernel/src/watch.rs @@ -196,23 +196,9 @@ fn not_revocable() -> ! { panic!("watch: a revoke reached a wait that does not end on one"); } -/// Park once: until a post reaches this registration, the deadline passes, or -/// this thread is cancelled. A return is not an answer — the caller re-reads -/// its condition. -#[track_caller] -pub fn wait>( - p: &Parkable, - armed: &Armed<'_, L>, - deadline: Deadline, -) -> Result<(), Cancelled> { - match wait_inner(p, armed, deadline, Cancel::Answers) { - Ok(()) => Ok(()), - Err(Ended::Cancelled) => Err(Cancelled(())), - Err(Ended::Revoked) => not_revocable(), - } -} - -/// The same as [`wait`], for a wait a kill may not end. +/// Park once, for a wait a kill may not end: until a post reaches this +/// registration or the deadline passes. A return is not an answer — the +/// caller re-reads its condition. #[track_caller] pub fn wait_uncancellable>(p: &Parkable, armed: &Armed<'_, L>, deadline: Deadline) { match wait_inner(p, armed, deadline, Cancel::Ignores) { @@ -256,25 +242,6 @@ pub fn wait_until>( Ok(()) } -/// Register, then park until `ready()` holds, for a wait a kill may not end -/// and no deadline bounds. -#[track_caller] -pub fn wait_uncancellable_until(p: &Parkable, watch: &Watch, token: u64, ready: impl Fn() -> bool) { - if ready() { - return; - } - // No current task cannot be answered by returning: the caller would carry - // on believing it holds a lock it never took. - // Class is fixed at `Other`: blocked time on a lock is the holder's reason, not the contender's. - let armed = arm(watch, token, WaitClass::Other) - .expect("watch: an uncancellable wait with no task to park"); - // Exits only on the predicate: returning without it here means returning - // without the lock held. - while !ready() { - wait_uncancellable(p, &armed, Deadline::never()); - } -} - #[track_caller] fn wait_inner>( _p: &Parkable, diff --git a/lib/logkeeper-api/src/lib.rs b/lib/logkeeper-api/src/lib.rs index d0bfa1d7c9e..044753cc52d 100644 --- a/lib/logkeeper-api/src/lib.rs +++ b/lib/logkeeper-api/src/lib.rs @@ -1,12 +1,12 @@ //! Asking `logkeeper` for this boot's log on this machine: one [`READ`] on //! its [`SERVICE`] port, answered by one [`SERVED`] frame carrying how many //! bytes are the boot so far and the read end of a pipe the log is written -//! into, from its first line; or the same as the machine's console carrier +//! into, from its first line; or the same as one of the console's carriers //! ([`carry`]). use toyos::endow; use toyos::{Connection, Pipe}; -use toyos_logstream::{CARRIER, CARRY, DELIVERED, READ, SERVED, SERVICE}; +use toyos_logstream::{CARRY, DELIVERED, READ, SERVED, SERVICE}; /// `logkeeper`'s answer. pub struct Served { @@ -36,12 +36,12 @@ fn answer(conn: &Connection) -> Result { Ok(Served { pipe, boot_so_far }) } -/// Ask for the log as the machine's console carrier, on -/// [`toyos_logstream::CARRIER`]: the answer is [`read`]'s, and the connection -/// stays open for the carrier to say what its channel has taken -/// ([`delivered`]). -pub fn carry() -> Result<(Connection, Served), String> { - let conn = endow::service(CARRIER).map_err(|e| format!("no `{CARRIER}` service: {e:?}"))?; +/// Ask for the log as one of the console's carriers, on `channel` +/// ([`toyos_logstream::CARRIER`] or [`toyos_logstream::WIRE`]): the answer is +/// [`read`]'s, and the connection stays open for the carrier to say what its +/// channel has taken ([`delivered`]). +pub fn carry(channel: &str) -> Result<(Connection, Served), String> { + let conn = endow::service(channel).map_err(|e| format!("no `{channel}` service: {e:?}"))?; conn.signal(CARRY).map_err(|e| format!("logkeeper would not take the request: {e:?}"))?; let served = answer(&conn)?; Ok((conn, served)) diff --git a/lib/logstream/src/lib.rs b/lib/logstream/src/lib.rs index 572e4333657..567b165b0b8 100644 --- a/lib/logstream/src/lib.rs +++ b/lib/logstream/src/lib.rs @@ -18,10 +18,10 @@ //! newline. A program's text never does: a record ends at a newline, and //! [`Text`] writes every control byte as text. //! -//! The same form goes to the console, where `logkeeper` is the one writer of -//! program lines, without the wall-clock stamp. A terminal shows either in -//! the console's form and in colour ([`Shown`]); no line of the log carries a -//! colour. +//! The same form goes to the console's channels, which `logkeeper` feeds; the +//! kernel's own console lines, before userland runs and from the machine's +//! stop, carry no wall-clock stamp. A terminal shows either in the console's +//! form and in colour ([`Shown`]); no line of the log carries a colour. //! //! Pure: `core` and `alloc`, no `unsafe`, no I/O. @@ -58,6 +58,29 @@ pub const CARRIER: &str = "log-carrier"; /// The one row that may receive [`CARRIER`]: the virtio console device's driver. pub const CARRIER_HOLDER: &str = "virtio-console"; +/// The name of the port the console UART's driver asks `logkeeper` for the log +/// on, as [`CARRIER`]'s carrier asks: from the first line the kernel did not +/// put on the UART itself before userland ran, and none of what the carrier's +/// channel takes. Its row alone receives it. +pub const WIRE: &str = "log-wire"; + +/// The one row that may receive [`WIRE`]: the console UART's driver. +pub const WIRE_HOLDER: &str = "uart"; + +/// The endowment label of the write end of the console's input: the pipe the +/// supervisor makes, whose read end is every program's standard input, and +/// hands [`WIRE_HOLDER`] alone to write what the UART receives into. +pub const CONSOLE_INPUT: &str = "console-input"; + +/// What the UART says as the carrier's channel takes the log off it: every +/// UART line before it the carrier's channel carries too, and the ones after +/// it are the UART's alone. +pub const CARRIED_ELSEWHERE: &str = + "console: the log is carried elsewhere from here; the UART takes the stop's and a fatal path's lines"; + +/// What the UART says as it carries the log again. +pub const BACK_ON_THE_UART: &str = "console: the UART carries the log again"; + /// What `logkeeper` says once the carrier's channel has taken nothing it is /// owed for its bound and the UART carries the log beside it: lines can then /// reach both channels, out of order. @@ -71,8 +94,8 @@ pub const CARRY: u32 = 8; pub const DELIVERED: u32 = 9; /// The manifest's name for the program that is the log: the supervisor endows it the -/// [`ORIGINS`] acceptor and a console it may write ([`CONSOLE`]), and gives -/// it a ring like every other program's, which it reads like every other. +/// [`ORIGINS`] acceptor, and gives it a ring like every other program's, which +/// it reads like every other. pub const LOGKEEPER: &str = "logkeeper"; /// The endowment label of the acceptor the supervisor hands `logkeeper` for its frames. @@ -80,10 +103,6 @@ pub const LOGKEEPER: &str = "logkeeper"; /// no program can register a ring under a name of its choosing. pub const ORIGINS: &str = "log-origins"; -/// The endowment label of the one console the supervisor gives a writable handle to: -/// `logkeeper`'s, where it puts every program's line with its head. -pub const CONSOLE: &str = "log-console"; - /// The endowment label of a program's waker of the bell [`REGISTER`] hands /// `logkeeper`: nothing rings it, and the program's waker closing — which only /// its exit does — is how `logkeeper` knows the ring has no writer left to wait diff --git a/sdk/abi/src/handle.rs b/sdk/abi/src/handle.rs index 976fb93a9bc..70130535078 100644 --- a/sdk/abi/src/handle.rs +++ b/sdk/abi/src/handle.rs @@ -83,22 +83,21 @@ pub enum ObjectKind { Connector = 7, Namespace = 8, File = 9, - Console = 10, - SysCap = 11, - Process = 12, - Null = 13, - Job = 14, - Notice = 15, - WakeLatency = 16, - EventSignal = 17, - EventWait = 18, - Pager = 19, - Paged = 20, - DeviceSet = 21, + SysCap = 10, + Process = 11, + Null = 12, + Job = 13, + Notice = 14, + WakeLatency = 15, + EventSignal = 16, + EventWait = 17, + Pager = 18, + Paged = 19, + DeviceSet = 20, } impl ObjectKind { - pub const ALL: [ObjectKind; 22] = [ + pub const ALL: [ObjectKind; 21] = [ Self::PipeRead, Self::PipeWrite, Self::Connection, @@ -109,7 +108,6 @@ impl ObjectKind { Self::Connector, Self::Namespace, Self::File, - Self::Console, Self::SysCap, Self::Process, Self::Null, diff --git a/sdk/abi/src/lib.rs b/sdk/abi/src/lib.rs index 9003b1ac914..8c7a29d0b26 100644 --- a/sdk/abi/src/lib.rs +++ b/sdk/abi/src/lib.rs @@ -31,6 +31,7 @@ pub mod pci; pub mod quit; pub mod syscall; pub mod trace; +pub mod uart; pub mod usersafe; pub use handle::{RawHandle, Rights, HANDLE_INVALID}; diff --git a/sdk/abi/src/syscall.rs b/sdk/abi/src/syscall.rs index 246b6390a34..dfe6558d977 100644 --- a/sdk/abi/src/syscall.rs +++ b/sdk/abi/src/syscall.rs @@ -361,8 +361,9 @@ pub const SYS_PAGED_OP: u64 = 157; pub const SYS_PAGED_NOTICES: u64 = 158; /// Take a connection's next whole message. See [`message_recv`]. pub const SYS_MESSAGE_RECV: u64 = 159; -/// Whether a console holder carries the log off the machine. See [`console_carried`]. -pub const SYS_CONSOLE_CARRIED: u64 = 160; +/// How far the console's channels have carried the kernel's records. See +/// [`log_spoken`]. +pub const SYS_LOG_SPOKEN: u64 = 160; /// Set the CPUs one of the caller's own threads may run on. See [`thread_set_affinity`]. pub const SYS_THREAD_SET_AFFINITY: u64 = 161; /// Read them back into a buffer of at least [`CPU_MASK_WORDS`] words, or @@ -387,19 +388,23 @@ pub const SYS_DEVICE_SET: u64 = 166; /// The device set's snapshot, read whole. See [`device_set_read`]. pub const SYS_DEVICE_SET_READ: u64 = 167; +/// Give the CPU to another runnable thread of the caller's band. See +/// [`yield_now`]. +pub const SYS_YIELD: u64 = 168; + /// Bins in the per-process syscall profile — one for every number this ABI /// issues, and one at the end for every number it does not. /// /// **The profile's parts sum to its total, and that is the whole requirement.** /// A bin array narrower than the ABI reaches drops calls out of the line while /// the total goes on counting them. -pub const SYSCALL_PROFILE_BINS: usize = 169; +pub const SYSCALL_PROFILE_BINS: usize = 170; /// Where a number this ABI does not issue is counted. Merging is a degradation /// a reader can see in the line; dropping is one nobody can. pub const SYSCALL_PROFILE_OTHER: usize = SYSCALL_PROFILE_BINS - 1; -const _: () = assert!(SYS_DEVICE_SET_READ < SYSCALL_PROFILE_OTHER as u64); +const _: () = assert!(SYS_YIELD < SYSCALL_PROFILE_OTHER as u64); pub const WNOHANG: u64 = 1; /// Every bit `SYS_PROCESS_WAIT`'s flag word defines; a wait carrying another @@ -414,8 +419,7 @@ crate::user_safe! { /// what makes endowing a device claim work with no special case: a claim /// carries no [`Rights::DUP`], so the move is the only expressible form and /// the parent provably no longer holds it. A child's descriptors are - /// endowments labelled [`FD_PREFIX`] and their number; a console among - /// them is the child's own, minted for it. + /// endowments labelled [`FD_PREFIX`] and their number. /// /// [`Rights::DUP`]: crate::handle::Rights::DUP #[derive(Clone, Copy)] @@ -685,8 +689,8 @@ pub enum FileType { File = 1, Pipe = 2, // 3 was `Keyboard` and 7 `Mouse`, the kernel's input streams: input is - // userland's, over the `input` port. - Serial = 4, + // userland's, over the `input` port. 4 was `Serial`, the kernel's console: + // the UART is its driver's. Framebuffer = 5, Tty = 6, Socket = 8, @@ -718,7 +722,6 @@ impl FileType { 0 => Some(Self::Unknown), 1 => Some(Self::File), 2 => Some(Self::Pipe), - 4 => Some(Self::Serial), 5 => Some(Self::Framebuffer), 6 => Some(Self::Tty), 8 => Some(Self::Socket), @@ -918,12 +921,36 @@ pub fn pwrite(handle: RawHandle, buf: &[u8], offset: u64) -> Result ! { loop { syscall(SYS_THREAD_EXIT, code as u64, 0, 0, 0); } } +/// [`SYS_THREAD_EXIT`]'s flag for a thread nobody will join: its place in its +/// process goes as it ends, and the range it names is released as +/// [`vm_release`] releases it, once the thread no longer runs on it. +pub const THREAD_EXIT_DETACHED: u64 = 1; + +/// Exit the current thread, which nobody will join, releasing `stack`'s +/// `len` bytes (none where `len` is 0): the stack it stands on, which only +/// the kernel can take away once the thread has left it. Returns only where +/// the range is refused, as [`vm_release`] would refuse it; a main thread's +/// exit is its process's, whatever this says. +/// +/// # Safety +/// Nothing but this thread uses `stack`, which ends with it. +pub unsafe fn thread_exit_detached(code: i32, stack: *mut u8, len: usize) -> SyscallError { + let word = syscall(SYS_THREAD_EXIT, code as u64, THREAD_EXIT_DETACHED, stack as u64, len as u64); + SyscallError::from_u64(word).unwrap_or(SyscallError::InvalidArgument) +} + +/// Let another runnable thread of the caller's band have the CPU, if one is +/// waiting for it; the caller runs on at once where none is. +pub fn yield_now() { + syscall(SYS_YIELD, 0, 0, 0, 0); +} + /// Exit the entire process (all threads) with `code`. Does not return. pub fn exit(code: i32) -> ! { loop { syscall(SYS_EXIT, code as u64, 0, 0, 0); } @@ -1589,6 +1616,9 @@ device_classes! { /// ([`crate::acpi`]): the kernel puts the machine in ACPI mode at the /// mint, and the ports the firmware's tables name are the holder's. Acpi = 10 => "acpi", + /// The machine's console UART, the holder's between the kernel's boot and + /// its stop ([`crate::uart`]). + Uart = 11 => "uart", } /// What identifies a PCI *card*, not the slot firmware put it in: the same @@ -2362,17 +2392,17 @@ pub unsafe fn shm_sub(shm: RawHandle, offset: u64, len: u64) -> Result Result<(), SyscallError> { - check(syscall(SYS_CONSOLE_CARRIED, console.0 as u64, elsewhere as u64, 0, 0)).map(drop) +/// [`Rights::LOG`]: crate::handle::Rights::LOG +pub fn log_spoken(syscap: RawHandle, at_ns: u64) -> Result { + check(syscall(SYS_LOG_SPOKEN, syscap.0 as u64, at_ns, 0, 0)) } /// The machine's [header](SYSINFO_HEADER_SIZE), then as much of the process diff --git a/sdk/abi/src/uart.rs b/sdk/abi/src/uart.rs new file mode 100644 index 00000000000..b7212e6c285 --- /dev/null +++ b/sdk/abi/src/uart.rs @@ -0,0 +1,46 @@ +//! What a claim on the machine's console UART hands the process that drives +//! it. +//! +//! **The UART is the kernel's before userland runs and after it stops**: the +//! kernel puts the boot on it until the hand-over that comes before any +//! process can claim it, and writes it again from the machine's stop, once +//! userland has stopped, and on every fatal path. Between the two it is the +//! holder's, and the kernel writes nothing to it, so a holder that drives +//! the line it finds keeps the kernel's programming. +//! +//! **The holder reaches the registers as its architecture places them** +//! ([`UartInfo`]): a 16550's at the ports it may `in` and `out` from the +//! claim's first read on, a PL011's in the register frame that read installs. +//! Its interrupt is records on the claim, as an ISA function's are; a level +//! line is masked by the kernel each time it is taken and unmasked by the +//! holder's acknowledgement ([`ACK`]). + +use crate::handle::RawHandle; + +/// A 16550 at the port [`UartInfo::port`] names and the seven after it. +pub const NS16550: u32 = 1; +/// A PL011, or an SBSA generic UART, whose registers sit at the same offsets +/// (Arm PL011 TRM r1p5, §3.3; Arm BSA 1.0, Appendix B), in [`UartInfo::frame`]. +pub const PL011: u32 = 2; + +crate::user_safe! { + /// The claim's description. + #[derive(Clone, Copy, PartialEq, Eq, Debug)] + pub struct UartInfo { + /// [`NS16550`] or [`PL011`]. + pub kind: u32, + /// A 16550's first port; 0 for a PL011. + pub port: u32, + /// A PL011's register frame, to map: carries `MAP` and `WRITE` and + /// neither `DUP` nor `TRANSFER`. Invalid for a 16550. + pub frame: RawHandle, + /// The frame's length in bytes; 0 for a 16550. + pub frame_len: u32, + /// Nonzero where its line is level-triggered: each record is + /// acknowledged ([`ACK`]) once what raised it is served. + pub level: u32, + } +} + +/// The word a holder writes to its claim to have a level line unmasked. +pub const ACK: u32 = 1; diff --git a/sdk/libc/src/posix_io.rs b/sdk/libc/src/posix_io.rs index 227a3bd710f..26c8421b4fc 100644 --- a/sdk/libc/src/posix_io.rs +++ b/sdk/libc/src/posix_io.rs @@ -266,7 +266,6 @@ unsafe fn answered(answer: Result, buf: *mut Stat) -> syscall::FileType::Symlink => S_IFLNK | 0o777, syscall::FileType::Pipe => S_IFIFO | 0o644, syscall::FileType::Tty => S_IFCHR | 0o644, - syscall::FileType::Serial => S_IFCHR | 0o644, syscall::FileType::Null => S_IFCHR | 0o666, _ => 0o644, }; diff --git a/sdk/libc/src/pthread.rs b/sdk/libc/src/pthread.rs index f01a5d15602..aed4bf68480 100644 --- a/sdk/libc/src/pthread.rs +++ b/sdk/libc/src/pthread.rs @@ -10,12 +10,10 @@ //! thread waits for its creator to publish the tid before it runs anything, //! so every holder of a handle can join it. //! -//! A thread that ends detached hands its block to [`REAPED`], and the next -//! `pthread_create` joins it and frees its stack, which no thread can free -//! while standing on it. +//! A thread that ends detached frees its block and has the kernel release +//! its stack as it ends, which no thread can free while standing on it. use alloc::boxed::Box; -use alloc::vec::Vec; use core::cell::Cell; use core::ptr; use core::sync::atomic::{AtomicPtr, AtomicU32, AtomicU64, Ordering}; @@ -63,9 +61,6 @@ static SELF: Cell<*mut Thread> = Cell::new(ptr::null_mut()); #[thread_local] static MARKER: u8 = 0; -/// Blocks of detached threads that have ended, for the next creator to reap. -static REAPED: Lock> = Lock::new(Vec::new()); - pub(crate) use toyos::sync::Lock; /// Reserved pages below a thread's stack, so that running off it faults: @@ -129,7 +124,11 @@ unsafe fn exit_thread(result: *mut u8) -> ! { } t.result.store(result, Ordering::Release); if t.state.swap(EXITED, Ordering::AcqRel) == DETACHED { - REAPED.lock().push(thread as usize); + // Nobody holds the block but this thread. + let Thread { reservation, reserved, .. } = *unsafe { Box::from_raw(thread) }; + // SAFETY: the stack is this thread's alone, and ends with it. + let refused = unsafe { syscall::thread_exit_detached(0, reservation, reserved) }; + panic!("libc: a detached thread's stack was refused at its exit: {refused:?}"); } } syscall::thread_exit(0) @@ -168,11 +167,6 @@ pub(crate) fn spawn_own(start: StartRoutine) { /// # Safety /// `attr` is null or a `pthread_attr_t`, and `start_routine` takes `arg`. unsafe fn create(attr: *const u64, start_routine: StartRoutine, arg: *mut u8, counted: bool) -> Result<*mut Thread, i32> { - let reaped = core::mem::take(&mut *REAPED.lock()); - for block in reaped { - unsafe { reap(block as *mut Thread) }; - } - let attr = if attr.is_null() { attr_default() } else { unsafe { *attr } }; let stack_size = (attr & !ATTR_DETACHED) as usize; let Some((reservation, reserved, stack)) = reserve_stack(stack_size) else { diff --git a/sdk/libc/src/stdio.rs b/sdk/libc/src/stdio.rs index 22512178f20..08daa8f7184 100644 --- a/sdk/libc/src/stdio.rs +++ b/sdk/libc/src/stdio.rs @@ -146,20 +146,13 @@ unsafe fn unregister_stream(f: *mut FILE) { } } -/// Whether this fd names a device a human is watching. -/// -/// Deliberately not `isatty`, which answers the narrower POSIX question and -/// returns false for `FileType::Serial` — which is exactly what every ToyOS -/// process gets for fds 0, 1 and 2, since `spawn_kernel` hands out -/// `Descriptor::SerialConsole`. Routing that through `isatty` would make every -/// program's stdout *fully* buffered: output would appear only at exit, and a -/// program that wedged or was killed on a timeout would lose all of it — the -/// failure mode that is hardest to tell apart from the program never having run. -/// C's rule is "interactive device", not "terminal", and a serial console is one. +/// Whether this fd names a device a human is watching: a terminal. C's rule +/// is "interactive device", which `isatty` answers too narrowly where a +/// device is interactive and not a terminal; ToyOS has no such device. unsafe fn fd_is_interactive(fd: i32) -> bool { use toyos_abi::syscall::FileType; match files::fstat(fd) { - Ok(st) => matches!(st.file_type, FileType::Tty | FileType::Serial), + Ok(st) => st.file_type == FileType::Tty, Err(_) => false, } } diff --git a/sdk/std/sys/thread.rs b/sdk/std/sys/thread.rs index c0f6dbddfbe..2b09e6b3dc6 100644 --- a/sdk/std/sys/thread.rs +++ b/sdk/std/sys/thread.rs @@ -6,12 +6,12 @@ use crate::io; use crate::mem::ManuallyDrop; use crate::num::NonZero; use crate::ptr; -use crate::sync::atomic::{AtomicPtr, AtomicU32, Ordering}; +use crate::sync::atomic::{AtomicU32, Ordering}; use crate::thread::ThreadInit; use crate::time::Duration; /// A thread's handle. Joined, it frees the thread's stack; dropped, it -/// detaches the thread, whose stack the next spawn frees once it has ended. +/// detaches the thread, which frees its own as it ends. pub struct Thread { block: *mut Block, } @@ -19,8 +19,8 @@ pub struct Thread { unsafe impl Send for Thread {} unsafe impl Sync for Thread {} -/// What a thread and its handle share. Freed by whoever reaps the thread: -/// its joiner, or for a detached thread the first spawn after it ended. +/// What a thread and its handle share. Freed by its joiner, or by a detached +/// thread itself as it ends. struct Block { tid: AtomicU32, state: AtomicU32, @@ -29,18 +29,12 @@ struct Block { reserved: usize, /// Taken by the thread as it starts. init: UnsafeCell>>, - /// The next block on [`REAPED`]. - next: *mut Block, } const JOINABLE: u32 = 0; const DETACHED: u32 = 1; const EXITED: u32 = 2; -/// Detached threads that have ended, linked through [`Block::next`]: a thread -/// cannot free the stack it stands on, and pushing allocates nothing. -static REAPED: AtomicPtr = AtomicPtr::new(ptr::null_mut()); - pub const DEFAULT_MIN_STACK_SIZE: usize = 2 * 1024 * 1024; /// Reserved pages below a thread's stack, so that running off it faults: one @@ -65,14 +59,6 @@ unsafe fn reap(block: *mut Block) { impl Thread { pub unsafe fn new(stack: usize, init: Box) -> io::Result { - let mut ended = REAPED.swap(ptr::null_mut(), Ordering::Acquire); - while !ended.is_null() { - // SAFETY: a block on the list was handed over by its ended thread. - let next = unsafe { (*ended).next }; - unsafe { reap(ended) }; - ended = next; - } - // The size asked for, in whole pages, committed before the thread // starts so that no touch of it can find no memory. let Some(stack_size) = stack.max(1).checked_next_multiple_of(syscall::PAGE_SIZE) else { @@ -98,7 +84,6 @@ impl Thread { reservation, reserved, init: UnsafeCell::new(Some(init)), - next: ptr::null_mut(), })); // SAFETY: entry point and stack are valid; the block outlives the thread. @@ -119,7 +104,7 @@ impl Thread { return Err(io::const_error!(io::ErrorKind::Uncategorized, "thread spawn failed")); } // Before the handle exists: only a dropped handle detaches the - // thread, so every reaper sees this through the handle or the list. + // thread, and a detached thread reads no tid. unsafe { (*block).tid.store(tid as u32, Ordering::Relaxed) }; Ok(Thread { block }) } @@ -134,10 +119,10 @@ impl Thread { impl Drop for Thread { fn drop(&mut self) { // SAFETY: the block lives until the thread is reaped, which only this - // handle or, once it is detached, the thread's own exit arranges. + // handle or, once it is detached, the thread's own exit does. let b = unsafe { &*self.block }; if b.state.compare_exchange(JOINABLE, DETACHED, Ordering::AcqRel, Ordering::Acquire).is_err() { - // It has ended already, so nothing will hand it to `REAPED`. + // It has ended already, so it left itself to be joined. unsafe { reap(self.block) }; } } @@ -155,16 +140,14 @@ extern "C" fn thread_trampoline(data: u64) { crate::sys::thread_local::destructors::run(); } crate::rt::thread_cleanup(); - // SAFETY: the block is this thread's until it is handed over below. + // SAFETY: the block is this thread's until its state says otherwise. if unsafe { &*block }.state.swap(EXITED, Ordering::AcqRel) == DETACHED { - let mut head = REAPED.load(Ordering::Relaxed); - loop { - unsafe { (*block).next = head }; - match REAPED.compare_exchange_weak(head, block, Ordering::Release, Ordering::Relaxed) { - Ok(_) => break, - Err(now) => head = now, - } - } + // Nobody holds the block but this thread, which frees it and leaves + // its stack to the kernel to release. + let Block { reservation, reserved, .. } = *unsafe { Box::from_raw(block) }; + // SAFETY: the stack is this thread's alone, and ends with it. + let refused = unsafe { syscall::thread_exit_detached(0, reservation, reserved) }; + rtabort!("a detached thread's stack was refused at its exit: {refused:?}"); } syscall::thread_exit(0); } @@ -182,8 +165,7 @@ pub fn current_os_id() -> Option { } pub fn yield_now() { - // No yield syscall yet — spin hint - core::hint::spin_loop(); + syscall::yield_now(); } pub fn set_name(name: &CStr) { diff --git a/sdk/toyos/src/device.rs b/sdk/toyos/src/device.rs index 292e54909e3..a5f982f56fa 100644 --- a/sdk/toyos/src/device.rs +++ b/sdk/toyos/src/device.rs @@ -22,6 +22,7 @@ device_info!( toyos_abi::FramebufferInfo, toyos_abi::pci::PciFunctionInfo, toyos_abi::acpi::AcpiInfo, + toyos_abi::uart::UartInfo, ); /// Read a claim's description, whose buffer fields are handles this call @@ -276,6 +277,39 @@ impl AcpiDev { } } +/// The machine's console UART, driven by this process between the kernel's +/// boot and its stop ([`toyos_abi::uart`]). +/// +/// **Read once as a description, which binds a 16550's ports to this process +/// and installs a PL011's register frame, and afterwards as interrupts**, as +/// an [`AcpiDev`] is. +pub struct UartDev(pub(crate) Device); + +impl UartDev { + /// The description, and the register frame mapped where it names one. + pub fn open(&self) -> Result<(toyos_abi::uart::UartInfo, Option), SyscallError> { + let info: toyos_abi::uart::UartInfo = read_info(&self.0)?; + let frame = match info.frame == toyos_abi::HANDLE_INVALID { + true => None, + false => Some(crate::shm::SharedMemory::adopt(crate::OwnedHandle(info.frame), info.frame_len as usize)?), + }; + Ok((info, frame)) + } + + pub fn irq(&self) -> Result { + irq_record(&self.0) + } + + /// What raised a level line is served: unmask it. + pub fn ack(&self) -> Result<(), SyscallError> { + syscall::write(self.0.0.0, &toyos_abi::uart::ACK.to_ne_bytes()).map(|_| ()) + } +} + +impl AsHandle for UartDev { + fn as_handle(&self) -> crate::BorrowedHandle<'_> { self.0.as_handle() } +} + impl AsHandle for AcpiDev { fn as_handle(&self) -> crate::BorrowedHandle<'_> { self.0.as_handle() } } diff --git a/sdk/toyos/src/endow.rs b/sdk/toyos/src/endow.rs index 37c8e4da636..9adb0a82aa0 100644 --- a/sdk/toyos/src/endow.rs +++ b/sdk/toyos/src/endow.rs @@ -69,7 +69,6 @@ from_handle! { SysCap => SysCap, Job => Job, Device => Device, - crate::Console => crate::Console, crate::Pipe => crate::Pipe, crate::wake::Waker => crate::wake::Waker::from, crate::wake::Bell => crate::wake::Bell::from, @@ -77,6 +76,7 @@ from_handle! { crate::FramebufferDev => |h| crate::FramebufferDev(Device(h)), crate::PciDev => |h| crate::PciDev(Device(h)), crate::AcpiDev => |h| crate::AcpiDev(Device(h)), + crate::UartDev => |h| crate::UartDev(Device(h)), } /// This process's endowment table, parsed once. diff --git a/sdk/toyos/src/lib.rs b/sdk/toyos/src/lib.rs index 4c63fd67655..1e07b7351dd 100644 --- a/sdk/toyos/src/lib.rs +++ b/sdk/toyos/src/lib.rs @@ -50,7 +50,7 @@ pub mod volatile; pub use ipc::Connection; pub use device::{ - AcpiDev, DmaRegion, FramebufferDev, PciDev, + AcpiDev, DmaRegion, FramebufferDev, PciDev, UartDev, }; pub use toyos_abi::RawHandle; @@ -290,7 +290,6 @@ macro_rules! owned_from { owned_from! { Pipe => |p| p.0; Device => |d| d.0; - Console => |c| c.0; ipc::Connection => |c| c.0; port::Acceptor => |a| a.0; port::Connector => |c| c.0; @@ -301,6 +300,7 @@ owned_from! { device::FramebufferDev => |d| d.0 .0; device::PciDev => |d| d.0 .0; device::AcpiDev => |d| d.0 .0; + device::UartDev => |d| d.0 .0; } #[cfg(all(feature = "std", target_os = "toyos", not(test)))] @@ -383,7 +383,6 @@ mod std_io { super::wake::Waker, super::wake::Bell, super::Device, - super::Console, super::ipc::Connection, super::port::Acceptor, super::port::Connector, @@ -394,31 +393,7 @@ mod std_io { super::device::FramebufferDev, super::device::PciDev, super::device::AcpiDev, + super::device::UartDev, ); } -/// A console handle: the one the supervisor endows `/system/bin/logkeeper` with, -/// the only one in the machine that may write. -/// -/// **A write takes whole lines and says how many bytes that was.** The -/// kernel queues each line for its console writer and takes only what its -/// queue has room for, so a short count is a writer ahead of the console — -/// poll for `WRITABLE` and send the rest. -pub struct Console(pub(crate) OwnedHandle); - -impl Console { - pub fn write_nonblock(&self, buf: &[u8]) -> Result { - self.0.write_nonblock(buf) - } - - /// Say whether this holder carries the log off the machine by another - /// channel (`toyos_abi::syscall::console_carried`); the console's end - /// lets go of it. - pub fn carry_elsewhere(&self, elsewhere: bool) -> Result<(), toyos_abi::syscall::SyscallError> { - toyos_abi::syscall::console_carried(self.0.raw(), elsewhere) - } -} - -impl AsHandle for Console { - fn as_handle(&self) -> BorrowedHandle<'_> { self.0.as_handle() } -} diff --git a/sdk/toyos/src/log/mod.rs b/sdk/toyos/src/log/mod.rs index 726321a1586..30299ff3450 100644 --- a/sdk/toyos/src/log/mod.rs +++ b/sdk/toyos/src/log/mod.rs @@ -68,6 +68,13 @@ pub fn cursor_words(cap: &SysCap) -> Result { words_for(|cursor| syscall::log_read(cap.as_handle().raw(), cursor, &mut [LogRecord::EMPTY])) } +/// Say the console's channels have delivered every kernel record stamped at or +/// before `at_ns`, and answer the newest stamp the console has spoken +/// (`toyos_abi::syscall::log_spoken`); `at_ns` 0 only reads it. +pub fn spoken(cap: &SysCap, at_ns: u64) -> Result { + syscall::log_spoken(cap.as_handle().raw(), at_ns) +} + /// [`cursor_words`] for a read of either kind. pub(crate) fn words_for(read: impl FnOnce(&mut [u64]) -> Result) -> Result { let mut head = [0; CURSOR_HEAD_WORDS]; diff --git a/system/logkeeper/src/carrier.rs b/system/logkeeper/src/carrier.rs index ce3591d3f17..5e7c031dbc9 100644 --- a/system/logkeeper/src/carrier.rs +++ b/system/logkeeper/src/carrier.rs @@ -1,50 +1,78 @@ -//! The machine's console carrier, asked for on [`toyos_logstream::CARRIER`]: -//! the one reader the console UART goes quiet for. +//! The console's carriers: a driver that asks for the log here, writes it to +//! its channel and says how much of its pipe the channel has taken +//! ([`DELIVERED`]). Two channels, one serving ([`Carriers`]): the VM's console +//! port, asked for on [`toyos_logstream::CARRIER`], and the console UART, on +//! [`toyos_logstream::WIRE`]. A carrier is admitted alone on its channel, fed +//! from where its channel starts, and let go with its connection. //! -//! **The UART goes quiet once the carrier's channel has taken the log's first -//! byte, and comes back once the carrier goes, the machine stops, or the -//! channel takes nothing of what it is owed for [`STALL`]** ([`Carriage`]): -//! a carrier that is stuck but alive never leaves the UART silent. The UART -//! is quiet again once the channel takes more; meanwhile both carry the log. +//! **The UART goes quiet once the port's channel has taken the log's first +//! byte, and comes back once the port's carrier goes, the machine stops, or +//! the port's channel takes nothing of what it is owed for [`STALL`]** +//! ([`Carriage`]): a carrier that is stuck but alive never leaves the UART +//! silent. The UART is quiet again once the channel takes more; meanwhile +//! both carry the log. The UART says each change on itself +//! ([`CARRIED_ELSEWHERE`], [`BACK_ON_THE_UART`]) and carries nothing between. //! -//! **A slow channel is never let go**: the carrier is handed the boot from -//! its first line like any reader, and keeps it however long its channel -//! takes. It says on its connection how much of its pipe its channel has -//! taken ([`DELIVERED`]), which is how the stop knows the channel has the log -//! up to the flush before the UART takes the rest ([`Carrier::stop`]). +//! **The port's carrier is handed the boot from its first line, and the +//! UART's from the first line after the records the kernel put on the UART +//! itself** ([`Hub::wire_from`]). A slow channel is never let go. What a +//! channel has taken is told the kernel as the newest stamp the console has +//! spoken (`toyos::log::spoken`), so a fatal path and the stop put on the UART +//! the kernel's records no channel has delivered. //! -//! **This program's own end is the kernel's to answer**: the console it says -//! the log is carried on goes with this process, and the kernel gives the log -//! back to the UART with it, so a crash here cannot leave the UART quiet. +//! **This program's end is its carriers' pipes'**: what follows on the UART +//! is the kernel's stop or a fatal path, from the last stamp spoken. //! -//! What the carrier's channel had not taken when the carrier went is on -//! neither (`issues/the-console-loses-what-its-carrier-had-not-delivered.md`). +//! What a channel had not taken when its carrier went is on neither +//! (`issues/the-console-loses-what-its-carrier-had-not-delivered.md`). +use std::collections::VecDeque; use std::sync::{Arc, Condvar, Mutex, MutexGuard}; use std::time::{Duration, Instant}; use toyos::ipc::{Connection, FrameRx, RxStep}; use toyos::poller::{Poller, READABLE, WRITABLE}; use toyos::port::Acceptor; -use toyos::{say, Console, Pipe}; +use toyos::syscap::SysCap; +use toyos::{say, Pipe}; +use toyos_abi::log::Severity; use toyos_abi::syscall::SyscallError; -use toyos_logstream::{CARRIER_STALLED, CARRY, DELIVERED}; +use toyos_logstream::{BACK_ON_THE_UART, CARRIED_ELSEWHERE, CARRIER_STALLED, CARRY, DELIVERED}; -use crate::serve::Hub; +use crate::serve::{Fed, Hub}; -/// How long the stop waits for the carrier's channel to take the log up to -/// the flush: policy, well inside the supervisor's flush bound, and far past -/// what a channel that takes bytes at all needs for the last rounds. +/// How long the stop waits for the channels to take the log up to the flush: +/// policy, well inside the supervisor's flush bound, and far past what a +/// channel that takes bytes at all needs for the last rounds. const STOP_WAIT: Duration = Duration::from_secs(2); -/// How long the carrier's channel may take nothing of the bytes it is owed +/// How long the port's channel may take nothing of the bytes it is owed /// before the UART carries the log too. Policy, short because the costs are /// lopsided: a false stall only duplicates lines on both channels, and a /// missed one leaves both silent. pub const STALL: Duration = Duration::from_secs(5); +/// Which of the console's channels a carrier is. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Channel { + /// The VM's console port, which the UART goes quiet for. + Port, + /// The console UART. + Wire, +} + +impl Channel { + const ALL: [Self; 2] = [Self::Port, Self::Wire]; + + fn name(self) -> &'static str { + match self { + Self::Port => "the console carrier's channel", + Self::Wire => "the UART", + } + } +} -/// What the kernel is to be told about the console UART. +/// What a change of the port's carrying makes the UART. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Tell { /// The log is carried elsewhere: the UART takes only the stop's and a @@ -54,8 +82,9 @@ pub enum Tell { Loud, } -/// Whether the UART is quiet, decided from what the carrier and the stop do. -/// Pure: each event answers what the kernel is told, if anything. +/// One channel's carrier, its counts and whether it carries the log: what +/// the UART's quiet is decided from on the port's. Pure: each event answers +/// the change it makes, if any. #[derive(Debug, Default)] pub struct Carriage { /// The carrier admitted, by the number of its admission. @@ -70,7 +99,9 @@ pub struct Carriage { /// The channel took nothing of what it was owed for [`STALL`]. stalled: bool, stopping: bool, - quiet: bool, + /// Its channel carries the log: it has taken bytes, has not stalled, and + /// the machine is not stopping. + carrying: bool, } /// Why a carrier is refused. @@ -168,113 +199,143 @@ impl Carriage { self.settle() } - pub fn quiet(&self) -> bool { - self.quiet + pub fn carrying(&self) -> bool { + self.carrying } fn settle(&mut self) -> Option { - let quiet = self.carrier.is_some() && self.delivered > 0 && !self.stopping && !self.stalled; - if quiet == self.quiet { + let carrying = self.carrier.is_some() && self.delivered > 0 && !self.stopping && !self.stalled; + if carrying == self.carrying { return None; } - self.quiet = quiet; - Some(if quiet { Tell::Quiet } else { Tell::Loud }) + self.carrying = carrying; + Some(if carrying { Tell::Quiet } else { Tell::Loud }) } } -/// The carrier's state, shared by its threads and the main loop. -pub struct Carrier { - console: Arc, +/// Both channels' carriers, shared by their threads and the main loop. +pub struct Carriers { + hub: Arc, + /// What the kernel is told the console has spoken through. + cap: Arc, state: Mutex, - /// Posted when the carrier's channel takes bytes, its feed moves, or it goes. + /// Posted when a channel takes bytes, a feed moves, or a carrier goes. moved: Condvar, } struct State { + feeds: [Feed; 2], + /// The newest stamp the kernel was told the console spoke. + spoken: u64, +} + +/// One channel's carrier and its feed. +#[derive(Default)] +struct Feed { carriage: Carriage, /// The replay offset the bytes written into the carrier's pipe reach. at: u64, + /// Each piece of the replay written into the pipe: the pipe's length once + /// it is written, and the replay offset it reaches. + written: VecDeque<(u64, u64)>, +} + +impl State { + fn feed(&mut self, channel: Channel) -> &mut Feed { + &mut self.feeds[channel as usize] + } + + /// Whether the UART is quiet: the port carries the log. + fn quiet(&self) -> bool { + self.feeds[Channel::Port as usize].carriage.carrying() + } } -impl Carrier { - /// Serve the carrier on `acceptor` for the life of the process, telling - /// the kernel through `console`. - pub fn serve(acceptor: Acceptor, console: Arc, hub: Arc) -> Arc { - let carrier = Arc::new(Self { - console, - state: Mutex::new(State { carriage: Carriage::default(), at: 0 }), +impl Carriers { + /// Serve each channel's carrier on its acceptor for the life of the + /// process, telling the kernel what was spoken through `cap`. + pub fn serve(port: Option, wire: Option, hub: Arc, cap: Arc) -> Arc { + let carriers = Arc::new(Self { + hub, + cap, + state: Mutex::new(State { feeds: Default::default(), spoken: 0 }), moved: Condvar::new(), }); - let theirs = Arc::clone(&carrier); - std::thread::Builder::new() - .name("log-carrier".into()) - .spawn(move || theirs.run(&acceptor, &hub)) - .expect("logkeeper: the carrier's thread could not be started"); - let theirs = Arc::clone(&carrier); + for (channel, acceptor) in [(Channel::Port, port), (Channel::Wire, wire)] { + let Some(acceptor) = acceptor else { continue }; + let theirs = Arc::clone(&carriers); + std::thread::Builder::new() + .name("log-carrier".into()) + .spawn(move || theirs.run(channel, &acceptor)) + .expect("logkeeper: a carrier's thread could not be started"); + } + let theirs = Arc::clone(&carriers); std::thread::Builder::new() .name("log-carrier-stall".into()) .spawn(move || theirs.watch()) - .expect("logkeeper: the carrier's stall thread could not be started"); - carrier - } - - /// Whether the UART is quiet, so program lines are not written to it. - pub fn quiet(&self) -> bool { - self.lock().carriage.quiet() + .expect("logkeeper: the carriers' stall thread could not be started"); + carriers } /// The machine stops, and `end` is how long the boot is at the flush: - /// wait, bounded, for the carrier's channel to take it all, then give the - /// UART what the log says from here. + /// wait, bounded, for every channel that carries the log to take it all, + /// then give the UART what the log says from here. pub fn stop(&self, end: u64) { let began = Instant::now(); let mut state = self.lock(); loop { - let caught_up = state.at >= end && state.carriage.owed() == 0; - if state.carriage.carrier.is_none() || caught_up { - break; - } + let quiet = state.quiet(); + let behind = Channel::ALL.into_iter().find(|&channel| { + let feed = &state.feeds[channel as usize]; + let carries = feed.carriage.carrier.is_some() && !(channel == Channel::Wire && quiet); + carries && (feed.at < end || feed.carriage.owed() > 0) + }); + let Some(channel) = behind else { break }; let Some(left) = STOP_WAIT.checked_sub(began.elapsed()) else { + let carriage = &state.feeds[channel as usize].carriage; say!( - "logkeeper: the console carrier's channel had taken {} of {} bytes {} ms into the stop; \ - the UART takes the stop's lines, and /log has the rest", - state.carriage.delivered, - state.carriage.sent, + "logkeeper: {} had taken {} of {} bytes {} ms into the stop; the UART takes the \ + stop's lines, and /log has the rest", + channel.name(), + carriage.delivered, + carriage.sent, STOP_WAIT.as_millis() ); break; }; - state = self.moved.wait_timeout(state, left).expect("logkeeper: the carrier's state is poisoned").0; + state = self.moved.wait_timeout(state, left).expect("logkeeper: the carriers' state is poisoned").0; } - let tell = state.carriage.stop(); - self.tell(tell); + let tell = state.feed(Channel::Port).carriage.stop(); + self.tell(Channel::Port, tell); + drop(state); + self.moved.notify_all(); } - /// The stop was refused: the carrier carries the log again, past what - /// the UART carried meanwhile ([`Hub::append_carried`]). + /// The stop was refused: the port carries the log again. pub fn resume(&self) { - let mut state = self.lock(); - let tell = state.carriage.resume(); - self.tell(tell); + let tell = self.lock().feed(Channel::Port).carriage.resume(); + self.tell(Channel::Port, tell); + self.moved.notify_all(); } - /// Watch the carrier's channel for [`STALL`], telling the kernel the UART - /// carries the log while the channel takes nothing it is owed. Waits on - /// the carrier's state changing where nothing is owed. + /// Watch the port's channel for [`STALL`], letting the UART carry the log + /// while the channel takes nothing it is owed. Waits on the carriers' + /// state changing where nothing is owed. fn watch(&self) -> ! { let mut state = self.lock(); loop { - let Some(at) = state.carriage.stall_at() else { - state = self.moved.wait(state).expect("logkeeper: the carrier's state is poisoned"); + let Some(at) = state.feed(Channel::Port).carriage.stall_at() else { + state = self.moved.wait(state).expect("logkeeper: the carriers' state is poisoned"); continue; }; let now = Instant::now(); if at > now { - state = self.moved.wait_timeout(state, at - now).expect("logkeeper: the carrier's state is poisoned").0; + state = self.moved.wait_timeout(state, at - now).expect("logkeeper: the carriers' state is poisoned").0; continue; } - let owed = state.carriage.owed(); - let tell = state.carriage.tick(now); + let carriage = &mut state.feed(Channel::Port).carriage; + let owed = carriage.owed(); + let tell = carriage.tick(now); // A channel that never took the first byte left the UART loud. if tell == Some(Tell::Loud) { say!( @@ -282,31 +343,30 @@ impl Carrier { STALL.as_secs() ); } - self.tell(tell); + self.tell(Channel::Port, tell); + self.moved.notify_all(); } } fn lock(&self) -> MutexGuard<'_, State> { - self.state.lock().expect("logkeeper: the carrier's state is poisoned") - } - - /// Tell the kernel, under the state's lock, so two threads' changes reach - /// it in the order they were decided. - fn tell(&self, tell: Option) { - let Some(tell) = tell else { return }; - match self.console.carry_elsewhere(tell == Tell::Quiet) { - Ok(()) => match tell { - Tell::Quiet => say!("logkeeper: the console carrier's channel takes the log, so the UART is quiet"), - Tell::Loud => say!("logkeeper: the UART carries the log again"), - }, - // A fatal path or the stop gave the UART the log for good. - Err(SyscallError::NotSupported) => {} - Err(e) => panic!("logkeeper: the kernel refused to be told where the log is carried: {e:?}"), + self.state.lock().expect("logkeeper: the carriers' state is poisoned") + } + + /// Say a change of the UART's quiet in the log, which only the port's + /// carrying makes; the UART's own feed says it on the UART. + fn tell(&self, channel: Channel, tell: Option) { + if channel != Channel::Port { + return; + } + match tell { + Some(Tell::Quiet) => say!("logkeeper: the console carrier's channel takes the log, so the UART is quiet"), + Some(Tell::Loud) => say!("logkeeper: the UART carries the log again"), + None => {} } } - /// The acceptor and the admitted carrier's connection. - fn run(self: &Arc, acceptor: &Acceptor, hub: &Arc) -> ! { + /// The acceptor and the admitted carrier's connection, on `channel`. + fn run(self: &Arc, channel: Channel, acceptor: &Acceptor) -> ! { const ACCEPT: u64 = 0; const CONNECTION: u64 = 1; let poller = Poller::new(2); @@ -327,7 +387,7 @@ impl Carrier { && let Ok(conn) = acceptor.accept() { if admitted.is_some() || asking.is_some() { - say!("logkeeper: refusing a second console carrier: one is served"); + say!("logkeeper: refusing a second carrier for {}: one is served", channel.name()); } else { asking = Some((conn, FrameRx::new())); } @@ -338,112 +398,176 @@ impl Carrier { if let Some((conn, mut rx)) = asking.take() { match rx.pump(&conn) { RxStep::Idle => asking = Some((conn, rx)), - RxStep::Frame { msg_type: CARRY, .. } => admitted = self.admit(conn, hub), + RxStep::Frame { msg_type: CARRY, .. } => admitted = self.admit(channel, conn), RxStep::Eof | RxStep::Malformed | RxStep::Frame { .. } => {} } continue; } let Some((who, conn, mut rx)) = admitted.take() else { continue }; - if self.read(who, &conn, &mut rx) { + if self.read(channel, who, &conn, &mut rx) { admitted = Some((who, conn, rx)); } } } - /// Hand the carrier on `conn` its pipe, and feed it from the boot's first - /// line on a thread of its own. - fn admit(self: &Arc, conn: Connection, hub: &Arc) -> Option<(u64, Connection, FrameRx<8>)> { - let who = match self.lock().carriage.admit() { + /// Hand the carrier on `conn` its pipe, and feed it on a thread of its own. + fn admit(self: &Arc, channel: Channel, conn: Connection) -> Option<(u64, Connection, FrameRx<8>)> { + let who = match self.lock().feed(channel).carriage.admit() { Ok(who) => who, Err(why) => { - say!("logkeeper: refusing a console carrier: {why:?}"); + say!("logkeeper: refusing a carrier for {}: {why:?}", channel.name()); return None; } }; - let Some(pipe) = hub.hand_over(&conn) else { - self.went(who); + let Some(pipe) = self.hub.hand_over(&conn) else { + self.went(channel, who); return None; }; - self.lock().at = 0; - let (carrier, hub) = (Arc::clone(self), Arc::clone(hub)); + { + let mut state = self.lock(); + let feed = state.feed(channel); + feed.at = 0; + feed.written.clear(); + } + let carriers = Arc::clone(self); let fed = std::thread::Builder::new().name("log-carried".into()).spawn(move || { - carrier.feed(who, &pipe, &hub); - carrier.went(who); + carriers.feed(channel, who, &pipe); + carriers.went(channel, who); }); if let Err(e) = fed { - say!("logkeeper: no thread for the console carrier: {e}"); - self.went(who); + say!("logkeeper: no thread for a carrier for {}: {e}", channel.name()); + self.went(channel, who); return None; } Some((who, conn, FrameRx::new())) } /// Every frame carrier `who` sent: whether it is still served. - fn read(&self, who: u64, conn: &Connection, rx: &mut FrameRx<8>) -> bool { + fn read(&self, channel: Channel, who: u64, conn: &Connection, rx: &mut FrameRx<8>) -> bool { loop { match rx.pump(conn) { RxStep::Idle => return true, RxStep::Frame { msg_type: DELIVERED, payload_len: 8, .. } => { let said = u64::from_le_bytes(rx.payload(8).try_into().expect("eight bytes")); let mut state = self.lock(); - match state.carriage.delivered(who, said, Instant::now()) { + match state.feed(channel).carriage.delivered(who, said, Instant::now()) { Ok(tell) => { - self.tell(tell); + self.tell(channel, tell); + self.spoken(&mut state, channel, said); drop(state); self.moved.notify_all(); } Err(why) => { drop(state); - say!("logkeeper: letting the console carrier go: {why:?}"); - self.went(who); + say!("logkeeper: letting the carrier for {} go: {why:?}", channel.name()); + self.went(channel, who); return false; } } } RxStep::Eof | RxStep::Malformed | RxStep::Frame { .. } => { - self.went(who); + self.went(channel, who); return false; } } } } - fn went(&self, who: u64) { - let mut state = self.lock(); - let tell = state.carriage.gone(who); - self.tell(tell); - drop(state); + /// `channel` has taken `delivered` bytes of its pipe: tell the kernel the + /// newest stamp of the replay that is all of it, where that is past what + /// it was told. + fn spoken(&self, state: &mut State, channel: Channel, delivered: u64) { + let feed = state.feed(channel); + let mut through = None; + while feed.written.front().is_some_and(|&(end, _)| end <= delivered) { + through = feed.written.pop_front().map(|(_, at)| at); + } + let Some(stamp) = through.and_then(|at| self.hub.stamp_through(at)) else { return }; + if stamp <= state.spoken { + return; + } + match toyos::log::spoken(&self.cap, stamp) { + Ok(now) => state.spoken = now, + Err(e) => panic!("logkeeper: the kernel refused to be told what the console spoke: {e:?}"), + } + } + + fn went(&self, channel: Channel, who: u64) { + let tell = self.lock().feed(channel).carriage.gone(who); + self.tell(channel, tell); self.moved.notify_all(); } - /// Write the boot into carrier `who`'s pipe until the pipe's reader goes. - fn feed(&self, who: u64, pipe: &Pipe, hub: &Hub) { + /// Write the log into carrier `who`'s pipe until the pipe's reader goes: + /// the port's from the boot's first line, the UART's from where the + /// kernel left it, and nothing of what the port carries. + fn feed(&self, channel: Channel, who: u64, pipe: &Pipe) { let poller = Poller::new(1); - let mut at = 0u64; + let wire = channel == Channel::Wire; + let mut at = if wire { self.hub.wire_from() } else { 0 }; + // Whether the UART last said the port carries the log. + let mut said_quiet = false; loop { - let chunk = hub.next_for_the_carrier(&mut at); - // Counted before the pipe has it, so no count the carrier gives - // of these bytes is ever past what it was sent. - if !self.lock().carriage.fed(who, chunk.len() as u64, Instant::now()) { + let fed = self.hub.next_for_a_carrier(&mut at, wire); + if wire { + let quiet = self.lock().quiet(); + if quiet != said_quiet { + said_quiet = quiet; + let line = if quiet { CARRIED_ELSEWHERE } else { BACK_ON_THE_UART }; + if !self.write(channel, who, pipe, &poller, &self.hub.own_line(Severity::Info, line), None) { + return; + } + } + if quiet { + continue; + } + } + let (bytes, reaches) = match fed { + Fed::Replay(bytes) => (bytes, Some(at)), + Fed::Said(bytes) => (bytes, None), + }; + if !self.write(channel, who, pipe, &poller, &bytes, reaches) { return; } - self.moved.notify_all(); - let mut written = 0; - while written < chunk.len() { - match pipe.write_nonblock(&chunk[written..]) { - Ok(n) => written += n, - Err(SyscallError::WouldBlock) => { - // No bound: a slow channel is not a failed one, and - // the pipe's reader going ends the wait. - poller.watch(pipe, WRITABLE, 0); - poller.wait(1, u64::MAX, |_| {}); - } - Err(_) => return, + } + } + + /// `bytes` into carrier `who`'s pipe, which reach replay offset `reaches` + /// where they are the replay's: whether `who` is still its channel's + /// carrier and the pipe's reader is there. + fn write(&self, channel: Channel, who: u64, pipe: &Pipe, poller: &Poller, bytes: &[u8], reaches: Option) -> bool { + // Counted before the pipe has them, so no count the carrier gives of + // these bytes is ever past what it was sent. + { + let mut state = self.lock(); + let feed = state.feed(channel); + if !feed.carriage.fed(who, bytes.len() as u64, Instant::now()) { + return false; + } + if let Some(at) = reaches { + let sent = feed.carriage.sent; + feed.written.push_back((sent, at)); + } + } + self.moved.notify_all(); + let mut written = 0; + while written < bytes.len() { + match pipe.write_nonblock(&bytes[written..]) { + Ok(n) => written += n, + Err(SyscallError::WouldBlock) => { + // No bound: a slow channel is not a failed one, and + // the pipe's reader going ends the wait. + poller.watch(pipe, WRITABLE, 0); + poller.wait(1, u64::MAX, |_| {}); } + Err(_) => return false, } - self.lock().at = at; - self.moved.notify_all(); } + if let Some(at) = reaches { + self.lock().feed(channel).at = at; + } + self.moved.notify_all(); + true } } @@ -465,11 +589,11 @@ mod tests { let t = Instant::now(); let mut carriage = Carriage::default(); let who = fed(&mut carriage, 100, t); - assert!(!carriage.quiet()); + assert!(!carriage.carrying()); assert_eq!(carriage.delivered(who, 0, t), Ok(None)); assert_eq!(carriage.delivered(who, 40, t), Ok(Some(Tell::Quiet))); assert_eq!(carriage.delivered(who, 100, t), Ok(None)); - assert!(carriage.quiet()); + assert!(carriage.carrying()); } /// The carrier's end, its driver's death among them, gives the log back @@ -481,7 +605,7 @@ mod tests { let who = fed(&mut carriage, 1, t); carriage.delivered(who, 1, t).expect("a count it was sent"); assert_eq!(carriage.gone(who), Some(Tell::Loud)); - assert!(!carriage.quiet()); + assert!(!carriage.carrying()); assert_eq!(carriage.gone(who), None, "one end is answered once"); // A later carrier carries it again from its own first byte. let next = fed(&mut carriage, 5, t); @@ -535,7 +659,7 @@ mod tests { assert_eq!(carriage.stop(), Some(Tell::Loud)); assert_eq!(carriage.gone(who), None); assert_eq!(carriage.resume(), None, "no carrier is left to carry it"); - assert!(!carriage.quiet()); + assert!(!carriage.carrying()); } /// **A channel that takes nothing it is owed for [`STALL`] has the UART @@ -548,7 +672,7 @@ mod tests { let mut carriage = Carriage::default(); let who = fed(&mut carriage, 10, t); carriage.delivered(who, 4, t).expect("a count it was sent"); - assert!(carriage.quiet()); + assert!(carriage.carrying()); assert_eq!(carriage.stall_at(), Some(t + STALL)); assert_eq!(carriage.tick(t + STALL / 2), None); // Progress moves the bound. @@ -556,7 +680,7 @@ mod tests { carriage.delivered(who, 6, later).expect("a count it was sent"); assert_eq!(carriage.tick(t + STALL), None, "the bound runs from the last progress"); assert_eq!(carriage.tick(later + STALL), Some(Tell::Loud)); - assert!(!carriage.quiet()); + assert!(!carriage.carrying()); assert_eq!(carriage.stall_at(), None, "a stall is said once"); assert_eq!(carriage.tick(later + STALL * 3), None); // More fed while stalled keeps the UART carrying it. diff --git a/system/logkeeper/src/main.rs b/system/logkeeper/src/main.rs index e44843a9d3d..b8f866095bd 100644 --- a/system/logkeeper/src/main.rs +++ b/system/logkeeper/src/main.rs @@ -1,10 +1,11 @@ //! `/system/bin/logkeeper` — the machine's log, written to a file by a process that can be //! killed without taking the kernel with it. //! -//! The kernel keeps the record ring and the console and writes no file; every -//! policy about where records go — what the files are called, how many there -//! are, when they are made durable, what happens when the stick stops -//! answering, who may read the log as it is written — is here. +//! The kernel keeps the record ring, writes the console only before userland +//! runs and from the machine's stop, and writes no file; every policy about +//! where records go — what the files are called, how many there are, when they +//! are made durable, what happens when the stick stops answering, who may read +//! the log as it is written, which channel carries the console — is here. //! //! # What goes in the log //! @@ -41,27 +42,21 @@ //! //! The file is. [`serve`]'s readers are the second: the same lines, in the same //! order, to whoever asks, from the boot's first line however late they ask. -//! The console is the third: each program's line with its head and no wall -//! clock, written on the one console handle the supervisor gives this program. The -//! kernel puts its own records on the console, and this program never does. -//! Neither of the two can slow the file: a reader is a thread with an offset -//! into what the file already has, and the console takes only the whole lines -//! it has room for — the rest waits here, bounded ([`CONSOLE_BYTES`]). -//! -//! **One reader can carry the console instead** ([`carrier`]): a VM's console -//! channel, asked for on [`toyos_logstream::CARRIER`]. Once its channel takes -//! the log, the kernel is told the UART is quiet, and no program line is -//! written to it; its end, or the stop, gives the console back. +//! The console's carriers are the third ([`carrier`]): the console UART's +//! driver, and a VM's console channel, which the UART goes quiet for while it +//! carries the log. Neither of the two can slow the file: each is a thread +//! with an offset into what the file already has. //! //! # Its whole authority //! //! One `SysCap` duplicate carrying `Rights::LOG | Rights::WAIT`, which its -//! manifest row asks for by the name `logread`; the origins acceptor and the -//! writable console the supervisor endows it; and what its row adds — the `log` acceptor, where this -//! machine's readers ask for the log and `inspect` asks where it is going -//! ([`inspect`]'s module, which grants nothing), and the `log-carrier` -//! acceptor, where the console's carrier asks. It claims no device and can -//! name no process. Writing files is ambient — a known residual of the +//! manifest row asks for by the name `logread`, and with which it tells the +//! kernel what the console's channels have delivered; the origins acceptor +//! the supervisor endows it; and what its row adds — the `log` acceptor, where +//! this machine's readers ask for the log and `inspect` asks where it is going +//! ([`inspect`]'s module, which grants nothing), and the `log-carrier` and +//! `log-wire` acceptors, where the console's carriers ask. It claims no device +//! and can name no process. Writing files is ambient — a known residual of the //! capability endowment, and not this program's to close. //! //! # Durability, which is a contract and not a hope @@ -70,8 +65,8 @@ //! round**, and again when the supervisor asks before the machine stops //! ([`toyos_logstream::FLUSH`]). The kernel waits on none of it: a panicking kernel's report is in its black box, and so are the stop's //! own last records. -//! **A line after that flush reaches the console and is held back from the -//! file**, to a bound ([`STOPPING_BYTES`]): the stop syncs no file still open, +//! **A program's line after that flush reaches the UART and is held back from +//! the file**, to a bound ([`STOPPING_BYTES`]): the stop syncs no file still open, //! so the file ends where the flush made it durable, and a reader of the //! served log is handed what the file holds and no more. A stop the kernel //! refused is the supervisor's [`toyos_logstream::RESUME`], and what was held is written @@ -90,20 +85,18 @@ use std::time::{Duration, Instant}; use toyos::endow::{self, Endowments, SYSCAP_LABEL}; use toyos::ipc::{self, Connection, RxStep}; use toyos::log::{LogTail, Record}; -use toyos::poller::{Poller, READABLE, WRITABLE}; +use toyos::poller::{Poller, READABLE}; use toyos::port::Acceptor; use toyos::say; use toyos::syscap::SysCap; use toyos::wake::Bell; -use toyos::Console; -use toyos_abi::syscall::SyscallError; use toyos_logstream::{ - ProgramLine, Registration, Tag, CARRIER, CONSOLE, FLUSH, FLUSHED, LOG_OPENED, MAX_TAG, ORIGINS, REGISTER, - RESUME, SERVICE, + ProgramLine, Registration, Tag, CARRIER, FLUSH, FLUSHED, LOG_OPENED, MAX_TAG, ORIGINS, REGISTER, RESUME, + SERVICE, WIRE, }; use toyos_wallclock::Civil; -use carrier::Carrier; +use carrier::Carriers; use origin::{Origin, Said}; use policy::{Step, LOG_WRITE_BUDGET}; use store::{Volume, DIR, MAX_LOG_BYTES, MAX_LOG_FILES}; @@ -112,15 +105,14 @@ use store::{Volume, DIR, MAX_LOG_BYTES, MAX_LOG_FILES}; /// boot's burst is a handful of syscalls rather than one per line. const BATCH: usize = 64; -/// The poll's tokens: the kernel's readiness, the supervisor's connection, the console's -/// room, and each program's end from [`ORIGIN_BASE`] up. +/// The poll's tokens: the kernel's readiness, the supervisor's connection, and +/// each program's end from [`ORIGIN_BASE`] up. const KERNEL_TOKEN: u64 = 0; const ORIGINS_TOKEN: u64 = 1; -const CONSOLE_TOKEN: u64 = 2; -const ORIGIN_BASE: u64 = 3; +const ORIGIN_BASE: u64 = 2; /// Programs whose rings this program reads at once: what one poller can watch -/// beside the other three sources. One past it is refused by name. +/// beside the other two sources. One past it is refused by name. const MAX_ORIGINS: usize = Poller::MAX_HANDLES as usize - ORIGIN_BASE as usize; /// How soon the rings are read again after a round that read any, and the @@ -134,10 +126,6 @@ const POLL_SLOW: Duration = Duration::from_millis(250); /// the volume keeps, so the stream is never the shorter of the two. const REPLAY_BYTES: usize = MAX_LOG_FILES * MAX_LOG_BYTES as usize; -/// Program lines the console may fall behind the log by. Past it the oldest -/// whole lines are counted and said, and `/log` has them. -const CONSOLE_BYTES: usize = 1 << 20; - fn main() { let Some(cap) = Endowments::get().take::(SYSCAP_LABEL) else { panic!("logkeeper: this program holds no system capability, so it holds no `logread`"); @@ -145,10 +133,6 @@ fn main() { let Some(acceptor) = Endowments::get().take::(ORIGINS) else { panic!("logkeeper: the supervisor endowed no `{ORIGINS}`, so no program's log can reach this one"); }; - let Some(console) = Endowments::get().take::(CONSOLE) else { - panic!("logkeeper: the supervisor endowed no `{CONSOLE}`, so no program's line can reach the console"); - }; - let console = Arc::new(console); // The supervisor connected before it started anything, so this is already queued. let from_supervisor: Connection = acceptor.accept().expect("logkeeper: the supervisor's origins connection"); @@ -174,7 +158,13 @@ fn main() { if let Some(acceptor) = endow::acceptor(SERVICE) { inspect::serve(acceptor, Arc::clone(&published), Arc::clone(&hub)); } - let carrier = endow::acceptor(CARRIER).map(|acceptor| Carrier::serve(acceptor, Arc::clone(&console), Arc::clone(&hub))); + let cap = Arc::new(cap); + // What the kernel put on the UART itself before userland ran, which the + // UART's carrier starts after. + let spoken = toyos::log::spoken(&cap, 0).unwrap_or_else(|e| panic!("logkeeper: SYS_LOG_SPOKEN refused ({e:?})")); + let (port, wire) = (endow::acceptor(CARRIER), endow::acceptor(WIRE)); + let carriers = + (port.is_some() || wire.is_some()).then(|| Carriers::serve(port, wire, Arc::clone(&hub), Arc::clone(&cap))); let words = toyos::log::cursor_words(&cap).unwrap_or_else(|e| panic!("logkeeper: SYS_LOG_READ sized no cursor ({e:?})")); @@ -183,10 +173,8 @@ fn main() { cap, from_supervisor, supervisor_rx: ipc::FrameRx::new(), - console, - carrier, - console_held: Vec::new(), - console_unshown: 0, + carriers, + spoken: Some(spoken), origins: Vec::new(), tail, records: vec![Record::EMPTY; BATCH], @@ -203,14 +191,13 @@ fn main() { /// Everything the loop owns. struct Log { - cap: SysCap, + cap: Arc, from_supervisor: Connection, supervisor_rx: ipc::FrameRx<{ 4 + MAX_TAG }>, - console: Arc, - carrier: Option>, - /// Program lines the console has not taken yet, whole lines only. - console_held: Vec, - console_unshown: u64, + carriers: Option>, + /// The newest stamp the kernel put on the UART itself, until the first + /// round has said where the UART's carrier starts after it. + spoken: Option, origins: Vec, tail: LogTail>, records: Vec, @@ -269,11 +256,8 @@ impl Log { // an edge needs: what arrives after a read and before the park has // a registration waiting for it. A watch is one-shot, so one that // answered here is spent and its source is read below. - poller.watch(&self.cap, READABLE, KERNEL_TOKEN); + poller.watch(&*self.cap, READABLE, KERNEL_TOKEN); poller.watch(&self.from_supervisor, READABLE, ORIGINS_TOKEN); - if !self.console_held.is_empty() { - poller.watch(&*self.console, WRITABLE, CONSOLE_TOKEN); - } for (i, origin) in self.origins.iter().enumerate() { poller.watch(&origin.alive, READABLE, ORIGIN_BASE + i as u64); } @@ -297,14 +281,13 @@ impl Log { self.flushed(); read_any = true; } - self.feed_console(); cadence = if read_any { POLL_QUICK } else { (cadence * 2).min(POLL_SLOW) }; if spent || read_any { continue; } // Nothing new: park until the kernel posts, the supervisor speaks, a program - // ends, the console has room, or the cadence comes round. + // ends, or the cadence comes round. poller.wait(1, cadence.as_nanos() as u64, |token| { if token >= ORIGIN_BASE { ended.push((token - ORIGIN_BASE) as usize); @@ -498,43 +481,51 @@ impl Log { out } - /// The lines, in stamp order, into the file, the readers and the console. + /// The lines, in stamp order, into the file, the readers and the console's + /// carriers; while the machine stops, held back from the file, and each + /// program's line to the UART's carrier. fn write(&mut self, mut lines: Vec, flushing: bool) { if lines.is_empty() { return; } lines.sort_by_key(|line| line.at_ns); - let carried = self.carried(); let mut file = String::new(); + // The kernel's own lines on the UART: what ends them in `file`. + let mut spoken_through = 0; + let mut programs = String::new(); for line in &lines { match &line.kind { Kind::Kernel(record) => { file.push_str(&format!("{}\n", record.dated(&stamp(self.boot_secs, record.at_ns())))); + if self.spoken.is_some_and(|spoken| line.at_ns <= spoken) { + spoken_through = file.len(); + } } Kind::Program { tag, owner, said } => { - let severity = said.severity; let tag = Tag::new(tag).expect("an origin's name is a tag"); let pid = (said.pid != *owner).then_some(said.pid); let at = stamp(self.boot_secs, Some(said.at_ns)); - let mut line = ProgramLine { + let line = ProgramLine { stamp: &at, at_ns: said.at_ns, - severity, + severity: said.severity, tid: said.tid, pid, tag, text: &said.text, }; - file.push_str(&format!("{line}\n")); - if !carried { - line.stamp = ""; - let console = format!("{line}\n"); - self.console_line(console.as_bytes()); + let line = format!("{line}\n"); + file.push_str(&line); + if self.stopping.is_some() { + programs.push_str(&line); } } } } if let Some(held) = &mut self.stopping { + // The kernel's records from the flush on are the stop's to put on + // the UART; a program's reach it only through its carrier. + self.hub.append_stopping(programs.as_bytes()); if held.text.len() + file.len() > STOPPING_BYTES { held.unwritten += lines.len() as u64; } else { @@ -545,63 +536,12 @@ impl Log { // The file first: a reader is served only what /log already holds, // whenever the machine stops between the two. self.to_volume(file.as_bytes(), !flushing); - self.hub.append(file.as_bytes()); - } - - /// One rendered program line for the console, held whole until it takes it. - /// - /// **Past the bound the oldest lines go, never the newest**: the newest - /// line is the one a watcher waits on — a test runner's end marker after - /// a flood — and every line gone is in `/log`. They go down to half the - /// bound at once, so a flood moves the held bytes once per half and not - /// once per line. The first held line stays: the console may have taken - /// its head. - fn console_line(&mut self, line: &[u8]) { - let held = &mut self.console_held; - if held.len() + line.len() > CONSOLE_BYTES { - let first = held.iter().position(|&b| b == b'\n').map_or(held.len(), |at| at + 1); - let mut cut = first; - while cut < held.len() && held.len() - (cut - first) + line.len() > CONSOLE_BYTES / 2 { - cut += held[cut..].iter().position(|&b| b == b'\n').expect("held lines are whole") + 1; - self.console_unshown += 1; - } - held.drain(first..cut); - if held.len() + line.len() > CONSOLE_BYTES { - self.console_unshown += 1; - return; - } - } - held.extend_from_slice(line); - } - - /// Whether the console's carrier carries the program lines, so the UART - /// takes none. - fn carried(&self) -> bool { - self.carrier.as_ref().is_some_and(|carrier| carrier.quiet()) - } - - /// As many held lines as the console takes now; the rest wait for its - /// room. None while the carrier carries them, which has them all. - fn feed_console(&mut self) { - if self.carried() { - self.console_held.clear(); - self.console_unshown = 0; - } - while !self.console_held.is_empty() { - match self.console.write_nonblock(&self.console_held) { - Ok(0) | Err(SyscallError::WouldBlock) => break, - Ok(n) => { - self.console_held.drain(..n); - } - Err(e) => panic!("logkeeper: its console refused a write: {e:?}"), - } - } - if self.console_unshown > 0 && self.console_held.len() < CONSOLE_BYTES / 2 { - toyos::warn!( - "logkeeper: {} line(s) went unshown on the console: it is slower than the log, and \ - {DIR} has them", - std::mem::replace(&mut self.console_unshown, 0) - ); + let from = self.hub.end(); + self.hub.append(file.as_bytes(), lines.last().map(|line| line.at_ns)); + // Every record the kernel put on the UART itself is committed before + // userland runs, so the first round has them all. + if self.spoken.take().is_some() { + self.hub.set_wire_from(from + spoken_through as u64); } } @@ -681,13 +621,12 @@ impl Log { fn flushed(&mut self) { let refused = self.sync().err(); self.answered(refused); - // The console's carrier takes the log up to here, and the UART what + // The console's carriers take the log up to here, and the UART what // the machine says from here as it stops. - if let Some(carrier) = &self.carrier { - carrier.stop(self.hub.end()); + if let Some(carriers) = &self.carriers { + carriers.stop(self.hub.end()); } self.stopping = Some(Stopping { text: String::new(), unwritten: 0 }); - self.feed_console(); if let Err(e) = self.from_supervisor.signal(FLUSHED) { panic!("logkeeper: the supervisor could not be told the log is whole: {e:?}"); } @@ -708,9 +647,9 @@ impl Log { ); } self.to_volume(held.text.as_bytes(), true); - self.hub.append_carried(held.text.as_bytes()); - if let Some(carrier) = &self.carrier { - carrier.resume(); + self.hub.resume(held.text.as_bytes()); + if let Some(carriers) = &self.carriers { + carriers.resume(); } } } diff --git a/system/logkeeper/src/serve.rs b/system/logkeeper/src/serve.rs index b51532fc6a7..e318267e23a 100644 --- a/system/logkeeper/src/serve.rs +++ b/system/logkeeper/src/serve.rs @@ -12,7 +12,13 @@ //! //! **A reader that is caught up waits on the replay growing**, and nothing //! else wakes it: there is no poll and no timer. +//! +//! The console's carriers read the same replay (`crate::carrier`): the hub +//! keeps the newest stamp each round reaches ([`Hub::stamp_through`]), where +//! the console UART's carrier starts ([`Hub::wire_from`]), and the lines said +//! while the machine stops, which the file holds back and the UART carries. +use std::collections::VecDeque; use std::io::Write; use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::{Arc, Condvar, Mutex}; @@ -38,12 +44,21 @@ const MAX_READERS: usize = 2; /// slot is let go: a wait on its sink taking bytes, bounded by this. const STALLED: Duration = Duration::from_secs(10); -/// The replay, and what of it the carrier is not owed. +/// Rounds whose stamps the hub keeps: a carrier whose channel is further +/// behind than this speaks for none of it until it catches up. +const ROUNDS: usize = 4096; + +/// The replay, and what the console's carriers read beside it. struct Held { replay: Replay, - /// Bytes the console UART carried while the machine stopped, which the - /// carrier skips ([`Hub::append_carried`]). - carried: Option<(u64, u64)>, + /// Each round's end in the replay and the newest stamp of its records, + /// oldest first, the last [`ROUNDS`] of them. + rounds: VecDeque<(u64, u64)>, + /// Where the console UART's carrier starts, once the first round says. + wire_from: Option, + /// Lines said while the machine stops, held back from the file and owed + /// the UART's carrier. + stopping: Vec, } /// The replay, and the wake a caught-up reader waits on. @@ -63,7 +78,12 @@ impl Hub { /// A reader is handed over by the `log` port's thread ([`Hub::read`]). pub fn start(cap: usize, boot_secs: Option) -> Self { let shared = Arc::new(Shared { - replay: Mutex::new(Held { replay: Replay::new(cap), carried: None }), + replay: Mutex::new(Held { + replay: Replay::new(cap), + rounds: VecDeque::new(), + wire_from: None, + stopping: Vec::new(), + }), grew: Condvar::new(), readers: AtomicUsize::new(0), boot_secs, @@ -78,27 +98,70 @@ impl Hub { admit(&self.shared, format!("reader {}", conn.as_handle().raw().0), PipeSink::new(pipe)); } - /// The lines the file has just taken, for every reader. - pub fn append(&self, lines: &[u8]) { - self.shared.replay.lock().expect("logkeeper: the replay is poisoned").replay.append(lines); + /// The lines the file has just taken, for every reader, and the newest + /// stamp among them where they are a round's. + pub fn append(&self, lines: &[u8], newest: Option) { + let mut held = self.lock(); + held.replay.append(lines); + if let Some(at_ns) = newest { + let end = held.replay.end(); + if held.rounds.len() == ROUNDS { + held.rounds.pop_front(); + } + held.rounds.push_back((end, at_ns)); + } + drop(held); self.shared.grew.notify_all(); } - /// [`Self::append`], of lines the console UART has carried already, which - /// the carrier skips: what the machine said while it stopped, once the - /// stop was refused. - pub fn append_carried(&self, lines: &[u8]) { - let mut held = self.shared.replay.lock().expect("logkeeper: the replay is poisoned"); - let from = held.replay.end(); - held.replay.append(lines); - held.carried = Some((from, held.replay.end())); + /// The newest stamp of the last round wholly before `at`: what a channel + /// that has taken the replay up to `at` has spoken. + pub fn stamp_through(&self, at: u64) -> Option { + let held = self.lock(); + held.rounds.iter().rev().find(|&&(end, _)| end <= at).map(|&(_, at_ns)| at_ns) + } + + /// The console UART's carrier starts at `at`, the end of the last line + /// the kernel put on the UART itself. Once. + pub fn set_wire_from(&self, at: u64) { + let mut held = self.lock(); + assert!(held.wire_from.is_none(), "logkeeper: where the UART's carrier starts is said twice"); + held.wire_from = Some(at); drop(held); self.shared.grew.notify_all(); } + /// Where the console UART's carrier starts, once the first round said. + pub fn wire_from(&self) -> u64 { + let mut held = self.lock(); + loop { + if let Some(at) = held.wire_from { + return at; + } + held = self.shared.grew.wait(held).expect("logkeeper: the replay is poisoned"); + } + } + + /// Lines said while the machine stops, for the UART's carrier alone. + pub fn append_stopping(&self, lines: &[u8]) { + self.lock().stopping.extend_from_slice(lines); + self.shared.grew.notify_all(); + } + + /// The stop was refused: what was said while it stood, which the file + /// takes now, for every reader, and none of it owed the UART beside. + pub fn resume(&self, lines: &[u8]) { + self.lock().stopping.clear(); + self.append(lines, None); + } + + fn lock(&self) -> std::sync::MutexGuard<'_, Held> { + self.shared.replay.lock().expect("logkeeper: the replay is poisoned") + } + /// How long the boot is so far. pub fn end(&self) -> u64 { - self.shared.replay.lock().expect("logkeeper: the replay is poisoned").replay.end() + self.lock().replay.end() } /// The pipe a reader on `conn` is handed, and the boot's length it starts @@ -107,15 +170,45 @@ impl Hub { hand_over(conn, self.end()) } - /// The bytes after `at` for the carrier, at most [`CHUNK`] of them, once - /// there are any, skipping what the UART carried; `at` moves past them. A - /// carrier the replay no longer holds the bytes of is handed the line that - /// says so instead. - pub fn next_for_the_carrier(&self, at: &mut u64) -> Vec { - next(&self.shared, at, true) + /// What a carrier at `at` is fed next, once there is any: at most + /// [`CHUNK`] bytes of the replay, and `at` past them, or the line that + /// says what the replay no longer holds; or, for the UART's carrier + /// caught up with the replay, the lines said while the machine stops. + pub fn next_for_a_carrier(&self, at: &mut u64, wire: bool) -> Fed { + let mut held = self.lock(); + loop { + match held.replay.next(*at, CHUNK) { + Next::Bytes(bytes) => { + *at += bytes.len() as u64; + return Fed::Replay(bytes.to_vec()); + } + Next::Evicted { lost, at: resume } => { + *at = resume; + return Fed::Said(evicted(self.shared.boot_secs, lost)); + } + Next::CaughtUp if wire && !held.stopping.is_empty() => { + return Fed::Said(std::mem::take(&mut held.stopping)); + } + Next::CaughtUp => held = self.shared.grew.wait(held).expect("logkeeper: the replay is poisoned"), + } + } + } + + /// One of this program's own lines, as the log renders it, which no ring + /// carries: what a carrier is told on its own channel. + pub fn own_line(&self, severity: Severity, text: &str) -> Vec { + own_line(self.shared.boot_secs, severity, text) } } +/// What a carrier is fed. +pub enum Fed { + /// Bytes of the replay, which a channel taking them speaks for. + Replay(Vec), + /// Lines that are not the replay's. + Said(Vec), +} + /// The read end goes to the reader with the boot's length so far, the write /// end stays here; `None` where the reader is already gone. fn hand_over(conn: &Connection, end: u64) -> Option { @@ -175,7 +268,7 @@ fn feed(shared: &Shared, mut sink: impl Write) -> (u64, Left) { let mut at = 0u64; let mut sent = 0u64; loop { - let chunk = next(shared, &mut at, false); + let chunk = next(shared, &mut at); match sink.write_all(&chunk) { Ok(()) => sent += chunk.len() as u64, Err(e) if e.kind() == std::io::ErrorKind::TimedOut => return (sent, Left::Stalled), @@ -184,21 +277,13 @@ fn feed(shared: &Shared, mut sink: impl Write) -> (u64, Left) { } } -/// The bytes after `at`, as [`Hub::next_for_the_carrier`] says, skipping what -/// the UART carried where `carrier` asks. -fn next(shared: &Shared, at: &mut u64, carrier: bool) -> Vec { +/// The bytes after `at`, at most [`CHUNK`] of them, once there are any; `at` +/// moves past them. A reader the replay no longer holds the bytes of is +/// handed the line that says so instead. +fn next(shared: &Shared, at: &mut u64) -> Vec { let mut held = shared.replay.lock().expect("logkeeper: the replay is poisoned"); loop { - let mut most = CHUNK; - if let (true, Some((from, to))) = (carrier, held.carried) { - if *at >= from { - *at = (*at).max(to); - held.carried = None; - } else { - most = most.min((from - *at) as usize); - } - } - match held.replay.next(*at, most) { + match held.replay.next(*at, CHUNK) { Next::Bytes(bytes) => { *at += bytes.len() as u64; return bytes.to_vec(); @@ -214,19 +299,16 @@ fn next(shared: &Shared, at: &mut u64, carrier: bool) -> Vec { /// The line a reader gets in place of what the replay no longer holds. fn evicted(boot_secs: Option, lost: u64) -> Vec { - let at_ns = toyos_abi::clock::stamp_ns(); let text = format!("logkeeper: the first {lost} bytes of this boot are no longer held here; /log has them"); + own_line(boot_secs, Severity::Warn, &text) +} + +/// One of this program's lines, stamped now, rendered as the log renders it. +fn own_line(boot_secs: Option, severity: Severity, text: &str) -> Vec { + let at_ns = toyos_abi::clock::stamp_ns(); let tag = Tag::new(LOGKEEPER).expect("logkeeper's own name is a tag"); let stamp = crate::stamp(boot_secs, Some(at_ns)); - let line = ProgramLine { - stamp: &stamp, - at_ns, - severity: Severity::Warn, - tid: 0, - pid: None, - tag, - text: text.as_bytes(), - }; + let line = ProgramLine { stamp: &stamp, at_ns, severity, tid: 0, pid: None, tag, text: text.as_bytes() }; format!("{line}\n").into_bytes() } @@ -264,3 +346,53 @@ impl Write for PipeSink { Ok(()) } } + +#[cfg(test)] +mod tests { + use super::*; + + fn replay(fed: Fed) -> Vec { + match fed { + Fed::Replay(bytes) => bytes, + Fed::Said(_) => panic!("lines that are not the replay's"), + } + } + + /// A channel that has taken the replay up to an offset has spoken the + /// newest stamp of the last round wholly before it, and nothing of a round + /// it has only begun. + #[test] + fn what_a_channel_took_speaks_for_the_rounds_it_took_whole() { + let hub = Hub::start(1 << 20, None); + hub.append(b"a\nb\n", Some(10)); + hub.append(b"c\n", Some(20)); + hub.resume(b"held\n"); + assert_eq!(hub.stamp_through(0), None); + assert_eq!(hub.stamp_through(3), None, "half the first round is none of it"); + assert_eq!(hub.stamp_through(4), Some(10)); + assert_eq!(hub.stamp_through(6), Some(20)); + assert_eq!(hub.stamp_through(11), Some(20), "lines with no round speak for nothing"); + } + + /// The UART's carrier starts where the kernel left the UART, and is handed + /// what was said while the machine stopped once it has the replay; a + /// refused stop's lines reach it through the replay, once. + #[test] + fn the_wire_starts_after_the_kernel_and_takes_the_stops_lines_last() { + let hub = Hub::start(1 << 20, None); + hub.append(b"kernel 1\nkernel 2\nprogram\n", Some(30)); + hub.set_wire_from(18); + let mut at = hub.wire_from(); + assert_eq!(replay(hub.next_for_a_carrier(&mut at, true)), b"program\n"); + hub.append_stopping(b"stopping\n"); + match hub.next_for_a_carrier(&mut at, true) { + Fed::Said(bytes) => assert_eq!(bytes, b"stopping\n"), + Fed::Replay(bytes) => panic!("the replay's {bytes:?} where the stop's lines were owed"), + } + hub.append_stopping(b"again\n"); + hub.resume(b"stopping\nagain\n"); + assert_eq!(replay(hub.next_for_a_carrier(&mut at, true)), b"stopping\nagain\n"); + let mut port = 0; + assert_eq!(replay(hub.next_for_a_carrier(&mut port, false)).len(), 41, "the port is fed from the first line"); + } +} diff --git a/system/supervisor/src/main.rs b/system/supervisor/src/main.rs index 9a7668ed5ee..a960c6c288e 100644 --- a/system/supervisor/src/main.rs +++ b/system/supervisor/src/main.rs @@ -140,7 +140,8 @@ use toyos::AsHandle; use toyos_abi::Rights; use toyos_blockring::wire::{Grant as BlockGrant, Scope}; use toyos_logstream::{ - Registration, Tag, ALIVE, CONSOLE, FLUSH, FLUSHED, LOGKEEPER, MAX_TAG, ORIGINS, REGISTER, RESUME, STOPPING, + Registration, Tag, ALIVE, CONSOLE_INPUT, FLUSH, FLUSHED, LOGKEEPER, MAX_TAG, ORIGINS, REGISTER, RESUME, STOPPING, + WIRE_HOLDER, }; use toyos_abi::inventory::{Arrival, Pci, PciAddr, Record}; use toyos_abi::syscall::{ @@ -298,10 +299,11 @@ struct Log { conn: Connection, /// The acceptor end, until `logkeeper` is started holding it. acceptor: Option, - /// The supervisor's console as every program but `logkeeper` gets it for its stdin: one - /// it may read and not write, so no program puts a line on the console but - /// through `logkeeper` and under its name. + /// The console's input as every program gets it for its stdin: the read + /// end of the pipe whose write end the console UART's driver is handed. stdin: toyos::OwnedHandle, + /// That write end, kept so a driver started again is handed it again. + console_input: toyos::Pipe, /// What `logkeeper` answers on the connection, kept across flushes. rx: RefCell>, /// Flushes asked for and not answered yet: one waited out past @@ -309,8 +311,8 @@ struct Log { unanswered: Cell, } -/// The rights a program's stdin console keeps: it reads it and polls it, and a -/// spawn duplicates it; it does not write it. +/// The rights a program's stdin keeps: it reads it and polls it, and a spawn +/// duplicates it. const STDIN_RIGHTS: Rights = Rights::READ.union(Rights::WAIT).union(Rights::DUP).union(Rights::TRANSFER); @@ -357,27 +359,25 @@ impl Log { .finish() .expect("supervisor: no namespace for the log's origins"); let conn = names.open(ORIGINS).expect("supervisor: the log's origins port refused the supervisor"); - let stdin = toyos::fd::with(0, |o| o.handle().map(|console| console.try_clone_narrowed(STDIN_RIGHTS))) - .ok() - .flatten() - .expect("supervisor: the kernel gave it no console as descriptor 0") - .expect("supervisor: its console would not narrow"); + let (input, console_input) = toyos::pipe_pair().expect("supervisor: no pipe for the console's input"); + let stdin = input.as_handle().try_clone_narrowed(STDIN_RIGHTS).expect("supervisor: the console's input would not narrow"); let log = Self { conn, acceptor: Some(acceptor), stdin, + console_input, rx: RefCell::new(ipc::FrameRx::new()), unanswered: Cell::new(0), }; - // The supervisor's own lines: a ring like every program's, as the two - // descriptors the kernel gave its console. + // The supervisor's own lines: a ring like every program's, as its + // stdout and stderr. let ring = new_ring(); let copy = ring.as_handle().try_clone_to_owned().expect("supervisor: its ring would not duplicate"); let held = toyos::fd::install(toyos::fd::Object::handle(copy), false, 0) .expect("supervisor: no descriptor for its own ring"); for (fd, stream) in [(1, toyos::log::stdio::Stream::Out), (2, toyos::log::stdio::Stream::Err)] { - // SAFETY: the kernel's console descriptors, which nothing in this - // process owns: std's stdio names them by number and asks again. + // SAFETY: std's stdio descriptors, which the kernel starts this + // process with none of: std names them by number and asks again. unsafe { toyos::fd::dup2(held, fd, false) }.expect("supervisor: its ring would not take its own descriptor"); toyos::log::stdio::forget(stream); } @@ -3200,8 +3200,8 @@ fn start<'a>( // Where the program's output goes. // // A service the supervisor starts gets a ring of its own as its stdout and stderr - // and the supervisor's console to read as its stdin; `logkeeper` also gets the acceptor - // the rings reach it on and the one console handle that may write. A + // and the console's input to read as its stdin; `logkeeper` also gets the acceptor + // the rings reach it on, and the console UART's driver the input's write end. A // launch keeps the descriptors its caller sent, but for a ring among them // — the caller's own log — which is replaced by the program's // ([`LaunchFd::Ring`]), and a stdio descriptor it sent nothing for is a @@ -3225,14 +3225,16 @@ fn start<'a>( .try_clone_to_owned() .map_err(|e| std::io::Error::other(format!("the log's origins did not duplicate: {e:?}")))?; command.endow(ORIGINS, origins.into()); - let console = toyos::fd::with(0, |o| o.handle().map(|console| console.try_clone_to_owned())) - .ok() - .flatten() - .expect("supervisor: its console is descriptor 0") - .expect("supervisor: its console would not duplicate for logkeeper"); - command.endow(CONSOLE, console.into()); starts_logkeeper = true; } + if program.name == WIRE_HOLDER { + let input = log + .console_input + .as_handle() + .try_clone_to_owned() + .map_err(|e| std::io::Error::other(format!("the console's input did not duplicate: {e:?}")))?; + command.endow(CONSOLE_INPUT, input.into()); + } (log, Some(ring)) } Output::Launch { log, fds, ring } => { @@ -3728,11 +3730,10 @@ fn read_system(path: &'static str, server: &Service<'_>) -> String { } } -/// The boot's end before `logkeeper` runs, which no ring reaches yet: said on -/// the console the kernel gave the supervisor, then a panic like any other. +/// The boot's end before `logkeeper` runs, which no reader of the +/// supervisor's ring reaches yet +/// (`issues/a-supervisor-that-fails-before-logkeeper-runs-says-why-nowhere.md`). fn bootstrap_fails(why: String) -> ! { - let line = format!("supervisor: {why}\n"); - let _ = toyos::fd::with(0, |console| console.handle().map(|console| toyos_abi::syscall::write(console.raw(), line.as_bytes()))); panic!("supervisor: {why}"); } diff --git a/system/uart/Cargo.toml b/system/uart/Cargo.toml new file mode 100644 index 00000000000..724298c03eb --- /dev/null +++ b/system/uart/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "uart" +description = "The console UART's driver: the log logkeeper hands it onto the UART between the kernel's hand-over and the machine's stop, and what the UART receives into the console's input." +version = "0.1.0" +edition = "2024" +license = "MIT OR Apache-2.0" + +[dependencies] +toyos = { path = "../../sdk/toyos" } +toyos-abi = { path = "../../sdk/abi" } +toyos-logstream = { path = "../../lib/logstream" } +logkeeper-api = { path = "../../lib/logkeeper-api" } + +[package.metadata.toyos.host] +exempt.owns = "the machine's console UART, claimed from the kernel between its boot and its stop" diff --git a/system/uart/src/hw.rs b/system/uart/src/hw.rs new file mode 100644 index 00000000000..dc5dc12cf36 --- /dev/null +++ b/system/uart/src/hw.rs @@ -0,0 +1,137 @@ +//! The machine's console UART, as its claim hands it over: a 16550 at the +//! ports its first read binds to this process, or a PL011 in the register +//! frame that read installs. The kernel programmed the line (rate, framing, +//! FIFOs) before userland ran, and nothing here changes it. + +use toyos::shm::SharedMemory; +use toyos::volatile::Window; +use toyos_abi::ioport; +use toyos_abi::uart::{UartInfo, NS16550, PL011}; + +use crate::line::Uart; + +/// A 16550's registers, by their offset from its first port (PC16550D). +mod ns16550 { + /// Receiver buffer, read; transmitter holding register, written. + pub const DATA: u16 = 0; + /// Interrupt enable: received data available, and THR empty. + pub const IER: u16 = 1; + pub const IER_RDA: u8 = 1 << 0; + pub const IER_THRE: u8 = 1 << 1; + /// Interrupt identification: bit 0 clear while an interrupt is pending. + /// Reading it takes back a THRE interrupt it names. + pub const IIR: u16 = 2; + pub const IIR_NONE: u8 = 1 << 0; + /// Line status: data ready, and THR empty, which in FIFO mode is the + /// whole transmit FIFO empty. + pub const LSR: u16 = 5; + pub const LSR_DR: u8 = 1 << 0; + pub const LSR_THRE: u8 = 1 << 5; + /// What the transmit FIFO takes once empty. + pub const FIFO: usize = 16; +} + +/// A PL011's registers, by their offset in its frame (PL011 TRM r1p5, §3.3), +/// which the SBSA generic UART keeps too. +mod pl011 { + pub const DR: usize = 0x000; + pub const FR: usize = 0x018; + pub const FR_RXFE: u32 = 1 << 4; + pub const FR_TXFF: u32 = 1 << 5; + /// Interrupt mask set/clear, masked interrupt status, interrupt clear: + /// the receive, transmit and receive timeout interrupts. + pub const IMSC: usize = 0x038; + pub const MIS: usize = 0x040; + pub const ICR: usize = 0x044; + pub const RX: u32 = 1 << 4; + pub const TX: u32 = 1 << 5; + pub const RT: u32 = 1 << 6; +} + +/// The UART the claim named. +pub enum Machine { + Ns16550 { base: u16, ier: u8 }, + Pl011 { frame: Window, _mapping: SharedMemory }, +} + +impl Machine { + /// The UART `info` names, its receiver's interrupt asked for. `None` for a + /// description this program does not know. + pub fn open(info: &UartInfo, frame: Option) -> Option { + let mut machine = match (info.kind, frame) { + (NS16550, None) => Self::Ns16550 { base: u16::try_from(info.port).ok()?, ier: 0 }, + (PL011, Some(mapping)) => { + // SAFETY: the mapping is the frame's whole, and is held here + // for as long as the window is. + let frame = unsafe { Window::new(mapping.as_ptr(), info.frame_len as usize) }; + Self::Pl011 { frame, _mapping: mapping } + } + _ => return None, + }; + machine.ask_rx(); + Some(machine) + } + + fn ask_rx(&mut self) { + match self { + Self::Ns16550 { base, ier } => { + *ier |= ns16550::IER_RDA; + ioport::out8(*base + ns16550::IER, *ier); + } + Self::Pl011 { frame, .. } => { + let mask: u32 = frame.read(pl011::IMSC); + frame.write(pl011::IMSC, mask | pl011::RX | pl011::RT); + } + } + } +} + +impl Uart for Machine { + fn room(&mut self) -> usize { + match self { + Self::Ns16550 { base, .. } => { + if ioport::in8(*base + ns16550::LSR) & ns16550::LSR_THRE != 0 { ns16550::FIFO } else { 0 } + } + Self::Pl011 { frame, .. } => usize::from(frame.read::(pl011::FR) & pl011::FR_TXFF == 0), + } + } + + fn send(&mut self, byte: u8) { + match self { + Self::Ns16550 { base, .. } => ioport::out8(*base + ns16550::DATA, byte), + Self::Pl011 { frame, .. } => frame.write(pl011::DR, u32::from(byte)), + } + } + + fn receive(&mut self) -> Option { + match self { + Self::Ns16550 { base, .. } => { + (ioport::in8(*base + ns16550::LSR) & ns16550::LSR_DR != 0).then(|| ioport::in8(*base + ns16550::DATA)) + } + Self::Pl011 { frame, .. } => { + (frame.read::(pl011::FR) & pl011::FR_RXFE == 0).then(|| frame.read::(pl011::DR) as u8) + } + } + } + + fn ask_tx(&mut self, on: bool) { + match self { + Self::Ns16550 { base, ier } => { + *ier = if on { *ier | ns16550::IER_THRE } else { *ier & !ns16550::IER_THRE }; + ioport::out8(*base + ns16550::IER, *ier); + } + Self::Pl011 { frame, .. } => { + let mask: u32 = frame.read(pl011::IMSC); + frame.write(pl011::IMSC, if on { mask | pl011::TX } else { mask & !pl011::TX }); + frame.write(pl011::ICR, pl011::TX); + } + } + } + + fn raising(&mut self) -> bool { + match self { + Self::Ns16550 { base, .. } => ioport::in8(*base + ns16550::IIR) & ns16550::IIR_NONE == 0, + Self::Pl011 { frame, .. } => frame.read::(pl011::MIS) != 0, + } + } +} diff --git a/system/uart/src/line.rs b/system/uart/src/line.rs new file mode 100644 index 00000000000..a95f4c7b9b5 --- /dev/null +++ b/system/uart/src/line.rs @@ -0,0 +1,227 @@ +//! The line between the log and the UART: what is owed the transmitter, what +//! the receiver has, and which interrupt the UART is asked for, decided over +//! any [`Uart`] — the machine's (`crate::hw`) or a test's model. +//! +//! **The transmitter's interrupt is asked for only while bytes are owed it and +//! it has refused one.** A 16550 raises THRE once its FIFO is empty, and a +//! PL011 its TX interrupt as its FIFO falls through the trigger level (PL011 +//! TRM r1p5, "Interrupts"), so a transmitter left full always raises one; one +//! asked for with nothing owed would raise for ever. +//! +//! **A service ends with the UART raising nothing it was asked for**, or says +//! it does not: a 16550's line, an ISA edge, rises again only once every cause +//! behind it is served, so a cause left standing would never be raised again. + +use std::collections::VecDeque; + +/// The UART's registers, as the line reaches them. +pub trait Uart { + /// How many bytes the transmitter takes now, without waiting. + fn room(&mut self) -> usize; + /// One byte into the transmitter; only within [`Self::room`]. + fn send(&mut self, byte: u8); + /// A byte the receiver holds, if one does. + fn receive(&mut self) -> Option; + /// Ask for the transmitter's interrupt, or stop asking; the receiver's is + /// always asked for. + fn ask_tx(&mut self, on: bool); + /// Whether a cause of an interrupt it was asked for still stands. + fn raising(&mut self) -> bool; +} + +/// The most rounds one service makes before it hands back: a receiver that +/// never empties is a host typing faster than the UART's own rate allows. +const ROUNDS: usize = 64; + +#[derive(Default)] +pub struct Line { + owed: VecDeque, + /// Bytes put into the transmitter since the line began. + sent: u64, + asking: bool, +} + +impl Line { + /// Owe the transmitter `bytes`, after what it is owed already. + pub fn owe(&mut self, bytes: &[u8]) { + self.owed.extend(bytes); + } + + /// Whether the transmitter is still owed bytes. + pub fn owes(&self) -> bool { + !self.owed.is_empty() + } + + /// Bytes the transmitter has taken since the line began. + pub fn sent(&self) -> u64 { + self.sent + } + + /// Serve `uart`: what it received onto `received`, what it is owed into + /// its transmitter while it takes it, and its transmitter's interrupt + /// asked for exactly while it is owed bytes it refused. Whether a cause + /// still stands past [`ROUNDS`], which the caller serves again before it + /// waits. + pub fn serve(&mut self, uart: &mut impl Uart, received: &mut Vec) -> bool { + for _ in 0..ROUNDS { + while let Some(byte) = uart.receive() { + received.push(byte); + } + while !self.owed.is_empty() { + let room = uart.room(); + if room == 0 { + break; + } + for byte in self.owed.drain(..room.min(self.owed.len())) { + uart.send(byte); + self.sent += 1; + } + } + let asking = !self.owed.is_empty(); + if asking != self.asking { + uart.ask_tx(asking); + self.asking = asking; + } + if !uart.raising() { + return false; + } + } + true + } +} + +#[cfg(test)] +mod tests { + use super::*; + + /// A 16550 in FIFO mode (PC16550D): sixteen bytes of transmit FIFO, + /// `THRE` once it is empty, its interrupt standing while asked for and + /// `THRE` set, and the receiver's while a byte waits. + #[derive(Default)] + struct Ns16550 { + fifo: VecDeque, + wire: Vec, + rx: VecDeque, + thre_asked: bool, + } + + impl Ns16550 { + /// The transmitter shifts out `n` bytes. + fn shift(&mut self, n: usize) { + for _ in 0..n.min(self.fifo.len()) { + let byte = self.fifo.pop_front().expect("bounded by its length"); + self.wire.push(byte); + } + } + } + + impl Uart for Ns16550 { + fn room(&mut self) -> usize { + if self.fifo.is_empty() { 16 } else { 0 } + } + + fn send(&mut self, byte: u8) { + assert!(self.fifo.len() < 16, "a byte past the FIFO's sixteen"); + self.fifo.push_back(byte); + } + + fn receive(&mut self) -> Option { + self.rx.pop_front() + } + + fn ask_tx(&mut self, on: bool) { + self.thre_asked = on; + } + + fn raising(&mut self) -> bool { + !self.rx.is_empty() || (self.thre_asked && self.fifo.is_empty()) + } + } + + /// Every byte owed reaches the wire in order, through a FIFO of sixteen, + /// and the transmitter's interrupt is asked for only while it is owed. + #[test] + fn what_is_owed_goes_out_in_order_and_the_interrupt_ends_with_it() { + let mut uart = Ns16550::default(); + let mut line = Line::default(); + let text: Vec = (0..100u8).collect(); + line.owe(&text); + let mut received = Vec::new(); + assert!(!line.serve(&mut uart, &mut received)); + assert_eq!(uart.fifo.len(), 16); + assert!(uart.thre_asked, "owed bytes and a full FIFO ask for THRE"); + while line.owes() { + uart.shift(16); + assert!(uart.raising(), "an emptied FIFO raises THRE while asked for"); + assert!(!line.serve(&mut uart, &mut received)); + } + uart.shift(16); + assert_eq!(uart.wire, text); + assert_eq!(line.sent(), 100); + assert!(!uart.thre_asked, "nothing owed asks for nothing"); + assert!(!uart.raising(), "an empty FIFO with nothing owed raises nothing"); + } + + /// What the receiver holds is handed on whatever the transmitter does. + #[test] + fn what_is_received_is_handed_on() { + let mut uart = Ns16550 { rx: b"run x\n".iter().copied().collect(), ..Default::default() }; + let mut line = Line::default(); + let mut received = Vec::new(); + assert!(!line.serve(&mut uart, &mut received)); + assert_eq!(received, b"run x\n"); + assert!(!uart.thre_asked); + } + + /// A receiver that never empties is served [`ROUNDS`] times and handed + /// back standing, so the caller serves it again before it waits. + #[test] + fn a_cause_that_stands_past_the_rounds_is_said() { + struct Flood; + impl Uart for Flood { + fn room(&mut self) -> usize { + 16 + } + fn send(&mut self, _: u8) {} + fn receive(&mut self) -> Option { + None + } + fn ask_tx(&mut self, _: bool) {} + fn raising(&mut self) -> bool { + true + } + } + assert!(Line::default().serve(&mut Flood, &mut Vec::new())); + } + + /// A PL011's transmitter takes one byte per look while `TXFF` is clear; + /// the line looks again for each. + #[test] + fn a_transmitter_that_takes_one_at_a_time_is_filled_one_at_a_time() { + #[derive(Default)] + struct Pl011 { + fifo: Vec, + } + impl Uart for Pl011 { + fn room(&mut self) -> usize { + usize::from(self.fifo.len() < 32) + } + fn send(&mut self, byte: u8) { + self.fifo.push(byte); + } + fn receive(&mut self) -> Option { + None + } + fn ask_tx(&mut self, _: bool) {} + fn raising(&mut self) -> bool { + false + } + } + let mut uart = Pl011::default(); + let mut line = Line::default(); + line.owe(&[7; 40]); + line.serve(&mut uart, &mut Vec::new()); + assert_eq!(uart.fifo.len(), 32); + assert!(line.owes()); + } +} diff --git a/system/uart/src/main.rs b/system/uart/src/main.rs new file mode 100644 index 00000000000..50a7a1c3679 --- /dev/null +++ b/system/uart/src/main.rs @@ -0,0 +1,138 @@ +//! `/system/bin/uart` — the console UART's driver: the one writer of the UART +//! from the kernel's hand-over before userland runs to the machine's stop. +//! +//! **It carries the log as `logkeeper` hands it over** on +//! [`toyos_logstream::WIRE`], from the first line the kernel did not put on +//! the UART itself, and says how much of it the UART has taken +//! ([`logkeeper_api::delivered`]), which is what the kernel's stop and fatal +//! paths start after. While a VM's console carries the log, `logkeeper` +//! hands it nothing but the line that says so. +//! +//! **What the UART receives is the console's input**: written into the pipe +//! the supervisor hands this program ([`toyos_logstream::CONSOLE_INPUT`]), +//! whose read end is every program's standard input. A byte no reader has +//! made room for is dropped, as a UART nobody reads drops it, and said. +//! +//! The kernel programmed the line before userland ran; this program moves +//! bytes and asks for interrupts ([`line`]), and waits on nothing else. It +//! exits 0 only on a machine with no console UART. + +mod hw; +mod line; + +use toyos::poller::{Poller, READABLE}; +use toyos::{endow, say, Pipe, UartDev}; +use toyos_abi::syscall::{DeviceType, SyscallError}; +use toyos_logstream::{CONSOLE_INPUT, WIRE}; + +use hw::Machine; +use line::Line; + +const TOKEN_CLAIM: u64 = 0; +const TOKEN_LOG: u64 = 1; + +/// What one read of the log takes: what a 16550 at 38400 baud sends in about +/// a second, and a round of a busy boot's lines. +const READ: usize = 4096; + +fn main() { + let Some(claim) = endow::device::(DeviceType::Uart) else { + say!("uart: no console UART claim — nothing to drive"); + std::process::exit(0); + }; + let (info, frame) = claim.open().unwrap_or_else(|e| panic!("uart: the claim's description: {e:?}")); + let level = info.level != 0; + let mut uart = Machine::open(&info, frame).unwrap_or_else(|| panic!("uart: a UART this program does not know: {info:?}")); + let input: Option = endow::Endowments::get().take(CONSOLE_INPUT); + let mut log = match logkeeper_api::carry(WIRE) { + Ok((conn, served)) => { + say!("uart: carrying the log on the console UART, from where the kernel left it"); + Some((conn, served.pipe)) + } + Err(why) => { + say!("uart: carrying no log: {why}"); + None + } + }; + + let poller = Poller::new(2); + let mut line = Line::default(); + let mut told = 0u64; + let mut received = Vec::new(); + // Bytes dropped since the input last took every byte received. + let mut dropped = 0u64; + let mut buf = vec![0u8; READ]; + loop { + let standing = line.serve(&mut uart, &mut received); + if let Some(input) = &input + && !received.is_empty() + { + match hand_on(input, &received) { + 0 if dropped > 0 => { + say!("uart: the console's input takes bytes again, after {dropped} were dropped"); + dropped = 0; + } + 0 => {} + lost => { + if dropped == 0 { + say!("uart: the console's input is full; what the UART receives is dropped until it is read"); + } + dropped += lost; + } + } + } + received.clear(); + if let Some((conn, pipe)) = &log { + if line.sent() > told { + told = line.sent(); + logkeeper_api::delivered(conn, told); + } + if !line.owes() { + match pipe.read_nonblock(&mut buf) { + Ok(0) => { + say!("uart: logkeeper closed the log's pipe; the UART carries nothing more of it"); + log = None; + } + Ok(n) => { + line.owe(&buf[..n]); + continue; + } + Err(SyscallError::WouldBlock) => {} + Err(e) => panic!("uart: logkeeper's pipe refused a read: {e:?}"), + } + } + } + if standing { + continue; + } + if level { + claim.ack().unwrap_or_else(|e| panic!("uart: the claim refused its acknowledgement: {e:?}")); + } + poller.watch(&claim, READABLE, TOKEN_CLAIM); + if let Some((_, pipe)) = &log + && !line.owes() + { + poller.watch(pipe, READABLE, TOKEN_LOG); + } + let mut interrupted = false; + poller.wait(1, u64::MAX, |token| interrupted |= token == TOKEN_CLAIM); + if interrupted { + // The records are counts; serving the UART is what answers them. + while claim.irq().is_ok() {} + } + } +} + +/// `received` into the console's input, as much as its readers have room +/// for: answers how many bytes found none. +fn hand_on(input: &Pipe, received: &[u8]) -> u64 { + let mut at = 0; + while at < received.len() { + match input.write_nonblock(&received[at..]) { + Ok(n) => at += n, + Err(SyscallError::WouldBlock) => break, + Err(e) => panic!("uart: the console's input refused a write: {e:?}"), + } + } + (received.len() - at) as u64 +} diff --git a/system/virtio-console/Cargo.toml b/system/virtio-console/Cargo.toml index 39c5385e44e..dc6f352da00 100644 --- a/system/virtio-console/Cargo.toml +++ b/system/virtio-console/Cargo.toml @@ -14,6 +14,7 @@ toyos-vdagent = { path = "../../lib/vdagent" } toyos-virtio = { path = "../../lib/virtio" } toyos-virtio-serial = { path = "../../lib/virtio-serial" } logkeeper-api = { path = "../../lib/logkeeper-api" } +toyos-logstream = { path = "../../lib/logstream" } [package.metadata.toyos.host] exempt.owns = "one virtio-serial function, claimed from the kernel, and the endpoints its named port is handed to" diff --git a/system/virtio-console/src/console.rs b/system/virtio-console/src/console.rs index a2ecf8923db..94c57034a96 100644 --- a/system/virtio-console/src/console.rs +++ b/system/virtio-console/src/console.rs @@ -49,7 +49,8 @@ struct Console { pub fn run(mut channel: Channel, probe: Probe, held: Vec, poller: Poller) -> ! { channel.send_control(Control { id: PORT_ID, event: Event::PortOpen, value: 1 }); - let (conn, served) = logkeeper_api::carry().unwrap_or_else(|why| panic!("virtio-console: logkeeper: {why}")); + let (conn, served) = + logkeeper_api::carry(toyos_logstream::CARRIER).unwrap_or_else(|why| panic!("virtio-console: logkeeper: {why}")); say!("virtio-console: carrying the log on port {PORT_ID}, from its first line"); let mut console = Console { channel, probe, log: served.pipe, taken: 0, told: 0, silent_since: None }; for control in held { diff --git a/tests/common/power.rs b/tests/common/power.rs index 4bbdea0c80b..2ef546d8839 100644 --- a/tests/common/power.rs +++ b/tests/common/power.rs @@ -46,116 +46,6 @@ pub fn ended( Ok(()) } -/// What the stop alerts where the console's wire was kept through the stop's -/// whole budget, in `kernel/src/log/console.rs`. -const WIRE_KEPT: &str = "kept the wire through the stop's"; - -/// What both of the stop's alerts say where the wire's holder let it go only -/// past the stop's budget or not at all, in `kernel/src/log/console.rs`. -pub const WIRE_LATE: &str = "the wire through the stop's"; - -/// What a staging says where the `klogd` it woke had not taken the wire -/// `DEAF_CPU` into the stop's wait, with `klogd`'s state and the run queue, in -/// `kernel/src/log/console.rs`. Every staged test reds on it. -const STAGED_KLOGD_UNRUN: &str = "console: the staged klogd has not taken the wire"; - -/// What a stop staged by `wire-held-at-the-last-word` logs where it holds the -/// console's wire itself, in `kernel/src/log/console.rs`; where it does not, -/// `klogd` holds it inside its hold from here to the seal. -const STOP_HOLDS_AT_THE_LAST_WORD: &str = "console: the stop holds the wire at the boot's last word, staged"; - -/// What a stop staged by `wire-held-across-the-stop` logs once `klogd` holds -/// the console's wire, in `kernel/src/log/console.rs`. -const WIRE_HELD: &str = "console: klogd holds the wire as the stop begins, staged"; - -/// The actuators that stage `klogd` holding the wire across the stop: until -/// the stop asks for it, or, `kept`, for good. -pub fn wire_staged(kept: bool) -> &'static [&'static str] { - if kept { &["wire-kept-through-the-stop"] } else { &["wire-held-across-the-stop"] } -} - -/// A stop that found `klogd` holding the console's wire, judged on the whole -/// `console` of a boot that ended with the `last` word: the staged hold, the -/// stop's record and the last word are all on it, in that order; where `klogd` -/// `kept` the wire the stop said so between the hold and the record, and -/// where it did not the console is clean. -pub fn judge_the_held_wire(console: &str, last: &str, kept: bool) -> Result<(), String> { - serial::Serial::named("staged stop", console.to_string()).must_not_say(STAGED_KLOGD_UNRUN)?; - let lines: Vec<&str> = console.lines().collect(); - let at = |what: &str| lines.iter().position(|l| l.contains(what)); - let held = at(WIRE_HELD).ok_or_else(|| format!("the staged klogd never said it held the wire\n{console}"))?; - let record = lines - .iter() - .position(|l| toyos_quiesce::Record::parse(l).is_some()) - .ok_or_else(|| format!("the stop's record is not on the console\n{console}"))?; - let said = at(last).ok_or_else(|| format!("{last:?} is not on the console\n{console}"))?; - if !(held < record && record < said) { - return Err(format!("the hold, the stop's record and {last:?} are on lines {held}, {record} and {said}\n{console}")); - } - match (kept, at(WIRE_KEPT)) { - (true, Some(alert)) if held < alert && alert < record => Ok(()), - (false, None) => serial::Serial::named("staged stop", console.to_string()).must_be_clean(), - (_, alert) => Err(format!( - "klogd {} the wire, and the stop's alert is on line {alert:?}\n{console}", - if kept { "kept" } else { "was asked for" }, - )), - } -} - -/// `klogd` holds the console's wire as the stop begins, on one CPU, where it -/// runs only when the stop gives the CPU back: the machine powers off with the -/// stop's record and its last word on the console. `kept`, `klogd` keeps the -/// wire through the stop's whole budget, and the stop says so and writes -/// over it. -pub fn machine_shutdown_wire_held(test_config: &Path, kept: bool) -> Result<(), String> { - let options = BootOptions { qmp: true, smp: 1, kernel_params: wire_staged(kept), ..Default::default() }; - let mut qemu = QemuInstance::boot_with_options(test_config, &[], &[], options); - let boot = serial::Serial::boot(&qemu); - boot.must_be_clean()?; - let mut console = boot.text().to_string(); - qemu::await_marker(&mut qemu, &mut console, Q35_S5_SUPPLIED, "the ACPI server to hand the kernel \\_S5")?; - let mut stop = qemu::QmpShutdown::open(qemu.qmp_socket(), qemu.budget(qemu::GUEST_QUIET)); - writeln!(qemu.stdin_mut(), "run shutdown").expect("write to QEMU stdin"); - qemu.flush_stdin(); - ended(&mut qemu, &mut stop, &mut console, SHUTTING_DOWN, "guest-shutdown")?; - judge_the_held_wire(&console, SHUTTING_DOWN, kept)?; - eprintln!(" [power] klogd held the wire across the stop{}; the record and the last word reached it", if kept { " and kept it" } else { "" }); - Ok(()) -} - -/// `klogd` is staged to hold the console's wire from the boot's last word to -/// past the stop's seal, on one CPU, which from the seal on runs nothing but -/// the stop: a stop that left the wire to `klogd` there powers off with the -/// last word on no console. This one holds the wire itself, says so above the -/// last word, and puts the last word on the console. -pub fn machine_shutdown_wire_at_the_seal(test_config: &Path) -> Result<(), String> { - let options = - BootOptions { qmp: true, smp: 1, kernel_params: &["wire-held-at-the-last-word"], ..Default::default() }; - let mut qemu = QemuInstance::boot_with_options(test_config, &[], &[], options); - let boot = serial::Serial::boot(&qemu); - boot.must_be_clean()?; - let mut console = boot.text().to_string(); - qemu::await_marker(&mut qemu, &mut console, Q35_S5_SUPPLIED, "the ACPI server to hand the kernel \\_S5")?; - let mut stop = qemu::QmpShutdown::open(qemu.qmp_socket(), qemu.budget(qemu::GUEST_QUIET)); - writeln!(qemu.stdin_mut(), "run shutdown").expect("write to QEMU stdin"); - qemu.flush_stdin(); - ended(&mut qemu, &mut stop, &mut console, SHUTTING_DOWN, "guest-shutdown")?; - let lines: Vec<&str> = console.lines().collect(); - let at = |what: &str| lines.iter().position(|l| l.contains(what)); - match (at(STOP_HOLDS_AT_THE_LAST_WORD), at(SHUTTING_DOWN)) { - (Some(holds), Some(said)) if holds < said => {} - (holds, said) => { - return Err(format!( - "the stop's hold of the wire at the last word is on line {holds:?} and {SHUTTING_DOWN:?} on \ - {said:?}\n{console}" - )) - } - } - serial::Serial::named("staged stop", console.clone()).must_be_clean()?; - eprintln!(" [power] the stop held the wire at the last word and across the seal; the last word reached it"); - Ok(()) -} - /// A stop that ends with a userland thread still running is followed by a /// power-off all the same, with the sleep type `acpiserver` handed the /// kernel: q35 hands over in ACPI mode, so the power-off diff --git a/tests/common/qemu.rs b/tests/common/qemu.rs index 0ecfe1e0bd0..1174dac0f61 100644 --- a/tests/common/qemu.rs +++ b/tests/common/qemu.rs @@ -1482,8 +1482,8 @@ pub fn build_toyos_bin_as(arch: Arch, crate_path: &Path, name: &str, profile: &s toyos_build::build::build_toyos_bin_as(&compile::repo_root(), arch, crate_path, name, profile, quiet) } -/// A kernel record's line, whichever writer spelled its head: `klogd`'s console -/// and a `/log` file's alike. Nothing else writes one — a program's line +/// A kernel record's line, whichever writer spelled its head: the kernel's own +/// console lines and a `/log` file's alike. Nothing else writes one — a program's line /// reaches both only through `logkeeper`, under the program's own head. pub fn is_kernel_line(line: &str) -> bool { toyos_logstream::is_kernel_line(line) @@ -1498,7 +1498,7 @@ pub fn user_text(line: &str) -> &str { /// File the userland half of one captured console line under `stdout`: a /// program's text, and nothing of the kernel's. Every console line is one -/// writer's whole — `klogd` is the wire's one writer — so a line is one or +/// writer's whole — the UART has one writer at a time — so a line is one or /// the other. fn push_user_half(line: &str, stdout: &mut String) { if is_kernel_line(line) { @@ -1944,11 +1944,10 @@ impl QemuInstance { } if line.contains(&format!("===TEST_START {want}===")) { in_test = true; - // **The runner's marker reaches the console through - // `logkeeper` and the kernel's records through `klogd`**, so - // the kernel's record of this test's spawn, and what - // followed it, may arrive before the marker that opens - // the window. Everything from that record on is this + // **The console carries `logkeeper`'s rounds in stamp + // order**, so the kernel's record of this test's spawn, + // and what followed it, may arrive before the marker + // that opens the window. Everything from that record on is this // test's, and moves into the window. let spawned = format!("/{want} pid="); let from = before diff --git a/tests/common/serial.rs b/tests/common/serial.rs index 158841ca6db..7f966838d77 100644 --- a/tests/common/serial.rs +++ b/tests/common/serial.rs @@ -252,7 +252,4 @@ const NEVER_CLEAN: &[&str] = &[ // The same file — a requester no enumerated function is: refused by the // unit, and on a boot that staged none, a device nothing here accounts for. "iommu: DMA FAULT owner=none", - // kernel/src/log/console.rs — the stop waited out its whole budget for - // `klogd` to let the console's wire go. - super::power::WIRE_LATE, ]; diff --git a/tests/common/vmconsole.rs b/tests/common/vmconsole.rs index e3daccb85c4..bd3a199c29e 100644 --- a/tests/common/vmconsole.rs +++ b/tests/common/vmconsole.rs @@ -5,16 +5,12 @@ use std::path::Path; use super::qemu::{self, BootOptions, QemuInstance, QmpMonitor}; use super::serial::Serial; +use toyos_logstream::BACK_ON_THE_UART; /// What `logkeeper` says once the console's carrier has taken the log's first -/// bytes and the kernel has been told the UART is quiet -/// (`system/logkeeper/src/carrier.rs`). +/// bytes and the UART is quiet (`system/logkeeper/src/carrier.rs`). const QUIETED: &str = "logkeeper: the console carrier's channel takes the log, so the UART is quiet"; -/// What the kernel puts on the UART as the log comes back to it -/// (`kernel/src/log/console.rs`). -const BACK_ON_THE_UART: &str = "console: the UART carries the log again"; - /// What the supervisor says of a virtio-serial driver's end, the first /// program line the UART can carry once the console's has ended. The /// console's ends on a panic; the clipboard's function's, in an image with no @@ -25,11 +21,11 @@ const PANICKED: &str = "ended, code=101"; /// **The UART carries the log again once the console's carrier goes.** The /// console's port is unplugged under the running guest, the one way to end /// its driver without the authority to: the driver ends on the device's -/// `DEVICE_REMOVE`, `logkeeper` sees its carrier gone, the kernel says so on -/// the UART, and the supervisor's account of the driver's end reaches the -/// harness there. Its parts are host-tested (`logkeeper`'s `Carriage`, -/// `kernel::carrier`); what only a guest has is the kernel, `logkeeper` and -/// the driver's process doing it. q35 only: no other machine of this suite +/// `DEVICE_REMOVE`, `logkeeper` sees its carrier gone and has the UART's driver +/// say so on the UART, and the supervisor's account of the driver's end +/// reaches the harness there. Its parts are host-tested (`logkeeper`'s +/// `Carriage`, the UART driver's `Line`); what only a guest has is +/// `logkeeper` and the two drivers' processes doing it. q35 only: no other machine of this suite /// carries a console port. pub fn the_uart_carries_the_log_again_once_the_console_carrier_goes(test_config: &Path) -> Result<(), String> { let mut qemu = QemuInstance::boot_with_options(test_config, &[], &[], BootOptions { qmp: true, ..Default::default() }); diff --git a/tests/guest/kernelprobe/src/arch/aarch64.rs b/tests/guest/kernelprobe/src/arch/aarch64.rs index 719bfd7963f..65ca3d922fd 100644 --- a/tests/guest/kernelprobe/src/arch/aarch64.rs +++ b/tests/guest/kernelprobe/src/arch/aarch64.rs @@ -231,6 +231,9 @@ pub mod first_entry { "stp x29, x30, [x0, #224]", "mov x0, #{exit}", "mov x1, xzr", + "mov x2, xzr", + "mov x3, xzr", + "mov x4, xzr", "svc #0", "brk #0", exit = const SYS_THREAD_EXIT, diff --git a/tests/guest/osbench/src/time.rs b/tests/guest/osbench/src/time.rs index 42be598d1af..8e6a96d3ff4 100644 --- a/tests/guest/osbench/src/time.rs +++ b/tests/guest/osbench/src/time.rs @@ -67,14 +67,10 @@ fn sleeps(suffix: &str) { } } -/// `thread::yield_now` with nothing else to run, `sched_yield` on Linux: 15 -/// rounds of 100,000 calls after one, picoseconds per call. ToyOS has no -/// yield to measure: its std's `yield_now` is a spin-loop hint -/// (`sdk/std/sys/thread.rs`). +/// `thread::yield_now` with nothing else to run, `sched_yield` on Linux and +/// `SYS_YIELD` on ToyOS: 15 rounds of 100,000 calls after one, picoseconds +/// per call. fn yields() { - if cfg!(target_os = "toyos") { - return crate::report::unsupported("time.yield", "no yield syscall: std's yield_now is a spin-loop hint"); - } const CALLS: u64 = 100_000; let round = || { let t = Instant::now(); diff --git a/tests/guest/rust-tests/src/bin/acpi_mediated.rs b/tests/guest/rust-tests/src/bin/acpi_mediated.rs index e783116470b..e99842e168a 100644 --- a/tests/guest/rust-tests/src/bin/acpi_mediated.rs +++ b/tests/guest/rust-tests/src/bin/acpi_mediated.rs @@ -310,12 +310,18 @@ fn nic_bar(holder: &Holder) -> u64 { } fn ports(holder: &Holder, info: &AcpiInfo) { - // COM1 and the CMOS index: the kernel's, both ways. - for port in [0x3F8u64, 0x3FD, 0x70, 0x20, 0xCF8] { + // The CMOS index, the 8259 and the configuration mechanism: the kernel's, both ways. + for port in [0x70u64, 0x20, 0xCF8] { assert_eq!(holder.read(Space::SystemIo, port, Width::Byte), Err(Refused::KernelPort), "acpi: port {port:#x} was read"); assert_eq!(holder.write(Space::SystemIo, port, Width::Byte, b'!'.into()), Err(Refused::KernelPort), "acpi: port {port:#x} was written"); } - assert_eq!(holder.write(Space::SystemIo, 0x3F7, Width::Word, 0), Err(Refused::KernelPort), "acpi: a word that ends on COM1"); + + // COM1, the console UART's row, which its own claim is for. + for port in [0x3F8u64, 0x3FD] { + assert_eq!(holder.read(Space::SystemIo, port, Width::Byte), Err(Refused::ClaimedPort), "acpi: port {port:#x} was read"); + assert_eq!(holder.write(Space::SystemIo, port, Width::Byte, b'!'.into()), Err(Refused::ClaimedPort), "acpi: port {port:#x} was written"); + } + assert_eq!(holder.write(Space::SystemIo, 0x3F7, Width::Word, 0), Err(Refused::ClaimedPort), "acpi: a word that ends on COM1"); assert_eq!(holder.read(Space::SystemIo, 0xFFFF, Width::Word), Err(Refused::PortSpan)); assert_eq!(holder.read(Space::SystemIo, 0x1_0000, Width::Byte), Err(Refused::PortSpan)); assert_eq!(holder.read(Space::SystemIo, 0x80, Width::QWord), Err(Refused::PortSpan)); diff --git a/tests/guest/rust-tests/src/bin/std_threads.rs b/tests/guest/rust-tests/src/bin/std_threads.rs index f000678a4c0..3b01d2a6785 100644 --- a/tests/guest/rust-tests/src/bin/std_threads.rs +++ b/tests/guest/rust-tests/src/bin/std_threads.rs @@ -1,6 +1,7 @@ //! What std's threads take from the kernel and give back: the CPU count and -//! the mask it is counted from, which a spawned thread and process inherit, and the -//! stack and table place of a thread once it has ended, joined or not. +//! the mask it is counted from, which a spawned thread and process inherit, the +//! stack and table place of a thread once it has ended, joined or not, and the +//! CPU a yield gives up. use std::collections::BTreeSet; use std::sync::atomic::{AtomicUsize, Ordering}; @@ -11,6 +12,11 @@ use toyos_abi::syscall::MAX_THREADS; /// Threads spawned and joined one after another. const JOINS: usize = 64; +/// Yields a thread makes on its one CPU before another runnable there runs: +/// one pass takes the other's arrival and the next yield runs it, and the +/// rest is slack for a pass that takes the arrival late. +const YIELDS: u64 = 16; + /// The base of the stack the calling thread runs on. fn stack_base() -> u64 { toyos_abi::syscall::stack_info().expect("a spawned thread has a stack").0 @@ -99,6 +105,50 @@ fn main() { joined.len(), bases.len() ); + + // A thread detached before it ends gives its table place and its stack + // back as it ends, with no spawn after it: a join of it finds no thread, + // and its stack is no longer this process's to protect. + let (said, told) = std::sync::mpsc::channel(); + let (go, gone) = std::sync::mpsc::channel::<()>(); + let handle = thread::spawn(move || { + said.send((own(), stack_base())).expect("main waits on this"); + gone.recv().expect("main says go once the handle is dropped"); + }); + let (tid, base) = told.recv().expect("the detached thread said who it is"); + drop(handle); + go.send(()).expect("the detached thread waits on this"); + let joined = toyos_abi::syscall::thread_join(tid); + assert_eq!( + toyos_abi::syscall::SyscallError::from_u64(joined), + Some(toyos_abi::syscall::SyscallError::NotFound), + "a join of a thread detached before it ended answered {joined:#x}, not that no such thread is left", + ); + // SAFETY: the range was the ended thread's stack, which nothing here holds. + let protected = unsafe { + toyos_abi::syscall::vm_protect(base as *mut u8, toyos_abi::syscall::PAGE_SIZE, toyos_abi::syscall::VmProt::ReadWrite) + }; + assert!(protected.is_err(), "the stack of a thread detached before it ended is still this process's"); + println!("std_threads: a thread detached before it ended left no place and no stack behind it"); + + // A yield with another thread of the band runnable gives it the CPU: a + // thread that yields until the other has run on its one CPU sees it run + // within a few passes, where a yield that kept the CPU would spin until + // its quantum ran out, a count of looks in the hundreds of thousands. + let mut one = [0u64; toyos_abi::syscall::CPU_MASK_WORDS]; + one[0] = 1; + toyos_abi::syscall::thread_set_affinity(own(), &one).expect("CPU 0 alone"); + static RAN: AtomicUsize = AtomicUsize::new(0); + let other = thread::spawn(|| RAN.store(1, Ordering::Release)); + let mut yields = 0u64; + while RAN.load(Ordering::Acquire) == 0 { + thread::yield_now(); + yields += 1; + } + other.join().expect("the other thread"); + assert!(yields <= YIELDS, "a thread yielding on its one CPU let another run only after {yields} yields"); + toyos_abi::syscall::thread_set_affinity(own(), &all).expect("every online CPU"); + println!("std_threads: a thread yielding on its one CPU let another run after {yields} yield(s)"); } /// The calling thread's id, as the affinity calls name it. diff --git a/tests/images/acpicase.toml b/tests/images/acpicase.toml index e65fa4cc211..c8a195332d4 100644 --- a/tests/images/acpicase.toml +++ b/tests/images/acpicase.toml @@ -7,11 +7,17 @@ # has no way to its stick # (`issues/the-acpi-servers-death-has-no-readback-on-the-t14.md`). [boot] -start = ["logkeeper", "test-runner"] +start = ["logkeeper", "uart", "test-runner"] [programs.logkeeper] service = true syscap = ["logread"] +serves = ["log-wire"] + +[programs.uart] +devices = ["uart"] +receives = ["log-wire"] +restart = true [programs.acpiserver] diff --git a/tests/images/benchcase.toml b/tests/images/benchcase.toml index b3714fc441e..b3d4f760ad9 100644 --- a/tests/images/benchcase.toml +++ b/tests/images/benchcase.toml @@ -17,18 +17,24 @@ # duplicate. No other benchmark's path uses them. An unprivileged # Linux program has no observation rights to match, which the report says. [boot] -start = ["logkeeper", "diskserver", "fileserver", "acpiserver", "netstack", "test-runner"] +start = ["logkeeper", "uart", "diskserver", "fileserver", "acpiserver", "netstack", "test-runner"] # `log`, which `lan_lease` waits for netstack's lease on. [programs.logkeeper] service = true syscap = ["logread"] -serves = ["log", "log-carrier"] +serves = ["log", "log-carrier", "log-wire"] + +[programs.uart] +devices = ["uart"] +receives = ["log-wire"] +restart = true # ACPI mode, as a shipped image and the bench's Linux run it. In the legacy # mode the T14's firmware hands over, it stops every CPU for 4.5 ms every 2.2 s # (`issues/the-t14s-firmware-interrupts-every-cpu-every-2-2-s-under-toyos.md`). # Started before `test-runner`, so its claim's enable precedes every job. + [programs.acpiserver] service = true devices = ["acpi"] diff --git a/tests/images/clipboardcase.toml b/tests/images/clipboardcase.toml index 7da46f3ca55..a6b443371ac 100644 --- a/tests/images/clipboardcase.toml +++ b/tests/images/clipboardcase.toml @@ -13,13 +13,18 @@ assets = ["assets"] [boot] -start = ["acpiserver", "logkeeper", "compositor", "vdagent", "test-runner"] +start = ["acpiserver", "logkeeper", "uart", "compositor", "vdagent", "test-runner"] # `every_boot_config_runs_logkeeper` is what refuses a boot config without it. [programs.logkeeper] service = true syscap = ["logread"] -serves = ["log-carrier"] +serves = ["log-carrier", "log-wire"] + +[programs.uart] +devices = ["uart"] +receives = ["log-wire"] +restart = true [programs.compositor] service = true diff --git a/tests/images/designatecase.toml b/tests/images/designatecase.toml index 688978a2b77..9d1c039c3ca 100644 --- a/tests/images/designatecase.toml +++ b/tests/images/designatecase.toml @@ -5,12 +5,17 @@ # boot but the one that is for it; no row here starts another, so no login # holds it. [boot] -start = ["logkeeper", "diskserver", "fileserver", "acpiserver", "test-runner"] +start = ["logkeeper", "uart", "diskserver", "fileserver", "acpiserver", "test-runner"] [programs.logkeeper] service = true syscap = ["logread"] -serves = ["log"] +serves = ["log", "log-wire"] + +[programs.uart] +devices = ["uart"] +receives = ["log-wire"] +restart = true [programs.acpiserver] service = true diff --git a/tests/images/drivescase.toml b/tests/images/drivescase.toml index 0d46ad1b53c..ece9ba79dc0 100644 --- a/tests/images/drivescase.toml +++ b/tests/images/drivescase.toml @@ -3,15 +3,21 @@ # its own until the job that judges them has run. [boot] -start = ["acpiserver", "logkeeper", "test-runner"] +start = ["acpiserver", "logkeeper", "uart", "test-runner"] # `every_boot_config_runs_logkeeper` is what refuses a boot config without it. [programs.logkeeper] service = true syscap = ["logread"] -serves = ["log-carrier"] +serves = ["log-carrier", "log-wire"] + +[programs.uart] +devices = ["uart"] +receives = ["log-wire"] +restart = true # Started on each function the kernel does not drive, never by `[boot] start`. + [programs.pciholder] drives = ["pci:00:ff:00/1234:11e8"] diff --git a/tests/images/floorcase.toml b/tests/images/floorcase.toml index 6f5a3dee4ba..cd6c007c7f4 100644 --- a/tests/images/floorcase.toml +++ b/tests/images/floorcase.toml @@ -10,7 +10,7 @@ # supervisor starts again once the memory is given back. Neither has a floor. [boot] -start = ["logkeeper", "acpiserver", "diskserver", "fileserver", "ps2server", "inputsink", "starveling", "test-runner"] +start = ["logkeeper", "uart", "acpiserver", "diskserver", "fileserver", "ps2server", "inputsink", "starveling", "test-runner"] [supervisor] memory-floor-mib = 64 @@ -22,11 +22,17 @@ memory-floor-mib = 64 [programs.logkeeper] service = true syscap = ["logread"] -serves = ["log"] +serves = ["log", "log-wire"] memory-floor-mib = 32 +[programs.uart] +devices = ["uart"] +receives = ["log-wire"] +restart = true + # The machine's ACPI fixed hardware, as `images/system.toml` serves it: the # boot's last job hands the machine back to firmware through it. + [programs.acpiserver] service = true devices = ["acpi"] diff --git a/tests/images/hostedclangcase.toml b/tests/images/hostedclangcase.toml index f79b87611f2..c9198fdaed7 100644 --- a/tests/images/hostedclangcase.toml +++ b/tests/images/hostedclangcase.toml @@ -2,12 +2,17 @@ # from the package staged on its ROOT: a ROOT of its own, since the package is # a hundred megabytes and no other test reads it, and DATA, where `/apps` is. [boot] -start = ["acpiserver", "logkeeper", "diskserver", "fileserver", "test-runner"] +start = ["acpiserver", "logkeeper", "uart", "diskserver", "fileserver", "test-runner"] [programs.logkeeper] service = true syscap = ["logread"] -serves = ["log-carrier"] +serves = ["log-carrier", "log-wire"] + +[programs.uart] +devices = ["uart"] +receives = ["log-wire"] +restart = true [programs.test-runner] diff --git a/tests/images/hotplugcase.toml b/tests/images/hotplugcase.toml index e8f6fed46ee..f27633dba50 100644 --- a/tests/images/hotplugcase.toml +++ b/tests/images/hotplugcase.toml @@ -4,17 +4,23 @@ # and one `pciholder` holds each `edu` there until the function leaves. [boot] -start = ["logkeeper", "acpiserver", "test-runner"] +start = ["logkeeper", "uart", "acpiserver", "test-runner"] # `every_boot_config_runs_logkeeper` is what refuses a boot config without it. [programs.logkeeper] service = true syscap = ["logread"] -serves = ["log-carrier"] +serves = ["log-carrier", "log-wire"] + +[programs.uart] +devices = ["uart"] +receives = ["log-wire"] +restart = true # QEMU's `pcie-root-port` is no port a device outside the machine plugs # into, and no firmware property can say so: this image declares it, and no # other image does (`system/acpiserver/src/apertures.rs`). + [programs.acpiserver] service = true devices = ["acpi"] diff --git a/tests/images/jobcase.toml b/tests/images/jobcase.toml index 166bd7f6892..dfe401ca192 100644 --- a/tests/images/jobcase.toml +++ b/tests/images/jobcase.toml @@ -2,16 +2,22 @@ # job list is the manifest's own and the last thing it does is hand the machine # back to firmware. This is the shape a T14 boot has. [boot] -start = ["acpiserver", "logkeeper", "diskserver", "fileserver", "test-runner"] +start = ["acpiserver", "logkeeper", "uart", "diskserver", "fileserver", "test-runner"] [programs.logkeeper] service = true syscap = ["logread"] -serves = ["log-carrier"] +serves = ["log-carrier", "log-wire"] + +[programs.uart] +devices = ["uart"] +receives = ["log-wire"] +restart = true # One binary name per job, never a command line: the stdin path's `run ` # has the only parser. `power` because the one job ends the machine, asking # the supervisor. + [programs.test-runner] receives = ["power"] args = ["reboot"] diff --git a/tests/images/latencycase.toml b/tests/images/latencycase.toml index 96c790ca88c..36576ded04b 100644 --- a/tests/images/latencycase.toml +++ b/tests/images/latencycase.toml @@ -11,16 +11,23 @@ # `power` because the job list is what ends the boot: there is no host on the # other end of the console on the machine this config exists for. [boot] -start = ["logkeeper", "diskserver", "fileserver", "acpiserver", "test-runner"] +start = ["logkeeper", "uart", "diskserver", "fileserver", "acpiserver", "test-runner"] [programs.logkeeper] service = true syscap = ["logread"] +serves = ["log-wire"] + +[programs.uart] +devices = ["uart"] +receives = ["log-wire"] +restart = true # ACPI mode, as a shipped image and the bench's Linux run it. In the legacy # mode the T14's firmware hands over, it stops every CPU for 4.5 ms every 2.2 s # (`issues/the-t14s-firmware-interrupts-every-cpu-every-2-2-s-under-toyos.md`). # Started before `test-runner`, so its claim's enable precedes every job. + [programs.acpiserver] service = true devices = ["acpi"] diff --git a/tests/images/launchcase.toml b/tests/images/launchcase.toml index 9d38c4afd11..3b9fa633096 100644 --- a/tests/images/launchcase.toml +++ b/tests/images/launchcase.toml @@ -3,12 +3,17 @@ # holds a launcher, and `cat` and `echo` are links to it, so a spawn of # either is a launch. [boot] -start = ["acpiserver", "logkeeper", "test-runner"] +start = ["acpiserver", "logkeeper", "uart", "test-runner"] [programs.logkeeper] service = true syscap = ["logread"] -serves = ["log-carrier"] +serves = ["log-carrier", "log-wire"] + +[programs.uart] +devices = ["uart"] +receives = ["log-wire"] +restart = true [programs.test-runner] starts = ["toybox"] diff --git a/tests/images/libccase.toml b/tests/images/libccase.toml index 87c7a7ecaf8..01757f427e7 100644 --- a/tests/images/libccase.toml +++ b/tests/images/libccase.toml @@ -2,15 +2,21 @@ # its own, `echo` among them, and `system` and `popen` run their commands by # `/system/bin/shell -c`. [boot] -start = ["acpiserver", "logkeeper", "test-runner"] +start = ["acpiserver", "logkeeper", "uart", "test-runner"] [programs.logkeeper] service = true syscap = ["logread"] -serves = ["log-carrier"] +serves = ["log-carrier", "log-wire"] + +[programs.uart] +devices = ["uart"] +receives = ["log-wire"] +restart = true # `roster` for `libc_read_restarts`, which orders a child's end after its # own read is parked by the kernel's roster, and `dup` to hand it on. + [programs.test-runner] syscap = ["dup", "roster"] diff --git a/tests/images/linkcase.toml b/tests/images/linkcase.toml index 045e1f0a7fe..99f87ba9bcd 100644 --- a/tests/images/linkcase.toml +++ b/tests/images/linkcase.toml @@ -3,13 +3,18 @@ # starts `/system/bin/virtio-net` on it and ends it, and only it does. [boot] -start = ["acpiserver", "logkeeper", "diskserver", "fileserver", "netstack", "test-runner"] +start = ["acpiserver", "logkeeper", "uart", "diskserver", "fileserver", "netstack", "test-runner"] # `log` is the port the job reads netstack's lines on. [programs.logkeeper] service = true syscap = ["logread"] -serves = ["log", "log-carrier"] +serves = ["log", "log-carrier", "log-wire"] + +[programs.uart] +devices = ["uart"] +receives = ["log-wire"] +restart = true [programs.netstack] service = true diff --git a/tests/images/metalcase.toml b/tests/images/metalcase.toml index 511d268c72b..56c7fc3430c 100644 --- a/tests/images/metalcase.toml +++ b/tests/images/metalcase.toml @@ -9,7 +9,7 @@ assets = ["assets"] [boot] -start = ["acpiserver", "logkeeper", "diskserver", "fileserver", "compositor", "soundserver", "netstack", "sshserver", "test-runner", "ps2server"] +start = ["acpiserver", "logkeeper", "uart", "diskserver", "fileserver", "compositor", "soundserver", "netstack", "sshserver", "test-runner", "ps2server"] # **Every image that carries a `TOYOS-LOG` partition runs this**, and every # image does. The kernel keeps the record ring and writes no file at all, so a @@ -22,7 +22,12 @@ start = ["acpiserver", "logkeeper", "diskserver", "fileserver", "compositor", "s [programs.logkeeper] service = true syscap = ["logread"] -serves = ["log"] +serves = ["log", "log-wire"] + +[programs.uart] +devices = ["uart"] +receives = ["log-wire"] +restart = true [programs.compositor] service = true @@ -78,7 +83,6 @@ restart = true receives = ["input"] devices = ["isa:0060,0064:1,12"] - # The union its guest binaries need on this machine shape; `log` is what # `ps2server_hold` reads ps2server's probe off. [programs.test-runner] diff --git a/tests/images/metaldevicecase.toml b/tests/images/metaldevicecase.toml index 6556d3af533..5cff3705b8e 100644 --- a/tests/images/metaldevicecase.toml +++ b/tests/images/metaldevicecase.toml @@ -12,11 +12,17 @@ # output-path choice are the only exercise of `toyos-hda` that hardware can # give. [boot] -start = ["acpiserver", "logkeeper", "diskserver", "fileserver", "soundserver", "test-runner"] +start = ["acpiserver", "logkeeper", "uart", "diskserver", "fileserver", "soundserver", "test-runner"] [programs.logkeeper] service = true syscap = ["logread"] +serves = ["log-wire"] + +[programs.uart] +devices = ["uart"] +receives = ["log-wire"] +restart = true [programs.soundserver] service = true diff --git a/tests/images/netcase.toml b/tests/images/netcase.toml index 0e645f4104d..388c12b4087 100644 --- a/tests/images/netcase.toml +++ b/tests/images/netcase.toml @@ -10,7 +10,7 @@ # announce itself. [boot] -start = ["acpiserver", "logkeeper", "diskserver", "fileserver", "netstack", "soundserver", "test-runner"] +start = ["acpiserver", "logkeeper", "uart", "diskserver", "fileserver", "netstack", "soundserver", "test-runner"] # `every_boot_config_runs_logkeeper` is what refuses a boot config without it: the # kernel keeps the record ring and writes no file, so such a boot's `/log` is @@ -18,9 +18,15 @@ start = ["acpiserver", "logkeeper", "diskserver", "fileserver", "netstack", "sou [programs.logkeeper] service = true syscap = ["logread"] -serves = ["log-carrier"] +serves = ["log-carrier", "log-wire"] + +[programs.uart] +devices = ["uart"] +receives = ["log-wire"] +restart = true # netstack over the link a NIC driver brings on `link`; it claims no device. + [programs.netstack] service = true serves = ["netstack", "link"] diff --git a/tests/images/panelcase.toml b/tests/images/panelcase.toml index b0c334ff31f..066e5d21d14 100644 --- a/tests/images/panelcase.toml +++ b/tests/images/panelcase.toml @@ -7,13 +7,18 @@ assets = ["assets"] [boot] -start = ["acpiserver", "logkeeper", "compositor"] +start = ["acpiserver", "logkeeper", "uart", "compositor"] # `every_boot_config_runs_logkeeper` is what refuses a boot config without it. [programs.logkeeper] service = true syscap = ["logread"] -serves = ["log-carrier"] +serves = ["log-carrier", "log-wire"] + +[programs.uart] +devices = ["uart"] +receives = ["log-wire"] +restart = true [programs.compositor] service = true diff --git a/tests/images/proctreecase.toml b/tests/images/proctreecase.toml index 4336d446196..33fbf47b2cb 100644 --- a/tests/images/proctreecase.toml +++ b/tests/images/proctreecase.toml @@ -16,7 +16,7 @@ # hold its `login` connector. [boot] -start = ["acpiserver", "logkeeper", "diskserver", "fileserver", "test-runner"] +start = ["acpiserver", "logkeeper", "uart", "diskserver", "fileserver", "test-runner"] [apps] folder = "read-write" @@ -27,10 +27,16 @@ folder = "read-write" [programs.logkeeper] service = true syscap = ["logread"] -serves = ["log-carrier"] +serves = ["log-carrier", "log-wire"] + +[programs.uart] +devices = ["uart"] +receives = ["log-wire"] +restart = true # `roster` because the guest asks which of its tree still runs, and `dup` # because test-runner hands each binary a duplicate of its capability. + [programs.test-runner] syscap = ["dup", "roster"] starts = ["toybox", "shell", "grants", "/apps"] diff --git a/tests/images/scalecase.toml b/tests/images/scalecase.toml index 3678d7b274a..1ff7a11b573 100644 --- a/tests/images/scalecase.toml +++ b/tests/images/scalecase.toml @@ -9,13 +9,18 @@ assets = ["assets"] [boot] -start = ["acpiserver", "logkeeper", "compositor", "ps2server", "test-runner"] +start = ["acpiserver", "logkeeper", "uart", "compositor", "ps2server", "test-runner"] # `every_boot_config_runs_logkeeper` is what refuses a boot config without it. [programs.logkeeper] service = true syscap = ["logread"] -serves = ["log-carrier"] +serves = ["log-carrier", "log-wire"] + +[programs.uart] +devices = ["uart"] +receives = ["log-wire"] +restart = true [programs.compositor] service = true diff --git a/tests/images/sessioncase.toml b/tests/images/sessioncase.toml index 3abf6db9e24..84b03213694 100644 --- a/tests/images/sessioncase.toml +++ b/tests/images/sessioncase.toml @@ -17,15 +17,21 @@ assets = ["assets"] [boot] -start = ["acpiserver", "logkeeper", "compositor", "ps2server", "test-runner"] +start = ["acpiserver", "logkeeper", "uart", "compositor", "ps2server", "test-runner"] [programs.logkeeper] service = true syscap = ["logread"] -serves = ["log-carrier"] +serves = ["log-carrier", "log-wire"] + +[programs.uart] +devices = ["uart"] +receives = ["log-wire"] +restart = true # `roster`, which the job reads to find the holder it detached and see it end # when forced; `dup`, because test-runner hands each job its capability. + [programs.test-runner] syscap = ["dup", "roster"] receives = ["power"] diff --git a/tests/images/testcases.toml b/tests/images/testcases.toml index 5c6bee0cfe6..8291ab52679 100644 --- a/tests/images/testcases.toml +++ b/tests/images/testcases.toml @@ -1,6 +1,6 @@ [boot] -start = ["logkeeper", "diskserver", "fileserver", "soundserver", "acpiserver", "test-runner"] +start = ["logkeeper", "uart", "diskserver", "fileserver", "soundserver", "acpiserver", "test-runner"] # **Every image that carries a `TOYOS-LOG` partition runs this**, and every # image does. The kernel keeps the record ring and writes no file at all, so a @@ -13,11 +13,17 @@ start = ["logkeeper", "diskserver", "fileserver", "soundserver", "acpiserver", " [programs.logkeeper] service = true syscap = ["logread"] -serves = ["log", "log-carrier"] +serves = ["log", "log-carrier", "log-wire"] + +[programs.uart] +devices = ["uart"] +receives = ["log-wire"] +restart = true # The machine's ACPI fixed hardware, as `images/system.toml` serves it: on the T14 # every row of this image runs in ACPI mode, and on q35 the stop tests power off # on the sleep type it hands the kernel. + [programs.acpiserver] service = true devices = ["acpi"] diff --git a/tests/images/undeclaredcase.toml b/tests/images/undeclaredcase.toml index 74feae3da22..301e8c1003e 100644 --- a/tests/images/undeclaredcase.toml +++ b/tests/images/undeclaredcase.toml @@ -2,11 +2,17 @@ # and no acpiserver. `a_claim_waits_out_an_undeclared_machine` judges the # first claim waiting its deadline out and both refused by name. [boot] -start = ["logkeeper", "test-runner"] +start = ["logkeeper", "uart", "test-runner"] [programs.logkeeper] service = true syscap = ["logread"] +serves = ["log-wire"] + +[programs.uart] +devices = ["uart"] +receives = ["log-wire"] +restart = true [programs.pciholder] drives = ["pci:00:ff:00/1234:11e8"] diff --git a/tests/images/virtclaimcase.toml b/tests/images/virtclaimcase.toml index 133a1a46f2a..a9b3e8332af 100644 --- a/tests/images/virtclaimcase.toml +++ b/tests/images/virtclaimcase.toml @@ -4,16 +4,23 @@ # IORT routes past the SMMUv3; `test_rs_claim_bar_fault` faults in edu's BAR # and names it as a syscall's buffer. [boot] -start = ["acpiserver", "logkeeper", "test-runner"] +start = ["acpiserver", "logkeeper", "uart", "test-runner"] [programs.logkeeper] service = true syscap = ["logread"] +serves = ["log-wire"] + +[programs.uart] +devices = ["uart"] +receives = ["log-wire"] +restart = true # `device` because the job mints the claim, `inventory` because it names the # function by the arrival the inventory gives it, and `dup` because # test-runner hands a job its capability as a duplicate and hands none # without it. + [programs.test-runner] receives = ["power"] args = ["--bound-ms=240000", "test_rs_claim_lpi", "test_rs_claim_unrouted", "test_rs_claim_bar_fault"] diff --git a/tests/images/virtjobcase.toml b/tests/images/virtjobcase.toml index f612149125e..db81338d932 100644 --- a/tests/images/virtjobcase.toml +++ b/tests/images/virtjobcase.toml @@ -4,11 +4,17 @@ # asserts on; each job's line comes only if the kernel kept what that job asks # about. [boot] -start = ["logkeeper", "test-runner"] +start = ["logkeeper", "uart", "test-runner"] [programs.logkeeper] service = true syscap = ["logread"] +serves = ["log-wire"] + +[programs.uart] +devices = ["uart"] +receives = ["log-wire"] +restart = true [programs.test-runner] receives = ["power"] diff --git a/tests/images/virtpaniccase.toml b/tests/images/virtpaniccase.toml index 964c6b4c283..a8155e5306e 100644 --- a/tests/images/virtpaniccase.toml +++ b/tests/images/virtpaniccase.toml @@ -2,11 +2,17 @@ # while its siblings make kernel records on the other CPUs, and takes the # machine with it, so nothing runs after it. [boot] -start = ["logkeeper", "test-runner"] +start = ["logkeeper", "uart", "test-runner"] [programs.logkeeper] service = true syscap = ["logread"] +serves = ["log-wire"] + +[programs.uart] +devices = ["uart"] +receives = ["log-wire"] +restart = true [programs.test-runner] receives = ["power"] diff --git a/tests/images/virtrebootcase.toml b/tests/images/virtrebootcase.toml index e8c5e3a8623..fd3fbf3a9f8 100644 --- a/tests/images/virtrebootcase.toml +++ b/tests/images/virtrebootcase.toml @@ -1,10 +1,16 @@ # The one job returns the machine to firmware. [boot] -start = ["logkeeper", "test-runner"] +start = ["logkeeper", "uart", "test-runner"] [programs.logkeeper] service = true syscap = ["logread"] +serves = ["log-wire"] + +[programs.uart] +devices = ["uart"] +receives = ["log-wire"] +restart = true [programs.test-runner] receives = ["power"] diff --git a/tests/images/virtsmpcase.toml b/tests/images/virtsmpcase.toml index db2548b8708..85d8d90f6ba 100644 --- a/tests/images/virtsmpcase.toml +++ b/tests/images/virtsmpcase.toml @@ -6,11 +6,17 @@ # `test_rs_timing_protection`. The harness puts all three on ROOT. Then the # machine powers off, every CPU but one turned off first. [boot] -start = ["logkeeper", "test-runner"] +start = ["logkeeper", "uart", "test-runner"] [programs.logkeeper] service = true syscap = ["logread"] +serves = ["log-wire"] + +[programs.uart] +devices = ["uart"] +receives = ["log-wire"] +restart = true [programs.test-runner] receives = ["power"] diff --git a/tests/toyos.rs b/tests/toyos.rs index 2e2c1d9fd29..c69697e61ad 100644 --- a/tests/toyos.rs +++ b/tests/toyos.rs @@ -414,10 +414,9 @@ const SCREEN_TESTS: &[(&str, qemu::Profile)] = &[ ("virt_debug_refused", qemu::Profile::Virt), ("virt_readonly_copyout", qemu::Profile::Virt), ("virt_ring0_timer_in_syscall", qemu::Profile::Virt), - // Emulated, with `virt_el1_smp`, `virt_off_names_the_cpus_left_on` and the - // reboots: under HVF a `klogd` the stop wakes can go undispatched past the - // stop's whole wait for the console's wire - // (issues/a-woken-klogd-can-wait-seconds-on-an-idle-cpu-under-hvf.md). + // Emulated, with `virt_el1_smp`, `virt_off_names_the_cpus_left_on` and + // `virt_reboot`: under HVF a woken thread can go undispatched for seconds + // (issues/a-woken-thread-can-wait-seconds-on-an-idle-cpu-under-hvf.md). ("virt_mask_windows", qemu::Profile::VirtEl2), ("virt_smp", qemu::Profile::VirtEl2), ("virt_el1_smp", qemu::Profile::VirtTcg), @@ -428,8 +427,6 @@ const SCREEN_TESTS: &[(&str, qemu::Profile)] = &[ ("virt_reboot", qemu::Profile::VirtEl2), ("virt_off_names_the_cpus_left_on", qemu::Profile::VirtEl2), ("virt_reboot_refused_without_psci", qemu::Profile::Virt), - ("virt_reboot_wire_held", qemu::Profile::VirtEl2), - ("virt_reboot_wire_kept", qemu::Profile::VirtEl2), // The job case entered at EL2, once: the entry's EL2 writes for the timer // and FP, which no boot under HVF runs. ("virt_jobs_at_el2", qemu::Profile::VirtEl2), @@ -531,6 +528,12 @@ const MACHINE_TESTS: &[&str] = &[ // again. Three processes, the kernel and QEMU's hot-unplug, which no host // build has. q35 only: no other machine carries a console port. "the_uart_carries_the_log_again_once_the_console_carrier_goes", + // The UART carries the boot once across the kernel's hand-over to its + // driver: the kernel's last inline record once, and the driver's first + // after it. The kernel's drain, logkeeper's start offset and the driver + // are three components only a boot has; q35 with no console port, so the + // UART is the one console, read whole, which no bench's is. + "the_uart_carries_the_boot_once_across_the_hand_over", // An AMD-Vi unit left blocking every device, switched off and programmed // before the boot disk is driven behind it: the register sequence and the // DMA it lets through are no host test's, and the AMD bench is staged by @@ -581,7 +584,7 @@ const MACHINE_TESTS: &[&str] = &[ "acpi_mediated_access", // The same boot on one CPU, where the power-off's CPU is the only one // there is: what it logs reaches the console by the stop's own drain or - // not at all, since no other CPU runs `klogd` beside it. + // not at all. "acpi_lock_given_back_on_one_cpu", // A power-off on the sleep type of a holder that is gone: the kernel's // static across a claim's release, and a machine that answers the write @@ -592,16 +595,6 @@ const MACHINE_TESTS: &[&str] = &[ // ends the machine, so only one QEMU reports stopping can be asked, and // the T14 hands over in legacy mode, where no holder means no quieting. "machine_shutdown_short_stop", - // The power-off with `klogd` holding the console's wire as the stop - // begins, on one CPU: a staged hold of a kernel thread, read off a console - // QEMU keeps past the power-off, and the T14 is never asked to power off. - "machine_shutdown_wire_held", - // The same with `klogd` keeping the wire through the stop's budget. - "machine_shutdown_wire_kept", - // The same with `klogd` staged inside its hold of the wire from the boot's - // last word past the seal, where the stop does not hold it: the one CPU - // runs nothing but the stop from the seal on. - "machine_shutdown_wire_at_the_seal", // A claimable function with a mappable BAR that no program of the boot // holds: QEMU's virtio NIC on `tests/images/testcases.toml`. The T14's one such // function is its I219, and a kernel that dies on this takes the bench's @@ -853,8 +846,8 @@ const METAL: &[(&str, metal::Metal)] = &[ metal::Metal { arms: TESTCASES, judge: |b| ioapic_topology(b[0].kernel().text()) }, ), ( - "klogd_hosted", - metal::Metal { arms: TESTCASES, judge: |b| klogd_hosted(&b[0].kernel()) }, + "console_uart_driven", + metal::Metal { arms: TESTCASES, judge: |b| console_uart_driven(&b[0].log()) }, ), ( "irq_census_conservation", @@ -3117,6 +3110,32 @@ fn console_image_boots() -> Result<(), String> { said.must_be_clean() } +/// On a machine whose UART is its one console, the kernel puts the boot on it +/// up to the hand-over and the UART's driver everything after, from +/// `logkeeper`: the boot's last inline record and the driver's claim are +/// each on it once. A start at the boot's first line repeats the first; one +/// past the hand-over loses the second. +fn the_uart_carries_the_boot_once_across_the_hand_over() -> Result<(), String> { + let mut qemu = QemuInstance::boot_with_options( + &compile::repo_root().join("tests/images/testcases.toml"), + &[], + &[], + BootOptions { profile: qemu::Profile::HeadlessNoConsolePort, ..Default::default() }, + ); + let mut console = format!("{}\n", qemu.boot_log()); + await_marker(&mut qemu, &mut console, UART_CARRIES, "the UART's driver to carry the log")?; + let said = serial::Serial::named("the UART", console.clone()); + said.must_be_clean()?; + for (needle, what) in [(bootlog::COMPLETE, "the boot's last inline record"), (UART_BOUND, "the driver's claim")] { + let times = console.lines().filter(|l| l.contains(needle)).count(); + if times != 1 { + return Err(format!("{what} ({needle:?}) is on the UART {times} times\n{console}")); + } + } + eprintln!(" [uart] {}", said.must_say(UART_BOUND)?.trim()); + Ok(()) +} + /// Eight spawns from each of a file the root server holds in memory, a copy /// on DATA that the spawn reads whole, and a copy in the kernel's `/tmp`: /// each child maps its parent's log ring, and once they have ended the census @@ -4093,10 +4112,8 @@ fn virt_failed_ap_leaves_no_hole(profile: qemu::Profile) -> Result<(), String> { /// `tests/images/virtrebootcase.toml`'s one job asks for a reboot: the boot's last word is /// `Rebooting.`, QEMU stops for `guest-reset`, and its trace of PSCI holds one -/// `SYSTEM_RESET` and neither `CPU_OFF` nor `SYSTEM_OFF`. With `staged` -/// actuators `klogd` holds the console's wire as the stop begins, judged by -/// [`power::judge_the_held_wire`]. -fn virt_reboot(profile: qemu::Profile, staged: &'static [&'static str]) -> Result<(), String> { +/// `SYSTEM_RESET` and neither `CPU_OFF` nor `SYSTEM_OFF`. +fn virt_reboot(profile: qemu::Profile) -> Result<(), String> { let config = compile::repo_root().join("tests/images/virtrebootcase.toml"); let trace = common::lane::dir().join("virt_reboot.psci"); let mut qemu = QemuInstance::boot_with_options( @@ -4107,7 +4124,6 @@ fn virt_reboot(profile: qemu::Profile, staged: &'static [&'static str]) -> Resul profile, qmp: true, psci_trace: Some(trace.clone()), - kernel_params: staged, ready_marker: "control registers: SCTLR_EL1=", ..Default::default() }, @@ -4128,9 +4144,6 @@ fn virt_reboot(profile: qemu::Profile, staged: &'static [&'static str]) -> Resul if asked != [PSCI_SYSTEM_RESET] { return Err(format!("QEMU traced {asked:x?} of CPU_OFF, SYSTEM_OFF and SYSTEM_RESET, not one SYSTEM_RESET")); } - if !staged.is_empty() { - power::judge_the_held_wire(&console, bootlog::REBOOTING, staged == power::wire_staged(true))?; - } eprintln!(" [virt] Rebooting., then one SYSTEM_RESET, and QEMU stopped for guest-reset"); Ok(()) } @@ -4584,11 +4597,7 @@ fn run_screen_test(name: &str, profile: qemu::Profile, test_config: &Path) -> Re "virt_el1_smp" => virt_smp(profile, "HVC", 1), "virt_failed_ap_leaves_no_hole" => virt_failed_ap_leaves_no_hole(profile), "virt_fatal_halts_the_others_first" => virt_fatal_halts_the_others_first(profile), - "virt_reboot" => virt_reboot(profile, &[]), - // The reboot with `klogd` holding the PL011's wire as the stop begins, - // then keeping it through the stop's budget. - "virt_reboot_wire_held" => virt_reboot(profile, power::wire_staged(false)), - "virt_reboot_wire_kept" => virt_reboot(profile, power::wire_staged(true)), + "virt_reboot" => virt_reboot(profile), "virt_off_names_the_cpus_left_on" => virt_off_names_the_cpus_left_on(profile), "virt_reboot_refused_without_psci" => virt_reboot_refused_without_psci(profile), "screen_fatal_behind_a_painter" => { @@ -4882,13 +4891,11 @@ fn run_machine_test(name: &str, test_config: &Path) -> Result<(), String> { "acpi_lock_given_back_on_one_cpu" => acpi_mediated_access(1), "acpi_supply_outlives_holder" => acpi_supply_outlives_holder(), "machine_shutdown_short_stop" => power::machine_shutdown_short_stop(test_config), - "machine_shutdown_wire_held" => power::machine_shutdown_wire_held(test_config, false), - "machine_shutdown_wire_kept" => power::machine_shutdown_wire_held(test_config, true), - "machine_shutdown_wire_at_the_seal" => power::machine_shutdown_wire_at_the_seal(test_config), "bar_map_again" => bar_map_again(test_config), "bar_map_behind_a_bridge" => bar_map_behind_a_bridge(test_config), "lent_device_memory_refused" => lent_device_memory_refused(test_config), "console_image_boots" => console_image_boots(), + "the_uart_carries_the_boot_once_across_the_hand_over" => the_uart_carries_the_boot_once_across_the_hand_over(), "a_spawn_from_an_image_frees_it_when_the_child_ends" => a_spawn_from_an_image_frees_it_when_the_child_ends(test_config), "a_bent_supervisor_is_refused_and_the_other_slot_boots" => { [qemu::Profile::Headless, qemu::Profile::Virt].into_iter().try_for_each(|profile| { @@ -6819,14 +6826,29 @@ fn iommu_firmware_left(log: &str) -> Result<(), String> { Ok(()) } -/// The machine's kernel thread is hosted. -fn klogd_hosted(boot: &serial::Serial) -> Result<(), String> { - boot.must_be_clean()?; - let line = boot.must_say("kthread: klogd")?; - eprintln!(" [kthread] {}", line.trim()); +/// The console UART is its driver's from the kernel's hand-over: the claim's +/// ports went to a process and the driver carries the log, or the machine +/// has no UART and the driver said so and ended. +fn console_uart_driven(log: &serial::Serial) -> Result<(), String> { + log.must_be_clean()?; + let line = match log.text().lines().find(|l| l.contains(NO_UART)) { + Some(none) => none, + None => { + log.must_say(UART_BOUND)?; + log.must_say(UART_CARRIES)? + } + }; + eprintln!(" [uart] {}", line.trim()); Ok(()) } +/// What `/system/bin/uart` says on a machine with no console UART. +const NO_UART: &str = "uart: no console UART claim"; +/// What the kernel says as the console UART's claim binds its ports. +const UART_BOUND: &str = "isa: the console UART's ports are pid"; +/// What `/system/bin/uart` says once its claim is read and its log asked for. +const UART_CARRIES: &str = "uart: carrying the log"; + /// Every I/O APIC this machine has, and whether its redirection table is a /// chip's rather than a floating bus's. fn ioapic_topology(log: &str) -> Result<(), String> { From 08e7ba2d80e9b4dbe38508ab9e23e7d8b7e088b5 Mon Sep 17 00:00:00 2001 From: japabu Date: Wed, 4 Nov 2026 01:29:47 +0100 Subject: [PATCH 2/6] images: the diagnostic image drives no UART `images/diag.toml` is for a machine with no serial port and claims no device, which `no_diag_program_claims_the_screen` holds; the check that every image running logkeeper drives the console UART names it as the one exception. Co-Authored-By: Claude Opus 5.5 --- build/build.rs | 6 ++++-- images/diag.toml | 12 +++--------- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/build/build.rs b/build/build.rs index 864b10eea5b..c5ea70c731f 100644 --- a/build/build.rs +++ b/build/build.rs @@ -3200,10 +3200,12 @@ mod tests { "{config} starts `logkeeper` and has no `[programs.logkeeper]` row to say what it holds", ); // The UART carries nothing past the kernel's hand-over but what - // its driver puts on it. + // its driver puts on it. The diagnostic image is for a machine with + // no UART, and claims no device (`no_diag_program_claims_the_screen`). let wire = toyos_logstream::WIRE_HOLDER; assert!( - parsed.boot.start.iter().any(|p| p == wire) + *config == "images/diag.toml" + || parsed.boot.start.iter().any(|p| p == wire) && parsed.programs.get(wire).is_some_and(|row| row.devices.iter().any(|d| d == "uart")), "{config} runs `logkeeper` and does not start `{wire}` holding the `uart` claim, so its console \ UART says nothing from the kernel's hand-over to the machine's stop", diff --git a/images/diag.toml b/images/diag.toml index bcb051c28da..b62615257c5 100644 --- a/images/diag.toml +++ b/images/diag.toml @@ -11,11 +11,11 @@ # guarantee is that this config declares no `devices`, which # `no_diag_program_claims_the_screen` checks. [boot] -start = ["logkeeper", "uart", "fileserver", "toybox"] +start = ["logkeeper", "fileserver", "toybox"] # **Every image that carries a `TOYOS-LOG` partition runs this**, and every -# image does. The kernel keeps the record ring and the console and writes no -# file at all, so a boot config without `logkeeper` is a boot whose `/log` is empty — +# image does. The kernel keeps the record ring and writes no file at all, so a +# boot config without `logkeeper` is a boot whose `/log` is empty — # `every_boot_config_runs_logkeeper` is what refuses one. It claims no device and # serves no port: its row's authority is `logread`, which is # `Rights::LOG | Rights::WAIT` on a `SysCap` duplicate, and the supervisor hands it every @@ -23,12 +23,6 @@ start = ["logkeeper", "uart", "fileserver", "toybox"] [programs.logkeeper] service = true syscap = ["logread"] -serves = ["log-wire"] - -[programs.uart] -devices = ["uart"] -receives = ["log-wire"] -restart = true [programs.toybox] args = ["pwd"] From 40db39760546319c4255cf8cf07c074f8c74e288 Mon Sep 17 00:00:00 2001 From: japabu Date: Wed, 4 Nov 2026 01:53:57 +0100 Subject: [PATCH 3/6] virtpancase drives the UART; a muted UART feed moves its offset `tests/images/virtpancase.toml` landed on main beside this branch and runs logkeeper, so it starts the UART's driver as every such image does (`every_boot_config_runs_logkeeper`). The UART's carrier records how far it passed over the replay while the port carried it, so the stop's wait reads where it stands rather than where it was muted. Co-Authored-By: Claude Opus 5.5 --- system/logkeeper/src/carrier.rs | 2 ++ tests/images/virtpancase.toml | 8 +++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/system/logkeeper/src/carrier.rs b/system/logkeeper/src/carrier.rs index 5e7c031dbc9..b1c39384f12 100644 --- a/system/logkeeper/src/carrier.rs +++ b/system/logkeeper/src/carrier.rs @@ -519,6 +519,8 @@ impl Carriers { } } if quiet { + // Passed over, which is what the stop's wait reads. + self.lock().feed(channel).at = at; continue; } } diff --git a/tests/images/virtpancase.toml b/tests/images/virtpancase.toml index 64423fc2d4e..a9c6c4dab50 100644 --- a/tests/images/virtpancase.toml +++ b/tests/images/virtpancase.toml @@ -1,11 +1,17 @@ # One CPU and one job, `pan_read`, on the test kernel: its kernel reads the # job's page at EL1, which PAN makes a fault that ends the machine. [boot] -start = ["logkeeper", "test-runner"] +start = ["logkeeper", "uart", "test-runner"] [programs.logkeeper] service = true syscap = ["logread"] +serves = ["log-wire"] + +[programs.uart] +devices = ["uart"] +receives = ["log-wire"] +restart = true [programs.test-runner] receives = ["power"] From 0638624c00626111a86289be359ce1e4d4081251 Mon Sep 17 00:00:00 2001 From: japabu Date: Wed, 4 Nov 2026 06:37:08 +0100 Subject: [PATCH 4/6] supervisor: the log's origins and the console's input go to the row that holds them The supervisor endowed the log's origins acceptor to the row named `logkeeper` and the console-input pipe's write end to the row named `uart`. A name grants no authority: the holder of the origins names every line's writer, and the holder of the console's input types at every program's standard input. A `[programs]` row now says `holds = ["log-origins"]` or `holds = ["console-input"]`; the manifest carries it as a `hold` line, and the supervisor endows the row that holds each. The build refuses a row that holds either unless it is that object's one holder (`HOLDINGS`, checked with `held_by_their_holders_alone` on every rendered manifest), and every boot config that runs logkeeper must give it the origins and its UART driver the console's input. The UART driver refuses to start without the console's input rather than drop every byte it receives unsaid. The issue on a supervisor that fails before logkeeper runs now records what the kernel's inline drain can and cannot carry: holding it past the hand-over would carry no line of the supervisor's, which is not a kernel record. Co-Authored-By: Claude Opus 5.5 --- Cargo.lock | 1 + build/build.rs | 48 +++++++++++++++---- images/console.toml | 2 + images/diag.toml | 1 + images/system.toml | 12 +++-- ...-before-logkeeper-runs-says-why-nowhere.md | 30 ++++++++---- lib/logstream/src/lib.rs | 15 +++--- lib/manifest/Cargo.toml | 2 + lib/manifest/src/lib.rs | 20 ++++++++ system/logkeeper/src/main.rs | 3 +- system/supervisor/src/main.rs | 25 +++++----- system/uart/src/main.rs | 10 ++-- tests/images/acpicase.toml | 2 + tests/images/benchcase.toml | 2 + tests/images/clipboardcase.toml | 2 + tests/images/designatecase.toml | 2 + tests/images/drivescase.toml | 2 + tests/images/floorcase.toml | 2 + tests/images/hostedclangcase.toml | 2 + tests/images/hotplugcase.toml | 2 + tests/images/jobcase.toml | 2 + tests/images/latencycase.toml | 2 + tests/images/launchcase.toml | 2 + tests/images/libccase.toml | 2 + tests/images/linkcase.toml | 2 + tests/images/metalcase.toml | 2 + tests/images/metaldevicecase.toml | 2 + tests/images/netcase.toml | 2 + tests/images/panelcase.toml | 2 + tests/images/proctreecase.toml | 2 + tests/images/scalecase.toml | 2 + tests/images/sessioncase.toml | 2 + tests/images/testcases.toml | 2 + tests/images/undeclaredcase.toml | 2 + tests/images/virtclaimcase.toml | 2 + tests/images/virtjobcase.toml | 2 + tests/images/virtpancase.toml | 2 + tests/images/virtpaniccase.toml | 2 + tests/images/virtrebootcase.toml | 2 + tests/images/virtsmpcase.toml | 2 + 40 files changed, 178 insertions(+), 47 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 90e34792a48..ca0f0ec6d93 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3858,6 +3858,7 @@ version = "0.1.0" dependencies = [ "toyos", "toyos-abi", + "toyos-logstream", ] [[package]] diff --git a/build/build.rs b/build/build.rs index c5ea70c731f..fa094e23a1d 100644 --- a/build/build.rs +++ b/build/build.rs @@ -196,6 +196,9 @@ struct ProgramConfig { syscap: Vec, /// The idle slot (`toyos_manifest::Program::slots`). slots: bool, + /// The machine-wide objects the supervisor makes once and endows this row + /// (`toyos_manifest::Program::holds`). + holds: Vec, /// Disk authorizations minted at its start (`toyos_manifest::authorize`). authorize: Vec, /// A system service: the supervisor starts it with `HOME` at its own `/state/` @@ -695,6 +698,16 @@ const HELD_ALONE: &[(&str, &[&str])] = &[ (toyos_vdagent::endpoint::SERVICE, &[toyos_vdagent::endpoint::SERVICE_HOLDER]), ]; +/// The objects the supervisor makes once and endows the row that holds them +/// (`toyos_manifest::HOLDINGS`), each with the one row that may: the acceptor +/// every program's log ring reaches `logkeeper` on, whose holder names every +/// line's writer, and the console's input, whose holder types at every +/// program's standard input. +const HOLDINGS: &[(&str, &str)] = &[ + (toyos_logstream::ORIGINS, toyos_logstream::LOGKEEPER), + (toyos_logstream::CONSOLE_INPUT, toyos_logstream::WIRE_HOLDER), +]; + /// Rows, as a refusal names them. fn named(rows: &[&str]) -> String { rows.iter().map(|r| format!("`{r}`")).collect::>().join(" and ") @@ -702,9 +715,9 @@ fn named(rows: &[&str]) -> String { /// Who may hold the authorities that change what the machine runs or what a /// package sees: each of [`HELD_ALONE`]'s ports, its holder and nothing else -/// — no other `[programs]` row and never `[apps]` — the idle slot, -/// [`toyos_update::slots::HOLDER`] alone, and the consent port, which no row -/// receives and its server alone serves. +/// — no other `[programs]` row and never `[apps]` — each of [`HOLDINGS`], its +/// one row, the idle slot, [`toyos_update::slots::HOLDER`] alone, and the +/// consent port, which no row receives and its server alone serves. /// /// **Checked on every manifest rendered, not only on the committed configs**, /// because the swap port's holder can replace any service's binary, the @@ -725,6 +738,11 @@ fn held_by_their_holders_alone(config: &SystemConfig) -> Result<(), String> { if name != consent::SERVER && program.serves.iter().any(|s| s == consent::PORT) { return Err(format!("`{name}` serves `{}`, which only `{}` may serve", consent::PORT, consent::SERVER)); } + for (held, holder) in HOLDINGS { + if name != holder && program.holds.iter().any(|h| h == held) { + return Err(format!("`{name}` holds `{held}`, which only `{holder}` may hold")); + } + } if name != toyos_update::slots::HOLDER && program.slots { return Err(format!("`{name}` asks for `slots`, which only `{}` may hold", toyos_update::slots::HOLDER)); } @@ -891,6 +909,7 @@ fn render_manifest(config: &SystemConfig) -> Vec { firmware: cfg.firmware.clone(), syscap: cfg.syscap.clone(), slots: cfg.slots, + holds: cfg.holds.clone(), authorize: cfg.authorize.clone(), service: cfg.service, state: cfg.state, @@ -3196,8 +3215,9 @@ mod tests { parsed.boot.start, ); assert!( - parsed.programs.contains_key("logkeeper"), - "{config} starts `logkeeper` and has no `[programs.logkeeper]` row to say what it holds", + parsed.programs.get("logkeeper").is_some_and(|row| row.holds.iter().any(|h| h == toyos_logstream::ORIGINS)), + "{config} starts `logkeeper` and no `[programs.logkeeper]` row holds `{}`, so no program's ring reaches it", + toyos_logstream::ORIGINS, ); // The UART carries nothing past the kernel's hand-over but what // its driver puts on it. The diagnostic image is for a machine with @@ -3206,9 +3226,12 @@ mod tests { assert!( *config == "images/diag.toml" || parsed.boot.start.iter().any(|p| p == wire) - && parsed.programs.get(wire).is_some_and(|row| row.devices.iter().any(|d| d == "uart")), - "{config} runs `logkeeper` and does not start `{wire}` holding the `uart` claim, so its console \ - UART says nothing from the kernel's hand-over to the machine's stop", + && parsed.programs.get(wire).is_some_and(|row| { + row.devices.iter().any(|d| d == "uart") + && row.holds.iter().any(|h| h == toyos_logstream::CONSOLE_INPUT) + }), + "{config} runs `logkeeper` and does not start `{wire}` holding the `uart` claim and the console's \ + input, so its console UART says nothing from the kernel's hand-over to the machine's stop", ); for (name, program) in &parsed.programs { let holds = program.syscap.iter().any(|s| s == "logread"); @@ -3455,6 +3478,15 @@ mod tests { assert!(held_by_their_holders_alone(&sshserver).is_err()); let apps: SystemConfig = toml::from_str("[apps]\nreceives = [\"swap\"]\n").unwrap(); assert!(held_by_their_holders_alone(&apps).is_err()); + let origins: SystemConfig = toml::from_str("[programs.shell]\nholds = [\"log-origins\"]\n").unwrap(); + assert!(held_by_their_holders_alone(&origins).is_err()); + let input: SystemConfig = toml::from_str("[programs.shell]\nholds = [\"console-input\"]\n").unwrap(); + assert!(held_by_their_holders_alone(&input).is_err()); + let held: SystemConfig = toml::from_str( + "[programs.logkeeper]\nholds = [\"log-origins\"]\n[programs.uart]\nholds = [\"console-input\"]\n", + ) + .unwrap(); + assert_eq!(held_by_their_holders_alone(&held), Ok(())); let slots: SystemConfig = toml::from_str("[programs.shell]\nslots = true\n").unwrap(); assert!(held_by_their_holders_alone(&slots).is_err()); let booted: SystemConfig = diff --git a/images/console.toml b/images/console.toml index 16e2d3bfa2b..b4385b31a50 100644 --- a/images/console.toml +++ b/images/console.toml @@ -35,9 +35,11 @@ start = ["logkeeper", "uart", "diskserver", "fileserver", "console", "acpiserver service = true syscap = ["logread"] serves = ["log", "log-wire"] +holds = ["log-origins"] [programs.uart] devices = ["uart"] +holds = ["console-input"] receives = ["log-wire"] restart = true diff --git a/images/diag.toml b/images/diag.toml index b62615257c5..98e5f1b6543 100644 --- a/images/diag.toml +++ b/images/diag.toml @@ -23,6 +23,7 @@ start = ["logkeeper", "fileserver", "toybox"] [programs.logkeeper] service = true syscap = ["logread"] +holds = ["log-origins"] [programs.toybox] args = ["pwd"] diff --git a/images/system.toml b/images/system.toml index 2230341804f..2b37f60a0e7 100644 --- a/images/system.toml +++ b/images/system.toml @@ -59,22 +59,26 @@ pkg = { state = true } # **Every image that carries a `TOYOS-LOG` partition runs this**, and every # image does. The kernel keeps the record ring, writes the console only before -# userland runs and from the stop, and writes no -# file at all, so a boot config without `logkeeper` is a boot whose `/log` is empty — +# userland runs and from the stop, and writes no file at all, so a boot config +# without `logkeeper` is a boot whose `/log` is empty — # `every_boot_config_runs_logkeeper` is what refuses one. It claims no device, and # `log` is the port it answers `inspect` on. `log-carrier` is where the VM's # console asks to carry the log, which quiets the UART while it does, and -# `log-wire` where the UART's driver asks. +# `log-wire` where the UART's driver asks. It holds `log-origins`, the +# acceptor every program's ring reaches it on, which no other row may hold. [programs.logkeeper] service = true syscap = ["logread"] serves = ["log", "log-carrier", "log-wire"] +holds = ["log-origins"] # The console UART's driver: the log `logkeeper` hands it over `log-wire` onto # the UART, from the kernel's hand-over to the machine's stop, and what the UART -# receives into the console's input. A machine with no console UART exits it 0. +# receives into the console's input, whose write end it holds and no other +# row may. A machine with no console UART exits it 0. [programs.uart] devices = ["uart"] +holds = ["console-input"] receives = ["log-wire"] restart = true diff --git a/issues/a-supervisor-that-fails-before-logkeeper-runs-says-why-nowhere.md b/issues/a-supervisor-that-fails-before-logkeeper-runs-says-why-nowhere.md index 987a7e8f937..5b484da8498 100644 --- a/issues/a-supervisor-that-fails-before-logkeeper-runs-says-why-nowhere.md +++ b/issues/a-supervisor-that-fails-before-logkeeper-runs-says-why-nowhere.md @@ -11,18 +11,28 @@ start, or `/system` cannot be read (`bootstrap_fails`, `system/supervisor/src/main.rs`). Until the kernel's console thread was deleted it wrote that line on the kernel console the kernel spawned it with, which the thread put on the UART. That console is gone with the thread: the -UART is its userland driver's, which `logkeeper` feeds, and `logkeeper` is a -program on `/system`. The line now goes to the supervisor's own ring, which -nothing reads before `logkeeper` runs, and then the supervisor panics. +UART is its userland driver's, which `logkeeper` feeds, and both are +programs on `/system`, which is what failed. The line goes to the +supervisor's own ring, which nothing reads, and the supervisor panics. -What is left is the kernel's: the supervisor's `exit:` record, and the -deadline's report, which put both on the UART and in the black box. Neither -says why. +What is left is the kernel's records: the supervisor's `exit:` and the file +server's, which no channel carries either, because the kernel's own writes +to the UART after the hand-over are the stop's and a fatal path's alone. A +boot armed with a deadline ends in its report, which puts them on the UART +and in the black box; a shipping image, armed with none, idles with nothing +said. Neither says why. + +Holding the kernel's inline drain past the hand-over would not carry the +line, which is not a kernel record, and would put every record's UART write +back on its producer — an interrupt handler or a lock holder — until the +driver runs. **Evidence:** the code; no test stages a broken `/system`. -**Owner:** the supervisor's. +**Owner:** the supervisor's, with the kernel's fatal path; the shape is the +owner's to rule (`#1165`). -**Exit:** a boot whose `/system` will not serve says why on a channel an -operator reads — the black box, or the UART at the fatal path that follows — -shown by a boot staged with a `/system` that does not serve. +**Exit:** a boot whose `/system` will not serve says why, and ends, on a +channel an operator reads on every machine — the UART where there is one, the +panel and the black box — shown by a boot staged with a `/system` that does +not serve. diff --git a/lib/logstream/src/lib.rs b/lib/logstream/src/lib.rs index 567b165b0b8..ae2115ce067 100644 --- a/lib/logstream/src/lib.rs +++ b/lib/logstream/src/lib.rs @@ -69,7 +69,7 @@ pub const WIRE_HOLDER: &str = "uart"; /// The endowment label of the write end of the console's input: the pipe the /// supervisor makes, whose read end is every program's standard input, and -/// hands [`WIRE_HOLDER`] alone to write what the UART receives into. +/// hands the row that holds it to write what the UART receives into. pub const CONSOLE_INPUT: &str = "console-input"; /// What the UART says as the carrier's channel takes the log off it: every @@ -93,14 +93,15 @@ pub const CARRY: u32 = 8; /// channel has taken, counted from the pipe's first byte, as a `u64` payload. pub const DELIVERED: u32 = 9; -/// The manifest's name for the program that is the log: the supervisor endows it the -/// [`ORIGINS`] acceptor, and gives it a ring like every other program's, which -/// it reads like every other. +/// The manifest's name for the program that is the log: the one row that may +/// hold [`ORIGINS`], and given a ring like every other program's, which it +/// reads like every other. pub const LOGKEEPER: &str = "logkeeper"; -/// The endowment label of the acceptor the supervisor hands `logkeeper` for its frames. -/// **Named by no manifest row**, so the one connector to it is the supervisor's own and -/// no program can register a ring under a name of its choosing. +/// The endowment label of the acceptor the supervisor hands the row that holds it, +/// `logkeeper`, for its frames. **No row receives its connector**, so the one +/// connector to it is the supervisor's own and no program can register a ring +/// under a name of its choosing. pub const ORIGINS: &str = "log-origins"; /// The endowment label of a program's waker of the bell [`REGISTER`] hands diff --git a/lib/manifest/Cargo.toml b/lib/manifest/Cargo.toml index 13315eee1c3..91ff15f019e 100644 --- a/lib/manifest/Cargo.toml +++ b/lib/manifest/Cargo.toml @@ -10,5 +10,7 @@ license = "MIT OR Apache-2.0" # would be a config the build system accepts and the supervisor cannot act on. [dependencies] toyos-abi = { path = "../../sdk/abi" } +# The names of the log's and the console's objects a row may hold. +toyos-logstream = { path = "../logstream" } # `toyos::launch::is_canonical`, the one rule a launch path is held to. toyos = { path = "../../sdk/toyos" } diff --git a/lib/manifest/src/lib.rs b/lib/manifest/src/lib.rs index 1922794704e..5ef727a03b8 100644 --- a/lib/manifest/src/lib.rs +++ b/lib/manifest/src/lib.rs @@ -23,6 +23,7 @@ //! drive one process of it per PCI function the matcher names //! syscap a right on the SysCap dup the supervisor endows //! slots the idle slot's partitions and the slot table +//! hold one of the objects the supervisor makes once and endows its one holder ([`HOLDINGS`]) //! authorize a disk authorization the supervisor mints at its start ([`authorize`]) //! service a system service: its `HOME` is `/state/`, not the session's //! state it keeps machine state of its own in `/state/` @@ -323,6 +324,10 @@ pub struct Program { /// runs is never among them. One program holds it — `build/build.rs` gates /// which. pub slots: bool, + /// Objects the supervisor makes once, for the whole machine, and endows + /// this row under their names ([`HOLDINGS`]): the signed image's word + /// alone, and `build/build.rs` gates which rows. + pub holds: Vec, /// Disk authorizations the supervisor mints at its start, by /// [`authorize::Authorization::word`]: the signed image's word alone, and /// `build/build.rs` gates which rows. @@ -503,6 +508,8 @@ pub enum RenderError { NoSuchRole { program: String, role: String }, /// An `authorize` entry naming no [`authorize::Authorization`]. NoSuchAuthorization { program: String, word: String }, + /// A `holds` entry naming none of [`HOLDINGS`]. + NoSuchHolding { program: String, name: String }, /// A `devices` entry that names no device, or a `drives` entry that is /// no PCI matcher. NoSuchDevice { program: String, entry: String }, @@ -520,6 +527,11 @@ pub enum RenderError { FirmwareWithoutDrives(String), } +/// What a row's `holds` may name: the acceptor every program's log ring +/// reaches `logkeeper` on, and the write end of the console's input, whose +/// read end is every program's standard input. +pub const HOLDINGS: &[&str] = &[toyos_logstream::ORIGINS, toyos_logstream::CONSOLE_INPUT]; + /// The firmware window a row's `firmware` entry names. pub fn firmware_window(word: &str) -> Option { match word { @@ -583,6 +595,12 @@ pub fn render(manifest: &Manifest) -> Result, RenderError> { if program.slots { out.push_str("slots\n"); } + for name in &program.holds { + if !HOLDINGS.contains(&name.as_str()) { + return Err(RenderError::NoSuchHolding { program: program.name.clone(), name: name.clone() }); + } + out.push_str(&format!("hold {name}\n")); + } for word in &program.authorize { if authorize::Authorization::parse(word).is_none() { return Err(RenderError::NoSuchAuthorization { program: program.name.clone(), word: word.clone() }); @@ -774,6 +792,7 @@ pub fn parse(text: &str) -> Manifest { "firmware" if firmware_window(rest).is_some() => program.firmware.push(rest.to_string()), "syscap" => program.syscap.push(rest.to_string()), "slots" if rest.is_empty() => program.slots = true, + "hold" if HOLDINGS.contains(&rest) => program.holds.push(rest.to_string()), "authorize" if authorize::Authorization::parse(rest).is_some() => program.authorize.push(rest.to_string()), "service" => program.service = true, "state" if rest.is_empty() => program.state = true, @@ -822,6 +841,7 @@ mod tests { name: "update".into(), source: Source::Path("/system/bin/update".into()), slots: true, + holds: vec![toyos_logstream::CONSOLE_INPUT.into()], state: true, authorize: vec!["tables".into(), "designate:data".into()], ..Program::default() diff --git a/system/logkeeper/src/main.rs b/system/logkeeper/src/main.rs index b8f866095bd..2e3c696183b 100644 --- a/system/logkeeper/src/main.rs +++ b/system/logkeeper/src/main.rs @@ -52,7 +52,8 @@ //! One `SysCap` duplicate carrying `Rights::LOG | Rights::WAIT`, which its //! manifest row asks for by the name `logread`, and with which it tells the //! kernel what the console's channels have delivered; the origins acceptor -//! the supervisor endows it; and what its row adds — the `log` acceptor, where +//! the supervisor endows the row that holds it (`holds = ["log-origins"]`); +//! and what its row adds — the `log` acceptor, where //! this machine's readers ask for the log and `inspect` asks where it is going //! ([`inspect`]'s module, which grants nothing), and the `log-carrier` and //! `log-wire` acceptors, where the console's carriers ask. It claims no device diff --git a/system/supervisor/src/main.rs b/system/supervisor/src/main.rs index a960c6c288e..6acd087e17e 100644 --- a/system/supervisor/src/main.rs +++ b/system/supervisor/src/main.rs @@ -140,8 +140,7 @@ use toyos::AsHandle; use toyos_abi::Rights; use toyos_blockring::wire::{Grant as BlockGrant, Scope}; use toyos_logstream::{ - Registration, Tag, ALIVE, CONSOLE_INPUT, FLUSH, FLUSHED, LOGKEEPER, MAX_TAG, ORIGINS, REGISTER, RESUME, STOPPING, - WIRE_HOLDER, + Registration, Tag, ALIVE, CONSOLE_INPUT, FLUSH, FLUSHED, MAX_TAG, ORIGINS, REGISTER, RESUME, STOPPING, }; use toyos_abi::inventory::{Arrival, Pci, PciAddr, Record}; use toyos_abi::syscall::{ @@ -297,10 +296,11 @@ const FLUSH_BOUND: Duration = Duration::from_millis(toyos_quiesce::FLUSH_MS); /// refusal is therefore a broken invariant and ends the supervisor loudly. struct Log { conn: Connection, - /// The acceptor end, until `logkeeper` is started holding it. + /// The acceptor end, until the row that holds the log's origins is + /// started holding it. acceptor: Option, /// The console's input as every program gets it for its stdin: the read - /// end of the pipe whose write end the console UART's driver is handed. + /// end of the pipe whose write end the row that holds it is handed. stdin: toyos::OwnedHandle, /// That write end, kept so a driver started again is handed it again. console_input: toyos::Pipe, @@ -3200,13 +3200,14 @@ fn start<'a>( // Where the program's output goes. // // A service the supervisor starts gets a ring of its own as its stdout and stderr - // and the console's input to read as its stdin; `logkeeper` also gets the acceptor - // the rings reach it on, and the console UART's driver the input's write end. A + // and the console's input to read as its stdin; the row that holds the + // log's origins also gets the acceptor the rings reach it on, and the one + // that holds the console's input that pipe's write end. A // launch keeps the descriptors its caller sent, but for a ring among them // — the caller's own log — which is replaced by the program's // ([`LaunchFd::Ring`]), and a stdio descriptor it sent nothing for is a // null, never this process's own. - let mut starts_logkeeper = false; + let mut endows_origins = false; let (log, ring) = match output { Output::Boot { log, job } => { // **Every service the supervisor starts is a job of its own**, @@ -3216,18 +3217,18 @@ fn start<'a>( command.inherit_handle(0, log.stdin.as_handle().into()); command.inherit_handle(1, ring.as_handle().into()); command.inherit_handle(2, ring.as_handle().into()); - if program.name == LOGKEEPER { + if program.holds.iter().any(|held| held == ORIGINS) { let Some(acceptor) = &log.acceptor else { - panic!("supervisor: `{LOGKEEPER}` is started twice, and the log's origins are the first's"); + panic!("supervisor: `{}` holds `{ORIGINS}`, which went to a start before it", program.name); }; let origins = acceptor .as_handle() .try_clone_to_owned() .map_err(|e| std::io::Error::other(format!("the log's origins did not duplicate: {e:?}")))?; command.endow(ORIGINS, origins.into()); - starts_logkeeper = true; + endows_origins = true; } - if program.name == WIRE_HOLDER { + if program.holds.iter().any(|held| held == CONSOLE_INPUT) { let input = log .console_input .as_handle() @@ -3272,7 +3273,7 @@ fn start<'a>( acceptors.remove(name.as_str()); } } - if starts_logkeeper { + if endows_origins { log.acceptor = None; } if let (Some(ring), Some(alive)) = (ring, alive) { diff --git a/system/uart/src/main.rs b/system/uart/src/main.rs index 50a7a1c3679..d174a9d969d 100644 --- a/system/uart/src/main.rs +++ b/system/uart/src/main.rs @@ -43,7 +43,9 @@ fn main() { let (info, frame) = claim.open().unwrap_or_else(|e| panic!("uart: the claim's description: {e:?}")); let level = info.level != 0; let mut uart = Machine::open(&info, frame).unwrap_or_else(|| panic!("uart: a UART this program does not know: {info:?}")); - let input: Option = endow::Endowments::get().take(CONSOLE_INPUT); + let input: Pipe = endow::Endowments::get() + .take(CONSOLE_INPUT) + .unwrap_or_else(|| panic!("uart: started without `{CONSOLE_INPUT}`, which its row holds")); let mut log = match logkeeper_api::carry(WIRE) { Ok((conn, served)) => { say!("uart: carrying the log on the console UART, from where the kernel left it"); @@ -64,10 +66,8 @@ fn main() { let mut buf = vec![0u8; READ]; loop { let standing = line.serve(&mut uart, &mut received); - if let Some(input) = &input - && !received.is_empty() - { - match hand_on(input, &received) { + if !received.is_empty() { + match hand_on(&input, &received) { 0 if dropped > 0 => { say!("uart: the console's input takes bytes again, after {dropped} were dropped"); dropped = 0; diff --git a/tests/images/acpicase.toml b/tests/images/acpicase.toml index c8a195332d4..d0b4f24bf01 100644 --- a/tests/images/acpicase.toml +++ b/tests/images/acpicase.toml @@ -13,9 +13,11 @@ start = ["logkeeper", "uart", "test-runner"] service = true syscap = ["logread"] serves = ["log-wire"] +holds = ["log-origins"] [programs.uart] devices = ["uart"] +holds = ["console-input"] receives = ["log-wire"] restart = true diff --git a/tests/images/benchcase.toml b/tests/images/benchcase.toml index b3d4f760ad9..354b3a66260 100644 --- a/tests/images/benchcase.toml +++ b/tests/images/benchcase.toml @@ -24,9 +24,11 @@ start = ["logkeeper", "uart", "diskserver", "fileserver", "acpiserver", "netstac service = true syscap = ["logread"] serves = ["log", "log-carrier", "log-wire"] +holds = ["log-origins"] [programs.uart] devices = ["uart"] +holds = ["console-input"] receives = ["log-wire"] restart = true diff --git a/tests/images/clipboardcase.toml b/tests/images/clipboardcase.toml index a6b443371ac..46b10e41b51 100644 --- a/tests/images/clipboardcase.toml +++ b/tests/images/clipboardcase.toml @@ -20,9 +20,11 @@ start = ["acpiserver", "logkeeper", "uart", "compositor", "vdagent", "test-runne service = true syscap = ["logread"] serves = ["log-carrier", "log-wire"] +holds = ["log-origins"] [programs.uart] devices = ["uart"] +holds = ["console-input"] receives = ["log-wire"] restart = true diff --git a/tests/images/designatecase.toml b/tests/images/designatecase.toml index 9d1c039c3ca..99ba82a03cb 100644 --- a/tests/images/designatecase.toml +++ b/tests/images/designatecase.toml @@ -11,9 +11,11 @@ start = ["logkeeper", "uart", "diskserver", "fileserver", "acpiserver", "test-ru service = true syscap = ["logread"] serves = ["log", "log-wire"] +holds = ["log-origins"] [programs.uart] devices = ["uart"] +holds = ["console-input"] receives = ["log-wire"] restart = true diff --git a/tests/images/drivescase.toml b/tests/images/drivescase.toml index ece9ba79dc0..8f2bdc023c0 100644 --- a/tests/images/drivescase.toml +++ b/tests/images/drivescase.toml @@ -10,9 +10,11 @@ start = ["acpiserver", "logkeeper", "uart", "test-runner"] service = true syscap = ["logread"] serves = ["log-carrier", "log-wire"] +holds = ["log-origins"] [programs.uart] devices = ["uart"] +holds = ["console-input"] receives = ["log-wire"] restart = true diff --git a/tests/images/floorcase.toml b/tests/images/floorcase.toml index cd6c007c7f4..03542d5d604 100644 --- a/tests/images/floorcase.toml +++ b/tests/images/floorcase.toml @@ -24,9 +24,11 @@ service = true syscap = ["logread"] serves = ["log", "log-wire"] memory-floor-mib = 32 +holds = ["log-origins"] [programs.uart] devices = ["uart"] +holds = ["console-input"] receives = ["log-wire"] restart = true diff --git a/tests/images/hostedclangcase.toml b/tests/images/hostedclangcase.toml index c9198fdaed7..cd0f80053cb 100644 --- a/tests/images/hostedclangcase.toml +++ b/tests/images/hostedclangcase.toml @@ -8,9 +8,11 @@ start = ["acpiserver", "logkeeper", "uart", "diskserver", "fileserver", "test-ru service = true syscap = ["logread"] serves = ["log-carrier", "log-wire"] +holds = ["log-origins"] [programs.uart] devices = ["uart"] +holds = ["console-input"] receives = ["log-wire"] restart = true diff --git a/tests/images/hotplugcase.toml b/tests/images/hotplugcase.toml index f27633dba50..deec0a3f851 100644 --- a/tests/images/hotplugcase.toml +++ b/tests/images/hotplugcase.toml @@ -11,9 +11,11 @@ start = ["logkeeper", "uart", "acpiserver", "test-runner"] service = true syscap = ["logread"] serves = ["log-carrier", "log-wire"] +holds = ["log-origins"] [programs.uart] devices = ["uart"] +holds = ["console-input"] receives = ["log-wire"] restart = true diff --git a/tests/images/jobcase.toml b/tests/images/jobcase.toml index dfe401ca192..cf83ea84602 100644 --- a/tests/images/jobcase.toml +++ b/tests/images/jobcase.toml @@ -8,9 +8,11 @@ start = ["acpiserver", "logkeeper", "uart", "diskserver", "fileserver", "test-ru service = true syscap = ["logread"] serves = ["log-carrier", "log-wire"] +holds = ["log-origins"] [programs.uart] devices = ["uart"] +holds = ["console-input"] receives = ["log-wire"] restart = true diff --git a/tests/images/latencycase.toml b/tests/images/latencycase.toml index 36576ded04b..3e98a3218e8 100644 --- a/tests/images/latencycase.toml +++ b/tests/images/latencycase.toml @@ -17,9 +17,11 @@ start = ["logkeeper", "uart", "diskserver", "fileserver", "acpiserver", "test-ru service = true syscap = ["logread"] serves = ["log-wire"] +holds = ["log-origins"] [programs.uart] devices = ["uart"] +holds = ["console-input"] receives = ["log-wire"] restart = true diff --git a/tests/images/launchcase.toml b/tests/images/launchcase.toml index 3b9fa633096..c4e14beb13c 100644 --- a/tests/images/launchcase.toml +++ b/tests/images/launchcase.toml @@ -9,9 +9,11 @@ start = ["acpiserver", "logkeeper", "uart", "test-runner"] service = true syscap = ["logread"] serves = ["log-carrier", "log-wire"] +holds = ["log-origins"] [programs.uart] devices = ["uart"] +holds = ["console-input"] receives = ["log-wire"] restart = true diff --git a/tests/images/libccase.toml b/tests/images/libccase.toml index 01757f427e7..215cbfb61a7 100644 --- a/tests/images/libccase.toml +++ b/tests/images/libccase.toml @@ -8,9 +8,11 @@ start = ["acpiserver", "logkeeper", "uart", "test-runner"] service = true syscap = ["logread"] serves = ["log-carrier", "log-wire"] +holds = ["log-origins"] [programs.uart] devices = ["uart"] +holds = ["console-input"] receives = ["log-wire"] restart = true diff --git a/tests/images/linkcase.toml b/tests/images/linkcase.toml index 99f87ba9bcd..3abffb050f4 100644 --- a/tests/images/linkcase.toml +++ b/tests/images/linkcase.toml @@ -10,9 +10,11 @@ start = ["acpiserver", "logkeeper", "uart", "diskserver", "fileserver", "netstac service = true syscap = ["logread"] serves = ["log", "log-carrier", "log-wire"] +holds = ["log-origins"] [programs.uart] devices = ["uart"] +holds = ["console-input"] receives = ["log-wire"] restart = true diff --git a/tests/images/metalcase.toml b/tests/images/metalcase.toml index 56c7fc3430c..316672185cb 100644 --- a/tests/images/metalcase.toml +++ b/tests/images/metalcase.toml @@ -23,9 +23,11 @@ start = ["acpiserver", "logkeeper", "uart", "diskserver", "fileserver", "composi service = true syscap = ["logread"] serves = ["log", "log-wire"] +holds = ["log-origins"] [programs.uart] devices = ["uart"] +holds = ["console-input"] receives = ["log-wire"] restart = true diff --git a/tests/images/metaldevicecase.toml b/tests/images/metaldevicecase.toml index 5cff3705b8e..785734db1db 100644 --- a/tests/images/metaldevicecase.toml +++ b/tests/images/metaldevicecase.toml @@ -18,9 +18,11 @@ start = ["acpiserver", "logkeeper", "uart", "diskserver", "fileserver", "soundse service = true syscap = ["logread"] serves = ["log-wire"] +holds = ["log-origins"] [programs.uart] devices = ["uart"] +holds = ["console-input"] receives = ["log-wire"] restart = true diff --git a/tests/images/netcase.toml b/tests/images/netcase.toml index 388c12b4087..6734fa40a6a 100644 --- a/tests/images/netcase.toml +++ b/tests/images/netcase.toml @@ -19,9 +19,11 @@ start = ["acpiserver", "logkeeper", "uart", "diskserver", "fileserver", "netstac service = true syscap = ["logread"] serves = ["log-carrier", "log-wire"] +holds = ["log-origins"] [programs.uart] devices = ["uart"] +holds = ["console-input"] receives = ["log-wire"] restart = true diff --git a/tests/images/panelcase.toml b/tests/images/panelcase.toml index 066e5d21d14..42eb7b2b88b 100644 --- a/tests/images/panelcase.toml +++ b/tests/images/panelcase.toml @@ -14,9 +14,11 @@ start = ["acpiserver", "logkeeper", "uart", "compositor"] service = true syscap = ["logread"] serves = ["log-carrier", "log-wire"] +holds = ["log-origins"] [programs.uart] devices = ["uart"] +holds = ["console-input"] receives = ["log-wire"] restart = true diff --git a/tests/images/proctreecase.toml b/tests/images/proctreecase.toml index 33fbf47b2cb..78ed7a762f3 100644 --- a/tests/images/proctreecase.toml +++ b/tests/images/proctreecase.toml @@ -28,9 +28,11 @@ folder = "read-write" service = true syscap = ["logread"] serves = ["log-carrier", "log-wire"] +holds = ["log-origins"] [programs.uart] devices = ["uart"] +holds = ["console-input"] receives = ["log-wire"] restart = true diff --git a/tests/images/scalecase.toml b/tests/images/scalecase.toml index 1ff7a11b573..752ebfb9093 100644 --- a/tests/images/scalecase.toml +++ b/tests/images/scalecase.toml @@ -16,9 +16,11 @@ start = ["acpiserver", "logkeeper", "uart", "compositor", "ps2server", "test-run service = true syscap = ["logread"] serves = ["log-carrier", "log-wire"] +holds = ["log-origins"] [programs.uart] devices = ["uart"] +holds = ["console-input"] receives = ["log-wire"] restart = true diff --git a/tests/images/sessioncase.toml b/tests/images/sessioncase.toml index 84b03213694..3de418e1f56 100644 --- a/tests/images/sessioncase.toml +++ b/tests/images/sessioncase.toml @@ -23,9 +23,11 @@ start = ["acpiserver", "logkeeper", "uart", "compositor", "ps2server", "test-run service = true syscap = ["logread"] serves = ["log-carrier", "log-wire"] +holds = ["log-origins"] [programs.uart] devices = ["uart"] +holds = ["console-input"] receives = ["log-wire"] restart = true diff --git a/tests/images/testcases.toml b/tests/images/testcases.toml index 8291ab52679..e30b63335d2 100644 --- a/tests/images/testcases.toml +++ b/tests/images/testcases.toml @@ -14,9 +14,11 @@ start = ["logkeeper", "uart", "diskserver", "fileserver", "soundserver", "acpise service = true syscap = ["logread"] serves = ["log", "log-carrier", "log-wire"] +holds = ["log-origins"] [programs.uart] devices = ["uart"] +holds = ["console-input"] receives = ["log-wire"] restart = true diff --git a/tests/images/undeclaredcase.toml b/tests/images/undeclaredcase.toml index 301e8c1003e..bc2de016a7a 100644 --- a/tests/images/undeclaredcase.toml +++ b/tests/images/undeclaredcase.toml @@ -8,9 +8,11 @@ start = ["logkeeper", "uart", "test-runner"] service = true syscap = ["logread"] serves = ["log-wire"] +holds = ["log-origins"] [programs.uart] devices = ["uart"] +holds = ["console-input"] receives = ["log-wire"] restart = true diff --git a/tests/images/virtclaimcase.toml b/tests/images/virtclaimcase.toml index a9b3e8332af..dfb9c91b165 100644 --- a/tests/images/virtclaimcase.toml +++ b/tests/images/virtclaimcase.toml @@ -10,9 +10,11 @@ start = ["acpiserver", "logkeeper", "uart", "test-runner"] service = true syscap = ["logread"] serves = ["log-wire"] +holds = ["log-origins"] [programs.uart] devices = ["uart"] +holds = ["console-input"] receives = ["log-wire"] restart = true diff --git a/tests/images/virtjobcase.toml b/tests/images/virtjobcase.toml index db81338d932..a3b7d34b6b2 100644 --- a/tests/images/virtjobcase.toml +++ b/tests/images/virtjobcase.toml @@ -10,9 +10,11 @@ start = ["logkeeper", "uart", "test-runner"] service = true syscap = ["logread"] serves = ["log-wire"] +holds = ["log-origins"] [programs.uart] devices = ["uart"] +holds = ["console-input"] receives = ["log-wire"] restart = true diff --git a/tests/images/virtpancase.toml b/tests/images/virtpancase.toml index a9c6c4dab50..6f565d876ce 100644 --- a/tests/images/virtpancase.toml +++ b/tests/images/virtpancase.toml @@ -7,9 +7,11 @@ start = ["logkeeper", "uart", "test-runner"] service = true syscap = ["logread"] serves = ["log-wire"] +holds = ["log-origins"] [programs.uart] devices = ["uart"] +holds = ["console-input"] receives = ["log-wire"] restart = true diff --git a/tests/images/virtpaniccase.toml b/tests/images/virtpaniccase.toml index a8155e5306e..aab1d830c38 100644 --- a/tests/images/virtpaniccase.toml +++ b/tests/images/virtpaniccase.toml @@ -8,9 +8,11 @@ start = ["logkeeper", "uart", "test-runner"] service = true syscap = ["logread"] serves = ["log-wire"] +holds = ["log-origins"] [programs.uart] devices = ["uart"] +holds = ["console-input"] receives = ["log-wire"] restart = true diff --git a/tests/images/virtrebootcase.toml b/tests/images/virtrebootcase.toml index fd3fbf3a9f8..46f59022218 100644 --- a/tests/images/virtrebootcase.toml +++ b/tests/images/virtrebootcase.toml @@ -6,9 +6,11 @@ start = ["logkeeper", "uart", "test-runner"] service = true syscap = ["logread"] serves = ["log-wire"] +holds = ["log-origins"] [programs.uart] devices = ["uart"] +holds = ["console-input"] receives = ["log-wire"] restart = true diff --git a/tests/images/virtsmpcase.toml b/tests/images/virtsmpcase.toml index 85d8d90f6ba..8b466411b18 100644 --- a/tests/images/virtsmpcase.toml +++ b/tests/images/virtsmpcase.toml @@ -12,9 +12,11 @@ start = ["logkeeper", "uart", "test-runner"] service = true syscap = ["logread"] serves = ["log-wire"] +holds = ["log-origins"] [programs.uart] devices = ["uart"] +holds = ["console-input"] receives = ["log-wire"] restart = true From 8678b19c7ddf42130097a419d526b24fd9092832 Mon Sep 17 00:00:00 2001 From: japabu Date: Wed, 4 Nov 2026 08:38:28 +0100 Subject: [PATCH 5/6] review r1 of #1165: the console's mark is exact, and the supervisor's end says why What the console has spoken was a stamp, compared across shards: a round logkeeper delivered could carry a record stamped after one another CPU had stamped and not yet committed, and every fatal path then left that record off the UART. It is now a read position per shard. The kernel's drain position is the mark: the inline drain moves it, SYS_LOG_SPOKEN moves it to at least the positions logkeeper's read reached through the rounds a channel delivered whole (taking a cursor, as SYS_LOG_READ does), and the stop and the fatal paths write every record past it. logkeeper holds a round's positions back to each shard's first record still waiting for a later round, so a round speaks only for what it wrote, and a channel handed a gap in the replay speaks for nothing after it. Where the UART's carrier starts is decided by position too: every line of the rounds before the hand-over but the kernel records the kernel put on the UART itself. SYS_LOG_SPOKEN needs a new right, Rights::CONSOLE (the `console` syscap), which the build gives logkeeper alone, since what it moves is kept off every fatal path's output; a log reader can no longer mute it. A UART carrier started again resumes where its channel left off, or passed over while the port carried the log, rather than at the hand-over, which replayed up to the whole replay at the UART's rate after every restart. The hand-over takes the boot drains' claim on the wire and never gives it back, so a drain still walking on another CPU finishes first and none starts after: one writer by construction. The supervisor's end is the machine's: the kernel ends the machine on it through the panic path, naming how it ended. Before it does, the supervisor's panic hook says the panic's message as its last words (SYS_LOG_LAST_WORDS, one alert record, the boot's first call alone, gated by Rights::LOG, one line of at most 512 bytes of text with no control character, validated at the boundary). The fatal path puts both on the UART, the panel and the black box, so a supervisor that fails before logkeeper runs says why again. The test harness can stage a ROOT without one of its files (Plan::root_omits), and a new QEMU row boots a /system without its release file and finds the reason on the UART before the kernel's panic. Also: AArch64 routes every SPI to the boot CPU, as the comments said, not to whichever CPU made the claim; the SPCR's Interrupt Type is read, and a UART whose interrupt is not a GIC's gets no interrupt row. On x86 COM1 stays the kernel's, Kept, where no I/O APIC carries its line and no row is filled. A thread spawn whose process has lost its image is an invariant violation, not a silent None. The thread table's forget contract names the detached leave. The guard-page record's hold on K6 is lifted. Co-Authored-By: Claude Opus 5.5 --- build/build.rs | 33 ++++- images/console.toml | 2 +- images/diag.toml | 2 +- images/system.toml | 2 +- ...-before-logkeeper-runs-says-why-nowhere.md | 38 ----- ...-threads-kernel-stack-has-no-guard-page.md | 6 +- ...oses-what-its-carrier-had-not-delivered.md | 5 +- kernel/proclife/src/table.rs | 5 +- kernel/src/arch/aarch64/console_uart.rs | 7 +- kernel/src/arch/aarch64/irqchip.rs | 11 +- kernel/src/arch/x86_64/pio.rs | 6 +- kernel/src/loader/mod.rs | 3 +- kernel/src/log/console.rs | 132 ++++++++---------- kernel/src/log/read.rs | 10 ++ kernel/src/process.rs | 10 +- kernel/src/syscall/dispatch.rs | 12 +- kernel/src/syscall/machine.rs | 35 ++++- lib/acpi/src/lib.rs | 2 +- lib/acpi/src/spcr.rs | 8 ++ lib/manifest/src/lib.rs | 5 + sdk/abi/src/handle.rs | 12 +- sdk/abi/src/log.rs | 50 +++++++ sdk/abi/src/syscall.rs | 40 ++++-- sdk/toyos/src/log/mod.rs | 30 +++- system/logkeeper/src/carrier.rs | 76 +++++++--- system/logkeeper/src/main.rs | 107 +++++++++++--- system/logkeeper/src/serve.rs | 128 ++++++++++------- system/supervisor/src/main.rs | 32 ++++- tests/common/qemu.rs | 11 +- tests/images/acpicase.toml | 2 +- tests/images/benchcase.toml | 2 +- tests/images/clipboardcase.toml | 2 +- tests/images/designatecase.toml | 2 +- tests/images/drivescase.toml | 2 +- tests/images/floorcase.toml | 2 +- tests/images/hostedclangcase.toml | 2 +- tests/images/hotplugcase.toml | 2 +- tests/images/jobcase.toml | 2 +- tests/images/latencycase.toml | 2 +- tests/images/launchcase.toml | 2 +- tests/images/libccase.toml | 2 +- tests/images/linkcase.toml | 2 +- tests/images/metalcase.toml | 2 +- tests/images/metaldevicecase.toml | 2 +- tests/images/netcase.toml | 2 +- tests/images/panelcase.toml | 2 +- tests/images/proctreecase.toml | 2 +- tests/images/scalecase.toml | 2 +- tests/images/sessioncase.toml | 2 +- tests/images/testcases.toml | 2 +- tests/images/undeclaredcase.toml | 2 +- tests/images/virtclaimcase.toml | 2 +- tests/images/virtjobcase.toml | 2 +- tests/images/virtpancase.toml | 2 +- tests/images/virtpaniccase.toml | 2 +- tests/images/virtrebootcase.toml | 2 +- tests/images/virtsmpcase.toml | 2 +- tests/toyos.rs | 37 +++++ 58 files changed, 622 insertions(+), 291 deletions(-) delete mode 100644 issues/a-supervisor-that-fails-before-logkeeper-runs-says-why-nowhere.md diff --git a/build/build.rs b/build/build.rs index fa094e23a1d..f48d49bee40 100644 --- a/build/build.rs +++ b/build/build.rs @@ -716,8 +716,10 @@ fn named(rows: &[&str]) -> String { /// Who may hold the authorities that change what the machine runs or what a /// package sees: each of [`HELD_ALONE`]'s ports, its holder and nothing else /// — no other `[programs]` row and never `[apps]` — each of [`HOLDINGS`], its -/// one row, the idle slot, [`toyos_update::slots::HOLDER`] alone, and the -/// consent port, which no row receives and its server alone serves. +/// one row, the `console` syscap, which keeps the kernel's records off a fatal +/// path's output, `logkeeper` alone, the idle slot, +/// [`toyos_update::slots::HOLDER`] alone, and the consent port, which no row +/// receives and its server alone serves. /// /// **Checked on every manifest rendered, not only on the committed configs**, /// because the swap port's holder can replace any service's binary, the @@ -743,6 +745,9 @@ fn held_by_their_holders_alone(config: &SystemConfig) -> Result<(), String> { return Err(format!("`{name}` holds `{held}`, which only `{holder}` may hold")); } } + if name != toyos_logstream::LOGKEEPER && program.syscap.iter().any(|s| s == "console") { + return Err(format!("`{name}` asks for the `console` syscap, which only `{}` may hold", toyos_logstream::LOGKEEPER)); + } if name != toyos_update::slots::HOLDER && program.slots { return Err(format!("`{name}` asks for `slots`, which only `{}` may hold", toyos_update::slots::HOLDER)); } @@ -1009,11 +1014,13 @@ pub fn trust_roots() -> Vec { roots.map(|cert| pem::encode_config(&pem::Pem::new("CERTIFICATE", cert.to_vec()), config)).collect::().into_bytes() } +#[allow(clippy::too_many_arguments)] fn build_and_assemble( root: &Path, config: &SystemConfig, env: &GuestEnv, extra_files: &[(String, Vec)], + omits: &[String], quiet: bool, arch: Arch, machines: crate::microcode::Machines, @@ -1057,6 +1064,12 @@ fn build_and_assemble( for (name, data) in extra_files { root_files.push((name.clone(), data.clone())); } + for omitted in omits { + let Some(at) = root_files.iter().position(|(name, _)| name == omitted) else { + panic!("root: '{omitted}' is to be left out, and the image has no such file"); + }; + root_files.remove(at); + } let symlinks: Vec<(String, String)> = config.symlinks.iter() .map(|(k, v)| (k.clone(), v.clone())) @@ -1194,6 +1207,9 @@ pub struct Plan { pub machines: crate::microcode::Machines, /// The scale `cargo run --scale` starts the desktop at. pub scale: Option, + /// Files of the image's own ROOT a test boot is staged without, each by + /// its ROOT-relative path: a `/system` missing one. + pub root_omits: Vec, } impl Plan { @@ -1206,6 +1222,7 @@ impl Plan { version: image::version_now(), machines: crate::microcode::Machines::Qemu, scale: None, + root_omits: Vec::new(), } } } @@ -1243,6 +1260,7 @@ pub fn plan_for(root: &Path, boot: &Boot, debug: bool, args: &[String]) -> Plan crate::microcode::Machines::Qemu }, scale: flags::CARGO_RUN.value(args, &flags::SCALE).map(String::from), + root_omits: Vec::new(), } } @@ -2060,7 +2078,7 @@ fn shipped_parts(root: &Path, boot: &Boot, plan: &Plan) -> (Vec, Vec, Us ) }; - let userland = build_and_assemble(root, &config, &env, &[], false, arch, plan.machines); + let userland = build_and_assemble(root, &config, &env, &[], &plan.root_omits, false, arch, plan.machines); let bl_bytes = fs::read(&bl_art).expect("Failed to read staged bootloader"); (kernel_bytes, bl_bytes, userland) @@ -2163,6 +2181,7 @@ fn root_image_key(plan: &Plan, image_key: &str, extra_files: &[(String, Vec) plan.config.hash(&mut h); plan.arch.hash(&mut h); plan.machines.hash(&mut h); + plan.root_omits.hash(&mut h); image_key.hash(&mut h); for (name, data) in extra_files { name.hash(&mut h); @@ -2279,7 +2298,7 @@ pub fn build_test_parts( }; let userland = USERLAND.get_or_build(root_image_key, || { - build_and_assemble(root, &config, &env, extra_files, quiet, arch, plan.machines) + build_and_assemble(root, &config, &env, extra_files, &plan.root_omits, quiet, arch, plan.machines) }); drop(building); @@ -3237,6 +3256,10 @@ mod tests { let holds = program.syscap.iter().any(|s| s == "logread"); if name == "logkeeper" { assert!(holds, "{config}: `logkeeper` writes the file and must read the cursor"); + assert!( + program.syscap.iter().any(|s| s == "console"), + "{config}: `logkeeper` feeds the console and must say what it spoke" + ); continue; } assert!( @@ -3482,6 +3505,8 @@ mod tests { assert!(held_by_their_holders_alone(&origins).is_err()); let input: SystemConfig = toml::from_str("[programs.shell]\nholds = [\"console-input\"]\n").unwrap(); assert!(held_by_their_holders_alone(&input).is_err()); + let console: SystemConfig = toml::from_str("[programs.test-runner]\nsyscap = [\"logread\", \"console\"]\n").unwrap(); + assert!(held_by_their_holders_alone(&console).is_err()); let held: SystemConfig = toml::from_str( "[programs.logkeeper]\nholds = [\"log-origins\"]\n[programs.uart]\nholds = [\"console-input\"]\n", ) diff --git a/images/console.toml b/images/console.toml index b4385b31a50..aeef875eba8 100644 --- a/images/console.toml +++ b/images/console.toml @@ -33,7 +33,7 @@ start = ["logkeeper", "uart", "diskserver", "fileserver", "console", "acpiserver # Rights::WAIT` on a `SysCap` duplicate, and serves `log` to the console. [programs.logkeeper] service = true -syscap = ["logread"] +syscap = ["logread", "console"] serves = ["log", "log-wire"] holds = ["log-origins"] diff --git a/images/diag.toml b/images/diag.toml index 98e5f1b6543..3d73f3b9398 100644 --- a/images/diag.toml +++ b/images/diag.toml @@ -22,7 +22,7 @@ start = ["logkeeper", "fileserver", "toybox"] # program's output beside that. [programs.logkeeper] service = true -syscap = ["logread"] +syscap = ["logread", "console"] holds = ["log-origins"] [programs.toybox] diff --git a/images/system.toml b/images/system.toml index 2b37f60a0e7..eb54b09cee3 100644 --- a/images/system.toml +++ b/images/system.toml @@ -68,7 +68,7 @@ pkg = { state = true } # acceptor every program's ring reaches it on, which no other row may hold. [programs.logkeeper] service = true -syscap = ["logread"] +syscap = ["logread", "console"] serves = ["log", "log-carrier", "log-wire"] holds = ["log-origins"] diff --git a/issues/a-supervisor-that-fails-before-logkeeper-runs-says-why-nowhere.md b/issues/a-supervisor-that-fails-before-logkeeper-runs-says-why-nowhere.md deleted file mode 100644 index 5b484da8498..00000000000 --- a/issues/a-supervisor-that-fails-before-logkeeper-runs-says-why-nowhere.md +++ /dev/null @@ -1,38 +0,0 @@ ---- -status: open -kind: defect -opened: 2026-11-04 ---- - -# A supervisor that fails before logkeeper runs says why nowhere - -The supervisor's boot ends by name when `/system`'s file server does not -start, or `/system` cannot be read (`bootstrap_fails`, -`system/supervisor/src/main.rs`). Until the kernel's console thread was -deleted it wrote that line on the kernel console the kernel spawned it with, -which the thread put on the UART. That console is gone with the thread: the -UART is its userland driver's, which `logkeeper` feeds, and both are -programs on `/system`, which is what failed. The line goes to the -supervisor's own ring, which nothing reads, and the supervisor panics. - -What is left is the kernel's records: the supervisor's `exit:` and the file -server's, which no channel carries either, because the kernel's own writes -to the UART after the hand-over are the stop's and a fatal path's alone. A -boot armed with a deadline ends in its report, which puts them on the UART -and in the black box; a shipping image, armed with none, idles with nothing -said. Neither says why. - -Holding the kernel's inline drain past the hand-over would not carry the -line, which is not a kernel record, and would put every record's UART write -back on its producer — an interrupt handler or a lock holder — until the -driver runs. - -**Evidence:** the code; no test stages a broken `/system`. - -**Owner:** the supervisor's, with the kernel's fatal path; the shape is the -owner's to rule (`#1165`). - -**Exit:** a boot whose `/system` will not serve says why, and ends, on a -channel an operator reads on every machine — the UART where there is one, the -panel and the black box — shown by a boot staged with a `/system` that does -not serve. diff --git a/issues/a-threads-kernel-stack-has-no-guard-page.md b/issues/a-threads-kernel-stack-has-no-guard-page.md index fe707fc5a0e..c17b9577536 100644 --- a/issues/a-threads-kernel-stack-has-no-guard-page.md +++ b/issues/a-threads-kernel-stack-has-no-guard-page.md @@ -22,10 +22,8 @@ the first write past the end faults. naming that thread's guard page, and a stack allocated without the guard reds it. -**Held** until `issues/the-kernel-still-creates-threads.md`'s K6, which deletes -the kernel-thread machinery and so changes which kernel stacks exist (the -owner's order: a component's refactor before its small fixes). What the design -then has to meet: AArch64's vectors run on the interrupted `SP_EL1`, so a guard +Every kernel stack is now a user thread's or a CPU's idle loop's (#1165 +deleted the kernel threads). What the design has to meet: AArch64's vectors run on the interrupted `SP_EL1`, so a guard is reached only once an EL1 entry checks `sp` against the running stack's bound and reports from a per-CPU overflow stack, which the idle stacks' guard lacks today too; and a stack slot's guard hole in the direct map decides when diff --git a/issues/the-console-loses-what-its-carrier-had-not-delivered.md b/issues/the-console-loses-what-its-carrier-had-not-delivered.md index 34b3c83ae59..48b69bd1b51 100644 --- a/issues/the-console-loses-what-its-carrier-had-not-delivered.md +++ b/issues/the-console-loses-what-its-carrier-had-not-delivered.md @@ -16,8 +16,9 @@ lines can reach neither channel: fed from the moment the port's goes, not from what the port had not taken; - at the machine's stop, the program lines in the UART's pipe that the UART had not taken when userland stopped. The kernel's stop and every fatal path - put the kernel's own records on the UART from the newest stamp a channel - delivered (`log::console::spoken`), so no kernel record is lost this way. + put on the UART every kernel record past the read positions of the rounds + a channel delivered whole (`log::console`), so no kernel record is lost + this way. The same holds across logkeeper's own death: its carriers' pipes end, and the UART carries nothing until the kernel's stop or a fatal path. `/log` diff --git a/kernel/proclife/src/table.rs b/kernel/proclife/src/table.rs index d9adab353d9..637eb10e85d 100644 --- a/kernel/proclife/src/table.rs +++ b/kernel/proclife/src/table.rs @@ -40,8 +40,9 @@ pub trait Lifecycle { /// this decision has nothing left to say about. fn set_location(&mut self, tid: Tid, to: ThreadLocation); - /// Take `tid` out of the table. Only a join does this — a zombie is - /// collected by whoever was entitled to its code. + /// Take `tid` out of the table: a join collecting the zombie it was + /// entitled to the code of, or a detached thread leaving, whose code + /// nobody is. fn forget_thread(&mut self, tid: Tid); /// Every thread of this process, in whatever order the container has. diff --git a/kernel/src/arch/aarch64/console_uart.rs b/kernel/src/arch/aarch64/console_uart.rs index 23971fee575..952e91739b8 100644 --- a/kernel/src/arch/aarch64/console_uart.rs +++ b/kernel/src/arch/aarch64/console_uart.rs @@ -76,7 +76,10 @@ pub fn init(_: &mut Registers, rsdp_addr: u64) -> bool { return false; } BASE.store(spcr.base.address, Ordering::Relaxed); - GSIV.store(spcr.gsiv, Ordering::Relaxed); + // A UART whose interrupt the SPCR names for no GIC has none this kernel takes. + if spcr.interrupt_type & toyos_acpi::ARM_GIC != 0 { + GSIV.store(spcr.gsiv, Ordering::Relaxed); + } log!("serial: {kind} at {:#x} (SPCR, GSIV {})", spcr.base.address, spcr.gsiv); true } @@ -91,7 +94,7 @@ pub fn fill_row() { return; } if gsiv < 32 { - return log!("isa: no console UART row — the SPCR names its interrupt GSIV {gsiv}, which is no SPI"); + return log!("isa: no console UART row — the SPCR names no GIC interrupt or GSIV {gsiv}, which is no SPI"); } crate::isa::fill( UART_ROW, diff --git a/kernel/src/arch/aarch64/irqchip.rs b/kernel/src/arch/aarch64/irqchip.rs index 93d02aac662..33860fd6f0a 100644 --- a/kernel/src/arch/aarch64/irqchip.rs +++ b/kernel/src/arch/aarch64/irqchip.rs @@ -108,6 +108,8 @@ static TIMER_EDGE: AtomicBool = AtomicBool::new(false); static DISTRIBUTOR: AtomicU64 = AtomicU64::new(0); /// The IOMMU's event SPI; zero until [`route_iommu_events`]. static IOMMU_EVENTS: AtomicU32 = AtomicU32::new(0); +/// The boot CPU's packed affinity, from [`init`], which every SPI is routed to. +static BOOT_CPU: AtomicU32 = AtomicU32::new(0); /// Wait until `done`, for at most `1 / per_second` of a second counted at the /// rate firmware states: the boot has no calibrated clock yet. @@ -182,6 +184,7 @@ pub fn init(rsdp_addr: u64) -> Gic { let gicd = gicd.expect("GIC: the MADT names no distributor"); let distributor = crate::mm::paging::map_mmio(gicd, FRAME, MmioPolicy::Uncacheable); DISTRIBUTOR.store(gicd, Relaxed); + BOOT_CPU.store(cpu::hardware_id(), Relaxed); let revision = distributor.read_u32(GICD_PIDR2) >> 4 & 0xF; assert!(revision >= 3, "GIC: GICD_PIDR2.ArchRev is {revision}, and this kernel drives a GICv3 or later"); @@ -319,9 +322,9 @@ pub(super) fn route_iommu_events(intid: u32) -> Result<(), u32> { Ok(()) } -/// Route SPI `intid` to this CPU at [`PRIORITY`], level- or edge-triggered, -/// masked and with nothing pending; [`set_spi_masked`] lets it in. Refused -/// with the distributor's limit where it takes no such SPI. +/// Route SPI `intid` to the boot CPU at [`PRIORITY`], level- or +/// edge-triggered, masked and with nothing pending; [`set_spi_masked`] lets +/// it in. Refused with the distributor's limit where it takes no such SPI. pub(super) fn route_spi(intid: u32, level: bool) -> Result<(), u32> { let gicd = distributor(); let limit = (32 * ((gicd.read_u32(GICD_TYPER) & 0x1F) + 1)).min(1020); @@ -340,7 +343,7 @@ pub(super) fn route_spi(intid: u32, level: bool) -> Result<(), u32> { let edge = 0b10 << (2 * (intid % 16)); let configured = gicd.read_u32(config); gicd.write_u32(config, if level { configured & !edge } else { configured | edge }); - gicd.write_u64(GICD_IROUTER + 8 * u64::from(intid), toyos_gicv3::unpacked_affinity(cpu::hardware_id())); + gicd.write_u64(GICD_IROUTER + 8 * u64::from(intid), toyos_gicv3::unpacked_affinity(BOOT_CPU.load(Relaxed))); gicd.write_u32(GICD_ICPENDR + word, bit); Ok(()) } diff --git a/kernel/src/arch/x86_64/pio.rs b/kernel/src/arch/x86_64/pio.rs index 95a90310da5..be5fc3065da 100644 --- a/kernel/src/arch/x86_64/pio.rs +++ b/kernel/src/arch/x86_64/pio.rs @@ -245,13 +245,15 @@ pub const I8042_ROW: usize = 0; pub const UART_ROW: usize = 2; /// Fill the console UART's row, where the boot found one: COM1's ports and -/// its line, IRQ 4. Only its class claims it. +/// its line, IRQ 4. Only its class claims it. A UART with no line stays the +/// kernel's, which the stop and the fatal paths still write. pub fn fill_uart_row() { if !crate::drivers::serial::has_console() { return; } let Some(wire) = ioapic::gsi_for_isa_irq(4) else { - return log!("isa: no console UART row — no I/O APIC carries its line"); + declare("the kernel's console UART", COM1.ports(), Mediated::Kept).expect("pio: COM1 is declared once, here"); + return log!("isa: no console UART row — no I/O APIC carries its line, so COM1 stays the kernel's"); }; crate::isa::fill( UART_ROW, diff --git a/kernel/src/loader/mod.rs b/kernel/src/loader/mod.rs index 5b3511b9e89..b3e64337ccd 100644 --- a/kernel/src/loader/mod.rs +++ b/kernel/src/loader/mod.rs @@ -475,7 +475,8 @@ pub fn spawn_supervisor(images: crate::bootimage::Objects) -> Pid { .union(Rights::INVENTORY) .union(Rights::COUNTERS) .union(Rights::TRACE) - .union(Rights::PAGER); + .union(Rights::PAGER) + .union(Rights::CONSOLE); let cap_handle = handles .install(crate::object::HandleEntry::new(cap, rights)) .expect("spawn_supervisor: an empty table refused the system capability"); diff --git a/kernel/src/log/console.rs b/kernel/src/log/console.rs index 471f8ac1b65..5865d3b73be 100644 --- a/kernel/src/log/console.rs +++ b/kernel/src/log/console.rs @@ -6,41 +6,49 @@ //! end the UART is the holder's, which `logkeeper` feeds, and no record of //! the kernel's touches it. //! -//! **The console has spoken every record stamped at or before [`spoken`]**: -//! what the inline drain put on the UART, and from the hand-over what -//! `logkeeper` says the console's channels have delivered ([`speak`]). The -//! stop and a fatal path put every record stamped after it on the UART, so -//! they repeat at most what a channel delivered and had not said yet, and -//! lose none of the kernel's. +//! **The console has spoken every record before [`DRAINED`]'s position in +//! its ring**: what the inline drain put on the UART, and from the hand-over +//! what `logkeeper` says the console's channels have delivered, by the +//! positions its own read reached ([`speak`]). The stop and a fatal path put +//! every record past them on the UART, so they repeat at most what a channel +//! delivered and had not said yet, and miss none of the kernel's. //! //! **One writer at a time**: the boot's, whose drains take turns through a -//! claim on the wire; then the holder's; then the stop's, once userland is -//! stopped, or a fatal path's, once every other CPU is. A holder the stop did -//! not stop within its budget, which the stop's record names, may still write -//! over it. +//! claim on the wire, which the hand-over takes for good; then the holder's; +//! then the stop's, once userland is stopped, or a fatal path's, once every +//! other CPU is. A holder the stop did not stop within its budget, which the +//! stop's record names, may still write over it. -use core::sync::atomic::{AtomicBool, AtomicU64, Ordering}; +use core::sync::atomic::{AtomicBool, Ordering}; + +use toyos_abi::syscall::SyscallError; use toyos_abi::log::LogRecord; use crate::drivers::serial::{self, BackendGuard, PanicUart}; -use super::read::{drain_ordered, Published, RecordSink, Shards}; +use super::read::{drain_ordered, Cursor, Published, RecordSink, Shards}; +use crate::time::{Duration, Tripwire}; /// Whether the UART is its claim's holder's; set once, at [`hand_over`]. static HANDED: AtomicBool = AtomicBool::new(false); -/// One position for every drain context, so a record never reaches the wire -/// twice: moved by the inline drain, then by the stop and the fatal paths. +/// What the console has spoken, a position per ring: moved by the inline +/// drain, by [`speak`] for the console's channels, and by the stop and the +/// fatal paths, so a record never reaches the wire twice from the kernel. static DRAINED: Published = Published::new(); -/// The newest stamp the console has spoken: see the module's header. -static SPOKEN: AtomicU64 = AtomicU64::new(0); - /// A boot drain's claim on the wire, which a drain that finds it held leaves /// to its holder: that drain walks to the newest commit before letting go. +/// The hand-over takes it and never gives it back. static WIRE: AtomicBool = AtomicBool::new(false); +/// How long the hand-over waits for a boot drain on another CPU to end. +const DRAIN_HELD: Tripwire = Tripwire::absurd( + Duration::from_secs(60), + "a boot drain writes what the boot committed, and no boot commits a minute of the UART's rate", +); + /// Positions for each of the `cpus` CPUs' shards. pub fn size(cpus: usize) { DRAINED.size(cpus); @@ -66,39 +74,34 @@ pub fn drain_inline() { } /// Give the UART to its claim's holder, having spoken every record committed -/// so far: the boot's last inline drain. The boot CPU's, once, before -/// userland can claim the UART. +/// so far: the boot's last inline drain, under the wire's claim, which a +/// drain on another CPU finishes its walk with first, and which is never given +/// back. The boot CPU's, once, before userland can claim the UART. pub fn hand_over() { - drain_inline(); + let deadline = crate::clock::nanos_since_boot() + DRAIN_HELD.nanos(); + while WIRE.swap(true, Ordering::Acquire) { + assert!(crate::clock::nanos_since_boot() < deadline, "log: a boot drain held the UART past {DRAIN_HELD}"); + core::hint::spin_loop(); + } + if serial::has_console() { + drain_records(); + } HANDED.store(true, Ordering::Release); } -/// Every committed record onto the UART, through its registers a burst at a -/// time, and the newest spoken stamp moved past each. +/// Every record the console has not spoken onto the UART, through its +/// registers a burst at a time. fn drain_records() { - struct Wire; - impl RecordSink for Wire { - fn put(&mut self, record: &LogRecord) -> bool { - write_line(record, serial::write); - SPOKEN.fetch_max(record.at_ns, Ordering::Relaxed); - true - } - } - drain_ordered(&Shards, &mut &DRAINED, &mut Wire); -} - -/// The newest stamp the console has spoken. -pub fn spoken() -> u64 { - SPOKEN.load(Ordering::Relaxed) + drain_ordered(&Shards, &mut &DRAINED, &mut Spoken(serial::write)); } -/// The console's channels have delivered every record stamped at or before -/// `at_ns`, as `logkeeper` counts them; answers the newest spoken stamp, -/// which never moves back, nor past the clock: no record committed after the -/// word, a fatal path's among them, is kept off the UART by it. -pub fn speak(at_ns: u64) -> u64 { - let at_ns = at_ns.min(crate::clock::stamp().map_or(0, |now| now.nanos())); - SPOKEN.fetch_max(at_ns, Ordering::Relaxed).max(at_ns) +/// The console's channels have delivered every record before the positions +/// `next`, one per ring, which `logkeeper`'s own read reached: what the +/// console has spoken moves to at least them, never back, and is answered. +/// A position past what its ring has issued is refused. +pub fn speak(next: &[u64]) -> Result<(usize, Cursor), SyscallError> { + Cursor::from_reader(next, &Shards).ok_or(SyscallError::InvalidArgument)?; + Ok((0, Cursor::at(DRAINED.advance(next)))) } /// The console as the stop holds it, from the moment userland has stopped to @@ -108,16 +111,14 @@ pub struct StopWire(()); impl StopWire { /// Every committed record the console has not spoken onto the UART. pub fn drain(&mut self) { - if !serial::has_console() { - return; + if serial::has_console() { + drain_records(); } - let mut sink = Unspoken { spoken: Some(spoken()), out: serial::write }; - drain_ordered(&Shards, &mut &DRAINED, &mut sink); } } -/// The UART, for the stop once it has stopped userland: the holder's last -/// line ended, so the stop's first is a line of its own. +/// The UART, for the stop once it has stopped userland: a line of its own, +/// since the holder may have been stopped mid-line. pub fn take_for_the_stop() -> StopWire { if !inline() { serial::write(b"\n"); @@ -129,8 +130,8 @@ pub fn take_for_the_stop() -> StopWire { /// path holds. Unbounded because interrupt latency doesn't matter while /// halting, and the report should be whole. pub fn drain_locked(guard: &mut BackendGuard) { - let spoken = after_the_holder(|bytes| guard.write_raw(bytes)); - drain_ordered(&Shards, &mut &DRAINED, &mut Unspoken { spoken, out: |bytes: &[u8]| guard.write_raw(bytes) }); + after_the_holder(|bytes| guard.write_raw(bytes)); + drain_ordered(&Shards, &mut &DRAINED, &mut Spoken(|bytes: &[u8]| guard.write_raw(bytes))); } /// Drain straight to the UART, over whoever holds its registers. @@ -138,33 +139,24 @@ pub fn drain_locked(guard: &mut BackendGuard) { /// # Safety /// Panic path only, once `serial::panic_flush` found no clean handoff; the position is unsynchronised and a record may reach the wire twice. pub unsafe fn drain_bypassed(uart: &mut PanicUart) { - let spoken = after_the_holder(|bytes| uart.write(bytes)); - drain_ordered(&Shards, &mut &DRAINED, &mut Unspoken { spoken, out: |bytes: &[u8]| uart.write(bytes) }); + after_the_holder(|bytes| uart.write(bytes)); + drain_ordered(&Shards, &mut &DRAINED, &mut Spoken(|bytes: &[u8]| uart.write(bytes))); } /// What a fatal path starts with: a line of its own, where a holder may have -/// left one unended, and the newest stamp spoken; before the hand-over the -/// position alone says what is spoken, an untimed boot's records among it. -fn after_the_holder(mut write: impl FnMut(&[u8])) -> Option { - if inline() { - return None; +/// left one unended. +fn after_the_holder(mut write: impl FnMut(&[u8])) { + if !inline() { + write(b"\n"); } - write(b"\n"); - Some(spoken()) } -/// Records stamped after `spoken` onto `out`; the rest advance the position -/// with no byte built. -struct Unspoken { - spoken: Option, - out: F, -} +/// Each record a drain walks, onto its output. +struct Spoken(F); -impl RecordSink for Unspoken { +impl RecordSink for Spoken { fn put(&mut self, record: &LogRecord) -> bool { - if self.spoken.is_none_or(|spoken| record.at_ns > spoken) { - write_line(record, &mut self.out); - } + write_line(record, &mut self.0); true } } diff --git a/kernel/src/log/read.rs b/kernel/src/log/read.rs index 38e1503239f..f20cc1dc132 100644 --- a/kernel/src/log/read.rs +++ b/kernel/src/log/read.rs @@ -84,6 +84,11 @@ impl Cursor { &self.next } + /// Positions answered as they are, with no loss of their own. + pub fn at(next: Vec) -> Self { + Self { next, lost: 0 } + } + pub fn lost(&self) -> u64 { self.lost } @@ -132,6 +137,11 @@ impl Published { self.aps.size(cpus.saturating_sub(1), |_| AtomicU64::new(FIRST_SEQ)); } + /// Move each ring's position to at least `next`'s, and answer them all. + pub fn advance(&self, next: &[u64]) -> Vec { + next.iter().enumerate().map(|(ring, &at)| self.word(ring).fetch_max(at, Ordering::Relaxed).max(at)).collect() + } + fn word(&self, ring: usize) -> &AtomicU64 { match ring.checked_sub(1) { None => &self.boot, diff --git a/kernel/src/process.rs b/kernel/src/process.rs index a1c84e29ced..dac71fd6fee 100644 --- a/kernel/src/process.rs +++ b/kernel/src/process.rs @@ -690,7 +690,7 @@ pub fn spawn_thread( let proc = table.get_mut(parent_process) .expect("spawn_thread: the entry the insert admission just answered for"); // Every thread carries the same image, so a crash report never asks this table. - let image = proc.image.clone()?; + let image = proc.image.clone().expect("spawn_thread: an admitted thread's process keeps its image until its last thread is out"); let tid = proc.threads.insert(ThreadEntry::new(thread_data)); // Before the thread's first instruction, which is the enqueue below: the // thread reads its own id here without a syscall (`toyos_abi::TCB_TID`). @@ -848,12 +848,13 @@ pub fn stats_from( /// The thread is in the process until the last table section marks it, so a machine stop waits for every record and release here. /// Waits on nothing: it runs on a killed thread whose one cancel may be spent, and at the exit boundary's preempt depth, where a park asserts. /// The section that marks it also lets the teardown's own hold on the publication go: the exit is published here when nothing below it is left, and otherwise by the last end below it. +/// The supervisor's end, whatever ended it, ends the machine: nothing starts it again. fn teardown(pid: Pid, tid: Tid, end: End, mark: i32, process_data: &Arc>) { - let main_thread = { + let (main_thread, supervisor) = { let guard = PROCESS_TABLE.lock(); let proc = guard.as_ref().unwrap().get(pid).expect("teardown: the process its last thread is in"); let main = proc.threads.get(proc.main_tid).expect("teardown: a claimed process gives up no thread"); - (Arc::clone(&main.thread_data), main.sched().cloned()) + ((Arc::clone(&main.thread_data), main.sched().cloned()), proc.node.parent().is_none()) }; let releases = crate::object::Releases::open(); let consumed = teardown_resources(process_data, &main_thread, pid); @@ -874,6 +875,9 @@ fn teardown(pid: Pid, tid: Tid, end: End, mark: i32, process_data: &Arc SYS_SHM_CREATE => sys_shm_create(a1, a2, RawHandle(a3 as u32)), SYS_SHM_MAP => sys_shm_map(RawHandle(a1 as u32), a2), SYS_SHM_SUB => sys_shm_sub(RawHandle(a1 as u32), a2, a3), - SYS_LOG_SPOKEN => sys_log_spoken(RawHandle(a1 as u32), a2), + SYS_LOG_SPOKEN => sys_log_spoken(&ctx, RawHandle(a1 as u32), UserAddr::new(a2)), + SYS_LOG_LAST_WORDS => { + if a3 == 0 || a3 > toyos_abi::log::MAX_LAST_WORDS as u64 { + return SyscallError::InvalidArgument.to_u64(); + } + let Some(text) = ctx.user_bytes(UserAddr::new(a2), a3) else { return bad_addr }; + sys_log_last_words(RawHandle(a1 as u32), &text) + } SYS_PORT_CREATE => sys_port_create(), SYS_PORT_MINT => { // Copied in before any handle is looked up: the kernel holds the badge, never the caller's page. diff --git a/kernel/src/syscall/machine.rs b/kernel/src/syscall/machine.rs index 115c4b95f1b..f2d6307caeb 100644 --- a/kernel/src/syscall/machine.rs +++ b/kernel/src/syscall/machine.rs @@ -5,10 +5,11 @@ //! the supervisor's `system.toml`; `SYS_SYSINFO`'s header is ambient. use alloc::vec::Vec; +use core::sync::atomic::{AtomicBool, Ordering}; use crate::log::console::StopWire; use crate::power; -use crate::user_ptr::{SyscallContext, UserBytesMut}; +use crate::user_ptr::{SyscallContext, UserBytes, UserBytesMut}; use crate::UserAddr; use crate::{log, process}; @@ -28,15 +29,35 @@ pub(super) fn sys_log_read( read_on_cursor(ctx, syscap, Rights::LOG, cursor, log::shard_count(), |next| log::user::read(next, out, capacity)) } -/// The console's channels have delivered every record stamped at or before -/// `at_ns`; answers the newest stamp the console has spoken. Requires a -/// `SysCap` carrying [`Rights::LOG`], whose holder carries the log to the -/// console (`log::console`). -pub(super) fn sys_log_spoken(syscap: RawHandle, at_ns: u64) -> u64 { +/// The console's channels have delivered every record before the cursor's +/// positions at `cursor`, which answers what the console has spoken +/// (`log::console::speak`). Requires a `SysCap` carrying [`Rights::CONSOLE`]: +/// what it moves is kept off every fatal path's output. +pub(super) fn sys_log_spoken(ctx: &SyscallContext, syscap: RawHandle, cursor: UserAddr) -> u64 { + read_on_cursor(ctx, syscap, Rights::CONSOLE, cursor, log::shard_count(), log::console::speak) +} + +/// The caller's last words, one alert record, the boot's first such call +/// alone: what the supervisor says before its end ends the machine +/// (`process::teardown`), for the fatal path to put on the console. Requires a +/// `SysCap` carrying [`Rights::LOG`]; text [`toyos_abi::log::last_words`] +/// refuses is `InvalidArgument`, and a second call `AlreadyExists`. +pub(super) fn sys_log_last_words(syscap: RawHandle, text: &UserBytes) -> u64 { + static SAID: AtomicBool = AtomicBool::new(false); if let Err(e) = demand_syscap(syscap, Rights::LOG) { return e.refuse(); } - log::console::speak(at_ns) + let mut copy = [0u8; toyos_abi::log::MAX_LAST_WORDS]; + let copy = &mut copy[..text.len()]; + text.read_at(0, copy); + let Some(words) = toyos_abi::log::last_words(copy) else { + return SyscallError::InvalidArgument.to_u64(); + }; + if SAID.swap(true, Ordering::Relaxed) { + return SyscallError::AlreadyExists.to_u64(); + } + crate::alert!("pid {}'s last words: {words}", process::current_process()); + 0 } /// Copies the diary's records into the caller's buffer; requires a `SysCap` carrying [`Rights::TRACE`]. diff --git a/lib/acpi/src/lib.rs b/lib/acpi/src/lib.rs index 9981e0a04c7..6b320540b10 100644 --- a/lib/acpi/src/lib.rs +++ b/lib/acpi/src/lib.rs @@ -50,7 +50,7 @@ pub use gtdt::{gtdt, Gtdt, TimerInterrupt, GTDT_NEEDED}; pub use pptt::{pptt, Place, Pptt, PpttRefused}; pub use mcfg::{ecam_allocations, AllocationRefused, Allocations, ConfigRegister, EcamWindow, SEGMENT_GROUP}; pub use resource::{io_ports, root_bridge, ResourceError, RootResources, MAX_LIST_BYTES}; -pub use spcr::{spcr, Gas, SerialInterface, Spcr, GAS_SYSTEM_MEMORY, SPCR_NEEDED}; +pub use spcr::{spcr, Gas, SerialInterface, Spcr, ARM_GIC, GAS_SYSTEM_MEMORY, SPCR_NEEDED}; /// Physical memory, as this decoder reads it. /// diff --git a/lib/acpi/src/spcr.rs b/lib/acpi/src/spcr.rs index 727a0a23da5..39aca4a8f46 100644 --- a/lib/acpi/src/spcr.rs +++ b/lib/acpi/src/spcr.rs @@ -8,7 +8,11 @@ use crate::{find_table, Phys, TableError, SDT_HEADER_LEN}; /// Interrupt at 54..58. const INTERFACE_TYPE: usize = SDT_HEADER_LEN; const BASE_ADDRESS: usize = 40; +const INTERRUPT_TYPE: usize = 52; const GSIV: usize = 54; +/// Interrupt Type's bit 3: the UART's interrupt is an Arm GIC interrupt, +/// [`Spcr::gsiv`] its INTID. +pub const ARM_GIC: u8 = 1 << 3; /// Every field this decoder reads lies below the GSIV's end. pub const SPCR_NEEDED: usize = GSIV + 4; @@ -60,6 +64,9 @@ impl SerialInterface { pub struct Spcr { pub interface: SerialInterface, pub base: Gas, + /// Which interrupt controllers the UART's interrupt is named for, a bit + /// each ([`ARM_GIC`] among them); zero for a UART that raises none. + pub interrupt_type: u8, /// The UART's interrupt as a global system interrupt. pub gsiv: u32, } @@ -78,6 +85,7 @@ pub fn spcr(phys: P, rsdp_addr: u64) -> Result { access_size: byte(BASE_ADDRESS + 3)?, address: table.u64_at(BASE_ADDRESS + 4).ok_or(short)?, }, + interrupt_type: byte(INTERRUPT_TYPE)?, gsiv: table.u32_at(GSIV).ok_or(short)?, }) } diff --git a/lib/manifest/src/lib.rs b/lib/manifest/src/lib.rs index 5ef727a03b8..33fde9e6235 100644 --- a/lib/manifest/src/lib.rs +++ b/lib/manifest/src/lib.rs @@ -210,6 +210,10 @@ const SYSCAP_RIGHTS: &[(&str, Rights)] = &[ // holder that has to spin — a name that looks complete and traps the one // program whose whole loop is read-then-park. ("logread", Rights::LOG.union(Rights::WAIT)), + // Say what the console's channels have delivered of the kernel's log, + // which every fatal path and the stop then leave off the UART: the console + // feeder's, never a reader's. + ("console", Rights::CONSOLE), // Power the machine off. The largest authority on the list — it ends every // process there is, including the ones that hold every other right here — and the // last but one to have been free: `SYS_SHUTDOWN` took no handle at all, so @@ -1122,6 +1126,7 @@ mod tests { ); assert!(!syscap_rights(&["roster".into()]).unwrap().contains(Rights::LOG)); assert!(!syscap_rights(&["logread".into()]).unwrap().contains(Rights::ROSTER)); + assert!(!syscap_rights(&["logread".into()]).unwrap().contains(Rights::CONSOLE), "reading is not speaking"); // Not the applet's name, and not the syscall's. assert!(syscap_rights(&["ps".into()]).is_err()); assert!(syscap_rights(&["sysinfo".into()]).is_err()); diff --git a/sdk/abi/src/handle.rs b/sdk/abi/src/handle.rs index 70130535078..ff6e6cd4efb 100644 --- a/sdk/abi/src/handle.rs +++ b/sdk/abi/src/handle.rs @@ -281,10 +281,17 @@ impl Rights { /// [`SpawnArgs::pager`]: crate::syscall::SpawnArgs::pager /// [`SYS_PAGER_CREATE`]: crate::syscall::SYS_PAGER_CREATE pub const PAGER: Rights = Rights(1 << 18); + /// On a `SysCap`: say what the console's channels have delivered of the + /// kernel's log ([`SYS_LOG_SPOKEN`]). What it says is kept off the UART + /// by every fatal path and the stop, so it is not a reader's: the one + /// program that feeds the console, `logkeeper`, holds it. + /// + /// [`SYS_LOG_SPOKEN`]: crate::syscall::SYS_LOG_SPOKEN + pub const CONSOLE: Rights = Rights(1 << 19); /// Every bit that has a caller. A wider set than this is a bug in whoever /// composed it, not a right nobody uses. - pub const ALL: Rights = Rights(0x7_ffff); + pub const ALL: Rights = Rights(0xf_ffff); pub const fn from_bits(bits: u32) -> Option { if bits & !Self::ALL.0 == 0 { Some(Rights(bits)) } else { None } @@ -315,7 +322,7 @@ impl Rights { /// refusal saying which right was missing. impl core::fmt::Debug for Rights { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - const NAMES: [(Rights, &str); 19] = [ + const NAMES: [(Rights, &str); 20] = [ (Rights::DUP, "DUP"), (Rights::TRANSFER, "TRANSFER"), (Rights::READ, "READ"), @@ -335,6 +342,7 @@ impl core::fmt::Debug for Rights { (Rights::FLOOR, "FLOOR"), (Rights::WAKE_LATENCY, "WAKE_LATENCY"), (Rights::PAGER, "PAGER"), + (Rights::CONSOLE, "CONSOLE"), ]; if self.0 == 0 { return f.write_str("NONE"); diff --git a/sdk/abi/src/log.rs b/sdk/abi/src/log.rs index 51a52c5ea2d..b3f9baaf97e 100644 --- a/sdk/abi/src/log.rs +++ b/sdk/abi/src/log.rs @@ -20,6 +20,34 @@ /// (`toyos-elide`). pub const MAX_RECORD_MESSAGE: usize = 992; +/// The longest last words a caller may say (`crate::syscall::log_last_words`), +/// in bytes: a reason, with room in its record for what the kernel says of it. +pub const MAX_LAST_WORDS: usize = 512; + +/// `text` as last words are taken: one line of 1 to [`MAX_LAST_WORDS`] bytes of +/// UTF-8 holding no control character, which the console and the panel +/// show as it is. +pub fn last_words(text: &[u8]) -> Option<&str> { + if text.is_empty() || text.len() > MAX_LAST_WORDS { + return None; + } + let text = core::str::from_utf8(text).ok()?; + (!text.chars().any(char::is_control)).then_some(text) +} + +/// `text` fitted into `buf` as [`last_words`] takes it: each control character +/// a space, cut at the last whole character that fits. +pub fn fit_last_words<'a>(text: &str, buf: &'a mut [u8; MAX_LAST_WORDS]) -> &'a str { + let mut len = 0; + for c in text.chars().map(|c| if c.is_control() { ' ' } else { c }) { + if len + c.len_utf8() > MAX_LAST_WORDS { + break; + } + len += c.encode_utf8(&mut buf[len..]).len(); + } + core::str::from_utf8(&buf[..len]).expect("whole characters are UTF-8") +} + /// One record on the wire, and one slot in a shard. A power of two so a reader /// indexes by shift and the kernel never does length arithmetic. pub const RECORD_BYTES: usize = 1024; @@ -463,4 +491,26 @@ mod tests { assert_eq!(CursorHead::from_words(head.to_words()), head); assert_eq!(CursorHead::from_words([0; CURSOR_HEAD_WORDS]), CursorHead::default()); } + + /// Last words are one line of text the console shows as it is: none, too + /// many bytes, broken UTF-8 or a control character is refused, and what + /// the fitting makes of any text is taken. + #[test] + fn last_words_are_one_line_of_text_and_any_text_fits_into_them() { + assert_eq!(last_words(b"supervisor: cannot read /system/etc/os-release"), Some("supervisor: cannot read /system/etc/os-release")); + assert_eq!(last_words("é".repeat(MAX_LAST_WORDS / 2).as_bytes()).map(str::len), Some(MAX_LAST_WORDS)); + for refused in [&b""[..], &[b'x'; MAX_LAST_WORDS + 1], b"two\nlines", b"\x1b[2Jcleared", b"\xff"] { + assert_eq!(last_words(refused), None, "{refused:?}"); + } + let mut buf = [0; MAX_LAST_WORDS]; + assert_eq!(fit_last_words("panicked at x.rs:1:2:\nwhy\t\u{9b}", &mut buf), "panicked at x.rs:1:2: why "); + let long = "x".repeat(MAX_LAST_WORDS - 1) + "é"; + let fitted = fit_last_words(&long, &mut buf).len(); + assert_eq!(fitted, MAX_LAST_WORDS - 1, "a character that does not fit whole is left out"); + for text in ["a\nb", &long, "\u{7f}\u{0}", "plain"] { + let mut buf = [0; MAX_LAST_WORDS]; + let fitted = fit_last_words(text, &mut buf); + assert_eq!(last_words(fitted.as_bytes()), Some(fitted), "{text:?}"); + } + } } diff --git a/sdk/abi/src/syscall.rs b/sdk/abi/src/syscall.rs index dfe6558d977..454126edfc1 100644 --- a/sdk/abi/src/syscall.rs +++ b/sdk/abi/src/syscall.rs @@ -361,7 +361,7 @@ pub const SYS_PAGED_OP: u64 = 157; pub const SYS_PAGED_NOTICES: u64 = 158; /// Take a connection's next whole message. See [`message_recv`]. pub const SYS_MESSAGE_RECV: u64 = 159; -/// How far the console's channels have carried the kernel's records. See +/// What the console's channels have carried of the kernel's records. See /// [`log_spoken`]. pub const SYS_LOG_SPOKEN: u64 = 160; /// Set the CPUs one of the caller's own threads may run on. See [`thread_set_affinity`]. @@ -391,6 +391,8 @@ pub const SYS_DEVICE_SET_READ: u64 = 167; /// Give the CPU to another runnable thread of the caller's band. See /// [`yield_now`]. pub const SYS_YIELD: u64 = 168; +/// The caller's last words, once per boot. See [`log_last_words`]. +pub const SYS_LOG_LAST_WORDS: u64 = 169; /// Bins in the per-process syscall profile — one for every number this ABI /// issues, and one at the end for every number it does not. @@ -398,13 +400,13 @@ pub const SYS_YIELD: u64 = 168; /// **The profile's parts sum to its total, and that is the whole requirement.** /// A bin array narrower than the ABI reaches drops calls out of the line while /// the total goes on counting them. -pub const SYSCALL_PROFILE_BINS: usize = 170; +pub const SYSCALL_PROFILE_BINS: usize = 171; /// Where a number this ABI does not issue is counted. Merging is a degradation /// a reader can see in the line; dropping is one nobody can. pub const SYSCALL_PROFILE_OTHER: usize = SYSCALL_PROFILE_BINS - 1; -const _: () = assert!(SYS_YIELD < SYSCALL_PROFILE_OTHER as u64); +const _: () = assert!(SYS_LOG_LAST_WORDS < SYSCALL_PROFILE_OTHER as u64); pub const WNOHANG: u64 = 1; /// Every bit `SYS_PROCESS_WAIT`'s flag word defines; a wait carrying another @@ -2392,17 +2394,33 @@ pub unsafe fn shm_sub(shm: RawHandle, offset: u64, len: u64) -> Result Result<(), SyscallError> { + let cursor = sized_cursor(cursor)?; + check(syscall(SYS_LOG_SPOKEN, syscap.0 as u64, cursor as u64, 0, 0)).map(drop) +} + +/// Say `text` as the caller's last words: one kernel record, the first such +/// call of the boot alone, before an end that ends the machine. The +/// supervisor's is one (the kernel ends the machine on it), and the fatal path +/// puts the record on the console with the rest the console has not spoken. +/// +/// `text` is what [`crate::log::last_words`] takes, or `InvalidArgument`; a +/// boot's second call is `AlreadyExists`. Needs a `SysCap` carrying +/// [`Rights::LOG`]. /// /// [`Rights::LOG`]: crate::handle::Rights::LOG -pub fn log_spoken(syscap: RawHandle, at_ns: u64) -> Result { - check(syscall(SYS_LOG_SPOKEN, syscap.0 as u64, at_ns, 0, 0)) +pub fn log_last_words(syscap: RawHandle, text: &[u8]) -> Result<(), SyscallError> { + check_unit(syscall(SYS_LOG_LAST_WORDS, syscap.0 as u64, text.as_ptr() as u64, text.len() as u64, 0)) } /// The machine's [header](SYSINFO_HEADER_SIZE), then as much of the process diff --git a/sdk/toyos/src/log/mod.rs b/sdk/toyos/src/log/mod.rs index 30299ff3450..69865b79ba2 100644 --- a/sdk/toyos/src/log/mod.rs +++ b/sdk/toyos/src/log/mod.rs @@ -47,6 +47,9 @@ macro_rules! error { }; } +use alloc::vec; +use alloc::vec::Vec; + use toyos_abi::log::{CursorHead, LogRecord, CURSOR_HEAD_WORDS}; use toyos_abi::syscall::{self, SyscallError}; @@ -68,11 +71,23 @@ pub fn cursor_words(cap: &SysCap) -> Result { words_for(|cursor| syscall::log_read(cap.as_handle().raw(), cursor, &mut [LogRecord::EMPTY])) } -/// Say the console's channels have delivered every kernel record stamped at or -/// before `at_ns`, and answer the newest stamp the console has spoken -/// (`toyos_abi::syscall::log_spoken`); `at_ns` 0 only reads it. -pub fn spoken(cap: &SysCap, at_ns: u64) -> Result { - syscall::log_spoken(cap.as_handle().raw(), at_ns) +/// Say the console's channels have delivered every kernel record before the +/// positions `at`, one per shard, and answer what the console has spoken +/// (`toyos_abi::syscall::log_spoken`); positions of zero only read it. +pub fn spoken(cap: &SysCap, at: &[u64]) -> Result, SyscallError> { + let mut cursor = vec![0; CURSOR_HEAD_WORDS + at.len()]; + cursor[CURSOR_HEAD_WORDS..].copy_from_slice(at); + syscall::log_spoken(cap.as_handle().raw(), &mut cursor)?; + Ok(cursor.split_off(CURSOR_HEAD_WORDS)) +} + +/// Say `text` as the caller's last words, fitted first to what the kernel +/// takes (`toyos_abi::log::fit_last_words`): one kernel record, the boot's +/// first such call alone (`toyos_abi::syscall::log_last_words`). +pub fn last_words(cap: &SysCap, text: &str) -> Result<(), SyscallError> { + let mut buf = [0; toyos_abi::log::MAX_LAST_WORDS]; + let fitted = toyos_abi::log::fit_last_words(text, &mut buf); + syscall::log_last_words(cap.as_handle().raw(), fitted.as_bytes()) } /// [`cursor_words`] for a read of either kind. @@ -121,6 +136,11 @@ impl + AsMut<[u64]>> LogTail { head_of(self.words.as_ref()).shards } + /// The next record wanted from each shard. + pub fn positions(&self) -> &[u64] { + &self.words.as_ref()[CURSOR_HEAD_WORDS..] + } + /// Fill as much of `out` as there is, oldest first, merged by timestamp. /// /// Answers an empty slice when there is nothing new; **it does not block**, diff --git a/system/logkeeper/src/carrier.rs b/system/logkeeper/src/carrier.rs index b1c39384f12..2686f8c2a4d 100644 --- a/system/logkeeper/src/carrier.rs +++ b/system/logkeeper/src/carrier.rs @@ -14,14 +14,16 @@ //! ([`CARRIED_ELSEWHERE`], [`BACK_ON_THE_UART`]) and carries nothing between. //! //! **The port's carrier is handed the boot from its first line, and the -//! UART's from the first line after the records the kernel put on the UART -//! itself** ([`Hub::wire_from`]). A slow channel is never let go. What a -//! channel has taken is told the kernel as the newest stamp the console has -//! spoken (`toyos::log::spoken`), so a fatal path and the stop put on the UART -//! the kernel's records no channel has delivered. +//! UART's from where the UART's last carrier left off, or else from the +//! hand-over with every line before it but the records the kernel put on the +//! UART itself** ([`Hub::wire_start`]). A slow channel is never let go. What a +//! channel has taken is told the kernel as the read positions of the rounds it +//! has taken whole (`toyos::log::spoken`), so a fatal path and the stop put on +//! the UART exactly the kernel's records no channel has delivered; a channel +//! handed a gap in the replay speaks for nothing after it. //! //! **This program's end is its carriers' pipes'**: what follows on the UART -//! is the kernel's stop or a fatal path, from the last stamp spoken. +//! is the kernel's stop or a fatal path, from what was last spoken. //! //! What a channel had not taken when its carrier went is on neither //! (`issues/the-console-loses-what-its-carrier-had-not-delivered.md`). @@ -39,7 +41,7 @@ use toyos_abi::log::Severity; use toyos_abi::syscall::SyscallError; use toyos_logstream::{BACK_ON_THE_UART, CARRIED_ELSEWHERE, CARRIER_STALLED, CARRY, DELIVERED}; -use crate::serve::{Fed, Hub}; +use crate::serve::{Fed, Hub, Positions}; /// How long the stop waits for the channels to take the log up to the flush: /// policy, well inside the supervisor's flush bound, and far past what a @@ -225,8 +227,8 @@ pub struct Carriers { struct State { feeds: [Feed; 2], - /// The newest stamp the kernel was told the console spoke. - spoken: u64, + /// The positions the kernel was last told the console spoke through. + spoken: Option, } /// One channel's carrier and its feed. @@ -238,6 +240,11 @@ struct Feed { /// Each piece of the replay written into the pipe: the pipe's length once /// it is written, and the replay offset it reaches. written: VecDeque<(u64, u64)>, + /// The replay offset the channel has taken, or passed over while quiet, + /// across its carriers: where the next one starts. + resumed: u64, + /// The carrier was handed a gap in the replay: it speaks for nothing. + gap: bool, } impl State { @@ -258,7 +265,7 @@ impl Carriers { let carriers = Arc::new(Self { hub, cap, - state: Mutex::new(State { feeds: Default::default(), spoken: 0 }), + state: Mutex::new(State { feeds: Default::default(), spoken: None }), moved: Condvar::new(), }); for (channel, acceptor) in [(Channel::Port, port), (Channel::Wire, wire)] { @@ -428,6 +435,7 @@ impl Carriers { let feed = state.feed(channel); feed.at = 0; feed.written.clear(); + feed.gap = false; } let carriers = Arc::clone(self); let fed = std::thread::Builder::new().name("log-carried".into()).spawn(move || { @@ -474,22 +482,27 @@ impl Carriers { } /// `channel` has taken `delivered` bytes of its pipe: tell the kernel the - /// newest stamp of the replay that is all of it, where that is past what - /// it was told. + /// positions of the rounds that is all of, where they are not what it was + /// told last. fn spoken(&self, state: &mut State, channel: Channel, delivered: u64) { let feed = state.feed(channel); let mut through = None; while feed.written.front().is_some_and(|&(end, _)| end <= delivered) { through = feed.written.pop_front().map(|(_, at)| at); } - let Some(stamp) = through.and_then(|at| self.hub.stamp_through(at)) else { return }; - if stamp <= state.spoken { + let Some(at) = through else { return }; + feed.resumed = feed.resumed.max(at); + if feed.gap { return; } - match toyos::log::spoken(&self.cap, stamp) { - Ok(now) => state.spoken = now, - Err(e) => panic!("logkeeper: the kernel refused to be told what the console spoke: {e:?}"), + let Some(positions) = self.hub.positions_through(at) else { return }; + if state.spoken.as_ref().is_some_and(|told| Arc::ptr_eq(told, &positions)) { + return; + } + if let Err(e) = toyos::log::spoken(&self.cap, &positions) { + panic!("logkeeper: the kernel refused to be told what the console spoke: {e:?}"); } + state.spoken = Some(positions); } fn went(&self, channel: Channel, who: u64) { @@ -499,16 +512,25 @@ impl Carriers { } /// Write the log into carrier `who`'s pipe until the pipe's reader goes: - /// the port's from the boot's first line, the UART's from where the - /// kernel left it, and nothing of what the port carries. + /// the port's from the boot's first line, the UART's from where its + /// channel left off ([`Hub::wire_start`]), and nothing of what the port + /// carries. fn feed(&self, channel: Channel, who: u64, pipe: &Pipe) { let poller = Poller::new(1); let wire = channel == Channel::Wire; - let mut at = if wire { self.hub.wire_from() } else { 0 }; + let (mut at, mut owed) = if wire { + let resumed = self.lock().feed(channel).resumed; + self.hub.wire_start(resumed) + } else { + (0, None) + }; // Whether the UART last said the port carries the log. let mut said_quiet = false; loop { - let fed = self.hub.next_for_a_carrier(&mut at, wire); + let fed = match owed.take() { + Some(owed) => Fed::Replay(owed.to_vec()), + None => self.hub.next_for_a_carrier(&mut at, wire), + }; if wire { let quiet = self.lock().quiet(); if quiet != said_quiet { @@ -519,14 +541,22 @@ impl Carriers { } } if quiet { - // Passed over, which is what the stop's wait reads. - self.lock().feed(channel).at = at; + // Passed over, which is what the stop's wait reads, and + // where a carrier started again resumes. + let mut state = self.lock(); + let feed = state.feed(channel); + feed.at = at; + feed.resumed = feed.resumed.max(at); continue; } } let (bytes, reaches) = match fed { Fed::Replay(bytes) => (bytes, Some(at)), Fed::Said(bytes) => (bytes, None), + Fed::Gap(bytes) => { + self.lock().feed(channel).gap = true; + (bytes, None) + } }; if !self.write(channel, who, pipe, &poller, &bytes, reaches) { return; diff --git a/system/logkeeper/src/main.rs b/system/logkeeper/src/main.rs index 2e3c696183b..cd3eb8f3586 100644 --- a/system/logkeeper/src/main.rs +++ b/system/logkeeper/src/main.rs @@ -98,6 +98,7 @@ use toyos_logstream::{ use toyos_wallclock::Civil; use carrier::Carriers; +use serve::Positions; use origin::{Origin, Said}; use policy::{Step, LOG_WRITE_BUDGET}; use store::{Volume, DIR, MAX_LOG_BYTES, MAX_LOG_FILES}; @@ -160,15 +161,17 @@ fn main() { inspect::serve(acceptor, Arc::clone(&published), Arc::clone(&hub)); } let cap = Arc::new(cap); + let words = + toyos::log::cursor_words(&cap).unwrap_or_else(|e| panic!("logkeeper: SYS_LOG_READ sized no cursor ({e:?})")); // What the kernel put on the UART itself before userland ran, which the - // UART's carrier starts after. - let spoken = toyos::log::spoken(&cap, 0).unwrap_or_else(|e| panic!("logkeeper: SYS_LOG_SPOKEN refused ({e:?})")); + // UART's carrier is not handed again. + let shards = words - toyos_abi::log::CURSOR_HEAD_WORDS; + let spoken = + toyos::log::spoken(&cap, &vec![0; shards]).unwrap_or_else(|e| panic!("logkeeper: SYS_LOG_SPOKEN refused ({e:?})")); let (port, wire) = (endow::acceptor(CARRIER), endow::acceptor(WIRE)); let carriers = (port.is_some() || wire.is_some()).then(|| Carriers::serve(port, wire, Arc::clone(&hub), Arc::clone(&cap))); - let words = - toyos::log::cursor_words(&cap).unwrap_or_else(|e| panic!("logkeeper: SYS_LOG_READ sized no cursor ({e:?})")); let tail = LogTail::new(vec![0; words]); let mut log = Log { cap, @@ -176,6 +179,7 @@ fn main() { supervisor_rx: ipc::FrameRx::new(), carriers, spoken: Some(spoken), + owed_wire: String::new(), origins: Vec::new(), tail, records: vec![Record::EMPTY; BATCH], @@ -196,9 +200,12 @@ struct Log { from_supervisor: Connection, supervisor_rx: ipc::FrameRx<{ 4 + MAX_TAG }>, carriers: Option>, - /// The newest stamp the kernel put on the UART itself, until the first - /// round has said where the UART's carrier starts after it. - spoken: Option, + /// What the kernel put on the UART itself, a read position per shard, + /// until the rounds have read past it and said where the UART's carrier + /// starts. + spoken: Option>, + /// Those rounds' lines but the kernel's records the UART carried. + owed_wire: String, origins: Vec, tail: LogTail>, records: Vec, @@ -442,8 +449,9 @@ impl Log { } let (now, later): (Vec, Vec) = lines.into_iter().partition(|line| all || line.at_ns <= cut); + let through = written_through(self.tail.positions(), &later); self.waiting = later; - self.write(now, all); + self.write(now, all, through); oldest } @@ -483,24 +491,27 @@ impl Log { } /// The lines, in stamp order, into the file, the readers and the console's - /// carriers; while the machine stops, held back from the file, and each + /// carriers, which are the kernel's log through the read positions + /// `through`; while the machine stops, held back from the file, and each /// program's line to the UART's carrier. - fn write(&mut self, mut lines: Vec, flushing: bool) { + fn write(&mut self, mut lines: Vec, flushing: bool, through: Positions) { if lines.is_empty() { return; } lines.sort_by_key(|line| line.at_ns); let mut file = String::new(); - // The kernel's own lines on the UART: what ends them in `file`. - let mut spoken_through = 0; + // What the UART is owed of these lines, while the kernel's own on it + // are yet to be read past. + let mut wire = String::new(); let mut programs = String::new(); for line in &lines { match &line.kind { Kind::Kernel(record) => { - file.push_str(&format!("{}\n", record.dated(&stamp(self.boot_secs, record.at_ns())))); - if self.spoken.is_some_and(|spoken| line.at_ns <= spoken) { - spoken_through = file.len(); + let text = format!("{}\n", record.dated(&stamp(self.boot_secs, record.at_ns()))); + if !self.spoken.as_ref().is_some_and(|spoken| on_the_uart(record, spoken)) { + wire.push_str(&text); } + file.push_str(&text); } Kind::Program { tag, owner, said } => { let tag = Tag::new(tag).expect("an origin's name is a tag"); @@ -517,6 +528,7 @@ impl Log { }; let line = format!("{line}\n"); file.push_str(&line); + wire.push_str(&line); if self.stopping.is_some() { programs.push_str(&line); } @@ -537,12 +549,13 @@ impl Log { // The file first: a reader is served only what /log already holds, // whenever the machine stops between the two. self.to_volume(file.as_bytes(), !flushing); - let from = self.hub.end(); - self.hub.append(file.as_bytes(), lines.last().map(|line| line.at_ns)); - // Every record the kernel put on the UART itself is committed before - // userland runs, so the first round has them all. - if self.spoken.take().is_some() { - self.hub.set_wire_from(from + spoken_through as u64); + self.hub.append(file.as_bytes(), Some(Arc::clone(&through))); + if let Some(spoken) = &self.spoken { + self.owed_wire.push_str(&wire); + if through.iter().zip(spoken).all(|(read, spoken)| read >= spoken) { + self.hub.set_wire_from(self.hub.end(), std::mem::take(&mut self.owed_wire).into_bytes()); + self.spoken = None; + } } } @@ -655,6 +668,27 @@ impl Log { } } +/// Whether the kernel put `record` on the UART itself, by the read positions +/// it had spoken through. +fn on_the_uart(record: &Record, spoken: &[u64]) -> bool { + spoken.get(usize::from(record.cpu)).is_some_and(|&through| record.seq < through) +} + +/// The kernel's read positions a round's written lines are the log through: +/// the cursor's, each shard's held back to its first record still `waiting` +/// for a later round. +fn written_through(cursor: &[u64], waiting: &[Line]) -> Positions { + let mut through = cursor.to_vec(); + for line in waiting { + if let Kind::Kernel(record) = &line.kind + && let Some(at) = through.get_mut(usize::from(record.cpu)) + { + *at = (*at).min(record.seq); + } + } + through.into() +} + fn ahead_note(ahead: u64, tag: &str) -> String { format!("logkeeper: {ahead} record(s) of {tag}'s were stamped ahead of the clock and are written as read") } @@ -695,4 +729,35 @@ mod tests { assert_eq!(toyos_inspect::LOG.port, toyos_logstream::SERVICE); assert_ne!(toyos_logstream::READ, toyos_inspect::MSG_INSPECT); } + + fn kernel(cpu: u16, seq: u64) -> super::Line { + let record = super::Record { cpu, seq, ..super::Record::EMPTY }; + super::Line { at_ns: 0, kind: super::Kind::Kernel(Box::new(record)) } + } + + /// **A round speaks for the kernel's log only as far as it wrote it**: a + /// shard's position is held back to its first record still waiting for + /// a later round, and the others' are the cursor's. + #[test] + fn a_round_is_through_the_cursor_but_what_waits() { + let waiting = [kernel(1, 7), kernel(1, 9)]; + assert_eq!(&*super::written_through(&[5, 12, 3], &waiting), &[5, 7, 3]); + assert_eq!(&*super::written_through(&[5, 12], &[]), &[5, 12]); + } + + /// The kernel's records the UART carried before the hand-over are those + /// before the position it spoke through in their shard, whatever their + /// stamps say. + #[test] + fn what_the_uart_carried_is_told_by_position() { + let on = |cpu, seq| match kernel(cpu, seq).kind { + super::Kind::Kernel(record) => super::on_the_uart(&record, &[4, 2]), + super::Kind::Program { .. } => unreachable!(), + }; + assert!(on(0, 3)); + assert!(!on(0, 4)); + assert!(on(1, 1)); + assert!(!on(1, 2)); + assert!(!on(2, 0), "a shard it spoke nothing of"); + } } diff --git a/system/logkeeper/src/serve.rs b/system/logkeeper/src/serve.rs index e318267e23a..5f04879bcd3 100644 --- a/system/logkeeper/src/serve.rs +++ b/system/logkeeper/src/serve.rs @@ -14,8 +14,9 @@ //! else wakes it: there is no poll and no timer. //! //! The console's carriers read the same replay (`crate::carrier`): the hub -//! keeps the newest stamp each round reaches ([`Hub::stamp_through`]), where -//! the console UART's carrier starts ([`Hub::wire_from`]), and the lines said +//! keeps the kernel's read positions each round is written through +//! ([`Hub::positions_through`]), where the console UART's carrier starts with +//! what it owes of the rounds before ([`Hub::wire_start`]), and the lines said //! while the machine stops, which the file holds back and the UART carries. use std::collections::VecDeque; @@ -44,18 +45,22 @@ const MAX_READERS: usize = 2; /// slot is let go: a wait on its sink taking bytes, bounded by this. const STALLED: Duration = Duration::from_secs(10); -/// Rounds whose stamps the hub keeps: a carrier whose channel is further +/// Rounds whose positions the hub keeps: a carrier whose channel is further /// behind than this speaks for none of it until it catches up. const ROUNDS: usize = 4096; +/// The kernel's read positions a round is written through, one per shard. +pub type Positions = Arc<[u64]>; + /// The replay, and what the console's carriers read beside it. struct Held { replay: Replay, - /// Each round's end in the replay and the newest stamp of its records, - /// oldest first, the last [`ROUNDS`] of them. - rounds: VecDeque<(u64, u64)>, - /// Where the console UART's carrier starts, once the first round says. - wire_from: Option, + /// Each round's end in the replay and the positions it is written + /// through, oldest first, the last [`ROUNDS`] of them. + rounds: VecDeque<(u64, Positions)>, + /// Where the console UART's carrier starts, and what it owes of the + /// rounds before: their lines but the kernel's records it carried itself. + wire_from: Option<(u64, Arc<[u8]>)>, /// Lines said while the machine stops, held back from the file and owed /// the UART's carrier. stopping: Vec, @@ -98,45 +103,48 @@ impl Hub { admit(&self.shared, format!("reader {}", conn.as_handle().raw().0), PipeSink::new(pipe)); } - /// The lines the file has just taken, for every reader, and the newest - /// stamp among them where they are a round's. - pub fn append(&self, lines: &[u8], newest: Option) { + /// The lines the file has just taken, for every reader, and the kernel's + /// read positions they are written through where they are a round's. + pub fn append(&self, lines: &[u8], through: Option) { let mut held = self.lock(); held.replay.append(lines); - if let Some(at_ns) = newest { + if let Some(positions) = through { let end = held.replay.end(); if held.rounds.len() == ROUNDS { held.rounds.pop_front(); } - held.rounds.push_back((end, at_ns)); + held.rounds.push_back((end, positions)); } drop(held); self.shared.grew.notify_all(); } - /// The newest stamp of the last round wholly before `at`: what a channel - /// that has taken the replay up to `at` has spoken. - pub fn stamp_through(&self, at: u64) -> Option { + /// The positions of the last round wholly before `at`: what a channel + /// that has taken the replay up to `at` has spoken of the kernel's log. + pub fn positions_through(&self, at: u64) -> Option { let held = self.lock(); - held.rounds.iter().rev().find(|&&(end, _)| end <= at).map(|&(_, at_ns)| at_ns) + held.rounds.iter().rev().find(|(end, _)| *end <= at).map(|(_, positions)| Arc::clone(positions)) } - /// The console UART's carrier starts at `at`, the end of the last line - /// the kernel put on the UART itself. Once. - pub fn set_wire_from(&self, at: u64) { + /// The console UART's carrier starts at `at`, past every kernel record the + /// kernel put on the UART itself, owing `owed` of the rounds before. Once. + pub fn set_wire_from(&self, at: u64, owed: Vec) { let mut held = self.lock(); assert!(held.wire_from.is_none(), "logkeeper: where the UART's carrier starts is said twice"); - held.wire_from = Some(at); + held.wire_from = Some((at, owed.into())); drop(held); self.shared.grew.notify_all(); } - /// Where the console UART's carrier starts, once the first round said. - pub fn wire_from(&self) -> u64 { + /// Where a carrier of the UART starts, once the rounds have said, given + /// the replay offset `resumed` its channel had taken or passed over + /// before: there, or where the kernel left the UART with what the rounds + /// before that owe it, which a channel that took less is handed again. + pub fn wire_start(&self, resumed: u64) -> (u64, Option>) { let mut held = self.lock(); loop { - if let Some(at) = held.wire_from { - return at; + if let Some((from, owed)) = &held.wire_from { + return if resumed >= *from { (resumed, None) } else { (*from, Some(Arc::clone(owed))) }; } held = self.shared.grew.wait(held).expect("logkeeper: the replay is poisoned"); } @@ -184,7 +192,7 @@ impl Hub { } Next::Evicted { lost, at: resume } => { *at = resume; - return Fed::Said(evicted(self.shared.boot_secs, lost)); + return Fed::Gap(evicted(self.shared.boot_secs, lost)); } Next::CaughtUp if wire && !held.stopping.is_empty() => { return Fed::Said(std::mem::take(&mut held.stopping)); @@ -207,6 +215,10 @@ pub enum Fed { Replay(Vec), /// Lines that are not the replay's. Said(Vec), + /// The line that says what the replay no longer holds, which the carrier + /// is past: its channel speaks for none of what follows, whose rounds the + /// skipped bytes are part of. + Gap(Vec), } /// The read end goes to the reader with the boot's length so far, the write @@ -354,45 +366,67 @@ mod tests { fn replay(fed: Fed) -> Vec { match fed { Fed::Replay(bytes) => bytes, - Fed::Said(_) => panic!("lines that are not the replay's"), + Fed::Said(_) | Fed::Gap(_) => panic!("lines that are not the replay's"), } } + fn at(positions: &[u64]) -> Option { + Some(positions.into()) + } + /// A channel that has taken the replay up to an offset has spoken the - /// newest stamp of the last round wholly before it, and nothing of a round - /// it has only begun. + /// kernel's log through the positions of the last round wholly before it, + /// and nothing of a round it has only begun. #[test] fn what_a_channel_took_speaks_for_the_rounds_it_took_whole() { let hub = Hub::start(1 << 20, None); - hub.append(b"a\nb\n", Some(10)); - hub.append(b"c\n", Some(20)); + hub.append(b"a\nb\n", at(&[3, 1])); + hub.append(b"c\n", at(&[3, 2])); hub.resume(b"held\n"); - assert_eq!(hub.stamp_through(0), None); - assert_eq!(hub.stamp_through(3), None, "half the first round is none of it"); - assert_eq!(hub.stamp_through(4), Some(10)); - assert_eq!(hub.stamp_through(6), Some(20)); - assert_eq!(hub.stamp_through(11), Some(20), "lines with no round speak for nothing"); + assert_eq!(hub.positions_through(0), None); + assert_eq!(hub.positions_through(3), None, "half the first round is none of it"); + assert_eq!(hub.positions_through(4), at(&[3, 1])); + assert_eq!(hub.positions_through(6), at(&[3, 2])); + assert_eq!(hub.positions_through(11), at(&[3, 2]), "lines with no round speak for nothing"); } - /// The UART's carrier starts where the kernel left the UART, and is handed - /// what was said while the machine stopped once it has the replay; a - /// refused stop's lines reach it through the replay, once. + /// The UART's carrier starts where the kernel left the UART, owing the + /// rounds before what the kernel did not put on it, and is handed what + /// was said while the machine stopped once it has the replay; a refused + /// stop's lines reach it through the replay, once. #[test] fn the_wire_starts_after_the_kernel_and_takes_the_stops_lines_last() { let hub = Hub::start(1 << 20, None); - hub.append(b"kernel 1\nkernel 2\nprogram\n", Some(30)); - hub.set_wire_from(18); - let mut at = hub.wire_from(); - assert_eq!(replay(hub.next_for_a_carrier(&mut at, true)), b"program\n"); + hub.append(b"kernel 1\nprogram 1\nkernel 2\n", at(&[2])); + hub.set_wire_from(28, b"program 1\n".to_vec()); + hub.append(b"program 2\n", at(&[2])); + let (mut from, owed) = hub.wire_start(0); + assert_eq!((from, owed.as_deref()), (28, Some(&b"program 1\n"[..]))); + assert_eq!(replay(hub.next_for_a_carrier(&mut from, true)), b"program 2\n"); hub.append_stopping(b"stopping\n"); - match hub.next_for_a_carrier(&mut at, true) { + match hub.next_for_a_carrier(&mut from, true) { Fed::Said(bytes) => assert_eq!(bytes, b"stopping\n"), - Fed::Replay(bytes) => panic!("the replay's {bytes:?} where the stop's lines were owed"), + _ => panic!("the replay where the stop's lines were owed"), } hub.append_stopping(b"again\n"); hub.resume(b"stopping\nagain\n"); - assert_eq!(replay(hub.next_for_a_carrier(&mut at, true)), b"stopping\nagain\n"); + assert_eq!(replay(hub.next_for_a_carrier(&mut from, true)), b"stopping\nagain\n"); let mut port = 0; - assert_eq!(replay(hub.next_for_a_carrier(&mut port, false)).len(), 41, "the port is fed from the first line"); + assert_eq!(replay(hub.next_for_a_carrier(&mut port, false)).len(), 53, "the port is fed from the first line"); + } + + /// **A UART carrier started again resumes where its channel left off**, + /// never from the hand-over: a channel that had taken the rounds the + /// kernel left it owes nothing of them again, and one that had not is + /// handed them again. + #[test] + fn a_uart_carrier_started_again_resumes_where_its_channel_left_off() { + let hub = Hub::start(1 << 20, None); + hub.append(b"kernel\nprogram\n", at(&[1])); + hub.set_wire_from(15, b"program\n".to_vec()); + hub.append(b"later\n", at(&[1])); + assert_eq!(hub.wire_start(21), (21, None), "a channel that took the rounds after resumes after them"); + assert_eq!(hub.wire_start(15), (15, None)); + assert_eq!(hub.wire_start(3).1.as_deref(), Some(&b"program\n"[..]), "one that took less owes them again"); } } diff --git a/system/supervisor/src/main.rs b/system/supervisor/src/main.rs index 6acd087e17e..6bd6ed6bf50 100644 --- a/system/supervisor/src/main.rs +++ b/system/supervisor/src/main.rs @@ -484,13 +484,16 @@ fn make_dir(path: &str) -> std::io::Result<()> { } fn main() { + let syscap: &'static SysCap = Box::leak(Box::new( + Endowments::get() + .take(SYSCAP_LABEL) + .expect("supervisor: the kernel spawns this program holding the system capability"), + )); + say_last_words_on_panic(syscap); // Before anything is started, and before the supervisor says anything: from here // the supervisor's own lines are records in its ring. let mut log = Log::open(); - let syscap: SysCap = Endowments::get() - .take(SYSCAP_LABEL) - .expect("supervisor: the kernel spawns this program holding the system capability"); let boot: &'static Boot = Box::leak(Box::new(Boot::take())); let sources = syscap.boot_sources().expect("supervisor: the kernel answers the system capability its boot sources"); let booted = toyos_supervisor::boot::read(&sources); @@ -560,7 +563,7 @@ fn main() { &[], Vec::new(), &unread, - &syscap, + syscap, &BTreeMap::new(), &no_grants, &minted, @@ -632,7 +635,7 @@ fn main() { minted, store: Store::default(), consent, - syscap: &syscap, + syscap, boot, acceptors, connectors, @@ -3732,12 +3735,27 @@ fn read_system(path: &'static str, server: &Service<'_>) -> String { } /// The boot's end before `logkeeper` runs, which no reader of the -/// supervisor's ring reaches yet -/// (`issues/a-supervisor-that-fails-before-logkeeper-runs-says-why-nowhere.md`). +/// supervisor's ring reaches yet: said as its last words. fn bootstrap_fails(why: String) -> ! { panic!("supervisor: {why}"); } +/// **The supervisor's end is the machine's**: the kernel ends the machine on +/// it, and puts on the console every record the console has not spoken. A +/// panic's message is first said as the supervisor's last words, the one +/// kernel record that says why, since its ring reaches nobody before +/// `logkeeper` runs; then to that ring, as every program's. +fn say_last_words_on_panic(syscap: &'static SysCap) { + let ring = std::panic::take_hook(); + std::panic::set_hook(Box::new(move |info| { + let why = info.payload_as_str().unwrap_or("a panic with no message"); + let at = info.location().map(|at| format!(" (at {}:{})", at.file(), at.line())).unwrap_or_default(); + // A second panic finds them said, and the first panic's are the reason. + let _ = toyos::log::last_words(syscap, &format!("{why}{at}")); + ring(info); + })); +} + /// The images the kernel hands the supervisor beside the system capability: /// the file server's executable and ROOT's bytes, which nobody writes /// (`toyos_abi::boot::BootImage`). Kept for the machine's life, since SYSTEM diff --git a/tests/common/qemu.rs b/tests/common/qemu.rs index 1174dac0f61..be5707cb4ee 100644 --- a/tests/common/qemu.rs +++ b/tests/common/qemu.rs @@ -1241,6 +1241,9 @@ pub struct BootOptions { /// the image is memoized on their names and bytes, so two boots staging /// different fixtures do not share one. pub extra_root_files: Vec<(String, Vec)>, + /// Files of the image's own ROOT left out, each by its ROOT-relative + /// path: a boot staged on a `/system` missing one. + pub root_omits: &'static [&'static str], /// DATA's size in place of the profile's, for a boot that writes more /// than [`NVME_SMALL`] holds, on a profile whose disk carries the image. pub data_bytes: Option, @@ -1295,6 +1298,7 @@ impl Default for BootOptions { mute: false, ready_marker: DEFAULT_READY, extra_root_files: Vec::new(), + root_omits: &[], data_bytes: None, psci_trace: None, rtc_base: None, @@ -1373,7 +1377,7 @@ fn kernel_of(options: &BootOptions) -> Vec<&'static str> { toyos_build::build::TEST_KERNEL.to_vec() } -// Nine, because an image is its architecture and its slots as much as its files and its kernel. +// Ten, because an image is its architecture and its slots as much as its files and its kernel. #[allow(clippy::too_many_arguments)] fn build_boot_image_with( arch: Arch, @@ -1381,6 +1385,7 @@ fn build_boot_image_with( c_tests: &[(String, Vec)], rust_tests: &[(String, Vec)], staged: &[(String, Vec)], + omits: &[&str], kernel_features: &[&str], kernel_params: &[&str], debug_wait: bool, @@ -1450,7 +1455,8 @@ fn build_boot_image_with( assert!(config.is_file(), "no image config at {}", config.display()); let quiet = !VERBOSE.load(Ordering::Relaxed); - let plan = toyos_build::build::Plan::new(arch, config, kernel_features, kernel_params); + let mut plan = toyos_build::build::Plan::new(arch, config, kernel_features, kernel_params); + plan.root_omits = omits.iter().map(|omitted| (*omitted).to_string()).collect(); let root = compile::repo_root(); match bent { None => toyos_build::build::build_test_image(&root, &plan, quiet, &extra_files), @@ -1546,6 +1552,7 @@ impl QemuInstance { c_tests, rust_tests, &options.extra_root_files, + options.root_omits, &features, ¶ms, options.debug_wait, diff --git a/tests/images/acpicase.toml b/tests/images/acpicase.toml index d0b4f24bf01..162d36dca52 100644 --- a/tests/images/acpicase.toml +++ b/tests/images/acpicase.toml @@ -11,7 +11,7 @@ start = ["logkeeper", "uart", "test-runner"] [programs.logkeeper] service = true -syscap = ["logread"] +syscap = ["logread", "console"] serves = ["log-wire"] holds = ["log-origins"] diff --git a/tests/images/benchcase.toml b/tests/images/benchcase.toml index 354b3a66260..ca473f3a725 100644 --- a/tests/images/benchcase.toml +++ b/tests/images/benchcase.toml @@ -22,7 +22,7 @@ start = ["logkeeper", "uart", "diskserver", "fileserver", "acpiserver", "netstac # `log`, which `lan_lease` waits for netstack's lease on. [programs.logkeeper] service = true -syscap = ["logread"] +syscap = ["logread", "console"] serves = ["log", "log-carrier", "log-wire"] holds = ["log-origins"] diff --git a/tests/images/clipboardcase.toml b/tests/images/clipboardcase.toml index 46b10e41b51..a294c41a183 100644 --- a/tests/images/clipboardcase.toml +++ b/tests/images/clipboardcase.toml @@ -18,7 +18,7 @@ start = ["acpiserver", "logkeeper", "uart", "compositor", "vdagent", "test-runne # `every_boot_config_runs_logkeeper` is what refuses a boot config without it. [programs.logkeeper] service = true -syscap = ["logread"] +syscap = ["logread", "console"] serves = ["log-carrier", "log-wire"] holds = ["log-origins"] diff --git a/tests/images/designatecase.toml b/tests/images/designatecase.toml index 99ba82a03cb..21384109e02 100644 --- a/tests/images/designatecase.toml +++ b/tests/images/designatecase.toml @@ -9,7 +9,7 @@ start = ["logkeeper", "uart", "diskserver", "fileserver", "acpiserver", "test-ru [programs.logkeeper] service = true -syscap = ["logread"] +syscap = ["logread", "console"] serves = ["log", "log-wire"] holds = ["log-origins"] diff --git a/tests/images/drivescase.toml b/tests/images/drivescase.toml index 8f2bdc023c0..1e43330e15e 100644 --- a/tests/images/drivescase.toml +++ b/tests/images/drivescase.toml @@ -8,7 +8,7 @@ start = ["acpiserver", "logkeeper", "uart", "test-runner"] # `every_boot_config_runs_logkeeper` is what refuses a boot config without it. [programs.logkeeper] service = true -syscap = ["logread"] +syscap = ["logread", "console"] serves = ["log-carrier", "log-wire"] holds = ["log-origins"] diff --git a/tests/images/floorcase.toml b/tests/images/floorcase.toml index 03542d5d604..025b503d55a 100644 --- a/tests/images/floorcase.toml +++ b/tests/images/floorcase.toml @@ -21,7 +21,7 @@ memory-floor-mib = 64 # to keep being handed over. [programs.logkeeper] service = true -syscap = ["logread"] +syscap = ["logread", "console"] serves = ["log", "log-wire"] memory-floor-mib = 32 holds = ["log-origins"] diff --git a/tests/images/hostedclangcase.toml b/tests/images/hostedclangcase.toml index cd0f80053cb..25d9e0804fd 100644 --- a/tests/images/hostedclangcase.toml +++ b/tests/images/hostedclangcase.toml @@ -6,7 +6,7 @@ start = ["acpiserver", "logkeeper", "uart", "diskserver", "fileserver", "test-ru [programs.logkeeper] service = true -syscap = ["logread"] +syscap = ["logread", "console"] serves = ["log-carrier", "log-wire"] holds = ["log-origins"] diff --git a/tests/images/hotplugcase.toml b/tests/images/hotplugcase.toml index deec0a3f851..9793c746353 100644 --- a/tests/images/hotplugcase.toml +++ b/tests/images/hotplugcase.toml @@ -9,7 +9,7 @@ start = ["logkeeper", "uart", "acpiserver", "test-runner"] # `every_boot_config_runs_logkeeper` is what refuses a boot config without it. [programs.logkeeper] service = true -syscap = ["logread"] +syscap = ["logread", "console"] serves = ["log-carrier", "log-wire"] holds = ["log-origins"] diff --git a/tests/images/jobcase.toml b/tests/images/jobcase.toml index cf83ea84602..0bad668ede3 100644 --- a/tests/images/jobcase.toml +++ b/tests/images/jobcase.toml @@ -6,7 +6,7 @@ start = ["acpiserver", "logkeeper", "uart", "diskserver", "fileserver", "test-ru [programs.logkeeper] service = true -syscap = ["logread"] +syscap = ["logread", "console"] serves = ["log-carrier", "log-wire"] holds = ["log-origins"] diff --git a/tests/images/latencycase.toml b/tests/images/latencycase.toml index 3e98a3218e8..47f8c4ae0a7 100644 --- a/tests/images/latencycase.toml +++ b/tests/images/latencycase.toml @@ -15,7 +15,7 @@ start = ["logkeeper", "uart", "diskserver", "fileserver", "acpiserver", "test-ru [programs.logkeeper] service = true -syscap = ["logread"] +syscap = ["logread", "console"] serves = ["log-wire"] holds = ["log-origins"] diff --git a/tests/images/launchcase.toml b/tests/images/launchcase.toml index c4e14beb13c..0e3e7959cfc 100644 --- a/tests/images/launchcase.toml +++ b/tests/images/launchcase.toml @@ -7,7 +7,7 @@ start = ["acpiserver", "logkeeper", "uart", "test-runner"] [programs.logkeeper] service = true -syscap = ["logread"] +syscap = ["logread", "console"] serves = ["log-carrier", "log-wire"] holds = ["log-origins"] diff --git a/tests/images/libccase.toml b/tests/images/libccase.toml index 215cbfb61a7..06641f9fd7a 100644 --- a/tests/images/libccase.toml +++ b/tests/images/libccase.toml @@ -6,7 +6,7 @@ start = ["acpiserver", "logkeeper", "uart", "test-runner"] [programs.logkeeper] service = true -syscap = ["logread"] +syscap = ["logread", "console"] serves = ["log-carrier", "log-wire"] holds = ["log-origins"] diff --git a/tests/images/linkcase.toml b/tests/images/linkcase.toml index 3abffb050f4..b4d597891d0 100644 --- a/tests/images/linkcase.toml +++ b/tests/images/linkcase.toml @@ -8,7 +8,7 @@ start = ["acpiserver", "logkeeper", "uart", "diskserver", "fileserver", "netstac # `log` is the port the job reads netstack's lines on. [programs.logkeeper] service = true -syscap = ["logread"] +syscap = ["logread", "console"] serves = ["log", "log-carrier", "log-wire"] holds = ["log-origins"] diff --git a/tests/images/metalcase.toml b/tests/images/metalcase.toml index 316672185cb..12324c47108 100644 --- a/tests/images/metalcase.toml +++ b/tests/images/metalcase.toml @@ -21,7 +21,7 @@ start = ["acpiserver", "logkeeper", "uart", "diskserver", "fileserver", "composi # the log on. [programs.logkeeper] service = true -syscap = ["logread"] +syscap = ["logread", "console"] serves = ["log", "log-wire"] holds = ["log-origins"] diff --git a/tests/images/metaldevicecase.toml b/tests/images/metaldevicecase.toml index 785734db1db..1144989cbb8 100644 --- a/tests/images/metaldevicecase.toml +++ b/tests/images/metaldevicecase.toml @@ -16,7 +16,7 @@ start = ["acpiserver", "logkeeper", "uart", "diskserver", "fileserver", "soundse [programs.logkeeper] service = true -syscap = ["logread"] +syscap = ["logread", "console"] serves = ["log-wire"] holds = ["log-origins"] diff --git a/tests/images/netcase.toml b/tests/images/netcase.toml index 6734fa40a6a..e11d47be550 100644 --- a/tests/images/netcase.toml +++ b/tests/images/netcase.toml @@ -17,7 +17,7 @@ start = ["acpiserver", "logkeeper", "uart", "diskserver", "fileserver", "netstac # empty. It claims no device and serves no port. [programs.logkeeper] service = true -syscap = ["logread"] +syscap = ["logread", "console"] serves = ["log-carrier", "log-wire"] holds = ["log-origins"] diff --git a/tests/images/panelcase.toml b/tests/images/panelcase.toml index 42eb7b2b88b..ebae27312ef 100644 --- a/tests/images/panelcase.toml +++ b/tests/images/panelcase.toml @@ -12,7 +12,7 @@ start = ["acpiserver", "logkeeper", "uart", "compositor"] # `every_boot_config_runs_logkeeper` is what refuses a boot config without it. [programs.logkeeper] service = true -syscap = ["logread"] +syscap = ["logread", "console"] serves = ["log-carrier", "log-wire"] holds = ["log-origins"] diff --git a/tests/images/proctreecase.toml b/tests/images/proctreecase.toml index 78ed7a762f3..ee8e8d5be23 100644 --- a/tests/images/proctreecase.toml +++ b/tests/images/proctreecase.toml @@ -26,7 +26,7 @@ folder = "read-write" # empty. It claims no device and serves no port. [programs.logkeeper] service = true -syscap = ["logread"] +syscap = ["logread", "console"] serves = ["log-carrier", "log-wire"] holds = ["log-origins"] diff --git a/tests/images/scalecase.toml b/tests/images/scalecase.toml index 752ebfb9093..3ca3c7451a7 100644 --- a/tests/images/scalecase.toml +++ b/tests/images/scalecase.toml @@ -14,7 +14,7 @@ start = ["acpiserver", "logkeeper", "uart", "compositor", "ps2server", "test-run # `every_boot_config_runs_logkeeper` is what refuses a boot config without it. [programs.logkeeper] service = true -syscap = ["logread"] +syscap = ["logread", "console"] serves = ["log-carrier", "log-wire"] holds = ["log-origins"] diff --git a/tests/images/sessioncase.toml b/tests/images/sessioncase.toml index 3de418e1f56..a0948659397 100644 --- a/tests/images/sessioncase.toml +++ b/tests/images/sessioncase.toml @@ -21,7 +21,7 @@ start = ["acpiserver", "logkeeper", "uart", "compositor", "ps2server", "test-run [programs.logkeeper] service = true -syscap = ["logread"] +syscap = ["logread", "console"] serves = ["log-carrier", "log-wire"] holds = ["log-origins"] diff --git a/tests/images/testcases.toml b/tests/images/testcases.toml index e30b63335d2..b95eb1b0c0c 100644 --- a/tests/images/testcases.toml +++ b/tests/images/testcases.toml @@ -12,7 +12,7 @@ start = ["logkeeper", "uart", "diskserver", "fileserver", "soundserver", "acpise # the log on. [programs.logkeeper] service = true -syscap = ["logread"] +syscap = ["logread", "console"] serves = ["log", "log-carrier", "log-wire"] holds = ["log-origins"] diff --git a/tests/images/undeclaredcase.toml b/tests/images/undeclaredcase.toml index bc2de016a7a..4a4740cbb78 100644 --- a/tests/images/undeclaredcase.toml +++ b/tests/images/undeclaredcase.toml @@ -6,7 +6,7 @@ start = ["logkeeper", "uart", "test-runner"] [programs.logkeeper] service = true -syscap = ["logread"] +syscap = ["logread", "console"] serves = ["log-wire"] holds = ["log-origins"] diff --git a/tests/images/virtclaimcase.toml b/tests/images/virtclaimcase.toml index dfb9c91b165..7fb7d6e5cf2 100644 --- a/tests/images/virtclaimcase.toml +++ b/tests/images/virtclaimcase.toml @@ -8,7 +8,7 @@ start = ["acpiserver", "logkeeper", "uart", "test-runner"] [programs.logkeeper] service = true -syscap = ["logread"] +syscap = ["logread", "console"] serves = ["log-wire"] holds = ["log-origins"] diff --git a/tests/images/virtjobcase.toml b/tests/images/virtjobcase.toml index a3b7d34b6b2..b54f57d5730 100644 --- a/tests/images/virtjobcase.toml +++ b/tests/images/virtjobcase.toml @@ -8,7 +8,7 @@ start = ["logkeeper", "uart", "test-runner"] [programs.logkeeper] service = true -syscap = ["logread"] +syscap = ["logread", "console"] serves = ["log-wire"] holds = ["log-origins"] diff --git a/tests/images/virtpancase.toml b/tests/images/virtpancase.toml index 6f565d876ce..01b064efada 100644 --- a/tests/images/virtpancase.toml +++ b/tests/images/virtpancase.toml @@ -5,7 +5,7 @@ start = ["logkeeper", "uart", "test-runner"] [programs.logkeeper] service = true -syscap = ["logread"] +syscap = ["logread", "console"] serves = ["log-wire"] holds = ["log-origins"] diff --git a/tests/images/virtpaniccase.toml b/tests/images/virtpaniccase.toml index aab1d830c38..61b67e1fe83 100644 --- a/tests/images/virtpaniccase.toml +++ b/tests/images/virtpaniccase.toml @@ -6,7 +6,7 @@ start = ["logkeeper", "uart", "test-runner"] [programs.logkeeper] service = true -syscap = ["logread"] +syscap = ["logread", "console"] serves = ["log-wire"] holds = ["log-origins"] diff --git a/tests/images/virtrebootcase.toml b/tests/images/virtrebootcase.toml index 46f59022218..c7cac56142a 100644 --- a/tests/images/virtrebootcase.toml +++ b/tests/images/virtrebootcase.toml @@ -4,7 +4,7 @@ start = ["logkeeper", "uart", "test-runner"] [programs.logkeeper] service = true -syscap = ["logread"] +syscap = ["logread", "console"] serves = ["log-wire"] holds = ["log-origins"] diff --git a/tests/images/virtsmpcase.toml b/tests/images/virtsmpcase.toml index 8b466411b18..55ed191acce 100644 --- a/tests/images/virtsmpcase.toml +++ b/tests/images/virtsmpcase.toml @@ -10,7 +10,7 @@ start = ["logkeeper", "uart", "test-runner"] [programs.logkeeper] service = true -syscap = ["logread"] +syscap = ["logread", "console"] serves = ["log-wire"] holds = ["log-origins"] diff --git a/tests/toyos.rs b/tests/toyos.rs index c69697e61ad..73276a0ccd8 100644 --- a/tests/toyos.rs +++ b/tests/toyos.rs @@ -534,6 +534,12 @@ const MACHINE_TESTS: &[&str] = &[ // are three components only a boot has; q35 with no console port, so the // UART is the one console, read whole, which no bench's is. "the_uart_carries_the_boot_once_across_the_hand_over", + // A supervisor that fails before `logkeeper` runs says why on the UART, + // and the kernel ends the machine on its end: its last words and the + // kernel's fatal path, which only a boot has, on a `/system` missing the + // first file it reads, which only a staged image is. q35 with no console + // port, so the UART is the one console. + "a_supervisor_that_fails_early_says_why_on_the_uart", // An AMD-Vi unit left blocking every device, switched off and programmed // before the boot disk is driven behind it: the register sequence and the // DMA it lets through are no host test's, and the AMD bench is staged by @@ -3136,6 +3142,36 @@ fn the_uart_carries_the_boot_once_across_the_hand_over() -> Result<(), String> { Ok(()) } +/// What the kernel's fatal path says of the supervisor's end. +const SUPERVISOR_ENDED: &str = "the supervisor ended, "; + +/// A boot whose `/system` lacks the release file the supervisor reads first: +/// the supervisor's reason is on the UART as its last words, before the +/// kernel's panic for its end, though nothing carries the log yet. +fn a_supervisor_that_fails_early_says_why_on_the_uart() -> Result<(), String> { + let mut qemu = QemuInstance::boot_with_options( + &compile::repo_root().join("tests/images/testcases.toml"), + &[], + &[], + BootOptions { + profile: qemu::Profile::HeadlessNoConsolePort, + root_omits: &[toyos_osrelease::PATH], + ready_marker: SUPERVISOR_ENDED, + ..Default::default() + }, + ); + let mut console = format!("{}\n", qemu.boot_log()); + await_marker(&mut qemu, &mut console, SUPERVISOR_ENDED, "the kernel to end the machine on the supervisor's end")?; + let why = format!("last words: supervisor: cannot read {}", toyos_osrelease::GUEST_PATH); + let said = console.find(&why).ok_or_else(|| format!("the UART does not say {why:?}\n{console}"))?; + let ended = console.find(SUPERVISOR_ENDED).expect("awaited above"); + if said > ended { + return Err(format!("the supervisor's reason is on the UART after the kernel's panic\n{console}")); + } + eprintln!(" [supervisor] {}", console[said..].lines().next().unwrap_or_default()); + Ok(()) +} + /// Eight spawns from each of a file the root server holds in memory, a copy /// on DATA that the spawn reads whole, and a copy in the kernel's `/tmp`: /// each child maps its parent's log ring, and once they have ended the census @@ -4896,6 +4932,7 @@ fn run_machine_test(name: &str, test_config: &Path) -> Result<(), String> { "lent_device_memory_refused" => lent_device_memory_refused(test_config), "console_image_boots" => console_image_boots(), "the_uart_carries_the_boot_once_across_the_hand_over" => the_uart_carries_the_boot_once_across_the_hand_over(), + "a_supervisor_that_fails_early_says_why_on_the_uart" => a_supervisor_that_fails_early_says_why_on_the_uart(), "a_spawn_from_an_image_frees_it_when_the_child_ends" => a_spawn_from_an_image_frees_it_when_the_child_ends(test_config), "a_bent_supervisor_is_refused_and_the_other_slot_boots" => { [qemu::Profile::Headless, qemu::Profile::Virt].into_iter().try_for_each(|profile| { From 8ac4f218780016002f7d9584240af0d013405f9d Mon Sep 17 00:00:00 2001 From: japabu Date: Wed, 4 Nov 2026 10:03:16 +0100 Subject: [PATCH 6/6] console: the kernel says so as it takes the UART back from its driver The stop and every fatal path take the console UART back from its driver, which may be behind the log: on a fast guest whose console port carried the log, the UART's driver was still sending boot lines from its pipe when the machine stopped, and had not yet said the log was carried elsewhere. The harness shows a virtio machine's UART only from that word or a kernel's death, so the stop's lines, which only the UART had, were hidden, and machine_shutdown_short_stop stalled waiting for its last word under KVM. The kernel now starts its own lines there with a line that says it writes the UART from here and that what its driver had not sent is in /log (toyos_abi::log::KERNEL_TAKES_THE_UART), and the harness's UART filter shows the UART from it on. Co-Authored-By: Claude Opus 5.5 --- build/vmconsole.rs | 21 +++++++++++++++++++-- kernel/src/log/console.rs | 15 ++++++++------- sdk/abi/src/log.rs | 5 +++++ 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/build/vmconsole.rs b/build/vmconsole.rs index 8fe472b21be..19beb542227 100644 --- a/build/vmconsole.rs +++ b/build/vmconsole.rs @@ -21,6 +21,7 @@ use std::process::Child; use std::time::{Duration, Instant}; use toyos_logstream::death::{died, Died}; +use toyos_abi::log::KERNEL_TAKES_THE_UART; use toyos_logstream::{Showing, CARRIED_ELSEWHERE}; use toyos_virtio_serial::{CONSOLE_PORT, MAX_PORTS, PORT_ID}; @@ -85,7 +86,9 @@ impl Painter { /// /// **Its lines up to [`CARRIED_ELSEWHERE`] are the console port's too**, so /// they are not shown, but for a kernel's death, which no other channel will -/// carry; every line after either is. +/// carry; every line after either is, and after the kernel taking the UART +/// back for the stop or a fatal path ([`KERNEL_TAKES_THE_UART`]), which may +/// come before its driver said the first. #[derive(Default)] pub struct Uart { pending: Vec, @@ -104,7 +107,7 @@ impl Uart { self.live = true; continue; } - self.live |= died(text.trim_end()) == Some(Died::Kernel); + self.live |= died(text.trim_end()) == Some(Died::Kernel) || text.contains(KERNEL_TAKES_THE_UART); if self.live { shown.push(line); } @@ -505,6 +508,20 @@ mod tests { assert_eq!(String::from_utf8(shown).expect("UTF-8"), death); } + /// **A stop that takes the UART from a driver still behind the log** is + /// shown from the kernel's word on: its driver never reached the quiet + /// line, and what it was still sending is the port's. + #[test] + fn the_kernels_taking_the_uart_is_shown_from() { + let behind = "[ 2.035 cpu0 kernel] pcidev: PCI 00:01."; + let stop = format!("\n{KERNEL_TAKES_THE_UART}\n[ 2.531 cpu0 kernel] mmio: x\n[ 2.551 cpu0 kernel] Shutting down.\n"); + let mut uart = Uart::default(); + let shown: Vec = uart.pass(format!("{behind}{stop}").as_bytes()).concat(); + let shown = String::from_utf8(shown).expect("UTF-8"); + assert!(shown.starts_with(KERNEL_TAKES_THE_UART), "{shown:?}"); + assert!(shown.ends_with("Shutting down.\n"), "{shown:?}"); + } + /// **The launch's merge**: the port's lines in order, then the UART's from /// the carrier's word on, the port drained before each UART read; the /// port's last unended line at its end; and what the UART says after the diff --git a/kernel/src/log/console.rs b/kernel/src/log/console.rs index 5865d3b73be..8bc057d3212 100644 --- a/kernel/src/log/console.rs +++ b/kernel/src/log/console.rs @@ -117,12 +117,10 @@ impl StopWire { } } -/// The UART, for the stop once it has stopped userland: a line of its own, -/// since the holder may have been stopped mid-line. +/// The UART, for the stop once it has stopped userland: from a line of its +/// own, since the holder may have been stopped mid-line or behind the log. pub fn take_for_the_stop() -> StopWire { - if !inline() { - serial::write(b"\n"); - } + after_the_holder(serial::write); StopWire(()) } @@ -143,11 +141,14 @@ pub unsafe fn drain_bypassed(uart: &mut PanicUart) { drain_ordered(&Shards, &mut &DRAINED, &mut Spoken(|bytes: &[u8]| uart.write(bytes))); } -/// What a fatal path starts with: a line of its own, where a holder may have -/// left one unended. +/// What the stop and a fatal path start with where the UART was its +/// holder's: a line of their own, which the holder may have left unended, +/// saying the kernel's lines follow ([`toyos_abi::log::KERNEL_TAKES_THE_UART`]). fn after_the_holder(mut write: impl FnMut(&[u8])) { if !inline() { write(b"\n"); + write(toyos_abi::log::KERNEL_TAKES_THE_UART.as_bytes()); + write(b"\n"); } } diff --git a/sdk/abi/src/log.rs b/sdk/abi/src/log.rs index b3f9baaf97e..0d898b87f91 100644 --- a/sdk/abi/src/log.rs +++ b/sdk/abi/src/log.rs @@ -20,6 +20,11 @@ /// (`toyos-elide`). pub const MAX_RECORD_MESSAGE: usize = 992; +/// The line the kernel puts on the console UART as it takes the UART back +/// from its driver, for the stop or a fatal path: what follows is the +/// kernel's, whatever the driver had not yet sent of the log before it. +pub const KERNEL_TAKES_THE_UART: &str = "console: the kernel writes the UART from here; what its driver had not sent is in /log"; + /// The longest last words a caller may say (`crate::syscall::log_last_words`), /// in bytes: a reason, with room in its record for what the kernel says of it. pub const MAX_LAST_WORDS: usize = 512;