Skip to content

Commit

Permalink
add random dev device in /dev
Browse files Browse the repository at this point in the history
generate cpuinfo, meminfo and cmdline file in /proc

generate passwd and hosts file in /etc
  • Loading branch information
minminm committed Dec 20, 2023
1 parent ba4c3d1 commit 0c02935
Show file tree
Hide file tree
Showing 9 changed files with 339 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/axfs_devfs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ extern crate alloc;

mod dir;
mod null;
mod random;
mod zero;

#[cfg(test)]
mod tests;

pub use self::dir::DirNode;
pub use self::null::NullDev;
pub use self::random::RandomDev;
pub use self::zero::ZeroDev;

use alloc::sync::Arc;
Expand Down
65 changes: 65 additions & 0 deletions crates/axfs_devfs/src/random.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/* Copyright (c) [2023] [Syswonder Community]
* [Rukos] is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/

use axfs_vfs::{VfsNodeAttr, VfsNodeOps, VfsNodePerm, VfsNodeType, VfsResult};
use core::sync::atomic::{AtomicU64, Ordering::SeqCst};

static SEED: AtomicU64 = AtomicU64::new(0xae_f3);

/// A random device behaves like `/dev/random`.
///
/// It always returns a chunk of random bytes when read, and all writes are discarded.
pub struct RandomDev;

/// Returns a 32-bit unsigned pseudo random interger using LCG.
fn rand_lcg32() -> u32 {
let new_seed = SEED
.load(SeqCst)
.wrapping_mul(6364136223846793005)
.wrapping_add(1);
SEED.store(new_seed, SeqCst);
(new_seed >> 33) as u32
}

impl VfsNodeOps for RandomDev {
fn get_attr(&self) -> VfsResult<VfsNodeAttr> {
Ok(VfsNodeAttr::new(
VfsNodePerm::default_file(),
VfsNodeType::CharDevice,
0,
0,
))
}

fn read_at(&self, _offset: u64, buf: &mut [u8]) -> VfsResult<usize> {
let len = buf.len() >> 2;
let remainder = buf.len() & 0x3;
for i in 0..len {
let random = rand_lcg32();
let start_idx = i * 4;
// MSB
buf[start_idx..start_idx + 4].copy_from_slice(random.to_be_bytes().as_ref());
}

let random = rand_lcg32();
buf[len * 4..].copy_from_slice(random.to_be_bytes()[..remainder].as_ref());

Ok(buf.len())
}

fn write_at(&self, _offset: u64, buf: &[u8]) -> VfsResult<usize> {
Ok(buf.len())
}

fn truncate(&self, _size: u64) -> VfsResult {
Ok(())
}

axfs_vfs::impl_vfs_non_dir_default! {}
}
7 changes: 6 additions & 1 deletion modules/ruxfs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ devfs = ["dep:axfs_devfs"]
ramfs = ["dep:axfs_ramfs"]
procfs = ["dep:axfs_ramfs"]
sysfs = ["dep:axfs_ramfs"]
etcfs = ["dep:axfs_ramfs"]
fatfs = ["dep:fatfs"]
myfs = ["dep:crate_interface"]
use-ramdisk = []
alloc = ["axalloc"]
fp_simd = []

default = ["devfs", "ramfs", "fatfs", "procfs", "sysfs"]
default = ["devfs", "ramfs", "fatfs", "procfs", "sysfs", "etcfs"]

[dependencies]
log = "0.4"
Expand All @@ -36,6 +39,8 @@ axfs_ramfs = { path = "../../crates/axfs_ramfs", optional = true }
ruxdriver = { path = "../ruxdriver", features = ["block"] }
axsync = { path = "../axsync" }
crate_interface = { path = "../../crates/crate_interface", optional = true }
axalloc = { path = "../axalloc", optional = true }
memory_addr = { path = "../../crates/memory_addr" }

[dependencies.fatfs]
git = "https://github.com/rafalh/rust-fatfs"
Expand Down
45 changes: 45 additions & 0 deletions modules/ruxfs/src/arch/aarch64.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* Copyright (c) [2023] [Syswonder Community]
* [Rukos] is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/

use alloc::format;
use alloc::string::String;

fn read_cpuid() -> u64 {
let val: u64;
unsafe {
core::arch::asm!("mrs {}, midr_el1", out(reg) val);
}
val
}

pub fn get_cpuinfo() -> String {
let cpuid = read_cpuid();
let mut cpuinfo = String::new();

cpuinfo.push_str(
format!(
"Processor\t: {} rev {} ({})\n",
"AArch64 Processor",
cpuid & 15,
"aarch64"
)
.as_ref(),
);
cpuinfo.push_str("Features\t: ");
#[cfg(feature = "fp_simd")]
cpuinfo.push_str("fp asimd");

cpuinfo.push_str(format!("\nCPU implementer\t: {:#02x}\n", cpuid >> 24).as_ref());
cpuinfo.push_str("CPU architecture: AArch64\n");
cpuinfo.push_str(format!("CPU variant\t: {:#x}\n", (cpuid >> 20) & 15).as_ref());
cpuinfo.push_str(format!("CPU part\t: {:#03x}\n", (cpuid >> 4) & 0xfff).as_ref());
cpuinfo.push_str(format!("CPU revision\t: {}\n", cpuid & 15).as_ref());

cpuinfo
}
49 changes: 49 additions & 0 deletions modules/ruxfs/src/arch/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/* Copyright (c) [2023] [Syswonder Community]
* [Rukos] is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/

use alloc::format;
use alloc::string::String;

#[cfg(target_arch = "x86_64")]
mod x86_64;

#[cfg(target_arch = "aarch64")]
mod aarch64;

pub fn get_cpuinfo() -> String {
cfg_if::cfg_if! {
if #[cfg(target_arch = "aarch64")] {
aarch64::get_cpuinfo()
} else if #[cfg(target_arch = "x86_64")] {
x86_64::get_cpuinfo()
} else {
String::new()
}
}
}

pub fn get_meminfo() -> String {
#[cfg(feature = "alloc")]
{
use core::ffi::c_ulong;
let allocator = axalloc::global_allocator();
let freeram = (allocator.available_bytes()
+ allocator.available_pages() * memory_addr::PAGE_SIZE_4K)
as c_ulong;
let totalram = freeram + allocator.used_bytes() as c_ulong;

let mut meminfo = String::new();
meminfo.push_str(format!("MemTotal: {:8}\n", totalram).as_ref());
meminfo.push_str(format!("MemFree: {:8}\n", freeram).as_ref());

meminfo
}
#[cfg(not(feature = "alloc"))]
String::new()
}
117 changes: 117 additions & 0 deletions modules/ruxfs/src/arch/x86_64.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/* Copyright (c) [2023] [Syswonder Community]
* [Rukos] is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/

use alloc::format;
use alloc::string::String;
use core::arch::x86_64::__cpuid;

#[derive(Default)]
struct CpuinfoX86 {
x86_family: u8,
x86_model: u8,
x86_mask: u8,
cpuid_level: i32, // Maximum supported CPUID level, -1=no CPUID
x86_model_id: [u32; 16],
extended_cpuid_level: u32, // Max extended CPUID function supported
}

impl CpuinfoX86 {
fn get_model_name(&self) -> [u8; 64] {
let mut model_name = [0; 64];
for i in 0..16 {
let bytes = self.x86_model_id[i].to_le_bytes();
model_name[i * 4..i * 4 + 4].copy_from_slice(bytes.as_ref());
}

model_name
}
}

/// Identifies and stores CPU's family, model, mask,
/// and the highest supported standard and extended CPUID levels.
fn cpu_dect(c: &mut CpuinfoX86) {
// Get vendor name
let eax = read_cpuid_eax(0x00000000);
c.cpuid_level = eax as i32;

// Set default x86 family
c.x86_family = 4;
if c.cpuid_level >= 0x00000001 {
let tfms = read_cpuid_eax(0x00000001);
c.x86_family = (tfms >> 8) as u8 & 0xf;
c.x86_model = (tfms >> 4) as u8 & 0xf;
c.x86_mask = tfms as u8 & 0xf;

if c.x86_family == 0xf {
c.x86_family += (tfms >> 20) as u8;
}
if c.x86_family >= 0x6 {
c.x86_model += ((tfms >> 16) as u8 & 0xf) << 4;
}
}

c.extended_cpuid_level = read_cpuid_eax(0x80000000);
}

fn gen_model_name(c: &mut CpuinfoX86) {
if c.extended_cpuid_level < 0x80000004 {
return;
}

let v = &mut c.x86_model_id;
(v[0], v[1], v[2], v[3]) = read_cpuid(0x80000002);
(v[4], v[5], v[6], v[7]) = read_cpuid(0x80000003);
(v[8], v[9], v[10], v[11]) = read_cpuid(0x80000004);

v[12] &= 0xffff_ff00;
}

fn read_cpuid(eax: u32) -> (u32, u32, u32, u32) {
let cpuid_result = unsafe { __cpuid(eax) };

(
cpuid_result.eax,
cpuid_result.ebx,
cpuid_result.ecx,
cpuid_result.edx,
)
}

fn read_cpuid_eax(eax: u32) -> u32 {
let cpuid_result = unsafe { __cpuid(eax) };

cpuid_result.eax
}

pub fn get_cpuinfo() -> String {
let mut c = CpuinfoX86::default();
cpu_dect(&mut c);
gen_model_name(&mut c);

let mut cpuinfo = String::new();
cpuinfo.push_str(format!("processor\t: {}\n", 0).as_ref());
cpuinfo.push_str(format!("cpu family\t: {}\n", c.x86_family).as_ref());
cpuinfo.push_str(format!("model\t\t: {}\n", c.x86_model).as_ref());

let model_bytes = c.get_model_name();
if model_bytes[0] == 0 {
cpuinfo.push_str("model name\t: unknown\n".as_ref());
} else {
let model_name = String::from_utf8_lossy(&model_bytes);
cpuinfo.push_str(format!("model name\t: {}\n", model_name).as_ref());
}

if c.x86_mask != 0 || c.cpuid_level >= 0 {
cpuinfo.push_str(format!("stepping\t: {}\n", c.x86_mask).as_ref());
} else {
cpuinfo.push_str("stepping\t: unknown\n".as_ref());
}

cpuinfo
}
8 changes: 8 additions & 0 deletions modules/ruxfs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ mod fs;
mod mounts;
mod root;

#[cfg(feature = "alloc")]
mod arch;

pub mod api;
pub mod fops;

Expand Down Expand Up @@ -104,6 +107,11 @@ pub fn prepare_commonfs(mount_points: &mut Vec<self::root::MountPoint>) {
#[cfg(feature = "sysfs")]
let mount_point = MountPoint::new("/sys", mounts::sysfs().unwrap());
mount_points.push(mount_point);

// Mount another ramfs as etcfs
#[cfg(feature = "etcfs")]
let mount_point = MountPoint::new("/etc", mounts::etcfs().unwrap());
mount_points.push(mount_point);
}

/// Initializes root filesystems.
Expand Down
Loading

0 comments on commit 0c02935

Please sign in to comment.