1 use crate::{
2 driver::base::{kobject::KObject, kset::KSet},
3 filesystem::{
4 sysfs::{sysfs_instance, Attribute, AttributeGroup},
5 vfs::syscall::ModeType,
6 },
7 init::initcall::INITCALL_CORE,
8 };
9 use alloc::{string::ToString, sync::Arc};
10 use system_error::SystemError;
11 use unified_init::macros::unified_init;
12
13 /// `/sys/kernel`的kset
14 static mut KERNEL_KSET_INSTANCE: Option<Arc<KSet>> = None;
15
16 #[inline(always)]
17 #[allow(dead_code)]
sys_kernel_kset() -> Arc<KSet>18 pub fn sys_kernel_kset() -> Arc<KSet> {
19 unsafe { KERNEL_KSET_INSTANCE.clone().unwrap() }
20 }
21
22 #[unified_init(INITCALL_CORE)]
ksysfs_init() -> Result<(), SystemError>23 fn ksysfs_init() -> Result<(), SystemError> {
24 let kernel_kset = KSet::new("kernel".to_string());
25 kernel_kset
26 .register(None)
27 .expect("register kernel kset failed");
28
29 sysfs_instance()
30 .create_groups(&kernel_kset.as_kobject(), &[&KernelAttrGroup])
31 .map_err(|e| {
32 kerror!("Failed to create sysfs groups for kernel kset: {:?}", e);
33 kernel_kset.unregister();
34 SystemError::ENOMEM
35 })?;
36
37 unsafe {
38 KERNEL_KSET_INSTANCE = Some(kernel_kset);
39 }
40
41 return Ok(());
42 }
43
44 #[derive(Debug)]
45 struct KernelAttrGroup;
46
47 impl AttributeGroup for KernelAttrGroup {
name(&self) -> Option<&str>48 fn name(&self) -> Option<&str> {
49 None
50 }
51
attrs(&self) -> &[&'static dyn Attribute]52 fn attrs(&self) -> &[&'static dyn Attribute] {
53 &[]
54 }
55
is_visible( &self, _kobj: Arc<dyn KObject>, attr: &'static dyn Attribute, ) -> Option<ModeType>56 fn is_visible(
57 &self,
58 _kobj: Arc<dyn KObject>,
59 attr: &'static dyn Attribute,
60 ) -> Option<ModeType> {
61 Some(attr.mode())
62 }
63 }
64