Skip to main content

starnix_modules/
lib.rs

1// Copyright 2023 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Increase recursion limit because LTO causes overflow.
6#![recursion_limit = "256"]
7
8use starnix_core::bpf::fs::BpfFs;
9use starnix_core::device::kobject::DeviceMetadata;
10use starnix_core::device::mem::{DevRandom, mem_device_init};
11use starnix_core::device::{DeviceMode, simple_device_ops};
12use starnix_core::fs::debugfs::debug_fs;
13use starnix_core::fs::devpts::{dev_pts_fs, tty_device_init};
14use starnix_core::fs::devtmpfs::dev_tmp_fs;
15use starnix_core::fs::fuchsia::{RemoteBundle, new_remote_fs, new_remote_vol};
16use starnix_core::fs::sysfs::sys_fs;
17use starnix_core::fs::tmpfs::tmp_fs;
18use starnix_core::task::Kernel;
19use starnix_core::vfs::fs_registry::FsRegistry;
20use starnix_core::vfs::pipe::register_pipe_fs;
21use starnix_modules_binderfs::BinderFs;
22use starnix_modules_cgroupfs::{CgroupV1Fs, cgroup2_fs};
23use starnix_modules_device_mapper::{create_device_mapper, device_mapper_init};
24use starnix_modules_ext4::ExtFilesystem;
25use starnix_modules_functionfs::FunctionFs;
26use starnix_modules_fuse::{new_fuse_fs, new_fusectl_fs, open_fuse_device};
27use starnix_modules_inotify::inotify::inotify_init;
28use starnix_modules_loop::{create_loop_control_device, loop_device_init};
29use starnix_modules_overlayfs::new_overlay_fs;
30use starnix_modules_procfs::proc_fs;
31use starnix_modules_pstore::pstore_fs;
32use starnix_modules_selinuxfs::selinux_fs;
33use starnix_modules_tracefs::trace_fs;
34use starnix_modules_tun::DevTun;
35use starnix_modules_zram::zram_device_init;
36use starnix_sync::{Locked, Unlocked};
37use starnix_uapi::device_id::DeviceId;
38use starnix_uapi::errors::Errno;
39
40fn misc_device_init(locked: &mut Locked<Unlocked>, kernel: &Kernel) -> Result<(), Errno> {
41    let registry = &kernel.device_registry;
42    let misc_class = registry.objects.misc_class();
43    registry.register_device(
44        locked,
45        kernel,
46        // TODO(https://fxbug.dev/322365477) consider making this configurable
47        "hw_random".into(),
48        DeviceMetadata::new("hwrng".into(), DeviceId::HW_RANDOM, DeviceMode::Char),
49        misc_class.clone(),
50        simple_device_ops::<DevRandom>,
51    )?;
52    registry.register_device(
53        locked,
54        kernel,
55        "fuse".into(),
56        DeviceMetadata::new("fuse".into(), DeviceId::FUSE, DeviceMode::Char),
57        misc_class.clone(),
58        open_fuse_device,
59    )?;
60    registry.register_device(
61        locked,
62        kernel,
63        "device-mapper".into(),
64        DeviceMetadata::new("mapper/control".into(), DeviceId::DEVICE_MAPPER, DeviceMode::Char),
65        misc_class.clone(),
66        create_device_mapper,
67    )?;
68    registry.register_device(
69        locked,
70        kernel,
71        "loop-control".into(),
72        DeviceMetadata::new("loop-control".into(), DeviceId::LOOP_CONTROL, DeviceMode::Char),
73        misc_class.clone(),
74        create_loop_control_device,
75    )?;
76    registry.register_device(
77        locked,
78        kernel,
79        "tun".into(),
80        DeviceMetadata::new("tun".into(), DeviceId::TUN, DeviceMode::Char),
81        misc_class,
82        simple_device_ops::<DevTun>,
83    )?;
84    Ok(())
85}
86
87/// Initializes common devices in `Kernel`.
88///
89/// Adding device nodes to devtmpfs requires the current running task. The `Kernel` constructor does
90/// not create an initial task, so this function should be triggered after a `CurrentTask` has been
91/// initialized.
92pub fn init_common_devices(locked: &mut Locked<Unlocked>, kernel: &Kernel) -> Result<(), Errno> {
93    misc_device_init(locked, kernel)?;
94    mem_device_init(locked, kernel)?;
95    tty_device_init(locked, kernel)?;
96    loop_device_init(locked, kernel)?;
97    device_mapper_init(locked, kernel)?;
98    zram_device_init(locked, kernel)?;
99    Ok(())
100}
101
102pub fn register_common_file_systems(_locked: &mut Locked<Unlocked>, kernel: &Kernel) {
103    let registry = kernel.expando.get::<FsRegistry>();
104    registry.register(b"binder".into(), BinderFs::new_fs);
105    registry.register(b"bpf".into(), BpfFs::new_fs);
106    registry.register(b"cgroup".into(), CgroupV1Fs::new_fs);
107    registry.register(b"cgroup2".into(), cgroup2_fs);
108    // Cpusets use the generic cgroup (v1) subsystem.
109    // From https://docs.kernel.org/admin-guide/cgroup-v1/cpusets.html
110    registry.register(b"cpuset".into(), CgroupV1Fs::new_fs_cpuset);
111    registry.register(b"debugfs".into(), debug_fs);
112    registry.register(b"devpts".into(), dev_pts_fs);
113    registry.register(b"devtmpfs".into(), dev_tmp_fs);
114    registry.register(b"ext4".into(), ExtFilesystem::new_fs);
115    registry.register(b"functionfs".into(), FunctionFs::new_fs);
116    registry.register(b"fuse".into(), new_fuse_fs);
117    registry.register(b"fusectl".into(), new_fusectl_fs);
118    registry.register(b"overlay".into(), new_overlay_fs);
119    register_pipe_fs(registry.as_ref());
120    registry.register(b"proc".into(), proc_fs);
121    registry.register(b"pstore".into(), pstore_fs);
122    registry.register(b"remotefs".into(), new_remote_fs);
123    registry.register(b"remotevol".into(), new_remote_vol);
124    registry.register(b"remote_bundle".into(), RemoteBundle::new_fs);
125    registry.register(b"selinuxfs".into(), selinux_fs);
126    registry.register(b"sysfs".into(), sys_fs);
127    registry.register(b"tmpfs".into(), tmp_fs);
128    registry.register(b"tracefs".into(), trace_fs);
129}
130
131pub fn register_common_syscalls(kernel: &Kernel) {
132    inotify_init(kernel);
133}