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