nros_executor_layout/lib.rs
1//! The executor backing's placement arithmetic.
2//!
3//! # Why this is its own crate
4//!
5//! The executor's per-entry storage lives in a `.bss` static. On an RTOS the
6//! allocator reservation it used to come out of is itself fixed, so whatever
7//! sizes that reservation has to know how big the backing is.
8//!
9//! The size is `arena + tables`. The arena is plain arithmetic
10//! `nros-node/build.rs` already computes. The tables are `repr(Rust)` layout —
11//! field order, padding and niche packing are the compiler's to choose and to
12//! CHANGE between versions — so they can never be re-derived outside it. That is
13//! the sizes-header mirror class (0088 -> 0114 -> 0122 -> 0123 -> 0245 -> 0268)
14//! this tree has re-fixed six times.
15//!
16//! So the arithmetic takes the per-type facts as DATA. `nros-node` supplies what
17//! its compiler says while building for the target; a build script supplies
18//! units recovered for a target it is not itself compiling (the way `nros-c`
19//! recovers `EXECUTOR_OPAQUE_U64S` through `nros-sizes-build`). One
20//! implementation, two sources of the inputs.
21//!
22//! It is a separate crate rather than a `pub fn` in `nros-node` for one reason:
23//! a build script runs on the HOST, and a build-dependency on `nros-node` would
24//! give it a host-compiled `nros-node` — the wrong layout, silently. This crate
25//! is `no_std` and dependency-free so both sides can reach it.
26//!
27//! # What it does NOT decide
28//!
29//! The counts. `cbs`/`sc`/`nodes`/`arena` come from `nros-node/build.rs` and,
30//! on a synced leaf, from the derived env `nros sync` writes. This crate only
31//! places regions.
32
33#![no_std]
34#![forbid(unsafe_code)]
35
36/// Size and alignment of one region's element type.
37#[derive(Clone, Copy, Debug, PartialEq, Eq)]
38pub struct RegionUnit {
39 /// `size_of::<T>()`.
40 pub size: usize,
41 /// `align_of::<T>()`. Must be a power of two; zero is rejected as 1.
42 pub align: usize,
43}
44
45/// Every element type the backing carves a table of, in declaration order.
46///
47/// The arena is absent on purpose: it is `[MaybeUninit<u8>]`, size 1 align 1 by
48/// definition, and its LENGTH is a count rather than a unit.
49#[derive(Clone, Copy, Debug, PartialEq, Eq)]
50pub struct RegionUnits {
51 pub callback_meta: RegionUnit,
52 pub sched_context: RegionUnit,
53 pub sched_context_id: RegionUnit,
54 pub sporadic_state: RegionUnit,
55 /// Placed only when [`Counts::alloc`] is set.
56 pub sporadic_atomic: RegionUnit,
57 pub remap_rule: RegionUnit,
58 pub node_record: RegionUnit,
59 pub concrete_session: RegionUnit,
60 pub extra_session_id: RegionUnit,
61 pub node_sched_entry: RegionUnit,
62 pub component_slot: RegionUnit,
63 pub group_name: RegionUnit,
64 pub group_sched_entry: RegionUnit,
65 pub violation: RegionUnit,
66 /// Alive supervision's per-SchedContext record, one per `sc`.
67 pub alive_slot: RegionUnit,
68 /// The per-callback-slot lifetime tag (generation + owning node), one per
69 /// `cbs`. Placed after every older region so each keeps its offset.
70 pub slot_tag: RegionUnit,
71 /// One parameter-store slot (`nros_params::ParameterSlot`), one per
72 /// [`Counts::params`]. Placed LAST, for the reason `slot_tag` was: an image
73 /// that carves no store (`params == 0`) keeps every offset and the total it
74 /// had before the region existed.
75 pub parameter_slot: RegionUnit,
76 /// The parameter SERVICES' home (`nros_node`'s `ParamHome`: their state,
77 /// the shared request/reply buffer pair and the staging slot a remote set
78 /// decodes its value into), one per [`Counts::param_home`]. Placed after
79 /// the store's slots for the same reason they are last: an image with no
80 /// store keeps every offset and its total.
81 pub param_home: RegionUnit,
82}
83
84/// How many of each region the image needs.
85///
86/// `remaps` and `violations` are fixed counts in `nros-node` rather than knobs;
87/// they are parameters here so this crate states no policy.
88#[derive(Clone, Copy, Debug, PartialEq, Eq)]
89pub struct Counts {
90 pub cbs: usize,
91 pub sc: usize,
92 pub nodes: usize,
93 /// Extra-session slots (and their ids): sessions a node opens on an RMW
94 /// backend other than the primary's. At most `nodes`; `nros-node` sizes
95 /// it from the image's backend count (0 when it links one), this crate
96 /// only places the region.
97 pub extra_sessions: usize,
98 pub arena: usize,
99 pub remaps: usize,
100 pub violations: usize,
101 /// `nros-node`'s `alloc` feature — it adds the sporadic-atomic table, so it
102 /// changes the SIZE and cannot be inferred from the counts.
103 pub alloc: bool,
104 /// Parameter-store slots carved from the backing. ZERO for an image the
105 /// build does not know to build a store, which is what keeps the region
106 /// off every image that merely links the declare path (a `.bss` static
107 /// would be unconditional). Policy is `nros-params`'; this crate only
108 /// places the region.
109 pub params: usize,
110 /// Parameter-service homes carved from the backing: one when the image
111 /// carves a store, zero otherwise. `nros-node` decides; this crate only
112 /// places the region.
113 pub param_home: usize,
114}
115
116/// Byte offset of each region, plus the total size and alignment.
117#[derive(Clone, Copy, Debug, PartialEq, Eq)]
118pub struct Offsets {
119 pub arena: usize,
120 pub entries: usize,
121 pub sched_contexts: usize,
122 pub sched_context_bindings: usize,
123 pub sporadic_states: usize,
124 pub sporadic_atomic_states: usize,
125 pub remaps: usize,
126 pub nodes: usize,
127 pub extra_sessions: usize,
128 pub extra_session_ids: usize,
129 pub node_sched_table: usize,
130 pub component_slots: usize,
131 pub active_groups: usize,
132 pub group_sched_table: usize,
133 pub monitor_violations: usize,
134 pub alive_slots: usize,
135 pub slot_tags: usize,
136 pub params: usize,
137 pub param_home: usize,
138 pub size: usize,
139 pub align: usize,
140}
141
142const fn align_up(off: usize, align: usize) -> usize {
143 off.div_ceil(align) * align
144}
145
146/// Place every region and report the offsets, total size and alignment.
147///
148/// Reproduces `#[repr(C)]` declaration-order layout: the arena first at offset
149/// 0, then each table aligned up to its own element alignment, and the total
150/// rounded up to the maximum alignment seen.
151pub const fn offsets(counts: Counts, units: RegionUnits) -> Offsets {
152 let Counts {
153 cbs,
154 sc,
155 nodes: node_slots,
156 extra_sessions: extra_slots,
157 arena,
158 remaps: remap_slots,
159 violations: violation_slots,
160 alloc,
161 params: param_slots,
162 param_home: home_slots,
163 } = counts;
164
165 let mut off = 0usize;
166 let mut max_align = 1usize;
167
168 // arena: [MaybeUninit<u8>; arena] — align 1, at offset 0.
169 let arena_off = 0usize;
170 off += arena;
171
172 macro_rules! place {
173 ($n:expr, $u:expr) => {{
174 let RegionUnit { size, align } = $u;
175 // A zero alignment is not a layout any compiler produces; treating
176 // it as 1 keeps this total rather than panicking in a `const fn`,
177 // and a caller that supplies one gets an answer it can compare.
178 let a = if align == 0 { 1 } else { align };
179 if a > max_align {
180 max_align = a;
181 }
182 off = align_up(off, a);
183 let at = off;
184 off += $n * size;
185 at
186 }};
187 }
188
189 let entries = place!(cbs, units.callback_meta);
190 let sched_contexts = place!(sc, units.sched_context);
191 let sched_context_bindings = place!(cbs, units.sched_context_id);
192 let sporadic_states = place!(sc, units.sporadic_state);
193 // Placed only under `alloc`, and the `else` must not move `off` — the
194 // no-alloc layout is the one every bare-metal image links.
195 let sporadic_atomic_states = if alloc {
196 place!(sc, units.sporadic_atomic)
197 } else {
198 off
199 };
200 let remaps = place!(remap_slots, units.remap_rule);
201 let nodes = place!(node_slots, units.node_record);
202 let extra_sessions = place!(extra_slots, units.concrete_session);
203 let extra_session_ids = place!(extra_slots, units.extra_session_id);
204 let node_sched_table = place!(node_slots, units.node_sched_entry);
205 let component_slots = place!(node_slots, units.component_slot);
206 let active_groups = place!(node_slots, units.group_name);
207 let group_sched_table = place!(cbs, units.group_sched_entry);
208 let monitor_violations = place!(violation_slots, units.violation);
209 let alive_slots = place!(sc, units.alive_slot);
210 let slot_tags = place!(cbs, units.slot_tag);
211 // Zero slots must not move `off` either: `place!` would still align it up
212 // to the slot's alignment, which is padding an image with no store would
213 // pay for nothing (and a total that moved for it). So an EMPTY region's
214 // offset may be misaligned for its type: a consumer must not build a slice
215 // from it (`nros_node`'s `carve` uses `region_slice`, issue 1771).
216 let params = if param_slots > 0 {
217 place!(param_slots, units.parameter_slot)
218 } else {
219 off
220 };
221 // Issue 1812 — same rule as the store: no home, no padding.
222 let param_home = if home_slots > 0 {
223 place!(home_slots, units.param_home)
224 } else {
225 off
226 };
227
228 Offsets {
229 arena: arena_off,
230 entries,
231 sched_contexts,
232 sched_context_bindings,
233 sporadic_states,
234 sporadic_atomic_states,
235 remaps,
236 nodes,
237 extra_sessions,
238 extra_session_ids,
239 node_sched_table,
240 component_slots,
241 active_groups,
242 group_sched_table,
243 monitor_violations,
244 alive_slots,
245 slot_tags,
246 params,
247 param_home,
248 size: align_up(off, max_align),
249 align: max_align,
250 }
251}
252
253/// The backing's total size in bytes — what a reservation has to hold.
254pub const fn size_of_backing(counts: Counts, units: RegionUnits) -> usize {
255 offsets(counts, units).size
256}