fic/ast/system.rs
1//! `.fis` system description AST nodes: the [`System`] and its memory
2//! regions, protection domains, and system composition.
3
4use serde::Serialize;
5use std::fmt::{self, Display, Formatter};
6use std::ops::Range;
7use std::path::PathBuf;
8
9use super::channel::ChannelDecl;
10use super::data::{ConstDecl, StructDecl};
11use super::infoflow::SecurityLabel;
12use super::types::{Address, PageSize};
13
14/// A parsed `.fis` system description.
15///
16/// Produced by [`crate::parser::parse_system`].
17///
18/// ```
19/// let system = fic::parser::parse_system("ProtectionDomain pd1(100);").unwrap();
20/// assert_eq!(system.protection_domains.len(), 1);
21/// ```
22#[derive(Debug, Clone, Serialize)]
23pub struct System {
24 /// Struct type declarations (the data layer), in source order.
25 pub structs: Vec<StructDecl>,
26 /// Constant declarations (the data layer), in source order.
27 pub consts: Vec<ConstDecl>,
28 /// Shared memory region declarations, in source order.
29 pub memory_regions: Vec<MemoryRegionDecl>,
30 /// Protection domain declarations, in source order.
31 pub protection_domains: Vec<ProtectionDomainDecl>,
32 /// Channel declarations, in source order.
33 pub channels: Vec<ChannelDecl>,
34 /// System composition declaration, if present.
35 ///
36 /// `None` means the default composition: all declared PDs run in parallel.
37 pub system: Option<SystemDecl>,
38 /// Source filename (set after parsing, used for diagnostics).
39 #[serde(skip)]
40 pub filename: String,
41}
42
43/// Memory region declaration.
44///
45/// ## `MemoryRegion` Declaration (`.fis`)
46///
47/// Declares a shared memory region available for mapping into one or more protection domains.
48///
49/// ```text
50/// [High|Low] MemoryRegion name(size [, pageSize] [, physAddr]);
51/// ```
52///
53/// | Parameter | Type | Required | Description |
54/// |------------|------|----------|-------------|
55/// | `size` | hex | yes | Total size in bytes; must be a multiple of `pageSize` |
56/// | `pageSize` | hex | no | `0x1000` (4 KiB, default) or `0x200000` (2 MiB); use `,` to skip and still provide `physAddr` |
57/// | `physAddr` | hex | no | Fixed physical address for MMIO; must be page-aligned |
58///
59/// ```text
60/// MemoryRegion buf(0x1000); // 4 KiB, Low
61/// High MemoryRegion secret(0x200000, 0x200000); // 2 MiB large page, High
62/// MemoryRegion mmio(0x1000, 0x1000, 0xFE00_0000); // MMIO at fixed phys addr
63/// MemoryRegion dev(0x1000, , 0xFE00_0000); // skip pageSize (default 4 KiB)
64/// ```
65///
66/// Language reference: [Memory Region Declarations](https://fiducia-lang.github.io/docs/lang-spec/system/memory-regions.html).
67#[derive(Debug, Clone, Serialize)]
68pub struct MemoryRegionDecl {
69 /// Information flow security label (defaults to `Low`).
70 pub security_label: SecurityLabel,
71 /// Region name (referenced by mappings).
72 pub name: String,
73 /// Total size in bytes.
74 pub size: Address,
75 /// Page granularity (defaults to 4KiB).
76 pub page_size: PageSize,
77 /// Optional fixed physical address for device MMIO.
78 pub phys_addr: Option<Address>,
79 /// Byte span of the entire declaration in the source.
80 pub span: Range<usize>,
81}
82
83/// Protection domain declaration.
84///
85/// ## `ProtectionDomain` Declaration (`.fis`)
86///
87/// Declares an isolated execution domain (seL4 protection domain).
88///
89/// ```text
90/// ProtectionDomain name(priority [, irq [, budget [, period]]]);
91/// ```
92///
93/// | Parameter | Type | Required | Default | Description |
94/// |------------|---------|----------|----------|-------------|
95/// | `priority` | decimal | yes | — | Scheduling priority, 0–254 |
96/// | `irq` | decimal | no | none | Hardware interrupt number |
97/// | `budget` | decimal | no | 1000 | Scheduling budget in microseconds |
98/// | `period` | decimal | no | = budget | Scheduling period in microseconds |
99///
100/// Any parameter except `priority` can be skipped by leaving its position empty
101/// (consecutive commas), allowing later parameters to be set without earlier ones:
102///
103/// ```text
104/// ProtectionDomain pd(100); // priority only
105/// ProtectionDomain driver(200, 33); // priority + IRQ
106/// ProtectionDomain rt(250, 10, 500, 2000); // all four
107/// ProtectionDomain pd(100, , 500); // skip IRQ, set budget
108/// ProtectionDomain pd(100, , , 2000); // skip IRQ and budget, set period
109/// ```
110///
111/// Language reference: [Protection Domain Declarations](https://fiducia-lang.github.io/docs/lang-spec/system/protection-domains.html).
112#[derive(Debug, Clone, Serialize)]
113pub struct ProtectionDomainDecl {
114 /// Protection domain name.
115 pub name: String,
116 /// Scheduling priority (0–254).
117 pub priority: u8,
118 /// Optional hardware IRQ number.
119 pub irq: Option<u16>,
120 /// Scheduling budget in microseconds.
121 pub budget: u32,
122 /// Scheduling period in microseconds.
123 pub period: u32,
124 /// The `.fi` implementing it, `<name>.fi` beside the `.fis`: set by the
125 /// front end as it binds the system to its programs, empty straight
126 /// from the parser.
127 #[serde(skip)]
128 pub fi: PathBuf,
129 /// Byte span of the entire declaration in the source.
130 pub span: Range<usize>,
131}
132
133/// System composition declaration.
134///
135/// ## `System` Declaration (`.fis`)
136///
137/// Lists the protection domains that run in parallel.
138/// Every PD name referenced here must be declared with `ProtectionDomain`.
139///
140/// ```text
141/// System = pd1 ||| pd2 ||| pd3;
142/// ```
143///
144/// At least one PD is required. The `|||` operator is the parallel composition operator.
145///
146/// ```text
147/// System = eth_outer ||| dd ||| eth_inner;
148/// ```
149///
150/// Language reference: [System composition](https://fiducia-lang.github.io/docs/lang-spec/system/protection-domains.html#system-composition).
151#[derive(Debug, Clone, Serialize)]
152pub struct SystemDecl {
153 /// Protection domain names with their byte offsets in the source.
154 pub pds: Vec<(String, usize)>,
155 /// Byte span of the entire declaration in the source.
156 pub span: Range<usize>,
157}
158
159impl ProtectionDomainDecl {
160 /// Default scheduling budget in microseconds, used when omitted in the `.fis` declaration.
161 ///
162 /// Period has no fixed default — it defaults to whatever budget is set to.
163 pub const DEFAULT_BUDGET: u32 = 1_000;
164}
165
166impl Display for MemoryRegionDecl {
167 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
168 write!(
169 f,
170 "security: {}, size: {}, page size: {}",
171 self.security_label, self.size, self.page_size
172 )?;
173 if let Some(phys) = &self.phys_addr {
174 write!(f, ", phys addr: {phys}")?;
175 }
176 Ok(())
177 }
178}
179
180impl Display for ProtectionDomainDecl {
181 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
182 write!(
183 f,
184 "priority: {}, budget: {}us, period: {}us",
185 self.priority, self.budget, self.period
186 )?;
187 if let Some(irq) = self.irq {
188 write!(f, ", irq: {irq}")?;
189 }
190 Ok(())
191 }
192}