From 3e674dfa9eaa6feefad9a00ebb0c1979c462920d Mon Sep 17 00:00:00 2001 From: chiichen Date: Thu, 31 Oct 2024 22:44:25 +0800 Subject: [PATCH] chore: use rand() to init random_num --- kernel/src/arch/x86_64/process/syscall.rs | 3 ++- kernel/src/arch/x86_64/rand.rs | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/kernel/src/arch/x86_64/process/syscall.rs b/kernel/src/arch/x86_64/process/syscall.rs index 7fe4944cf..d809e09a8 100644 --- a/kernel/src/arch/x86_64/process/syscall.rs +++ b/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 { diff --git a/kernel/src/arch/x86_64/rand.rs b/kernel/src/arch/x86_64/rand.rs index 6916c64dd..a08881e6e 100644 --- a/kernel/src/arch/x86_64/rand.rs +++ b/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() -> [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::()); + bytes[index..index + to_copy].copy_from_slice(&random_bytes[..to_copy]); + + index += to_copy; + remaining -= to_copy; + } + + bytes +}