Skip to content

Commit 3e674df

Browse files
committed
chore: use rand() to init random_num
1 parent be0407d commit 3e674df

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

kernel/src/arch/x86_64/process/syscall.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::{
55
arch::{
66
interrupt::TrapFrame,
77
process::table::{USER_CS, USER_DS},
8+
rand::rand_bytes,
89
CurrentIrqArch,
910
},
1011
exception::InterruptArch,
@@ -74,7 +75,7 @@ impl Syscall {
7475

7576
// 生成16字节随机数
7677
// TODO 暂时设为0
77-
param.init_info_mut().rand_num = [0u8; 16];
78+
param.init_info_mut().rand_num = rand_bytes::<16>();
7879

7980
// 把proc_init_info写到用户栈上
8081
let mut ustack_message = unsafe {

kernel/src/arch/x86_64/rand.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,23 @@ use core::arch::x86_64::_rdtsc;
33
pub fn rand() -> usize {
44
return unsafe { (_rdtsc() * _rdtsc() + 998244353_u64 * _rdtsc()) as usize };
55
}
6+
7+
//TODO move it out from arch module
8+
pub fn rand_bytes<const N: usize>() -> [u8; N] {
9+
let mut bytes = [0u8; N];
10+
let mut remaining = N;
11+
let mut index = 0;
12+
13+
while remaining > 0 {
14+
let random_num = rand();
15+
let random_bytes = random_num.to_le_bytes();
16+
17+
let to_copy = core::cmp::min(remaining, size_of::<usize>());
18+
bytes[index..index + to_copy].copy_from_slice(&random_bytes[..to_copy]);
19+
20+
index += to_copy;
21+
remaining -= to_copy;
22+
}
23+
24+
bytes
25+
}

0 commit comments

Comments
 (0)