starry-kernel 0.6.1

A Linux-compatible OS kernel built on ArceOS unikernel
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
//! Special devices

mod card0;
#[cfg(feature = "rknpu")]
mod card1;
#[cfg(feature = "rknpu")]
mod dma_heap;
mod drm;
#[cfg(feature = "input")]
pub mod event;
mod fb;
#[cfg(feature = "k230-kpu")]
mod kpu;
#[cfg(feature = "dev-log")]
mod log;
mod r#loop;
#[cfg(feature = "ext4")]
mod loop_block;
#[cfg(feature = "ext4")]
pub use r#loop::LoopDevice;
#[cfg(all(feature = "sg2002", not(feature = "plat-dyn")))]
pub mod ion;
#[cfg(feature = "memtrack")]
mod memtrack;
#[cfg(feature = "rknpu")]
mod rknpu_drm;
mod rtc;
#[cfg(all(feature = "sg2002", not(feature = "plat-dyn")))]
pub mod tpu;
pub mod tty;

#[cfg(all(feature = "sg2002", not(feature = "plat-dyn")))]
mod cvi_camera;
#[cfg(all(feature = "sg2002", not(feature = "plat-dyn")))]
mod cvi_usb_camera;
#[cfg(all(feature = "sg2002", not(feature = "plat-dyn")))]
mod pinmux;
#[cfg(all(feature = "sg2002", not(feature = "plat-dyn")))]
pub(super) mod pwm;
#[cfg(all(feature = "sg2002", not(feature = "plat-dyn")))]
mod tty_serial;

use alloc::{format, sync::Arc};
use core::any::Any;

use ax_errno::AxError;
use ax_sync::Mutex;
use axfs_ng_vfs::{DeviceId, Filesystem, NodeFlags, NodeType, VfsResult};
#[cfg(all(feature = "sg2002", not(feature = "plat-dyn")))]
use spin::Once;

#[cfg(all(feature = "sg2002", not(feature = "plat-dyn")))]
pub static ION_DEVICE: Once<Arc<ion::IonDevice>> = Once::new();
#[cfg(feature = "dev-log")]
pub use log::bind_dev_log;
use rand::{Rng, SeedableRng, rngs::SmallRng};

use crate::pseudofs::{Device, DeviceOps, DirMaker, DirMapping, SimpleDir, SimpleFs};

const RANDOM_SEED: &[u8; 32] = b"0123456789abcdef0123456789abcdef";

pub(crate) fn new_devfs() -> Filesystem {
    SimpleFs::new_with("devfs".into(), 0x01021994, builder)
}

struct Null;

impl DeviceOps for Null {
    fn read_at(&self, _buf: &mut [u8], _offset: u64) -> VfsResult<usize> {
        Ok(0)
    }

    fn write_at(&self, buf: &[u8], _offset: u64) -> VfsResult<usize> {
        Ok(buf.len())
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn flags(&self) -> NodeFlags {
        NodeFlags::NON_CACHEABLE | NodeFlags::STREAM
    }
}

/// Placeholder root block device. starry has no real block-device backend for
/// the root mount; this node exists only so tools that resolve the root device
/// by scanning /dev (e.g. busybox `rdev`) can find a block node whose `rdev`
/// matches the root filesystem's `st_dev`. Real block I/O is unsupported:
/// read/write return `EIO` rather than silently succeeding, so the node never
/// masquerades as a working disk for `dd`/`blkid`/`fsck`.
struct RootBlk;

impl DeviceOps for RootBlk {
    fn read_at(&self, _buf: &mut [u8], _offset: u64) -> VfsResult<usize> {
        Err(AxError::Io)
    }

    fn write_at(&self, _buf: &[u8], _offset: u64) -> VfsResult<usize> {
        Err(AxError::Io)
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn flags(&self) -> NodeFlags {
        NodeFlags::NON_CACHEABLE
    }
}

struct Zero;

impl DeviceOps for Zero {
    fn read_at(&self, buf: &mut [u8], _offset: u64) -> VfsResult<usize> {
        buf.fill(0);
        Ok(buf.len())
    }

    fn write_at(&self, buf: &[u8], _offset: u64) -> VfsResult<usize> {
        Ok(buf.len())
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn flags(&self) -> NodeFlags {
        NodeFlags::NON_CACHEABLE | NodeFlags::STREAM
    }
}

struct Random {
    rng: Mutex<SmallRng>,
}

impl Random {
    pub fn new() -> Self {
        Self {
            rng: Mutex::new(SmallRng::from_seed(*RANDOM_SEED)),
        }
    }
}

impl DeviceOps for Random {
    fn read_at(&self, buf: &mut [u8], _offset: u64) -> VfsResult<usize> {
        self.rng.lock().fill_bytes(buf);
        Ok(buf.len())
    }

    fn write_at(&self, buf: &[u8], _offset: u64) -> VfsResult<usize> {
        Ok(buf.len())
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn flags(&self) -> NodeFlags {
        NodeFlags::NON_CACHEABLE | NodeFlags::STREAM
    }
}

struct Full;

impl DeviceOps for Full {
    fn read_at(&self, buf: &mut [u8], _offset: u64) -> VfsResult<usize> {
        buf.fill(0);
        Ok(buf.len())
    }

    fn write_at(&self, _buf: &[u8], _offset: u64) -> VfsResult<usize> {
        Err(AxError::StorageFull)
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn flags(&self) -> NodeFlags {
        NodeFlags::NON_CACHEABLE | NodeFlags::STREAM
    }
}

struct CpuDmaLatency;

impl DeviceOps for CpuDmaLatency {
    fn read_at(&self, _buf: &mut [u8], _offset: u64) -> VfsResult<usize> {
        Err(AxError::InvalidInput)
    }

    fn write_at(&self, buf: &[u8], _offset: u64) -> VfsResult<usize> {
        Ok(buf.len())
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn flags(&self) -> NodeFlags {
        NodeFlags::NON_CACHEABLE
    }
}

fn builder(fs: Arc<SimpleFs>) -> DirMaker {
    let mut root = DirMapping::new();
    root.add(
        "null",
        Device::new(
            fs.clone(),
            NodeType::CharacterDevice,
            DeviceId::new(1, 3),
            Arc::new(Null),
        ),
    );
    root.add(
        "zero",
        Device::new(
            fs.clone(),
            NodeType::CharacterDevice,
            DeviceId::new(1, 5),
            Arc::new(Zero),
        ),
    );
    root.add(
        "full",
        Device::new(
            fs.clone(),
            NodeType::CharacterDevice,
            DeviceId::new(1, 7),
            Arc::new(Full),
        ),
    );
    root.add(
        "random",
        Device::new(
            fs.clone(),
            NodeType::CharacterDevice,
            DeviceId::new(1, 8),
            Arc::new(Random::new()),
        ),
    );
    root.add(
        "urandom",
        Device::new(
            fs.clone(),
            NodeType::CharacterDevice,
            DeviceId::new(1, 9),
            Arc::new(Random::new()),
        ),
    );
    // Root block device node. Its rdev must equal the root filesystem's st_dev
    // so that tools resolving the root device by scanning /dev (e.g. busybox
    // `rdev`, which stats "/" then looks for a block node with a matching
    // st_rdev) can find it. The root mount is the first mount, so its
    // `DEVICE_COUNTER` id is 1 (== `DeviceId::new(0, 1).0`).
    root.add(
        "vda",
        Device::new(
            fs.clone(),
            NodeType::BlockDevice,
            DeviceId::new(0, 1),
            Arc::new(RootBlk),
        ),
    );
    if ax_display::has_display() {
        root.add(
            "fb0",
            Device::new(
                fs.clone(),
                NodeType::CharacterDevice,
                DeviceId::new(29, 0),
                Arc::new(fb::FrameBuffer::new()),
            ),
        );
    }

    root.add(
        "tty",
        Device::new(
            fs.clone(),
            NodeType::CharacterDevice,
            DeviceId::new(5, 0),
            Arc::new(tty::CurrentTty),
        ),
    );
    root.add(
        "console",
        Device::new(
            fs.clone(),
            NodeType::CharacterDevice,
            DeviceId::new(5, 1),
            tty::N_TTY.clone(),
        ),
    );

    root.add(
        "ptmx",
        Device::new(
            fs.clone(),
            NodeType::CharacterDevice,
            DeviceId::new(5, 2),
            Arc::new(tty::Ptmx(fs.clone())),
        ),
    );
    root.add(
        "pts",
        SimpleDir::new_maker(fs.clone(), Arc::new(tty::PtsDir)),
    );
    #[cfg(feature = "dev-log")]
    root.add(
        "log",
        crate::pseudofs::SimpleFile::new(fs.clone(), NodeType::Socket, || Ok("")),
    );

    #[cfg(feature = "memtrack")]
    root.add(
        "memtrack",
        Device::new(
            fs.clone(),
            NodeType::CharacterDevice,
            DeviceId::new(114, 514),
            Arc::new(memtrack::MemTrack),
        ),
    );

    root.add(
        "cpu_dma_latency",
        Device::new(
            fs.clone(),
            NodeType::CharacterDevice,
            DeviceId::new(10, 1024),
            Arc::new(CpuDmaLatency),
        ),
    );
    root.add(
        "rtc0",
        Device::new(
            fs.clone(),
            NodeType::CharacterDevice,
            rtc::RTC0_DEVICE_ID,
            Arc::new(rtc::Rtc),
        ),
    );

    #[cfg(feature = "k230-kpu")]
    {
        if let Some(kpu_device) = kpu::KpuDevice::probe().map(Arc::new) {
            root.add(
                "kpu",
                Device::new(
                    fs.clone(),
                    NodeType::CharacterDevice,
                    kpu::KPU_DEVICE_ID,
                    kpu_device.clone(),
                ),
            );
            root.add(
                "kpu0",
                Device::new(
                    fs.clone(),
                    NodeType::CharacterDevice,
                    kpu::KPU_DEVICE_ID,
                    kpu_device,
                ),
            );
        }
    }

    // This is mounted to a tmpfs in `new_procfs`
    root.add(
        "shm",
        SimpleDir::new_maker(fs.clone(), Arc::new(DirMapping::new())),
    );
    {
        let mut bus_dir = DirMapping::new();
        bus_dir.add(
            "usb",
            SimpleDir::new_maker(fs.clone(), Arc::new(DirMapping::new())),
        );
        root.add("bus", SimpleDir::new_maker(fs.clone(), Arc::new(bus_dir)));
    }

    // /dev/dri/card0 — simpledrm-class DRM character device. Advertised
    // unconditionally so libdrm/libudev see the DRM node even before
    // there's a display device behind it.
    let dri_card0 = card0::Card0::new();
    let mut dri_dir = DirMapping::new();
    dri_dir.add(
        "card0",
        Device::new(
            fs.clone(),
            NodeType::CharacterDevice,
            DeviceId::new(226, 0),
            dri_card0.clone(),
        ),
    );
    dri_dir.add(
        "renderD128",
        Device::new(
            fs.clone(),
            NodeType::CharacterDevice,
            DeviceId::new(226, 128),
            dri_card0,
        ),
    );

    #[cfg(feature = "rknpu")]
    {
        // DMA heap devices (rknpu only)
        let mut dma_heap_dir = DirMapping::new();
        dma_heap_dir.add(
            "system",
            Device::new(
                fs.clone(),
                NodeType::CharacterDevice,
                dma_heap::DMA_HEAP_SYSTEM_DEVICE_ID,
                Arc::new(dma_heap::DmaHeapSystem::new()),
            ),
        );
        root.add(
            "dma_heap",
            SimpleDir::new_maker(fs.clone(), Arc::new(dma_heap_dir)),
        );

        // RockChip-specific NPU companion card (DRM card1).
        dri_dir.add(
            "card1",
            Device::new(
                fs.clone(),
                NodeType::CharacterDevice,
                card1::CARD1_SYSTEM_DEVICE_ID,
                Arc::new(card1::Card1::new()),
            ),
        );
    }
    root.add("dri", SimpleDir::new_maker(fs.clone(), Arc::new(dri_dir)));

    // Loop devices (major 7, minor = device index)
    for i in 0..16 {
        let dev_id = DeviceId::new(7, i);
        root.add(
            format!("loop{i}"),
            Device::new(
                fs.clone(),
                NodeType::BlockDevice,
                dev_id,
                Arc::new(r#loop::LoopDevice::new(i, dev_id)),
            ),
        );
    }

    // Input devices
    #[cfg(feature = "input")]
    root.add(
        "input",
        SimpleDir::new_maker(fs.clone(), Arc::new(event::input_devices(fs.clone()))),
    );

    #[cfg(all(feature = "sg2002", not(feature = "plat-dyn")))]
    {
        root.add(
            "cvi-tpu0",
            Device::new(
                fs.clone(),
                NodeType::CharacterDevice,
                DeviceId::new(240, 0),
                Arc::new(unsafe { tpu::TpuDevice::new() }),
            ),
        );
        let ion_device = Arc::new(ion::IonDevice::new());
        ION_DEVICE.call_once(|| ion_device.clone());
        root.add(
            "ion",
            Device::new(
                fs.clone(),
                NodeType::CharacterDevice,
                DeviceId::new(10, 56),
                ion_device,
            ),
        );
        root.add(
            "ttyS1",
            Device::new(
                fs.clone(),
                NodeType::CharacterDevice,
                DeviceId::new(4, 65),
                Arc::new(tty_serial::new_tty_s1(115200)),
            ),
        );
        root.add(
            "ttyS2",
            Device::new(
                fs.clone(),
                NodeType::CharacterDevice,
                DeviceId::new(4, 66),
                Arc::new(tty_serial::new_tty_s2(115200)),
            ),
        );
        root.add(
            "cvi-camera0",
            Device::new(
                fs.clone(),
                NodeType::CharacterDevice,
                DeviceId::new(10, 201),
                Arc::new(cvi_camera::CviCamera::new()),
            ),
        );
        root.add(
            "cvi-usb-camera0",
            Device::new(
                fs.clone(),
                NodeType::CharacterDevice,
                DeviceId::new(10, 202),
                Arc::new(cvi_usb_camera::CviCamera::new()),
            ),
        );
        root.add(
            "pinmux",
            Device::new(
                fs.clone(),
                NodeType::CharacterDevice,
                DeviceId::new(1, 1),
                Arc::new(pinmux::PinmuxDev),
            ),
        );
    }

    SimpleDir::new_maker(fs, Arc::new(root))
}