18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2022 ARM Ltd.
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci#ifndef __ASM_VECTORS_H
68c2ecf20Sopenharmony_ci#define __ASM_VECTORS_H
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/bug.h>
98c2ecf20Sopenharmony_ci#include <linux/percpu.h>
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <asm/fixmap.h>
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ciextern char vectors[];
148c2ecf20Sopenharmony_ciextern char tramp_vectors[];
158c2ecf20Sopenharmony_ciextern char __bp_harden_el1_vectors[];
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci/*
188c2ecf20Sopenharmony_ci * Note: the order of this enum corresponds to two arrays in entry.S:
198c2ecf20Sopenharmony_ci * tramp_vecs and __bp_harden_el1_vectors. By default the canonical
208c2ecf20Sopenharmony_ci * 'full fat' vectors are used directly.
218c2ecf20Sopenharmony_ci */
228c2ecf20Sopenharmony_cienum arm64_bp_harden_el1_vectors {
238c2ecf20Sopenharmony_ci#ifdef CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY
248c2ecf20Sopenharmony_ci	/*
258c2ecf20Sopenharmony_ci	 * Perform the BHB loop mitigation, before branching to the canonical
268c2ecf20Sopenharmony_ci	 * vectors.
278c2ecf20Sopenharmony_ci	 */
288c2ecf20Sopenharmony_ci	EL1_VECTOR_BHB_LOOP,
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci	/*
318c2ecf20Sopenharmony_ci	 * Make the SMC call for firmware mitigation, before branching to the
328c2ecf20Sopenharmony_ci	 * canonical vectors.
338c2ecf20Sopenharmony_ci	 */
348c2ecf20Sopenharmony_ci	EL1_VECTOR_BHB_FW,
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci	/*
378c2ecf20Sopenharmony_ci	 * Use the ClearBHB instruction, before branching to the canonical
388c2ecf20Sopenharmony_ci	 * vectors.
398c2ecf20Sopenharmony_ci	 */
408c2ecf20Sopenharmony_ci	EL1_VECTOR_BHB_CLEAR_INSN,
418c2ecf20Sopenharmony_ci#endif /* CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY */
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci	/*
448c2ecf20Sopenharmony_ci	 * Remap the kernel before branching to the canonical vectors.
458c2ecf20Sopenharmony_ci	 */
468c2ecf20Sopenharmony_ci	EL1_VECTOR_KPTI,
478c2ecf20Sopenharmony_ci};
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci#ifndef CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY
508c2ecf20Sopenharmony_ci#define EL1_VECTOR_BHB_LOOP		-1
518c2ecf20Sopenharmony_ci#define EL1_VECTOR_BHB_FW		-1
528c2ecf20Sopenharmony_ci#define EL1_VECTOR_BHB_CLEAR_INSN	-1
538c2ecf20Sopenharmony_ci#endif /* !CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY */
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci/* The vectors to use on return from EL0. e.g. to remap the kernel */
568c2ecf20Sopenharmony_ciDECLARE_PER_CPU_READ_MOSTLY(const char *, this_cpu_vector);
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci#ifndef CONFIG_UNMAP_KERNEL_AT_EL0
598c2ecf20Sopenharmony_ci#define TRAMP_VALIAS	0ul
608c2ecf20Sopenharmony_ci#endif
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_cistatic inline const char *
638c2ecf20Sopenharmony_ciarm64_get_bp_hardening_vector(enum arm64_bp_harden_el1_vectors slot)
648c2ecf20Sopenharmony_ci{
658c2ecf20Sopenharmony_ci	if (arm64_kernel_unmapped_at_el0())
668c2ecf20Sopenharmony_ci		return (char *)(TRAMP_VALIAS + SZ_2K * slot);
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	WARN_ON_ONCE(slot == EL1_VECTOR_KPTI);
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	return __bp_harden_el1_vectors + SZ_2K * slot;
718c2ecf20Sopenharmony_ci}
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci#endif /* __ASM_VECTORS_H */
74