Skip to content

Commit

Permalink
chore: use rand() to init random_num
Browse files Browse the repository at this point in the history
  • Loading branch information
chiichen committed Oct 31, 2024
1 parent be0407d commit 3e674df
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
3 changes: 2 additions & 1 deletion kernel/src/arch/x86_64/process/syscall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{
arch::{
interrupt::TrapFrame,
process::table::{USER_CS, USER_DS},
rand::rand_bytes,
CurrentIrqArch,
},
exception::InterruptArch,
Expand Down Expand Up @@ -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 {
Expand Down
20 changes: 20 additions & 0 deletions kernel/src/arch/x86_64/rand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 3e674df

Please sign in to comment.