Skip to content

Commit aa6acde

Browse files
wildea01ctmarinas
authored andcommitted
arm64: Implement branch predictor hardening for affected Cortex-A CPUs
Cortex-A57, A72, A73 and A75 are susceptible to branch predictor aliasing and can theoretically be attacked by malicious code. This patch implements a PSCI-based mitigation for these CPUs when available. The call into firmware will invalidate the branch predictor state, preventing any malicious entries from affecting other victim contexts. Co-developed-by: Marc Zyngier <[email protected]> Signed-off-by: Will Deacon <[email protected]> Signed-off-by: Catalin Marinas <[email protected]>
1 parent a65d219 commit aa6acde

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

arch/arm64/kernel/bpi.S

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,27 @@ ENTRY(__bp_harden_hyp_vecs_start)
5353
vectors __kvm_hyp_vector
5454
.endr
5555
ENTRY(__bp_harden_hyp_vecs_end)
56+
ENTRY(__psci_hyp_bp_inval_start)
57+
sub sp, sp, #(8 * 18)
58+
stp x16, x17, [sp, #(16 * 0)]
59+
stp x14, x15, [sp, #(16 * 1)]
60+
stp x12, x13, [sp, #(16 * 2)]
61+
stp x10, x11, [sp, #(16 * 3)]
62+
stp x8, x9, [sp, #(16 * 4)]
63+
stp x6, x7, [sp, #(16 * 5)]
64+
stp x4, x5, [sp, #(16 * 6)]
65+
stp x2, x3, [sp, #(16 * 7)]
66+
stp x0, x1, [sp, #(16 * 8)]
67+
mov x0, #0x84000000
68+
smc #0
69+
ldp x16, x17, [sp, #(16 * 0)]
70+
ldp x14, x15, [sp, #(16 * 1)]
71+
ldp x12, x13, [sp, #(16 * 2)]
72+
ldp x10, x11, [sp, #(16 * 3)]
73+
ldp x8, x9, [sp, #(16 * 4)]
74+
ldp x6, x7, [sp, #(16 * 5)]
75+
ldp x4, x5, [sp, #(16 * 6)]
76+
ldp x2, x3, [sp, #(16 * 7)]
77+
ldp x0, x1, [sp, #(16 * 8)]
78+
add sp, sp, #(8 * 18)
79+
ENTRY(__psci_hyp_bp_inval_end)

arch/arm64/kernel/cpu_errata.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ static int cpu_enable_trap_ctr_access(void *__unused)
5353
DEFINE_PER_CPU_READ_MOSTLY(struct bp_hardening_data, bp_hardening_data);
5454

5555
#ifdef CONFIG_KVM
56+
extern char __psci_hyp_bp_inval_start[], __psci_hyp_bp_inval_end[];
57+
5658
static void __copy_hyp_vect_bpi(int slot, const char *hyp_vecs_start,
5759
const char *hyp_vecs_end)
5860
{
@@ -94,6 +96,9 @@ static void __install_bp_hardening_cb(bp_hardening_cb_t fn,
9496
spin_unlock(&bp_lock);
9597
}
9698
#else
99+
#define __psci_hyp_bp_inval_start NULL
100+
#define __psci_hyp_bp_inval_end NULL
101+
97102
static void __install_bp_hardening_cb(bp_hardening_cb_t fn,
98103
const char *hyp_vecs_start,
99104
const char *hyp_vecs_end)
@@ -118,6 +123,21 @@ static void install_bp_hardening_cb(const struct arm64_cpu_capabilities *entry,
118123

119124
__install_bp_hardening_cb(fn, hyp_vecs_start, hyp_vecs_end);
120125
}
126+
127+
#include <linux/psci.h>
128+
129+
static int enable_psci_bp_hardening(void *data)
130+
{
131+
const struct arm64_cpu_capabilities *entry = data;
132+
133+
if (psci_ops.get_version)
134+
install_bp_hardening_cb(entry,
135+
(bp_hardening_cb_t)psci_ops.get_version,
136+
__psci_hyp_bp_inval_start,
137+
__psci_hyp_bp_inval_end);
138+
139+
return 0;
140+
}
121141
#endif /* CONFIG_HARDEN_BRANCH_PREDICTOR */
122142

123143
#define MIDR_RANGE(model, min, max) \
@@ -260,6 +280,28 @@ const struct arm64_cpu_capabilities arm64_errata[] = {
260280
.capability = ARM64_WORKAROUND_858921,
261281
MIDR_ALL_VERSIONS(MIDR_CORTEX_A73),
262282
},
283+
#endif
284+
#ifdef CONFIG_HARDEN_BRANCH_PREDICTOR
285+
{
286+
.capability = ARM64_HARDEN_BRANCH_PREDICTOR,
287+
MIDR_ALL_VERSIONS(MIDR_CORTEX_A57),
288+
.enable = enable_psci_bp_hardening,
289+
},
290+
{
291+
.capability = ARM64_HARDEN_BRANCH_PREDICTOR,
292+
MIDR_ALL_VERSIONS(MIDR_CORTEX_A72),
293+
.enable = enable_psci_bp_hardening,
294+
},
295+
{
296+
.capability = ARM64_HARDEN_BRANCH_PREDICTOR,
297+
MIDR_ALL_VERSIONS(MIDR_CORTEX_A73),
298+
.enable = enable_psci_bp_hardening,
299+
},
300+
{
301+
.capability = ARM64_HARDEN_BRANCH_PREDICTOR,
302+
MIDR_ALL_VERSIONS(MIDR_CORTEX_A75),
303+
.enable = enable_psci_bp_hardening,
304+
},
263305
#endif
264306
{
265307
}

0 commit comments

Comments
 (0)