-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
generate cpuinfo, meminfo and cmdline file in /proc generate passwd and hosts file in /etc
- Loading branch information
Showing
9 changed files
with
339 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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! {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.