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::devtmpfs::dev_tmp_fs;
13use starnix_core::fs::fuchsia::{RemoteBundle, new_remote_fs, new_remote_vol};
14use starnix_core::fs::sysfs::sys_fs;
15use starnix_core::fs::tmpfs::tmp_fs;
16use starnix_core::task::Kernel;
17use starnix_core::vfs::fs_registry::FsRegistry;
18use starnix_core::vfs::pipe::register_pipe_fs;
19use starnix_modules_binderfs::BinderFs;
20use starnix_modules_cgroupfs::{CgroupV1Fs, cgroup2_fs};
21use starnix_modules_debugfs::debug_fs;
22use starnix_modules_device_mapper::{create_device_mapper, device_mapper_init};
23use starnix_modules_devpts::{dev_pts_fs, tty_device_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_uapi::device_id::DeviceId;
37use starnix_uapi::errors::Errno;
38use starnix_uapi::fs_type::FileSystemTypeFlags;
39
40fn misc_device_init(kernel: &Kernel) -> Result<(), Errno> {
41    let registry = &kernel.device_registry;
42    let misc_class = registry.objects.misc_class();
43    registry.register_device(
44        kernel,
45        // TODO(https://fxbug.dev/322365477) consider making this configurable
46        "hw_random".into(),
47        DeviceMetadata::new("hwrng".into(), DeviceId::HW_RANDOM, DeviceMode::Char),
48        misc_class.clone(),
49        simple_device_ops::<DevRandom>,
50    )?;
51    registry.register_device(
52        kernel,
53        "fuse".into(),
54        DeviceMetadata::new("fuse".into(), DeviceId::FUSE, DeviceMode::Char),
55        misc_class.clone(),
56        open_fuse_device,
57    )?;
58    registry.register_device(
59        kernel,
60        "device-mapper".into(),
61        DeviceMetadata::new("mapper/control".into(), DeviceId::DEVICE_MAPPER, DeviceMode::Char),
62        misc_class.clone(),
63        create_device_mapper,
64    )?;
65    registry.register_device(
66        kernel,
67        "loop-control".into(),
68        DeviceMetadata::new("loop-control".into(), DeviceId::LOOP_CONTROL, DeviceMode::Char),
69        misc_class.clone(),
70        create_loop_control_device,
71    )?;
72    registry.register_device(
73        kernel,
74        "tun".into(),
75        DeviceMetadata::new("tun".into(), DeviceId::TUN, DeviceMode::Char),
76        misc_class,
77        simple_device_ops::<DevTun>,
78    )?;
79    Ok(())
80}
81
82/// Initializes common devices in `Kernel`.
83///
84/// Adding device nodes to devtmpfs requires the current running task. The `Kernel` constructor does
85/// not create an initial task, so this function should be triggered after a `CurrentTask` has been
86/// initialized.
87pub fn init_common_devices(kernel: &Kernel) -> Result<(), Errno> {
88    misc_device_init(kernel)?;
89    mem_device_init(kernel)?;
90    tty_device_init(kernel)?;
91    loop_device_init(kernel)?;
92    device_mapper_init(kernel)?;
93    zram_device_init(kernel)?;
94    Ok(())
95}
96
97pub fn register_common_file_systems(kernel: &Kernel) {
98    let registry = kernel.expando.get::<FsRegistry>();
99    registry.register(b"binder".into(), FileSystemTypeFlags::empty(), BinderFs::new_fs);
100    registry.register(b"bpf".into(), FileSystemTypeFlags::empty(), BpfFs::new_fs);
101    registry.register(b"cgroup".into(), FileSystemTypeFlags::empty(), CgroupV1Fs::new_fs);
102    registry.register(b"cgroup2".into(), FileSystemTypeFlags::empty(), cgroup2_fs);
103    // Cpusets use the generic cgroup (v1) subsystem.
104    // From https://docs.kernel.org/admin-guide/cgroup-v1/cpusets.html
105    registry.register(b"cpuset".into(), FileSystemTypeFlags::empty(), CgroupV1Fs::new_fs_cpuset);
106    registry.register(b"debugfs".into(), FileSystemTypeFlags::empty(), debug_fs);
107    registry.register(b"devpts".into(), FileSystemTypeFlags::empty(), dev_pts_fs);
108    registry.register(b"devtmpfs".into(), FileSystemTypeFlags::empty(), dev_tmp_fs);
109    registry.register(b"erofs".into(), FileSystemTypeFlags::empty(), starnix_modules_erofs::new_fs);
110    registry.register(b"ext4".into(), FileSystemTypeFlags::REQUIRES_DEV, ExtFilesystem::new_fs);
111    registry.register(b"functionfs".into(), FileSystemTypeFlags::empty(), FunctionFs::new_fs);
112    registry.register(b"fuse".into(), FileSystemTypeFlags::empty(), new_fuse_fs);
113    registry.register(b"fusectl".into(), FileSystemTypeFlags::empty(), new_fusectl_fs);
114    registry.register(b"overlay".into(), FileSystemTypeFlags::empty(), new_overlay_fs);
115    register_pipe_fs(registry.as_ref());
116    registry.register(b"proc".into(), FileSystemTypeFlags::empty(), proc_fs);
117    registry.register(b"pstore".into(), FileSystemTypeFlags::empty(), pstore_fs);
118    registry.register(b"remotefs".into(), FileSystemTypeFlags::empty(), new_remote_fs);
119    registry.register(b"remotevol".into(), FileSystemTypeFlags::empty(), new_remote_vol);
120    registry.register(b"remote_bundle".into(), FileSystemTypeFlags::empty(), RemoteBundle::new_fs);
121    registry.register(b"selinuxfs".into(), FileSystemTypeFlags::empty(), selinux_fs);
122    registry.register(b"sysfs".into(), FileSystemTypeFlags::empty(), sys_fs);
123    registry.register(b"tmpfs".into(), FileSystemTypeFlags::empty(), tmp_fs);
124    registry.register(b"tracefs".into(), FileSystemTypeFlags::empty(), trace_fs);
125}
126
127pub fn register_common_syscalls(kernel: &Kernel) {
128    inotify_init(kernel);
129}