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