Debian Codegraph @

.cargo/vendor/getrandom/src/backends/rndr.rs

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