1 use core::arch::x86_64::_rdtsc; 2 3 use alloc::vec::Vec; 4 5 use crate::{ 6 libs::rand::GRandFlags, 7 syscall::{user_access::UserBufferWriter, Syscall, SystemError}, 8 }; 9 10 pub fn rand() -> usize { 11 return unsafe { (_rdtsc() * _rdtsc() + 998244353_u64 * _rdtsc()) as usize }; 12 } 13 14 impl Syscall { 15 /// ## 将随机字节填入buf 16 /// 17 /// ### 该系统调用与linux不一致,因为目前没有其他随机源 18 pub fn get_random(buf: *mut u8, len: usize, flags: GRandFlags) -> Result<usize, SystemError> { 19 if flags.bits() == (GRandFlags::GRND_INSECURE.bits() | GRandFlags::GRND_RANDOM.bits()) { 20 return Err(SystemError::EINVAL); 21 } 22 23 let mut writer = UserBufferWriter::new(buf, len, true)?; 24 25 let mut ret = Vec::new(); 26 let mut count = 0; 27 while count < len { 28 let rand = rand(); 29 for offset in 0..4 { 30 ret.push((rand >> offset * 2) as u8); 31 count += 1; 32 } 33 } 34 35 writer.copy_to_user(&ret, 0)?; 36 Ok(len) 37 } 38 } 39