starry-kernel 0.12.1

A Linux-compatible OS kernel built on ArceOS unikernel
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
use alloc::{borrow::Cow, boxed::Box, format, sync::Arc, vec::Vec};

use ax_lazyinit::LazyLock;
use axfs_ng_vfs::{NodePermission, VfsError, VfsResult};
use rdif_pwm::{Pwm, PwmError, PwmPolarity, PwmState};

use crate::{
    pseudofs::{
        DirMaker, DirectRwFsFileOps, NodeOpsMux, RwFile, SimpleDir, SimpleDirOps, SimpleFile,
        SimpleFileOperation, SimpleFileOps, SimpleFs, SpecialFsFile,
    },
    sync::Mutex,
};

/// The class exists even on platforms without a PWM controller.
pub(crate) fn pwm_class_dir_maker(fs: Arc<SimpleFs>) -> DirMaker {
    SimpleDir::new_maker(fs.clone(), Arc::new(PwmClassDir { fs }))
}

struct PwmChannel {
    exported: bool,
    generation: usize,
    requested: PwmState,
}
impl PwmChannel {
    fn export(&mut self) -> VfsResult<()> {
        if self.exported {
            return Err(VfsError::ResourceBusy);
        }
        self.generation = self
            .generation
            .checked_add(1)
            .ok_or(VfsError::ValueOverflow)?;
        self.exported = true;
        Ok(())
    }

    fn unexport(&mut self, controller: &mut Pwm, channel: usize) -> VfsResult<()> {
        if !self.exported {
            return Err(VfsError::NoSuchDevice);
        }
        controller.disable(channel).map_err(map_error)?;
        self.requested.enabled = false;
        self.exported = false;
        Ok(())
    }

    fn apply(&mut self, controller: &mut Pwm, channel: usize, next: PwmState) -> VfsResult<()> {
        controller.apply(channel, next).map_err(map_error)?;
        self.requested = next;
        Ok(())
    }
}

struct PwmChip {
    number: usize,
    device: rdrive::Device<Pwm>,
    channels: Mutex<Vec<PwmChannel>>,
}

// Device IDs are allocated in firmware enumeration order. The registry is the
// only discovery source; sysfs never matches vendor compatibles or maps MMIO.
static CHIPS: LazyLock<Vec<PwmChip>> = LazyLock::new(|| {
    let mut chips = Vec::new();
    let mut number = 0;
    for device in rdrive::get_list::<Pwm>() {
        let result = (|| {
            let mut controller = device.lock().map_err(|_| VfsError::Io)?;
            (0..controller.channel_count())
                .map(|channel| {
                    controller
                        .get_state(channel)
                        .map(|requested| PwmChannel {
                            exported: false,
                            generation: 0,
                            requested,
                        })
                        .map_err(map_error)
                })
                .collect::<VfsResult<Vec<_>>>()
        })();
        match result {
            Ok(channels) if !channels.is_empty() => {
                let count = channels.len();
                chips.push(PwmChip {
                    number,
                    device,
                    channels: Mutex::new(channels),
                });
                number += count;
            }
            Ok(_) => warn!("PWM controller has no channels"),
            Err(err) => warn!(
                "PWM controller {:?} state unavailable: {err:?}",
                device.descriptor().device_id()
            ),
        }
    }
    chips
});

struct PwmAttrFile {
    ops: Arc<dyn SimpleFileOps>,
}
impl DirectRwFsFileOps for PwmAttrFile {
    fn read_at(&self, buf: &mut [u8], offset: u64) -> VfsResult<usize> {
        let data = self.ops.read_all()?;
        if offset >= data.len() as u64 {
            return Ok(0);
        }
        let data = &data[offset as usize..];
        let len = buf.len().min(data.len());
        buf[..len].copy_from_slice(&data[..len]);
        Ok(len)
    }
    fn write_at(&self, buf: &[u8], offset: u64) -> VfsResult<usize> {
        if offset != 0 {
            return Err(VfsError::InvalidInput);
        }
        self.ops.write_all(buf)?;
        Ok(buf.len())
    }
}
fn attribute(
    fs: Arc<SimpleFs>,
    permissions: NodePermission,
    ops: impl SimpleFileOps,
) -> NodeOpsMux {
    SpecialFsFile::new_regular_with_perm(fs, PwmAttrFile { ops: Arc::new(ops) }, permissions).into()
}
struct PwmClassDir {
    fs: Arc<SimpleFs>,
}
impl SimpleDirOps for PwmClassDir {
    fn child_names<'a>(&'a self) -> Box<dyn Iterator<Item = Cow<'a, str>> + 'a> {
        Box::new(
            CHIPS
                .iter()
                .map(|chip| Cow::Owned(format!("pwmchip{}", chip.number))),
        )
    }
    fn lookup_child(&self, name: &str) -> VfsResult<NodeOpsMux> {
        let number = name
            .strip_prefix("pwmchip")
            .and_then(|v| v.parse::<usize>().ok())
            .ok_or(VfsError::NotFound)?;
        let index = CHIPS
            .iter()
            .position(|chip| chip.number == number)
            .ok_or(VfsError::NotFound)?;
        Ok(NodeOpsMux::Dir(SimpleDir::new_maker(
            self.fs.clone(),
            Arc::new(PwmChipDir {
                fs: self.fs.clone(),
                index,
            }),
        )))
    }
}
struct PwmChipDir {
    fs: Arc<SimpleFs>,
    index: usize,
}
impl SimpleDirOps for PwmChipDir {
    fn child_names<'a>(&'a self) -> Box<dyn Iterator<Item = Cow<'a, str>> + 'a> {
        let mut names = alloc::vec![
            Cow::Borrowed("export"),
            Cow::Borrowed("unexport"),
            Cow::Borrowed("npwm")
        ];
        for (index, channel) in CHIPS[self.index].channels.lock().iter().enumerate() {
            if channel.exported {
                names.push(Cow::Owned(format!("pwm{index}")));
            }
        }
        Box::new(names.into_iter())
    }
    fn lookup_child(&self, name: &str) -> VfsResult<NodeOpsMux> {
        let index = self.index;
        match name {
            "export" | "unexport" => {
                let export = name == "export";
                Ok(attribute(
                    self.fs.clone(),
                    NodePermission::OWNER_WRITE,
                    RwFile::new(move |request| match request {
                        SimpleFileOperation::Read => Err(VfsError::PermissionDenied),
                        SimpleFileOperation::Write(data) => {
                            let channel = usize::try_from(parse_number(data)?)
                                .map_err(|_| VfsError::InvalidInput)?;
                            set_exported(index, channel, export)?;
                            Ok(None::<Vec<u8>>)
                        }
                    }),
                ))
            }
            "npwm" => Ok(SimpleFile::new_regular(self.fs.clone(), move || {
                Ok(format!("{}\n", CHIPS[index].channels.lock().len()))
            })
            .into()),
            _ => {
                let channel = name
                    .strip_prefix("pwm")
                    .and_then(|v| v.parse::<usize>().ok())
                    .ok_or(VfsError::NotFound)?;
                let generation =
                    exported_channel(&CHIPS[index].channels.lock(), channel, None)?.generation;
                Ok(NodeOpsMux::Dir(SimpleDir::new_maker(
                    self.fs.clone(),
                    Arc::new(PwmChannelDir {
                        fs: self.fs.clone(),
                        index,
                        channel,
                        generation,
                    }),
                )))
            }
        }
    }
    fn is_cacheable(&self) -> bool {
        false
    }
}
struct PwmChannelDir {
    fs: Arc<SimpleFs>,
    index: usize,
    channel: usize,
    generation: usize,
}
#[derive(Clone, Copy)]
enum Attribute {
    Period,
    Duty,
    Enable,
    Polarity,
}
impl SimpleDirOps for PwmChannelDir {
    fn child_names<'a>(&'a self) -> Box<dyn Iterator<Item = Cow<'a, str>> + 'a> {
        Box::new(
            ["period", "duty_cycle", "enable", "polarity"]
                .into_iter()
                .map(Cow::Borrowed),
        )
    }
    fn lookup_child(&self, name: &str) -> VfsResult<NodeOpsMux> {
        let attr = match name {
            "period" => Attribute::Period,
            "duty_cycle" => Attribute::Duty,
            "enable" => Attribute::Enable,
            "polarity" => Attribute::Polarity,
            _ => return Err(VfsError::NotFound),
        };
        let index = self.index;
        let channel = self.channel;
        let generation = self.generation;
        exported_channel(&CHIPS[index].channels.lock(), channel, Some(generation))?;
        Ok(attribute(
            self.fs.clone(),
            NodePermission::from_bits_truncate(0o644),
            RwFile::new(move |request| match request {
                SimpleFileOperation::Read => {
                    let channels = CHIPS[index].channels.lock();
                    let state = exported_channel(&channels, channel, Some(generation))?.requested;
                    let value = match attr {
                        Attribute::Period => format!("{}\n", state.period_ns),
                        Attribute::Duty => format!("{}\n", state.duty_ns),
                        Attribute::Enable => format!("{}\n", u8::from(state.enabled)),
                        Attribute::Polarity => match state.polarity {
                            PwmPolarity::Normal => "normal\n".into(),
                            PwmPolarity::Inversed => "inversed\n".into(),
                        },
                    };
                    Ok(Some(value.into_bytes()))
                }
                SimpleFileOperation::Write(data) => {
                    write_attribute(index, channel, generation, attr, data)?;
                    Ok(None)
                }
            }),
        ))
    }
    fn is_cacheable(&self) -> bool {
        false
    }
}
fn parse_number(data: &[u8]) -> VfsResult<u64> {
    core::str::from_utf8(data)
        .ok()
        .and_then(|s| s.trim().parse().ok())
        .ok_or(VfsError::InvalidInput)
}
fn exported_channel(
    channels: &[PwmChannel],
    channel: usize,
    generation: Option<usize>,
) -> VfsResult<&PwmChannel> {
    let entry = channels.get(channel).ok_or(VfsError::NotFound)?;
    // An open attribute belongs to one export lifetime. Re-exporting the same
    // hardware channel must not revive descriptors from the removed directory.
    if generation.is_some_and(|value| value != entry.generation || !entry.exported) {
        return Err(VfsError::NoSuchDevice);
    }
    if !entry.exported {
        return Err(VfsError::NotFound);
    }
    Ok(entry)
}
fn set_exported(index: usize, channel: usize, export: bool) -> VfsResult<()> {
    let chip = &CHIPS[index];
    let mut channels = chip.channels.lock();
    let entry = channels.get_mut(channel).ok_or(VfsError::NoSuchDevice)?;
    if export {
        entry.export()
    } else {
        let mut controller = chip.device.lock().map_err(|_| VfsError::Io)?;
        entry.unexport(&mut controller, channel)
    }
}
fn write_attribute(
    index: usize,
    channel: usize,
    generation: usize,
    attr: Attribute,
    data: &[u8],
) -> VfsResult<()> {
    let chip = &CHIPS[index];
    // Lock order: per-chip sysfs state -> rdrive controller -> clock provider.
    // No driver callback enters sysfs. The candidate is published only on success.
    let mut channels = chip.channels.lock();
    let mut next = exported_channel(&channels, channel, Some(generation))?.requested;
    match attr {
        Attribute::Period => next.period_ns = parse_number(data)?,
        Attribute::Duty => next.duty_ns = parse_number(data)?,
        Attribute::Enable => {
            next.enabled = match parse_number(data)? {
                0 => false,
                1 => true,
                _ => return Err(VfsError::InvalidInput),
            }
        }
        Attribute::Polarity => {
            next.polarity = match core::str::from_utf8(data)
                .map_err(|_| VfsError::InvalidInput)?
                .trim()
            {
                "normal" => PwmPolarity::Normal,
                "inversed" => PwmPolarity::Inversed,
                _ => return Err(VfsError::InvalidInput),
            }
        }
    }
    let mut controller = chip.device.lock().map_err(|_| VfsError::Io)?;
    channels[channel].apply(&mut controller, channel, next)
}
fn map_error(error: PwmError) -> VfsError {
    match error {
        PwmError::InvalidChannel | PwmError::InvalidPeriod | PwmError::InvalidDuty => {
            VfsError::InvalidInput
        }
        PwmError::UnsupportedPolarity => VfsError::OperationNotSupported,
        PwmError::Clock | PwmError::InvalidMapping => VfsError::Io,
    }
}

#[cfg(all(test, not(axtest)))]
mod tests {
    use rdif_pwm::{DriverGeneric, Interface};

    use super::*;

    // The failure is at the hardware boundary; no OS runtime is replaced.
    struct UnavailableOutput;
    impl DriverGeneric for UnavailableOutput {
        fn name(&self) -> &str {
            "unavailable-output"
        }
    }
    impl Interface for UnavailableOutput {
        fn channel_count(&self) -> usize {
            1
        }
        fn get_state(&mut self, _channel: usize) -> Result<PwmState, PwmError> {
            Ok(PwmState::normal(0, 0, false))
        }
        fn apply(&mut self, _channel: usize, state: PwmState) -> Result<(), PwmError> {
            if state.enabled {
                Err(PwmError::Clock)
            } else {
                Ok(())
            }
        }
    }

    #[test]
    fn failed_submission_preserves_requested_state() {
        let mut controller = Pwm::new(UnavailableOutput);
        let initial = PwmState::normal(1_000_000, 250_000, false);
        let mut channel = PwmChannel {
            exported: false,
            generation: 0,
            requested: initial,
        };
        channel.export().unwrap();
        assert_eq!(channel.export(), Err(VfsError::ResourceBusy));
        let rejected = PwmState {
            duty_ns: 300_000,
            enabled: true,
            polarity: PwmPolarity::Inversed,
            ..initial
        };
        assert_eq!(
            channel.apply(&mut controller, 0, rejected),
            Err(VfsError::Io)
        );
        assert_eq!(channel.requested, initial);
        assert!(channel.exported);
        let accepted = PwmState {
            enabled: false,
            ..rejected
        };
        channel.apply(&mut controller, 0, accepted).unwrap();
        assert_eq!(channel.requested, accepted);
        let mut channels = [channel];
        let generation = channels[0].generation;
        channels[0].unexport(&mut controller, 0).unwrap();
        assert!(matches!(
            exported_channel(&channels, 0, Some(generation)),
            Err(VfsError::NoSuchDevice)
        ));
        channels[0].export().unwrap();
        assert!(matches!(
            exported_channel(&channels, 0, Some(generation)),
            Err(VfsError::NoSuchDevice)
        ));
        assert!(exported_channel(&channels, 0, Some(channels[0].generation)).is_ok());
        channels[0].unexport(&mut controller, 0).unwrap();
        assert_eq!(
            channels[0].unexport(&mut controller, 0),
            Err(VfsError::NoSuchDevice)
        );
    }
}