Skip to main content

strat9_kernel/ipc/v2/
mod.rs

1/*
2 * This file is part of strat9-os.
3 *
4 * strat9-os is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * strat9-os is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with strat9-os. If not, see <https://www.gnu.org/licenses/>.
16 *
17 * Website: https://www.strat9-os.org
18 * Repository: https://github.com/strat9-os/strat9-os
19 *
20 * Copyright (C) 2023-26 Guillaume Gielly
21 */
22
23//! Kernel core of IPC ABI v2: generation handles, endpoints, reply targets,
24//! notifications, memory objects, silo quotas and async-ring operations
25//! (§3–§12). The namespace table lives in `crate::namespace::v2`.
26
27pub mod async_ops;
28pub mod endpoint;
29pub mod handle;
30pub mod memobj;
31pub mod notif;
32pub mod quota;
33pub mod selftest;
34
35pub use handle::{Entry, HandleTable, ObjRef, MAX_HANDLES};
36
37use crate::{
38    process::{process::Process, TaskId},
39    sync::SpinLock,
40};
41use alloc::{
42    sync::{Arc, Weak},
43    vec::Vec,
44};
45use endpoint::{abandon_call, cancel_call, CallSlot};
46
47/// Weak registry of live call slots so task/endpoint death can cancel calls
48/// without ever touching another address space.
49static CALL_SLOTS: SpinLock<Vec<Weak<CallSlot>>> = SpinLock::new(Vec::new());
50
51/// Register a call slot for exit/endpoint cleanup.
52///
53/// Dead entries are otherwise pruned only by the exit/close scans, so a
54/// long-lived client calling in a loop would grow the registry by one entry
55/// per call. Pruning whenever the length reaches a power of two keeps it
56/// proportional to the number of live calls at amortised O(1) per call.
57pub fn register_call(slot: &Arc<CallSlot>) {
58    let mut guard = CALL_SLOTS.lock();
59    let len = guard.len();
60    if len >= 64 && len.is_power_of_two() {
61        guard.retain(|weak| weak.strong_count() > 0);
62    }
63    guard.push(Arc::downgrade(slot));
64}
65
66fn collect_calls<F: Fn(&CallSlot) -> bool>(filter: F) -> Vec<Arc<CallSlot>> {
67    let mut guard = CALL_SLOTS.lock();
68    let mut found = Vec::new();
69    guard.retain(|weak| match weak.upgrade() {
70        Some(slot) => {
71            if filter(&slot) {
72                found.push(slot);
73            }
74            true
75        }
76        None => false,
77    });
78    drop(guard);
79    found
80}
81
82/// Cancel every call whose caller is `task_id`. Called when a task exits so a
83/// late server reply fails with `EPIPE` instead of writing a freed buffer.
84pub fn cleanup_for_task(task_id: TaskId) {
85    let slots = collect_calls(|slot| *slot.caller.lock() == Some(task_id));
86    for slot in slots {
87        // The caller is gone: a reply already stored (`Done`) will never be
88        // copied out, so its capabilities must be released too.
89        abandon_call(&slot);
90    }
91}
92
93/// Cancel every call attached to `endpoint_id` (called on endpoint close).
94pub fn cancel_calls_for_endpoint(endpoint_id: u64) {
95    let slots = collect_calls(|slot| slot.endpoint_id == endpoint_id);
96    for slot in slots {
97        cancel_call(&slot);
98    }
99}
100
101/// Drop every handle owned by a dying process, applying object side effects.
102///
103/// `fork_clone` shares objects with the parent; the child has its own table,
104/// so this only releases the child's references.
105pub fn cleanup_process(process: &Process) {
106    process.ipc_v2.lock().remove_all();
107}