Skip to content

Commit

Permalink
Support x86_64-unknown-uefi
Browse files Browse the repository at this point in the history
  • Loading branch information
josephlr committed Jun 12, 2019
1 parent bcad3fb commit 4200c9f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ matrix:
- rustup target add x86_64-unknown-netbsd
- rustup target add x86_64-unknown-redox
- rustup target add x86_64-fortanix-unknown-sgx
- cargo install cargo-xbuild # For no_std targets
script:
- cargo build --target=x86_64-sun-solaris --all-features
- cargo build --target=x86_64-unknown-cloudabi --all-features
Expand All @@ -114,6 +115,7 @@ matrix:
- cargo build --target=x86_64-unknown-netbsd --all-features
- cargo build --target=x86_64-unknown-redox --all-features
- cargo build --target=x86_64-fortanix-unknown-sgx --all-features
- cargo xbuild --target=x86_64-unknown-uefi
# also test minimum dependency versions are usable
- cargo generate-lockfile -Z minimal-versions
- cargo build --target=x86_64-sun-solaris --all-features
Expand All @@ -123,6 +125,7 @@ matrix:
- cargo build --target=x86_64-unknown-netbsd --all-features
- cargo build --target=x86_64-unknown-redox --all-features
- cargo build --target=x86_64-fortanix-unknown-sgx --all-features
- cargo xbuild --target=x86_64-unknown-uefi

# Trust cross-built/emulated targets. We must repeat all non-default values.
- rust: stable
Expand Down
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,8 @@ stdweb = { version = "0.4.9", optional = true }
[target.wasm32-wasi.dependencies]
libc = "0.2.54"

[target.'cfg(any(target_env = "sgx", target_os = "uefi"))'.dependencies]
lazy_static = { version = "1.3.0", features = ["spin_no_std"] }

[features]
std = []
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ mod_use!(cfg(target_os = "redox"), use_file);
mod_use!(cfg(target_os = "solaris"), solaris_illumos);
mod_use!(cfg(windows), windows);
mod_use!(cfg(target_env = "sgx"), rdrand);
mod_use!(cfg(target_os = "uefi"), rdrand);
mod_use!(cfg(target_os = "wasi"), wasi);

mod_use!(
Expand Down Expand Up @@ -231,6 +232,7 @@ mod_use!(
target_os = "openbsd",
target_os = "redox",
target_os = "solaris",
target_os = "uefi",
target_env = "sgx",
windows,
all(
Expand Down
19 changes: 14 additions & 5 deletions src/rdrand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@
//! Implementation for SGX using RDRAND instruction
use crate::Error;
use core::mem;
use core::arch::x86_64::_rdrand64_step;
use core::arch::x86_64::{__cpuid, _rdrand64_step};
use core::num::NonZeroU32;

#[cfg(not(target_feature = "rdrand"))]
compile_error!("enable rdrand target feature!");
use lazy_static::lazy_static;

// Recommendation from "Intel® Digital Random Number Generator (DRNG) Software
// Implementation Guide" - Section 5.2.1 and "Intel® 64 and IA-32 Architectures
Expand All @@ -31,11 +29,22 @@ fn rdrand() -> Result<[u8; WORD_SIZE], Error> {
}
};
}
error!("RDRAND failed, CPU issue likely");
Err(Error::UNKNOWN)
}

fn cpuid_check() -> bool {
const FEATURE_INFO_LEAF: u32 = 1;
const RDRAND_FLAG: u32 = 1 << 30;
// SAFETY: All platforms with CPUID support leaf 1
unsafe { __cpuid(FEATURE_INFO_LEAF).ecx & RDRAND_FLAG != 0 }
}

pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
lazy_static! { static ref HAS_RDRAND: bool = cpuid_check(); }
if cfg!(not(target_feature = "rdrand")) && !*HAS_RDRAND {
return Err(Error::UNAVAILABLE);
}

// We use chunks_exact_mut instead of chunks_mut as it allows almost all
// calls to memcpy to be elided by the compiler.
let mut chunks = dest.chunks_exact_mut(WORD_SIZE);
Expand Down

0 comments on commit 4200c9f

Please sign in to comment.