Skip to content

Commit

Permalink
chore: x86 random
Browse files Browse the repository at this point in the history
  • Loading branch information
chiichen committed Oct 31, 2024
1 parent 8ac8cf6 commit 080a5b0
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 8 deletions.
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
24 changes: 22 additions & 2 deletions kernel/src/arch/x86_64/rand.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
use core::arch::x86_64::_rdtsc;

pub fn rand() -> usize {
return unsafe { (_rdtsc() * _rdtsc() + 998244353_u64 * _rdtsc()) as usize };
pub fn rand() -> u64 {
return unsafe { _rdtsc() * _rdtsc() + 998244353_u64 * _rdtsc() };
}

//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::<u64>());
bytes[index..index + to_copy].copy_from_slice(&random_bytes[..to_copy]);

index += to_copy;
remaining -= to_copy;
}

bytes
}
4 changes: 2 additions & 2 deletions kernel/src/driver/net/e1000e/e1000e_driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ impl E1000EDriver {
smoltcp::wire::EthernetAddress(device.mac_address()),
));

iface_config.random_seed = rand() as u64;
iface_config.random_seed = rand();

let inner: Arc<SpinLock<E1000EDevice>> = Arc::new(SpinLock::new(device));
let result = E1000EDriver { inner };
Expand Down Expand Up @@ -196,7 +196,7 @@ impl E1000EInterface {
let mut iface_config = smoltcp::iface::Config::new(HardwareAddress::Ethernet(
smoltcp::wire::EthernetAddress(driver.inner.lock().mac_address()),
));
iface_config.random_seed = rand() as u64;
iface_config.random_seed = rand();

let iface =
smoltcp::iface::Interface::new(iface_config, &mut driver, Instant::now().into());
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/driver/net/loopback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ impl LoopbackInterface {
let mut iface_config = smoltcp::iface::Config::new(HardwareAddress::Ethernet(
smoltcp::wire::EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x01]),
));
iface_config.random_seed = rand() as u64;
iface_config.random_seed = rand();

let mut iface =
smoltcp::iface::Interface::new(iface_config, &mut driver, Instant::now().into());
Expand Down
4 changes: 2 additions & 2 deletions kernel/src/driver/net/virtio_net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ impl VirtioInterface {
let mut iface_config = iface::Config::new(wire::HardwareAddress::Ethernet(
wire::EthernetAddress(device_inner.inner.lock().mac_address()),
));
iface_config.random_seed = rand() as u64;
iface_config.random_seed = rand();

let iface = iface::Interface::new(iface_config, &mut device_inner, Instant::now().into());

Expand Down Expand Up @@ -510,7 +510,7 @@ impl VirtIONicDeviceInner {
wire::EthernetAddress(driver_net.mac_address()),
));

iface_config.random_seed = rand() as u64;
iface_config.random_seed = rand();

let inner = Arc::new(SpinLock::new(VirtIoNetImpl::new(driver_net)));
let result = VirtIONicDeviceInner { inner };
Expand Down

0 comments on commit 080a5b0

Please sign in to comment.