Skip to content

Commit 4200c9f

Browse files
committed
Support x86_64-unknown-uefi
1 parent bcad3fb commit 4200c9f

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ matrix:
106106
- rustup target add x86_64-unknown-netbsd
107107
- rustup target add x86_64-unknown-redox
108108
- rustup target add x86_64-fortanix-unknown-sgx
109+
- cargo install cargo-xbuild # For no_std targets
109110
script:
110111
- cargo build --target=x86_64-sun-solaris --all-features
111112
- cargo build --target=x86_64-unknown-cloudabi --all-features
@@ -114,6 +115,7 @@ matrix:
114115
- cargo build --target=x86_64-unknown-netbsd --all-features
115116
- cargo build --target=x86_64-unknown-redox --all-features
116117
- cargo build --target=x86_64-fortanix-unknown-sgx --all-features
118+
- cargo xbuild --target=x86_64-unknown-uefi
117119
# also test minimum dependency versions are usable
118120
- cargo generate-lockfile -Z minimal-versions
119121
- cargo build --target=x86_64-sun-solaris --all-features
@@ -123,6 +125,7 @@ matrix:
123125
- cargo build --target=x86_64-unknown-netbsd --all-features
124126
- cargo build --target=x86_64-unknown-redox --all-features
125127
- cargo build --target=x86_64-fortanix-unknown-sgx --all-features
128+
- cargo xbuild --target=x86_64-unknown-uefi
126129

127130
# Trust cross-built/emulated targets. We must repeat all non-default values.
128131
- rust: stable

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,8 @@ stdweb = { version = "0.4.9", optional = true }
3939
[target.wasm32-wasi.dependencies]
4040
libc = "0.2.54"
4141

42+
[target.'cfg(any(target_env = "sgx", target_os = "uefi"))'.dependencies]
43+
lazy_static = { version = "1.3.0", features = ["spin_no_std"] }
44+
4245
[features]
4346
std = []

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ mod_use!(cfg(target_os = "redox"), use_file);
190190
mod_use!(cfg(target_os = "solaris"), solaris_illumos);
191191
mod_use!(cfg(windows), windows);
192192
mod_use!(cfg(target_env = "sgx"), rdrand);
193+
mod_use!(cfg(target_os = "uefi"), rdrand);
193194
mod_use!(cfg(target_os = "wasi"), wasi);
194195

195196
mod_use!(
@@ -231,6 +232,7 @@ mod_use!(
231232
target_os = "openbsd",
232233
target_os = "redox",
233234
target_os = "solaris",
235+
target_os = "uefi",
234236
target_env = "sgx",
235237
windows,
236238
all(

src/rdrand.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@
99
//! Implementation for SGX using RDRAND instruction
1010
use crate::Error;
1111
use core::mem;
12-
use core::arch::x86_64::_rdrand64_step;
12+
use core::arch::x86_64::{__cpuid, _rdrand64_step};
1313
use core::num::NonZeroU32;
14-
15-
#[cfg(not(target_feature = "rdrand"))]
16-
compile_error!("enable rdrand target feature!");
14+
use lazy_static::lazy_static;
1715

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

35+
fn cpuid_check() -> bool {
36+
const FEATURE_INFO_LEAF: u32 = 1;
37+
const RDRAND_FLAG: u32 = 1 << 30;
38+
// SAFETY: All platforms with CPUID support leaf 1
39+
unsafe { __cpuid(FEATURE_INFO_LEAF).ecx & RDRAND_FLAG != 0 }
40+
}
41+
3842
pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
43+
lazy_static! { static ref HAS_RDRAND: bool = cpuid_check(); }
44+
if cfg!(not(target_feature = "rdrand")) && !*HAS_RDRAND {
45+
return Err(Error::UNAVAILABLE);
46+
}
47+
3948
// We use chunks_exact_mut instead of chunks_mut as it allows almost all
4049
// calls to memcpy to be elided by the compiler.
4150
let mut chunks = dest.chunks_exact_mut(WORD_SIZE);

0 commit comments

Comments
 (0)