consortium-cfg 0.5.0

Configuration subsystem support for the Consortium project.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
// Copyright 2026 Ethan Wu
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// SPDX-License-Identifier: Apache-2.0

//! Structured remoteproc memory and GNU linker generation.

use std::collections::HashSet;
use std::fmt::Write as _;

use consortium_cfg_common::config::VisibleCore;
pub use consortium_cfg_common::database::VectorPlacement;
use thiserror::Error;

/// One firmware address window shared by linker, ELF, and DTS generation.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RemoteprocMemoryWindow {
    pub origin: u64,
    pub length: u64,
    pub reserve: bool,
}

impl RemoteprocMemoryWindow {
    pub fn end(self) -> Option<u64> {
        self.origin.checked_add(self.length)
    }

    pub fn contains(self, start: u64, length: u64) -> bool {
        let Some(end) = start.checked_add(length) else {
            return false;
        };
        self.origin <= start && self.end().is_some_and(|limit| end <= limit)
    }
}

/// One input-to-output section binding for an auxiliary linker region.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LinkerSectionBinding {
    pub output: String,
    pub inputs: Vec<String>,
    pub keep: bool,
    pub insert_after: String,
}

/// A named vector or user-defined auxiliary region.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RemoteprocNamedRegion {
    pub name: String,
    pub attributes: LinkerRegionAttributes,
    pub window: RemoteprocMemoryWindow,
    pub sections: Vec<LinkerSectionBinding>,
}

/// Canonical contract for one Linux-managed remote core.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RemoteprocMemoryLayout {
    pub core: VisibleCore,
    pub compatible: String,
    pub vector_section: String,
    /// How `FLASH` relates to the vector window in the generated `memory.x`.
    pub vector_placement: VectorPlacement,
    /// Mandatory vector-table window immediately before the code window.
    pub vector: RemoteprocNamedRegion,
    pub flash: RemoteprocMemoryWindow,
    /// Mandatory remoteproc shared memory containing `.resource_table`.
    pub shmem: RemoteprocNamedRegion,
    pub ram: RemoteprocMemoryWindow,
    /// Optional manifest-defined regions.
    pub regions: Vec<RemoteprocNamedRegion>,
}

impl RemoteprocMemoryLayout {
    pub fn validate(&self) -> Result<(), LinkerMemoryError> {
        validate_linker_window("FLASH", self.flash)?;
        validate_linker_window("RAM", self.ram)?;
        validate_linker_window(&self.vector.name, self.vector.window)?;
        validate_linker_window(&self.shmem.name, self.shmem.window)?;
        LinkerRegionName::new(&self.vector.name)?;
        LinkerRegionName::new(&self.shmem.name)?;
        validate_attributes(&self.vector.name, self.vector.attributes)?;
        validate_attributes(&self.shmem.name, self.shmem.attributes)?;

        let mut names = HashSet::from([
            "FLASH".to_string(),
            "RAM".to_string(),
            self.vector.name.clone(),
            self.shmem.name.clone(),
        ]);
        if names.len() != 4 {
            return Err(LinkerMemoryError::Duplicate(
                "mandatory remoteproc region".into(),
            ));
        }
        let mut prior: Vec<(&str, RemoteprocMemoryWindow)> = vec![
            (&self.vector.name, self.vector.window),
            ("FLASH", self.flash),
            (&self.shmem.name, self.shmem.window),
            ("RAM", self.ram),
        ];
        for (index, (left_name, left)) in prior.iter().enumerate() {
            for (right_name, right) in prior.iter().skip(index + 1) {
                if overlaps_window(*left, *right) {
                    return Err(LinkerMemoryError::Overlap {
                        first: (*left_name).into(),
                        second: (*right_name).into(),
                    });
                }
            }
        }
        if self.vector_placement == VectorPlacement::Flash
            && self.vector.window.end() != Some(self.flash.origin)
        {
            return Err(LinkerMemoryError::NotAdjacent {
                first: self.vector.name.clone(),
                second: "FLASH".into(),
            });
        }
        for region in &self.regions {
            LinkerRegionName::new(&region.name)?;
            if !names.insert(region.name.clone()) {
                return Err(LinkerMemoryError::Duplicate(region.name.clone()));
            }
            validate_attributes(&region.name, region.attributes)?;
            validate_linker_window(&region.name, region.window)?;
            validate_bindings(&region.name, &region.sections)?;
            for (name, window) in &prior {
                if overlaps_window(*window, region.window) {
                    return Err(LinkerMemoryError::Overlap {
                        first: (*name).into(),
                        second: region.name.clone(),
                    });
                }
            }
            prior.push((&region.name, region.window));
        }
        for (name, window) in &prior {
            if window.reserve {
                validate_page_alignment(name, *window)?;
            }
        }
        Ok(())
    }

    pub fn linker_memory(&self) -> Result<LinkerMemory, LinkerMemoryError> {
        self.validate()?;
        let flash = LinkerRegionName::new("FLASH")?;
        // With `VectorPlacement::Flash`, `cortex-m-rt` places `.vector_table` at
        // `ORIGIN(FLASH)`, so `FLASH` starts at the vector window and `_stext`
        // (below) keeps code in the manifest's flash window; `validate`
        // guarantees the two are adjacent. With `Separate`, `FLASH` is the flash
        // window as declared.
        let (flash_origin, flash_length, stext) = match self.vector_placement {
            VectorPlacement::Flash => (
                self.vector.window.origin,
                self.flash.origin - self.vector.window.origin + self.flash.length,
                Some(self.flash.origin),
            ),
            VectorPlacement::Separate => (self.flash.origin, self.flash.length, None),
        };
        let mut regions = vec![
            LinkerMemoryRegion {
                name: LinkerRegionName::new(&self.vector.name)?,
                role: LinkerRegionRole::Vector,
                attributes: self.vector.attributes,
                origin: self.vector.window.origin,
                length: self.vector.window.length,
                sections: Vec::new(),
            },
            LinkerMemoryRegion {
                name: flash,
                role: LinkerRegionRole::Flash,
                attributes: LinkerRegionAttributes::new(true, false, true),
                origin: flash_origin,
                length: flash_length,
                sections: Vec::new(),
            },
            LinkerMemoryRegion {
                name: LinkerRegionName::new(&self.shmem.name)?,
                role: LinkerRegionRole::Auxiliary,
                attributes: self.shmem.attributes,
                origin: self.shmem.window.origin,
                length: self.shmem.window.length,
                sections: vec![LinkerSectionBinding {
                    output: ".resource_table".into(),
                    inputs: vec![".resource_table".into(), ".resource_table.*".into()],
                    keep: true,
                    insert_after: ".rodata".into(),
                }],
            },
        ];
        regions.extend(self.regions.iter().map(|region| LinkerMemoryRegion {
            name: LinkerRegionName::new(&region.name).expect("layout validated"),
            role: LinkerRegionRole::Auxiliary,
            attributes: region.attributes,
            origin: region.window.origin,
            length: region.window.length,
            sections: region.sections.clone(),
        }));
        regions.push(LinkerMemoryRegion {
            name: LinkerRegionName::new("RAM")?,
            role: LinkerRegionRole::Ram,
            attributes: LinkerRegionAttributes::new(true, true, true),
            origin: self.ram.origin,
            length: self.ram.length,
            sections: Vec::new(),
        });
        let memory = LinkerMemory::new(regions)?;
        Ok(match stext {
            Some(stext) => memory.with_stext(stext),
            None => memory,
        })
    }

    /// Every configured window in which an ELF load segment may reside.
    pub fn load_windows(&self) -> impl Iterator<Item = RemoteprocMemoryWindow> + '_ {
        [self.vector.window, self.flash, self.shmem.window, self.ram]
            .into_iter()
            .chain(self.regions.iter().map(|region| region.window))
    }
}

fn validate_linker_window(
    name: &str,
    window: RemoteprocMemoryWindow,
) -> Result<(), LinkerMemoryError> {
    if window.length == 0 {
        return Err(LinkerMemoryError::Empty(name.into()));
    }
    if window.end().is_none() {
        return Err(LinkerMemoryError::Overflow(name.into()));
    }
    if !window.origin.is_multiple_of(4) || !window.length.is_multiple_of(4) {
        return Err(LinkerMemoryError::Unaligned {
            name: name.into(),
            alignment: 4,
        });
    }
    Ok(())
}

fn validate_page_alignment(
    name: &str,
    window: RemoteprocMemoryWindow,
) -> Result<(), LinkerMemoryError> {
    if !window.origin.is_multiple_of(4096) || !window.length.is_multiple_of(4096) {
        return Err(LinkerMemoryError::Unaligned {
            name: name.into(),
            alignment: 4096,
        });
    }
    Ok(())
}

fn overlaps_window(left: RemoteprocMemoryWindow, right: RemoteprocMemoryWindow) -> bool {
    overlaps(
        left.origin,
        left.end().expect("validated"),
        right.origin,
        right.end().expect("validated"),
    )
}

fn overlaps(a_start: u64, a_end: u64, b_start: u64, b_end: u64) -> bool {
    a_start < b_end && b_start < a_end
}

/// `FLASH` deliberately spans the vector region: `cortex-m-rt` links
/// `.vector_table` at `ORIGIN(FLASH)` and offers no way to name another region.
fn flash_holds_vector(flash: &LinkerMemoryRegion, vector: &LinkerMemoryRegion) -> bool {
    flash.role == LinkerRegionRole::Flash
        && vector.role == LinkerRegionRole::Vector
        && flash.origin <= vector.origin
        && vector.origin + vector.length <= flash.origin + flash.length
}

fn validate_attributes(
    name: &str,
    attributes: LinkerRegionAttributes,
) -> Result<(), LinkerMemoryError> {
    if attributes.read || attributes.write || attributes.execute {
        Ok(())
    } else {
        Err(LinkerMemoryError::EmptyAttributes(name.into()))
    }
}

fn validate_bindings(
    region: &str,
    bindings: &[LinkerSectionBinding],
) -> Result<(), LinkerMemoryError> {
    let mut outputs = HashSet::new();
    for binding in bindings {
        const CORTEX_M_RT_OUTPUTS: &[&str] = &[
            ".vector_table",
            ".text",
            ".rodata",
            ".data",
            ".bss",
            ".uninit",
        ];
        if !valid_section_pattern(&binding.output, false)
            || !valid_section_pattern(&binding.insert_after, false)
            || binding.inputs.is_empty()
            || binding
                .inputs
                .iter()
                .any(|input| !valid_section_pattern(input, true))
            || CORTEX_M_RT_OUTPUTS.contains(&binding.output.as_str())
        {
            return Err(LinkerMemoryError::InvalidSection {
                region: region.into(),
                section: binding.output.clone(),
            });
        }
        if !outputs.insert(binding.output.as_str()) {
            return Err(LinkerMemoryError::DuplicateSection(binding.output.clone()));
        }
    }
    Ok(())
}

fn valid_section_pattern(value: &str, wildcard: bool) -> bool {
    value.starts_with('.')
        && value.len() > 1
        && value.chars().all(|character| {
            character.is_ascii_alphanumeric()
                || matches!(character, '.' | '_' | '-')
                || (wildcard && character == '*')
        })
}

/// A typed GNU linker `MEMORY` command plus optional section-placement
/// augmentations for `cortex-m-rt`'s `link.x`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LinkerMemory {
    regions: Vec<LinkerMemoryRegion>,
    stext: Option<u64>,
}

impl LinkerMemory {
    pub fn new(mut regions: Vec<LinkerMemoryRegion>) -> Result<Self, LinkerMemoryError> {
        regions.sort_by(|left, right| {
            left.role
                .cmp(&right.role)
                .then_with(|| left.name.as_str().cmp(right.name.as_str()))
        });
        let mut names = HashSet::new();
        let mut outputs = HashSet::new();
        for region in &regions {
            let name = region.name.as_str();
            if !names.insert(name) {
                return Err(LinkerMemoryError::Duplicate(name.into()));
            }
            validate_attributes(name, region.attributes)?;
            validate_linker_window(
                name,
                RemoteprocMemoryWindow {
                    origin: region.origin,
                    length: region.length,
                    reserve: false,
                },
            )?;
            validate_bindings(name, &region.sections)?;
            for section in &region.sections {
                if !outputs.insert(section.output.as_str()) {
                    return Err(LinkerMemoryError::DuplicateSection(section.output.clone()));
                }
            }
        }
        for (index, left) in regions.iter().enumerate() {
            let left_end = left.origin + left.length;
            for right in regions.iter().skip(index + 1) {
                let right_end = right.origin + right.length;
                if overlaps(left.origin, left_end, right.origin, right_end)
                    && !flash_holds_vector(left, right)
                    && !flash_holds_vector(right, left)
                {
                    return Err(LinkerMemoryError::Overlap {
                        first: left.name.as_str().into(),
                        second: right.name.as_str().into(),
                    });
                }
            }
        }
        Ok(Self {
            regions,
            stext: None,
        })
    }

    /// Pins `cortex-m-rt`'s `_stext`, so `.text` starts at `stext` instead of
    /// directly after `.vector_table`.
    pub fn with_stext(mut self, stext: u64) -> Self {
        self.stext = Some(stext);
        self
    }

    pub fn regions(&self) -> &[LinkerMemoryRegion] {
        &self.regions
    }

    /// Renders the generated `memory.x`. The `MEMORY` command is followed only
    /// by explicitly configured section bindings; ordinary placement remains
    /// owned by `cortex-m-rt`'s `link.x`.
    pub fn render(&self) -> String {
        let mut out = String::from("/* Auto-generated by Consortium. Do not edit. */\nMEMORY\n{\n");
        for region in &self.regions {
            writeln!(
                out,
                "  {} ({}) : ORIGIN = {:#010x}, LENGTH = {:#x}",
                region.name.as_str(),
                region.attributes.render(),
                region.origin,
                region.length
            )
            .expect("writing a String cannot fail");
        }
        out.push_str("}\n");
        if let Some(stext) = self.stext {
            writeln!(out, "\n_stext = {stext:#010x};").expect("String write");
        }
        for region in &self.regions {
            for section in &region.sections {
                out.push_str("\nSECTIONS\n{\n");
                writeln!(out, "  {} :", section.output).expect("String write");
                out.push_str("  {\n");
                for input in &section.inputs {
                    if section.keep {
                        writeln!(out, "    KEEP(*({input}))").expect("String write");
                    } else {
                        writeln!(out, "    *({input})").expect("String write");
                    }
                }
                writeln!(out, "  }} > {}", region.name.as_str()).expect("String write");
                writeln!(out, "}} INSERT AFTER {};", section.insert_after).expect("String write");
            }
        }
        out
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct LinkerRegionName(String);

impl LinkerRegionName {
    pub fn new(value: impl Into<String>) -> Result<Self, LinkerMemoryError> {
        let value = value.into();
        let mut chars = value.chars();
        let valid = chars
            .next()
            .is_some_and(|first| first.is_ascii_alphabetic() || first == '_')
            && chars.all(|character| character.is_ascii_alphanumeric() || character == '_');
        if valid {
            Ok(Self(value))
        } else {
            Err(LinkerMemoryError::InvalidName(value))
        }
    }

    pub fn as_str(&self) -> &str {
        &self.0
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum LinkerRegionRole {
    Vector,
    Flash,
    Auxiliary,
    Ram,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct LinkerRegionAttributes {
    pub read: bool,
    pub write: bool,
    pub execute: bool,
}

impl LinkerRegionAttributes {
    pub const fn new(read: bool, write: bool, execute: bool) -> Self {
        Self {
            read,
            write,
            execute,
        }
    }

    fn render(self) -> String {
        let mut out = String::new();
        if self.read {
            out.push('r');
        }
        if self.write {
            out.push('w');
        }
        if self.execute {
            out.push('x');
        }
        out
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LinkerMemoryRegion {
    pub name: LinkerRegionName,
    pub role: LinkerRegionRole,
    pub attributes: LinkerRegionAttributes,
    pub origin: u64,
    pub length: u64,
    pub sections: Vec<LinkerSectionBinding>,
}

#[derive(Debug, Clone, PartialEq, Eq, Error)]
pub enum LinkerMemoryError {
    #[error("linker memory region '{0}' has zero length")]
    Empty(String),
    #[error("linker memory region '{0}' address range overflows")]
    Overflow(String),
    #[error("linker memory region '{name}' must be {alignment}-byte aligned")]
    Unaligned { name: String, alignment: u64 },
    #[error("linker memory region '{0}' has no access attributes")]
    EmptyAttributes(String),
    #[error("linker memory region name '{0}' is not a valid linker identifier")]
    InvalidName(String),
    #[error("linker memory region '{0}' is declared more than once")]
    Duplicate(String),
    #[error("linker output section '{0}' is declared more than once")]
    DuplicateSection(String),
    #[error("linker section '{section}' for region '{region}' is invalid")]
    InvalidSection { region: String, section: String },
    #[error("linker memory regions '{first}' and '{second}' overlap")]
    Overlap { first: String, second: String },
    #[error("remoteproc region '{first}' must end exactly where '{second}' begins")]
    NotAdjacent { first: String, second: String },
}

#[cfg(test)]
mod tests {
    use super::*;

    fn region(name: &str, role: LinkerRegionRole, origin: u64, length: u64) -> LinkerMemoryRegion {
        LinkerMemoryRegion {
            name: LinkerRegionName::new(name).unwrap(),
            role,
            attributes: LinkerRegionAttributes::new(true, true, false),
            origin,
            length,
            sections: Vec::new(),
        }
    }

    #[test]
    fn renders_contiguous_vector_flash_and_mandatory_shmem() {
        let vector = region(
            "NS_VECTOR_TBL",
            LinkerRegionRole::Vector,
            0x8010_0000,
            0x1000,
        );
        let mut shmem = region("SHMEM", LinkerRegionRole::Auxiliary, 0x812f_8000, 0x8000);
        shmem.sections.push(LinkerSectionBinding {
            output: ".resource_table".into(),
            inputs: vec![".resource_table".into(), ".resource_table.*".into()],
            keep: true,
            insert_after: ".rodata".into(),
        });
        let memory = LinkerMemory::new(vec![
            region("RAM", LinkerRegionRole::Ram, 0x80a0_0000, 0x100000),
            shmem,
            region("FLASH", LinkerRegionRole::Flash, 0x8010_1000, 0x7ff000),
            vector,
        ])
        .unwrap();
        let rendered = memory.render();
        assert!(rendered.starts_with(
            "/* Auto-generated by Consortium. Do not edit. */\nMEMORY\n{\n  NS_VECTOR_TBL (rw)"
        ));
        assert!(rendered.contains("FLASH (rw) : ORIGIN = 0x80101000"));
        assert!(rendered.contains("SHMEM (rw) : ORIGIN = 0x812f8000"));
        assert!(rendered.contains("KEEP(*(.resource_table.*))"));
        assert!(rendered.contains("} INSERT AFTER .rodata;"));
    }

    #[test]
    fn validates_names_alignment_duplicates_and_overlap() {
        assert!(matches!(
            LinkerRegionName::new("bad-name"),
            Err(LinkerMemoryError::InvalidName(_))
        ));
        assert!(matches!(
            LinkerMemory::new(vec![
                region("FLASH", LinkerRegionRole::Flash, 0, 0x1000),
                region("FLASH", LinkerRegionRole::Ram, 0x2000, 0x1000),
            ]),
            Err(LinkerMemoryError::Duplicate(_))
        ));
        assert!(matches!(
            LinkerMemory::new(vec![region("RAM", LinkerRegionRole::Ram, 0, 0)]),
            Err(LinkerMemoryError::Empty(_))
        ));
        assert!(matches!(
            LinkerMemory::new(vec![region("RAM", LinkerRegionRole::Ram, u64::MAX - 3, 8)]),
            Err(LinkerMemoryError::Overflow(_))
        ));
        assert!(matches!(
            LinkerMemory::new(vec![
                region("FLASH", LinkerRegionRole::Flash, 0, 0x2000),
                region("RAM", LinkerRegionRole::Ram, 0x1000, 0x2000),
            ]),
            Err(LinkerMemoryError::Overlap { .. })
        ));
        assert!(matches!(
            LinkerMemory::new(vec![region("FLASH", LinkerRegionRole::Flash, 1, 0x1000)]),
            Err(LinkerMemoryError::Unaligned { .. })
        ));
    }
}