This repository was archived by the owner on Nov 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
Add support for using the Arm v8.5-RNG #38
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
// | ||
// Copyright (c) 2022 Amazon.com, Inc. or its affiliates. | ||
// | ||
// Author(s): | ||
// - Ali Saidi <alisaidi@amazon.com> | ||
|
||
|
||
/// Implement an interface for accessing Arm v8.5 RNG instructions. | ||
/// An empty struct is used to confirm that the system has the | ||
/// instructions available. | ||
/// # Example: | ||
/// ```no_run | ||
/// use cortex_a::asm::random::ArmRng; | ||
/// if let Some(rng) = ArmRng::new() { | ||
/// let rand_num = rng.rndr(); | ||
/// } | ||
/// ``` | ||
#[derive(Copy, Clone, Debug)] | ||
pub struct ArmRng; | ||
|
||
use crate::registers::ID_AA64ISAR0_EL1; | ||
use core::arch::asm; | ||
use tock_registers::interfaces::Readable; | ||
|
||
impl ArmRng { | ||
/// Return an empty object that is used to gate calling | ||
/// rndr and rndrss on discovery of the feature so each | ||
/// call doesn't need to confirm it. | ||
#[inline] | ||
pub fn new() -> Option<Self> { | ||
#[cfg(not(target_arch = "aarch64"))] | ||
return None; | ||
|
||
#[cfg(target_arch = "aarch64")] | ||
if ID_AA64ISAR0_EL1.is_set(ID_AA64ISAR0_EL1::RNDR) { | ||
Some(ArmRng) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
/// Return an random number from the Arm v8.5 RNG. | ||
/// This returns an option because the instruction can fail | ||
/// (e.g. the entropy is exhausted or the RNG has failed.) | ||
#[inline] | ||
pub fn rndr(&self) -> Option<u64> { | ||
let mut flags: u64; | ||
let mut data: u64; | ||
|
||
#[cfg(target_arch = "aarch64")] | ||
unsafe { | ||
asm!( | ||
"mrs {o}, s3_3_c2_c4_0", | ||
"mrs {f}, nzcv", | ||
o = out(reg) data, | ||
f = out(reg) flags, | ||
options(nomem, nostack)); | ||
} | ||
if cfg!(not(target_arch = "aarch64")) || flags != 0 { | ||
None | ||
} else { | ||
Some(data) | ||
} | ||
} | ||
|
||
/// Return an random number from the Arm v8.5 RNG after reseeding it | ||
/// This returns an option because the instruction can fail | ||
/// (e.g. the entropy is exhausted or the RNG has failed.) | ||
#[inline] | ||
pub fn rndrss(&self) -> Option<u64> { | ||
let mut flags: u64; | ||
let mut data: u64; | ||
|
||
#[cfg(target_arch = "aarch64")] | ||
unsafe { | ||
asm!( | ||
"mrs {o}, s3_3_c2_c4_1", | ||
"mrs {f}, nzcv", | ||
o = out(reg) data, | ||
f = out(reg) flags, | ||
options(nomem, nostack)); | ||
} | ||
|
||
if cfg!(not(target_arch = "aarch64")) || flags != 0 { | ||
None | ||
} else { | ||
Some(data) | ||
} | ||
} | ||
|
||
|
||
} | ||
|
||
|
||
#[cfg(all(test, target_os = "linux"))] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
pub fn test_rndr() { | ||
// This works on Linux from userspace since Linux emulatates | ||
// the Arm ID registers on the userspace undef. | ||
if let Some(rand) = ArmRng::new() { | ||
assert!(rand.rndr().unwrap() != 0); | ||
assert!(rand.rndrss().unwrap() != 0); | ||
} | ||
} | ||
|
||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
// | ||
// Copyright (c) 2022 Amazon.com, Inc. or its affiliates. | ||
// | ||
// Author(s): | ||
// - Ali Saidi <alisaidi@amazon.com> | ||
|
||
//! AArch64 Instruction Set Architecture Feature Register 0 - EL1 | ||
//! | ||
//! Provides information about the implemented instruction set. | ||
|
||
use tock_registers::{interfaces::Readable, register_bitfields}; | ||
|
||
register_bitfields! {u64, | ||
pub ID_AA64ISAR0_EL1 [ | ||
/// Support for Random Number instructions in AArch64. | ||
/// | ||
/// 0000 No random number instructions are implemented | ||
/// 0001 RNDR and RNDRSS are implemented | ||
/// | ||
/// All other values are reserved. | ||
RNDR OFFSET(60) NUMBITS(4) [ | ||
Supported = 0b0001, | ||
NotSupported = 0b0000 | ||
], | ||
] | ||
} | ||
|
||
pub struct Reg; | ||
|
||
impl Readable for Reg { | ||
type T = u64; | ||
type R = ID_AA64ISAR0_EL1::Register; | ||
|
||
sys_coproc_read_raw!(u64, "ID_AA64ISAR0_EL1", "x"); | ||
} | ||
|
||
pub const ID_AA64ISAR0_EL1: Reg = Reg {}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.