xref: /DragonOS/kernel/src/syscall/misc.rs (revision b8ed38251dc255b0c525801b5dbf37d3b0d0d61e)
1 use crate::{
2     arch::{mm::LockedFrameAllocator, rand::rand},
3     libs::rand::GRandFlags,
4     mm::allocator::page_frame::FrameAllocator,
5 };
6 use alloc::vec::Vec;
7 use core::cmp;
8 use system_error::SystemError;
9 
10 use super::{user_access::UserBufferWriter, Syscall};
11 
12 #[repr(C)]
13 
14 /// 系统信息
15 ///
16 /// 参考 https://code.dragonos.org.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     /// ### 该系统调用与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             // 对 len - count 的长度进行判断,remain_len 小于4则循环次数和 remain_len 相等
78             let remain_len = len - count;
79             let step = cmp::min(remain_len, 4);
80             let rand = rand();
81             for offset in 0..step {
82                 ret.push((rand >> (offset * 2)) as u8);
83                 count += 1;
84             }
85         }
86 
87         writer.copy_to_user(&ret, 0)?;
88         Ok(len)
89     }
90 }
91