Эх сурвалжийг харах

chore: use rand() to init random_num

chiichen 5 сар өмнө
parent
commit
3e674dfa9e

+ 2 - 1
kernel/src/arch/x86_64/process/syscall.rs

@@ -5,6 +5,7 @@ use crate::{
     arch::{
         interrupt::TrapFrame,
         process::table::{USER_CS, USER_DS},
+        rand::rand_bytes,
         CurrentIrqArch,
     },
     exception::InterruptArch,
@@ -74,7 +75,7 @@ impl Syscall {
 
         // 生成16字节随机数
         // TODO 暂时设为0
-        param.init_info_mut().rand_num = [0u8; 16];
+        param.init_info_mut().rand_num = rand_bytes::<16>();
 
         // 把proc_init_info写到用户栈上
         let mut ustack_message = unsafe {

+ 20 - 0
kernel/src/arch/x86_64/rand.rs

@@ -3,3 +3,23 @@ use core::arch::x86_64::_rdtsc;
 pub fn rand() -> usize {
     return unsafe { (_rdtsc() * _rdtsc() + 998244353_u64 * _rdtsc()) as usize };
 }
+
+//TODO move it out from arch module
+pub fn rand_bytes<const N: usize>() -> [u8; N] {
+    let mut bytes = [0u8; N];
+    let mut remaining = N;
+    let mut index = 0;
+
+    while remaining > 0 {
+        let random_num = rand();
+        let random_bytes = random_num.to_le_bytes();
+
+        let to_copy = core::cmp::min(remaining, size_of::<usize>());
+        bytes[index..index + to_copy].copy_from_slice(&random_bytes[..to_copy]);
+
+        index += to_copy;
+        remaining -= to_copy;
+    }
+
+    bytes
+}