1 use alloc::vec::Vec; 2 use system_error::SystemError; 3 4 use crate::{ 5 arch::{mm::LockedFrameAllocator, rand::rand}, 6 libs::rand::GRandFlags, 7 mm::allocator::page_frame::FrameAllocator, 8 }; 9 10 use super::{user_access::UserBufferWriter, Syscall}; 11 12 #[repr(C)] 13 14 /// 系统信息 15 /// 16 /// 参考 https://opengrok.ringotek.cn/xref/linux-6.1.9/include/uapi/linux/sysinfo.h#8 17 #[derive(Debug, Default, Copy, Clone)] 18 pub struct SysInfo { 19 uptime: u64, 20 loads: [u64; 3], 21 totalram: u64, 22 freeram: u64, 23 sharedram: u64, 24 bufferram: u64, 25 totalswap: u64, 26 freeswap: u64, 27 procs: u16, 28 pad: u16, 29 totalhigh: u64, 30 freehigh: u64, 31 mem_unit: u32, 32 // 这后面还有一小段,但是我们不需要 33 } 34 35 impl Syscall { 36 pub fn sysinfo(info: *mut SysInfo) -> Result<usize, SystemError> { 37 let mut writer = UserBufferWriter::new(info, core::mem::size_of::<SysInfo>(), true)?; 38 let mut sysinfo = SysInfo::default(); 39 40 let mem = unsafe { LockedFrameAllocator.usage() }; 41 sysinfo.uptime = 0; 42 sysinfo.loads = [0; 3]; 43 sysinfo.totalram = mem.total().bytes() as u64; 44 sysinfo.freeram = mem.free().bytes() as u64; 45 sysinfo.sharedram = 0; 46 sysinfo.bufferram = 0; 47 sysinfo.totalswap = 0; 48 sysinfo.freeswap = 0; 49 sysinfo.procs = 0; 50 sysinfo.pad = 0; 51 sysinfo.totalhigh = 0; 52 sysinfo.freehigh = 0; 53 sysinfo.mem_unit = 0; 54 55 writer.copy_one_to_user(&sysinfo, 0)?; 56 57 return Ok(0); 58 } 59 60 pub fn umask(_mask: u32) -> Result<usize, SystemError> { 61 kwarn!("SYS_UMASK has not yet been implemented\n"); 62 return Ok(0o777); 63 } 64 65 /// ## 将随机字节填入buf 66 /// 67 /// ### 该系统调用与linux不一致,因为目前没有其他随机源 68 pub fn get_random(buf: *mut u8, len: usize, flags: GRandFlags) -> Result<usize, SystemError> { 69 if flags.bits() == (GRandFlags::GRND_INSECURE.bits() | GRandFlags::GRND_RANDOM.bits()) { 70 return Err(SystemError::EINVAL); 71 } 72 73 let mut writer = UserBufferWriter::new(buf, len, true)?; 74 75 let mut ret = Vec::new(); 76 let mut count = 0; 77 while count < len { 78 let rand = rand(); 79 for offset in 0..4 { 80 ret.push((rand >> offset * 2) as u8); 81 count += 1; 82 } 83 } 84 85 writer.copy_to_user(&ret, 0)?; 86 Ok(len) 87 } 88 } 89