Skip to content

Commit 1a50ec0

Browse files
rth7680willdeacon
authored andcommitted
arm64: Implement archrandom.h for ARMv8.5-RNG
Expose the ID_AA64ISAR0.RNDR field to userspace, as the RNG system registers are always available at EL0. Implement arch_get_random_seed_long using RNDR. Given that the TRNG is likely to be a shared resource between cores, and VMs, do not explicitly force re-seeding with RNDRRS. In order to avoid code complexity and potential issues with hetrogenous systems only provide values after cpufeature has finalized the system capabilities. Signed-off-by: Richard Henderson <[email protected]> [Modified to only function after cpufeature has finalized the system capabilities and move all the code into the header -- broonie] Signed-off-by: Mark Brown <[email protected]> Reviewed-by: Mark Rutland <[email protected]> Reviewed-by: Ard Biesheuvel <[email protected]> [will: Advertise HWCAP via /proc/cpuinfo] Signed-off-by: Will Deacon <[email protected]>
1 parent 46cf053 commit 1a50ec0

File tree

10 files changed

+108
-1
lines changed

10 files changed

+108
-1
lines changed

Documentation/arm64/cpu-feature-registers.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ infrastructure:
117117
+------------------------------+---------+---------+
118118
| Name | bits | visible |
119119
+------------------------------+---------+---------+
120+
| RNDR | [63-60] | y |
121+
+------------------------------+---------+---------+
120122
| TS | [55-52] | y |
121123
+------------------------------+---------+---------+
122124
| FHM | [51-48] | y |

Documentation/arm64/elf_hwcaps.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,10 @@ HWCAP2_FRINT
204204

205205
Functionality implied by ID_AA64ISAR1_EL1.FRINTTS == 0b0001.
206206

207+
HWCAP2_RNG
208+
209+
Functionality implied by ID_AA64ISAR0_EL1.RNDR == 0b0001.
210+
207211

208212
4. Unused AT_HWCAP bits
209213
-----------------------

arch/arm64/Kconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,6 +1484,18 @@ config ARM64_PTR_AUTH
14841484

14851485
endmenu
14861486

1487+
menu "ARMv8.5 architectural features"
1488+
1489+
config ARCH_RANDOM
1490+
bool "Enable support for random number generation"
1491+
default y
1492+
help
1493+
Random number generation (part of the ARMv8.5 Extensions)
1494+
provides a high bandwidth, cryptographically secure
1495+
hardware random number generator.
1496+
1497+
endmenu
1498+
14871499
config ARM64_SVE
14881500
bool "ARM Scalable Vector Extension support"
14891501
default y

arch/arm64/include/asm/archrandom.h

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
#ifndef _ASM_ARCHRANDOM_H
3+
#define _ASM_ARCHRANDOM_H
4+
5+
#ifdef CONFIG_ARCH_RANDOM
6+
7+
#include <linux/random.h>
8+
#include <asm/cpufeature.h>
9+
10+
static inline bool __arm64_rndr(unsigned long *v)
11+
{
12+
bool ok;
13+
14+
/*
15+
* Reads of RNDR set PSTATE.NZCV to 0b0000 on success,
16+
* and set PSTATE.NZCV to 0b0100 otherwise.
17+
*/
18+
asm volatile(
19+
__mrs_s("%0", SYS_RNDR_EL0) "\n"
20+
" cset %w1, ne\n"
21+
: "=r" (*v), "=r" (ok)
22+
:
23+
: "cc");
24+
25+
return ok;
26+
}
27+
28+
static inline bool __must_check arch_get_random_long(unsigned long *v)
29+
{
30+
return false;
31+
}
32+
33+
static inline bool __must_check arch_get_random_int(unsigned int *v)
34+
{
35+
return false;
36+
}
37+
38+
static inline bool __must_check arch_get_random_seed_long(unsigned long *v)
39+
{
40+
/*
41+
* Only support the generic interface after we have detected
42+
* the system wide capability, avoiding complexity with the
43+
* cpufeature code and with potential scheduling between CPUs
44+
* with and without the feature.
45+
*/
46+
if (!cpus_have_const_cap(ARM64_HAS_RNG))
47+
return false;
48+
49+
return __arm64_rndr(v);
50+
}
51+
52+
53+
static inline bool __must_check arch_get_random_seed_int(unsigned int *v)
54+
{
55+
unsigned long val;
56+
bool ok = arch_get_random_seed_long(&val);
57+
58+
*v = val;
59+
return ok;
60+
}
61+
62+
#else
63+
64+
static inline bool __arm64_rndr(unsigned long *v) { return false; }
65+
66+
#endif /* CONFIG_ARCH_RANDOM */
67+
#endif /* _ASM_ARCHRANDOM_H */

arch/arm64/include/asm/cpucaps.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@
5656
#define ARM64_WORKAROUND_CAVIUM_TX2_219_PRFM 46
5757
#define ARM64_WORKAROUND_1542419 47
5858
#define ARM64_WORKAROUND_1319367 48
59+
#define ARM64_HAS_RNG 49
5960

60-
#define ARM64_NCAPS 49
61+
#define ARM64_NCAPS 50
6162

6263
#endif /* __ASM_CPUCAPS_H */

arch/arm64/include/asm/hwcap.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
#define KERNEL_HWCAP_SVESM4 __khwcap2_feature(SVESM4)
8787
#define KERNEL_HWCAP_FLAGM2 __khwcap2_feature(FLAGM2)
8888
#define KERNEL_HWCAP_FRINT __khwcap2_feature(FRINT)
89+
#define KERNEL_HWCAP_RNG __khwcap2_feature(RNG)
8990

9091
/*
9192
* This yields a mask that user programs can use to figure out what

arch/arm64/include/asm/sysreg.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,9 @@
365365
#define SYS_CTR_EL0 sys_reg(3, 3, 0, 0, 1)
366366
#define SYS_DCZID_EL0 sys_reg(3, 3, 0, 0, 7)
367367

368+
#define SYS_RNDR_EL0 sys_reg(3, 3, 2, 4, 0)
369+
#define SYS_RNDRRS_EL0 sys_reg(3, 3, 2, 4, 1)
370+
368371
#define SYS_PMCR_EL0 sys_reg(3, 3, 9, 12, 0)
369372
#define SYS_PMCNTENSET_EL0 sys_reg(3, 3, 9, 12, 1)
370373
#define SYS_PMCNTENCLR_EL0 sys_reg(3, 3, 9, 12, 2)
@@ -539,6 +542,7 @@
539542
ENDIAN_SET_EL1 | SCTLR_EL1_UCI | SCTLR_EL1_RES1)
540543

541544
/* id_aa64isar0 */
545+
#define ID_AA64ISAR0_RNDR_SHIFT 60
542546
#define ID_AA64ISAR0_TS_SHIFT 52
543547
#define ID_AA64ISAR0_FHM_SHIFT 48
544548
#define ID_AA64ISAR0_DP_SHIFT 44

arch/arm64/include/uapi/asm/hwcap.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,6 @@
6565
#define HWCAP2_SVESM4 (1 << 6)
6666
#define HWCAP2_FLAGM2 (1 << 7)
6767
#define HWCAP2_FRINT (1 << 8)
68+
#define HWCAP2_RNG (1 << 9)
6869

6970
#endif /* _UAPI__ASM_HWCAP_H */

arch/arm64/kernel/cpufeature.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ static void cpu_enable_cnp(struct arm64_cpu_capabilities const *cap);
119119
* sync with the documentation of the CPU feature register ABI.
120120
*/
121121
static const struct arm64_ftr_bits ftr_id_aa64isar0[] = {
122+
ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_RNDR_SHIFT, 4, 0),
122123
ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_TS_SHIFT, 4, 0),
123124
ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_FHM_SHIFT, 4, 0),
124125
ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_DP_SHIFT, 4, 0),
@@ -1566,6 +1567,18 @@ static const struct arm64_cpu_capabilities arm64_features[] = {
15661567
.sign = FTR_UNSIGNED,
15671568
.min_field_value = 1,
15681569
},
1570+
#endif
1571+
#ifdef CONFIG_ARCH_RANDOM
1572+
{
1573+
.desc = "Random Number Generator",
1574+
.capability = ARM64_HAS_RNG,
1575+
.type = ARM64_CPUCAP_SYSTEM_FEATURE,
1576+
.matches = has_cpuid_feature,
1577+
.sys_reg = SYS_ID_AA64ISAR0_EL1,
1578+
.field_pos = ID_AA64ISAR0_RNDR_SHIFT,
1579+
.sign = FTR_UNSIGNED,
1580+
.min_field_value = 1,
1581+
},
15691582
#endif
15701583
{},
15711584
};
@@ -1638,6 +1651,7 @@ static const struct arm64_cpu_capabilities arm64_elf_hwcaps[] = {
16381651
HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_FHM_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_ASIMDFHM),
16391652
HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_TS_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_FLAGM),
16401653
HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_TS_SHIFT, FTR_UNSIGNED, 2, CAP_HWCAP, KERNEL_HWCAP_FLAGM2),
1654+
HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_RNDR_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_RNG),
16411655
HWCAP_CAP(SYS_ID_AA64PFR0_EL1, ID_AA64PFR0_FP_SHIFT, FTR_SIGNED, 0, CAP_HWCAP, KERNEL_HWCAP_FP),
16421656
HWCAP_CAP(SYS_ID_AA64PFR0_EL1, ID_AA64PFR0_FP_SHIFT, FTR_SIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_FPHP),
16431657
HWCAP_CAP(SYS_ID_AA64PFR0_EL1, ID_AA64PFR0_ASIMD_SHIFT, FTR_SIGNED, 0, CAP_HWCAP, KERNEL_HWCAP_ASIMD),

arch/arm64/kernel/cpuinfo.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ static const char *const hwcap_str[] = {
8484
"svesm4",
8585
"flagm2",
8686
"frint",
87+
"rng",
8788
NULL
8889
};
8990

0 commit comments

Comments
 (0)