|
| 1 | +//! RNDR register backend for aarch64 targets |
| 2 | +// Arm Architecture Reference Manual for A-profile architecture |
| 3 | +// ARM DDI 0487K.a, ID032224, D23.2.147 RNDR, Random Number |
| 4 | + |
| 5 | +#[cfg(not(target_arch = "aarch64"))] |
| 6 | +compile_error!("the `rndr` backend can be enabled only for AArch64 targets!"); |
| 7 | + |
| 8 | +use crate::{util::slice_as_uninit, Error}; |
| 9 | +use core::arch::asm; |
| 10 | +use core::mem::{size_of, MaybeUninit}; |
| 11 | + |
| 12 | +const RETRY_LIMIT: usize = 5; |
| 13 | + |
| 14 | +/// Read a random number from the aarch64 RNDR register |
| 15 | +/// |
| 16 | +/// Callers must ensure that FEAT_RNG is available on the system |
| 17 | +/// The function assumes that the RNDR register is available |
| 18 | +/// If it fails to read a random number, it will retry up to 5 times |
| 19 | +/// After 5 failed reads the function will return `None` |
| 20 | +#[target_feature(enable = "rand")] |
| 21 | +unsafe fn rndr() -> Option<u64> { |
| 22 | + for _ in 0..RETRY_LIMIT { |
| 23 | + let mut x: u64; |
| 24 | + let mut nzcv: u64; |
| 25 | + |
| 26 | + // AArch64 RNDR register is accessible by s3_3_c2_c4_0 |
| 27 | + asm!( |
| 28 | + "mrs {x}, RNDR", |
| 29 | + "mrs {nzcv}, NZCV", |
| 30 | + x = out(reg) x, |
| 31 | + nzcv = out(reg) nzcv, |
| 32 | + ); |
| 33 | + |
| 34 | + // If the hardware returns a genuine random number, PSTATE.NZCV is set to 0b0000 |
| 35 | + if nzcv == 0 { |
| 36 | + return Some(x); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + None |
| 41 | +} |
| 42 | + |
| 43 | +#[target_feature(enable = "rand")] |
| 44 | +unsafe fn rndr_fill(dest: &mut [MaybeUninit<u8>]) -> Option<()> { |
| 45 | + let mut chunks = dest.chunks_exact_mut(size_of::<u64>()); |
| 46 | + for chunk in chunks.by_ref() { |
| 47 | + let src = rndr()?.to_ne_bytes(); |
| 48 | + chunk.copy_from_slice(slice_as_uninit(&src)); |
| 49 | + } |
| 50 | + |
| 51 | + let tail = chunks.into_remainder(); |
| 52 | + let n = tail.len(); |
| 53 | + if n > 0 { |
| 54 | + let src = rndr()?.to_ne_bytes(); |
| 55 | + tail.copy_from_slice(slice_as_uninit(&src[..n])); |
| 56 | + } |
| 57 | + Some(()) |
| 58 | +} |
| 59 | + |
| 60 | +fn is_rndr_available() -> bool { |
| 61 | + cfg_if::cfg_if! { |
| 62 | + if #[cfg(target_feature = "rand")] { |
| 63 | + true |
| 64 | + } else if #[cfg(target_os = "linux")] { |
| 65 | + /// Check whether FEAT_RNG is available on the system |
| 66 | + /// |
| 67 | + /// Requires the caller either be running in EL1 or be on a system supporting MRS |
| 68 | + /// emulation. Due to the above, the implementation is currently restricted to Linux. |
| 69 | + /// |
| 70 | + /// Relying on runtime detection bumps minimum supported Linux kernel version to 4.11. |
| 71 | + fn mrs_check() -> bool { |
| 72 | + let mut id_aa64isar0: u64; |
| 73 | + |
| 74 | + // If FEAT_RNG is implemented, ID_AA64ISAR0_EL1.RNDR (bits 60-63) are 0b0001 |
| 75 | + // This is okay to do from EL0 in Linux because Linux will emulate MRS as per |
| 76 | + // https://docs.kernel.org/arch/arm64/cpu-feature-registers.html |
| 77 | + unsafe { |
| 78 | + asm!( |
| 79 | + "mrs {id}, ID_AA64ISAR0_EL1", |
| 80 | + id = out(reg) id_aa64isar0, |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + (id_aa64isar0 >> 60) & 0xf >= 1 |
| 85 | + } |
| 86 | + |
| 87 | + #[path = "../src/lazy.rs"] mod lazy; |
| 88 | + static RNDR_GOOD: lazy::LazyBool = lazy::LazyBool::new(); |
| 89 | + RNDR_GOOD.unsync_init(mrs_check) |
| 90 | + } else if #[cfg(feature = "std")] { |
| 91 | + extern crate std; |
| 92 | + #[path = "../src/lazy.rs"] mod lazy; |
| 93 | + static RNDR_GOOD: lazy::LazyBool = lazy::LazyBool::new(); |
| 94 | + RNDR_GOOD.unsync_init(|| std::arch::is_aarch64_feature_detected!("rand")) |
| 95 | + } else { |
| 96 | + compile_error!( |
| 97 | + "RNDR `no_std` runtime detection is currently supported only on Linux targets. \ |
| 98 | + Either enable the `std` crate feature, or `rand` target feature at compile time." |
| 99 | + ); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> { |
| 105 | + if is_rndr_available() { |
| 106 | + // SAFETY: after this point, we know the `rand` target feature is enabled |
| 107 | + unsafe { rndr_fill(dest).ok_or(Error::RNDR_FAILURE) } |
| 108 | + } else { |
| 109 | + Err(Error::RNDR_NOT_AVAILABLE) |
| 110 | + } |
| 111 | +} |
0 commit comments