Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix broken rdrand implementation #154

Merged
merged 1 commit into from
Jan 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions src/arch/x86_64/kernel/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ use crate::environment;
use crate::x86::controlregs::*;
use crate::x86::cpuid::*;
use crate::x86::msr::*;
use core::arch::x86_64::__rdtscp as rdtscp;
use core::arch::x86_64::_rdtsc as rdtsc;
use core::arch::x86_64::{__rdtscp as rdtscp, _rdrand32_step, _rdrand64_step, _rdtsc as rdtsc};
use core::convert::TryInto;
use core::sync::atomic::spin_loop_hint;
use core::{fmt, u32};
Expand All @@ -35,6 +34,9 @@ const EFER_LMSLE: u64 = 1 << 13;
const EFER_FFXSR: u64 = 1 << 14;
const EFER_TCE: u64 = 1 << 15;

// See Intel SDM - Volume 1 - Section 7.3.17.1
const RDRAND_RETRY_LIMIT: usize = 10;

static mut CPU_FREQUENCY: CpuFrequency = CpuFrequency::new();
static mut CPU_SPEEDSTEP: CpuSpeedStep = CpuSpeedStep::new();
static mut PHYSICAL_ADDRESS_BITS: u8 = 0;
Expand Down Expand Up @@ -894,14 +896,13 @@ pub fn generate_random_number32() -> Option<u32> {
if SUPPORTS_RDRAND {
let mut value: u32 = 0;

while core::arch::x86_64::_rdrand32_step(&mut value) == 1 {
spin_loop_hint();
for _ in 0..RDRAND_RETRY_LIMIT {
if _rdrand32_step(&mut value) == 1 {
return Some(value);
}
}

Some(value)
} else {
None
}
None
}
}

Expand All @@ -910,14 +911,13 @@ pub fn generate_random_number64() -> Option<u64> {
if SUPPORTS_RDRAND {
let mut value: u64 = 0;

while core::arch::x86_64::_rdrand64_step(&mut value) == 1 {
spin_loop_hint();
for _ in 0..RDRAND_RETRY_LIMIT {
if _rdrand64_step(&mut value) == 1 {
return Some(value);
}
}

Some(value)
} else {
None
}
None
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/drivers/virtio/transport/channel_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.
// copied, modified, or distributed except according to those terms.
2 changes: 1 addition & 1 deletion src/drivers/virtio/transport/mmio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.
// copied, modified, or distributed except according to those terms.