From f762eb8913c992d932147f3e62ab47519fef8a91 Mon Sep 17 00:00:00 2001 From: Qingsong Chen Date: Thu, 28 Nov 2024 05:53:22 +0000 Subject: [PATCH] Remove the `lazy_static` dependency --- Cargo.lock | 4 --- kernel/Cargo.toml | 4 --- kernel/comps/block/Cargo.toml | 4 --- kernel/comps/input/Cargo.toml | 4 --- kernel/src/fs/procfs/filesystems.rs | 20 ++++--------- kernel/src/fs/procfs/mod.rs | 13 ++++++++ kernel/src/fs/rootfs.rs | 3 +- kernel/src/lib.rs | 1 + kernel/src/prelude.rs | 2 -- kernel/src/syscall/mod.rs | 4 +++ kernel/src/syscall/uname.rs | 46 ++++++++++++++--------------- osdk/Cargo.toml | 1 - ostd/Cargo.toml | 1 - 13 files changed, 48 insertions(+), 59 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 860b85a38..ce48713b3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -85,7 +85,6 @@ dependencies = [ "bitflags 1.3.2", "component", "int-to-c-enum", - "lazy_static", "log", "ostd", "spin 0.9.8", @@ -125,7 +124,6 @@ dependencies = [ "bitflags 1.3.2", "component", "int-to-c-enum", - "lazy_static", "log", "ostd", "spin 0.9.8", @@ -182,7 +180,6 @@ dependencies = [ "int-to-c-enum", "intrusive-collections", "keyable-arc", - "lazy_static", "lending-iterator", "libflate", "log", @@ -1146,7 +1143,6 @@ dependencies = [ "inherit-methods-macro", "int-to-c-enum", "intrusive-collections", - "lazy_static", "linux-boot-params", "log", "multiboot2", diff --git a/kernel/Cargo.toml b/kernel/Cargo.toml index b5eee841a..1443b9436 100644 --- a/kernel/Cargo.toml +++ b/kernel/Cargo.toml @@ -65,10 +65,6 @@ fixed = "1.28.0" [target.riscv64gc-unknown-none-elf.dependencies] riscv = { version = "0.11.1", features = ["s-mode"] } -[dependencies.lazy_static] -version = "1.0" -features = ["spin_no_std"] - [features] all = ["cvm_guest"] diff --git a/kernel/comps/block/Cargo.toml b/kernel/comps/block/Cargo.toml index e0c8770f6..86fc0056d 100644 --- a/kernel/comps/block/Cargo.toml +++ b/kernel/comps/block/Cargo.toml @@ -17,7 +17,3 @@ log = "0.4" static_assertions = "1.1.0" [features] - -[dependencies.lazy_static] -version = "1.0" -features = ["spin_no_std"] diff --git a/kernel/comps/input/Cargo.toml b/kernel/comps/input/Cargo.toml index 111aee787..ea4d55c61 100644 --- a/kernel/comps/input/Cargo.toml +++ b/kernel/comps/input/Cargo.toml @@ -17,7 +17,3 @@ log = "0.4" ascii = { version = "1.1", default-features = false, features = ["alloc"] } [features] - -[dependencies.lazy_static] -version = "1.0" -features = ["spin_no_std"] diff --git a/kernel/src/fs/procfs/filesystems.rs b/kernel/src/fs/procfs/filesystems.rs index ce1a23db9..e3ab7fbea 100644 --- a/kernel/src/fs/procfs/filesystems.rs +++ b/kernel/src/fs/procfs/filesystems.rs @@ -2,6 +2,8 @@ use alloc::format; +use spin::Once; + use crate::{ fs::{ procfs::template::{FileOps, ProcFileBuilder}, @@ -22,7 +24,7 @@ impl FileSystemsFileOps { impl FileOps for FileSystemsFileOps { fn data(&self) -> Result> { let mut result = String::new(); - for fs in FILESYSTEM_TYPES.iter() { + for fs in FILESYSTEM_TYPES.get().unwrap().iter() { if fs.is_nodev { result.push_str(&format!("nodev\t{}\n", fs.name)); } else { @@ -33,25 +35,15 @@ impl FileOps for FileSystemsFileOps { } } -lazy_static! { - static ref FILESYSTEM_TYPES: Vec = { - vec![ - FileSystemType::new("proc", true), - FileSystemType::new("ramfs", true), - FileSystemType::new("devpts", true), - FileSystemType::new("ext2", false), - FileSystemType::new("exfat", false), - ] - }; -} +pub(super) static FILESYSTEM_TYPES: Once> = Once::new(); -struct FileSystemType { +pub(super) struct FileSystemType { name: String, is_nodev: bool, } impl FileSystemType { - fn new(name: &str, is_nodev: bool) -> Self { + pub(super) fn new(name: &str, is_nodev: bool) -> Self { FileSystemType { name: name.to_string(), is_nodev, diff --git a/kernel/src/fs/procfs/mod.rs b/kernel/src/fs/procfs/mod.rs index 3931efaad..f8524218a 100644 --- a/kernel/src/fs/procfs/mod.rs +++ b/kernel/src/fs/procfs/mod.rs @@ -2,6 +2,7 @@ use core::sync::atomic::{AtomicU64, Ordering}; +use filesystems::{FileSystemType, FILESYSTEM_TYPES}; use loadavg::LoadAvgFileOps; use sys::SysDirOps; @@ -31,6 +32,18 @@ mod self_; mod sys; mod template; +pub(super) fn init() { + FILESYSTEM_TYPES.call_once(|| { + vec![ + FileSystemType::new("proc", true), + FileSystemType::new("ramfs", true), + FileSystemType::new("devpts", true), + FileSystemType::new("ext2", false), + FileSystemType::new("exfat", false), + ] + }); +} + /// Magic number. const PROC_MAGIC: u64 = 0x9fa0; /// Root Inode ID. diff --git a/kernel/src/fs/rootfs.rs b/kernel/src/fs/rootfs.rs index 06bdadf49..be8291951 100644 --- a/kernel/src/fs/rootfs.rs +++ b/kernel/src/fs/rootfs.rs @@ -8,7 +8,7 @@ use spin::Once; use super::{ fs_resolver::{FsPath, FsResolver}, path::MountNode, - procfs::ProcFS, + procfs::{self, ProcFS}, ramfs::RamFS, utils::{FileSystem, InodeMode, InodeType}, }; @@ -17,6 +17,7 @@ use crate::prelude::*; /// Unpack and prepare the rootfs from the initramfs CPIO buffer. pub fn init(initramfs_buf: &[u8]) -> Result<()> { init_root_mount(); + procfs::init(); println!("[kernel] unpacking the initramfs.cpio.gz to rootfs ..."); let fs = FsResolver::new(); diff --git a/kernel/src/lib.rs b/kernel/src/lib.rs index 9487e67a7..0589d723d 100644 --- a/kernel/src/lib.rs +++ b/kernel/src/lib.rs @@ -100,6 +100,7 @@ pub fn init() { sched::init(); fs::rootfs::init(boot::initramfs()).unwrap(); device::init().unwrap(); + syscall::init(); vdso::init(); process::init(); } diff --git a/kernel/src/prelude.rs b/kernel/src/prelude.rs index 356e07e48..490d625ae 100644 --- a/kernel/src/prelude.rs +++ b/kernel/src/prelude.rs @@ -47,8 +47,6 @@ macro_rules! current_thread { }; } -pub(crate) use lazy_static::lazy_static; - pub(crate) use crate::{ context::{Context, CurrentUserSpace, ReadCString}, current, current_thread, diff --git a/kernel/src/syscall/mod.rs b/kernel/src/syscall/mod.rs index e17a5c7e7..22677ca6f 100644 --- a/kernel/src/syscall/mod.rs +++ b/kernel/src/syscall/mod.rs @@ -360,3 +360,7 @@ macro_rules! log_syscall_entry { } }; } + +pub(super) fn init() { + uname::init(); +} diff --git a/kernel/src/syscall/uname.rs b/kernel/src/syscall/uname.rs index ae010b23d..f84a79e5e 100644 --- a/kernel/src/syscall/uname.rs +++ b/kernel/src/syscall/uname.rs @@ -1,29 +1,13 @@ // SPDX-License-Identifier: MPL-2.0 +use spin::Once; + use super::SyscallReturn; use crate::prelude::*; // We don't use the real name and version of our os here. Instead, we pick up fake values witch is the same as the ones of linux. // The values are used to fool glibc since glibc will check the version and os name. -lazy_static! { - /// used to fool glibc - static ref SYS_NAME: CString = CString::new("Linux").unwrap(); - static ref NODE_NAME: CString = CString::new("WHITLEY").unwrap(); - static ref RELEASE: CString = CString::new("5.13.0").unwrap(); - static ref VERSION: CString = CString::new("5.13.0").unwrap(); - static ref MACHINE: CString = CString::new("x86_64").unwrap(); - static ref DOMAIN_NAME: CString = CString::new("").unwrap(); - static ref UTS_NAME: UtsName = { - let mut uts_name = UtsName::new(); - copy_cstring_to_u8_slice(&SYS_NAME, &mut uts_name.sysname); - copy_cstring_to_u8_slice(&NODE_NAME, &mut uts_name.nodename); - copy_cstring_to_u8_slice(&RELEASE, &mut uts_name.release); - copy_cstring_to_u8_slice(&VERSION, &mut uts_name.version); - copy_cstring_to_u8_slice(&MACHINE, &mut uts_name.machine); - copy_cstring_to_u8_slice(&DOMAIN_NAME, &mut uts_name.domainname); - uts_name - }; -} +static UTS_NAME: Once = Once::new(); const UTS_FIELD_LEN: usize = 65; @@ -51,14 +35,28 @@ impl UtsName { } } -fn copy_cstring_to_u8_slice(src: &CStr, dst: &mut [u8]) { - let src = src.to_bytes_with_nul(); - let len = src.len().min(dst.len()); - dst[..len].copy_from_slice(&src[..len]); +pub(super) fn init() { + UTS_NAME.call_once(|| { + let copy_slice = |src: &[u8], dst: &mut [u8]| { + let len = src.len().min(dst.len()); + dst[..len].copy_from_slice(&src[..len]); + }; + + let mut uts_name = UtsName::new(); + copy_slice(b"Linux", &mut uts_name.sysname); + copy_slice(b"WHITLEY", &mut uts_name.nodename); + copy_slice(b"5.13.0", &mut uts_name.release); + copy_slice(b"5.13.0", &mut uts_name.version); + copy_slice(b"x86_64", &mut uts_name.machine); + copy_slice(b"", &mut uts_name.domainname); + + uts_name + }); } pub fn sys_uname(old_uname_addr: Vaddr, ctx: &Context) -> Result { debug!("old uname addr = 0x{:x}", old_uname_addr); - ctx.user_space().write_val(old_uname_addr, &*UTS_NAME)?; + ctx.user_space() + .write_val(old_uname_addr, UTS_NAME.get().unwrap())?; Ok(SyscallReturn::Return(0)) } diff --git a/osdk/Cargo.toml b/osdk/Cargo.toml index e8e868f4c..c5c28dd47 100644 --- a/osdk/Cargo.toml +++ b/osdk/Cargo.toml @@ -17,7 +17,6 @@ env_logger = "0.11.0" inferno = "0.11.21" indexmap = "2.2.1" indicatif = "0.17.8" # For a commandline progress bar -lazy_static = "1.4.0" log = "0.4.20" quote = "1.0.35" regex = "1.10.4" diff --git a/ostd/Cargo.toml b/ostd/Cargo.toml index 0dca04dd7..ebffe3aa3 100644 --- a/ostd/Cargo.toml +++ b/ostd/Cargo.toml @@ -27,7 +27,6 @@ id-alloc = { path = "libs/id-alloc", version = "0.1.0" } inherit-methods-macro = { git = "https://github.com/asterinas/inherit-methods-macro", rev = "98f7e3e", version = "0.1.0" } int-to-c-enum = { path = "../kernel/libs/int-to-c-enum", version = "0.1.0" } intrusive-collections = { version = "0.9.6", features = ["nightly"] } -lazy_static = { version = "1.0", features = ["spin_no_std"] } linux-boot-params = { version = "0.9.4", path = "libs/linux-bzimage/boot-params" } log = "0.4" num = { version = "0.4", default-features = false }