Skip to content

Commit bc20606

Browse files
committed
Merge branch 'for-next/rng' into for-next/core
* for-next/rng: (2 commits) arm64: Use v8.5-RNG entropy for KASLR seed ...
2 parents ab3906c + 2e8e1ea commit bc20606

File tree

11 files changed

+124
-2
lines changed

11 files changed

+124
-2
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
@@ -232,6 +232,10 @@ HWCAP2_DGH
232232

233233
Functionality implied by ID_AA64ISAR1_EL1.DGH == 0b0001.
234234

235+
HWCAP2_RNG
236+
237+
Functionality implied by ID_AA64ISAR0_EL1.RNDR == 0b0001.
238+
235239
4. Unused AT_HWCAP bits
236240
-----------------------
237241

arch/arm64/Kconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1528,6 +1528,14 @@ config ARM64_E0PD
15281528

15291529
This option enables E0PD for TTBR1 where available.
15301530

1531+
config ARCH_RANDOM
1532+
bool "Enable support for random number generation"
1533+
default y
1534+
help
1535+
Random number generation (part of the ARMv8.5 Extensions)
1536+
provides a high bandwidth, cryptographically secure
1537+
hardware random number generator.
1538+
15311539
endmenu
15321540

15331541
config ARM64_SVE

arch/arm64/include/asm/archrandom.h

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
static inline bool __init __early_cpu_has_rndr(void)
63+
{
64+
/* Open code as we run prior to the first call to cpufeature. */
65+
unsigned long ftr = read_sysreg_s(SYS_ID_AA64ISAR0_EL1);
66+
return (ftr >> ID_AA64ISAR0_RNDR_SHIFT) & 0xf;
67+
}
68+
69+
#else
70+
71+
static inline bool __arm64_rndr(unsigned long *v) { return false; }
72+
static inline bool __init __early_cpu_has_rndr(void) { return false; }
73+
74+
#endif /* CONFIG_ARCH_RANDOM */
75+
#endif /* _ASM_ARCHRANDOM_H */

arch/arm64/include/asm/cpucaps.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@
5757
#define ARM64_WORKAROUND_1542419 47
5858
#define ARM64_WORKAROUND_SPECULATIVE_AT_NVHE 48
5959
#define ARM64_HAS_E0PD 49
60+
#define ARM64_HAS_RNG 50
6061

61-
#define ARM64_NCAPS 50
62+
#define ARM64_NCAPS 51
6263

6364
#endif /* __ASM_CPUCAPS_H */

arch/arm64/include/asm/hwcap.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,9 @@
9191
#define KERNEL_HWCAP_SVEF64MM __khwcap2_feature(SVEF64MM)
9292
#define KERNEL_HWCAP_SVEBF16 __khwcap2_feature(SVEBF16)
9393
#define KERNEL_HWCAP_I8MM __khwcap2_feature(I8MM)
94-
#define KERNEL_HWCAP_DGH __khwcap2_feature(DGH)
9594
#define KERNEL_HWCAP_BF16 __khwcap2_feature(BF16)
95+
#define KERNEL_HWCAP_DGH __khwcap2_feature(DGH)
96+
#define KERNEL_HWCAP_RNG __khwcap2_feature(RNG)
9697

9798
/*
9899
* 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
@@ -366,6 +366,9 @@
366366
#define SYS_CTR_EL0 sys_reg(3, 3, 0, 0, 1)
367367
#define SYS_DCZID_EL0 sys_reg(3, 3, 0, 0, 7)
368368

369+
#define SYS_RNDR_EL0 sys_reg(3, 3, 2, 4, 0)
370+
#define SYS_RNDRRS_EL0 sys_reg(3, 3, 2, 4, 1)
371+
369372
#define SYS_PMCR_EL0 sys_reg(3, 3, 9, 12, 0)
370373
#define SYS_PMCNTENSET_EL0 sys_reg(3, 3, 9, 12, 1)
371374
#define SYS_PMCNTENCLR_EL0 sys_reg(3, 3, 9, 12, 2)
@@ -552,6 +555,7 @@
552555
#define MAIR_ATTRIDX(attr, idx) ((attr) << ((idx) * 8))
553556

554557
/* id_aa64isar0 */
558+
#define ID_AA64ISAR0_RNDR_SHIFT 60
555559
#define ID_AA64ISAR0_TS_SHIFT 52
556560
#define ID_AA64ISAR0_FHM_SHIFT 48
557561
#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
@@ -72,5 +72,6 @@
7272
#define HWCAP2_I8MM (1 << 13)
7373
#define HWCAP2_BF16 (1 << 14)
7474
#define HWCAP2_DGH (1 << 15)
75+
#define HWCAP2_RNG (1 << 16)
7576

7677
#endif /* _UAPI__ASM_HWCAP_H */

arch/arm64/kernel/cpufeature.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ static void cpu_enable_cnp(struct arm64_cpu_capabilities const *cap);
121121
* sync with the documentation of the CPU feature register ABI.
122122
*/
123123
static const struct arm64_ftr_bits ftr_id_aa64isar0[] = {
124+
ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_RNDR_SHIFT, 4, 0),
124125
ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_TS_SHIFT, 4, 0),
125126
ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_FHM_SHIFT, 4, 0),
126127
ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_DP_SHIFT, 4, 0),
@@ -1658,6 +1659,18 @@ static const struct arm64_cpu_capabilities arm64_features[] = {
16581659
.min_field_value = 1,
16591660
.cpu_enable = cpu_enable_e0pd,
16601661
},
1662+
#endif
1663+
#ifdef CONFIG_ARCH_RANDOM
1664+
{
1665+
.desc = "Random Number Generator",
1666+
.capability = ARM64_HAS_RNG,
1667+
.type = ARM64_CPUCAP_SYSTEM_FEATURE,
1668+
.matches = has_cpuid_feature,
1669+
.sys_reg = SYS_ID_AA64ISAR0_EL1,
1670+
.field_pos = ID_AA64ISAR0_RNDR_SHIFT,
1671+
.sign = FTR_UNSIGNED,
1672+
.min_field_value = 1,
1673+
},
16611674
#endif
16621675
{},
16631676
};
@@ -1736,6 +1749,7 @@ static const struct arm64_cpu_capabilities arm64_elf_hwcaps[] = {
17361749
HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_FHM_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_ASIMDFHM),
17371750
HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_TS_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_FLAGM),
17381751
HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_TS_SHIFT, FTR_UNSIGNED, 2, CAP_HWCAP, KERNEL_HWCAP_FLAGM2),
1752+
HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_RNDR_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_RNG),
17391753
HWCAP_CAP(SYS_ID_AA64PFR0_EL1, ID_AA64PFR0_FP_SHIFT, FTR_SIGNED, 0, CAP_HWCAP, KERNEL_HWCAP_FP),
17401754
HWCAP_CAP(SYS_ID_AA64PFR0_EL1, ID_AA64PFR0_FP_SHIFT, FTR_SIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_FPHP),
17411755
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
@@ -91,6 +91,7 @@ static const char *const hwcap_str[] = {
9191
"i8mm",
9292
"bf16",
9393
"dgh",
94+
"rng",
9495
NULL
9596
};
9697

0 commit comments

Comments
 (0)