Skip to content

Commit b877e98

Browse files
David BrazdilMarc Zyngier
authored andcommitted
KVM: arm64: Build hyp-entry.S separately for VHE/nVHE
hyp-entry.S contains implementation of KVM hyp vectors. This code is mostly shared between VHE/nVHE, therefore compile it under both VHE and nVHE build rules. nVHE-specific host HVC handler is hidden behind __KVM_NVHE_HYPERVISOR__. Adjust code which selects which KVM hyp vecs to install to choose the correct VHE/nVHE symbol. Signed-off-by: David Brazdil <[email protected]> Signed-off-by: Marc Zyngier <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent f50b6f6 commit b877e98

File tree

8 files changed

+48
-17
lines changed

8 files changed

+48
-17
lines changed

arch/arm64/include/asm/kvm_asm.h

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,17 @@
6060
DECLARE_KVM_VHE_SYM(sym); \
6161
DECLARE_KVM_NVHE_SYM(sym)
6262

63-
/* Translate a kernel address of @sym into its equivalent linear mapping */
64-
#define kvm_ksym_ref(sym) \
63+
#define CHOOSE_VHE_SYM(sym) sym
64+
#define CHOOSE_NVHE_SYM(sym) kvm_nvhe_sym(sym)
65+
#define CHOOSE_HYP_SYM(sym) (has_vhe() ? CHOOSE_VHE_SYM(sym) \
66+
: CHOOSE_NVHE_SYM(sym))
67+
68+
/* Translate a kernel address @ptr into its equivalent linear mapping */
69+
#define kvm_ksym_ref(ptr) \
6570
({ \
66-
void *val = &sym; \
71+
void *val = (ptr); \
6772
if (!is_kernel_in_hyp_mode()) \
68-
val = lm_alias(&sym); \
73+
val = lm_alias((ptr)); \
6974
val; \
7075
})
7176
#define kvm_ksym_ref_nvhe(sym) kvm_ksym_ref(kvm_nvhe_sym(sym))
@@ -76,7 +81,14 @@ struct kvm_vcpu;
7681
extern char __kvm_hyp_init[];
7782
extern char __kvm_hyp_init_end[];
7883

79-
extern char __kvm_hyp_vector[];
84+
DECLARE_KVM_HYP_SYM(__kvm_hyp_vector);
85+
#define __kvm_hyp_vector CHOOSE_HYP_SYM(__kvm_hyp_vector)
86+
87+
#ifdef CONFIG_KVM_INDIRECT_VECTORS
88+
extern atomic_t arm64_el2_vector_last_slot;
89+
DECLARE_KVM_HYP_SYM(__bp_harden_hyp_vecs);
90+
#define __bp_harden_hyp_vecs CHOOSE_HYP_SYM(__bp_harden_hyp_vecs)
91+
#endif
8092

8193
extern void __kvm_flush_vm_context(void);
8294
extern void __kvm_tlb_flush_vmid_ipa(struct kvm *kvm, phys_addr_t ipa);

arch/arm64/include/asm/mmu.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,6 @@ struct bp_hardening_data {
4545
bp_hardening_cb_t fn;
4646
};
4747

48-
#if (defined(CONFIG_HARDEN_BRANCH_PREDICTOR) || \
49-
defined(CONFIG_HARDEN_EL2_VECTORS))
50-
51-
extern char __bp_harden_hyp_vecs[];
52-
extern atomic_t arm64_el2_vector_last_slot;
53-
#endif /* CONFIG_HARDEN_BRANCH_PREDICTOR || CONFIG_HARDEN_EL2_VECTORS */
54-
5548
#ifdef CONFIG_HARDEN_BRANCH_PREDICTOR
5649
DECLARE_PER_CPU_READ_MOSTLY(struct bp_hardening_data, bp_hardening_data);
5750

arch/arm64/kernel/image-vars.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,17 @@ __efistub__ctype = _ctype;
6666
/* Symbols defined in debug-sr.c (not yet compiled with nVHE build rules). */
6767
KVM_NVHE_ALIAS(__kvm_get_mdcr_el2);
6868

69+
/* Symbols defined in entry.S (not yet compiled with nVHE build rules). */
70+
KVM_NVHE_ALIAS(__guest_exit);
71+
KVM_NVHE_ALIAS(abort_guest_exit_end);
72+
KVM_NVHE_ALIAS(abort_guest_exit_start);
73+
74+
/* Symbols defined in hyp-init.S (not yet compiled with nVHE build rules). */
75+
KVM_NVHE_ALIAS(__kvm_handle_stub_hvc);
76+
6977
/* Symbols defined in switch.c (not yet compiled with nVHE build rules). */
7078
KVM_NVHE_ALIAS(__kvm_vcpu_run_nvhe);
79+
KVM_NVHE_ALIAS(hyp_panic);
7180

7281
/* Symbols defined in sysreg-sr.c (not yet compiled with nVHE build rules). */
7382
KVM_NVHE_ALIAS(__kvm_enable_ssbs);
@@ -89,6 +98,21 @@ KVM_NVHE_ALIAS(__vgic_v3_restore_aprs);
8998
KVM_NVHE_ALIAS(__vgic_v3_save_aprs);
9099
KVM_NVHE_ALIAS(__vgic_v3_write_vmcr);
91100

101+
/* Alternative callbacks for init-time patching of nVHE hyp code. */
102+
KVM_NVHE_ALIAS(arm64_enable_wa2_handling);
103+
KVM_NVHE_ALIAS(kvm_patch_vector_branch);
104+
KVM_NVHE_ALIAS(kvm_update_va_mask);
105+
106+
/* Global kernel state accessed by nVHE hyp code. */
107+
KVM_NVHE_ALIAS(arm64_ssbd_callback_required);
108+
KVM_NVHE_ALIAS(kvm_host_data);
109+
110+
/* Kernel constant needed to compute idmap addresses. */
111+
KVM_NVHE_ALIAS(kimage_voffset);
112+
113+
/* Kernel symbols used to call panic() from nVHE hyp code (via ERET). */
114+
KVM_NVHE_ALIAS(panic);
115+
92116
#endif /* CONFIG_KVM */
93117

94118
#endif /* __ARM64_KERNEL_IMAGE_VARS_H */

arch/arm64/kvm/arm.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1285,7 +1285,7 @@ static void cpu_init_hyp_mode(void)
12851285
* so that we can use adr_l to access per-cpu variables in EL2.
12861286
*/
12871287
tpidr_el2 = ((unsigned long)this_cpu_ptr(&kvm_host_data) -
1288-
(unsigned long)kvm_ksym_ref(kvm_host_data));
1288+
(unsigned long)kvm_ksym_ref(&kvm_host_data));
12891289

12901290
pgd_ptr = kvm_mmu_get_httbr();
12911291
hyp_stack_ptr = __this_cpu_read(kvm_arm_hyp_stack_page) + PAGE_SIZE;

arch/arm64/kvm/hyp/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ subdir-ccflags-y := -I$(incdir) \
1010
-DDISABLE_BRANCH_PROFILING \
1111
$(DISABLE_STACKLEAK_PLUGIN)
1212

13-
obj-$(CONFIG_KVM) += hyp.o nvhe/
13+
obj-$(CONFIG_KVM) += hyp.o vhe/ nvhe/
1414
obj-$(CONFIG_KVM_INDIRECT_VECTORS) += smccc_wa.o
1515

1616
hyp-y := vgic-v3-sr.o timer-sr.o aarch32.o vgic-v2-cpuif-proxy.o sysreg-sr.o \
17-
debug-sr.o entry.o switch.o fpsimd.o tlb.o hyp-entry.o
17+
debug-sr.o entry.o switch.o fpsimd.o tlb.o
1818

1919
# KVM code is run at a different exception code with a different map, so
2020
# compiler instrumentation that inserts callbacks or checks into the code may

arch/arm64/kvm/hyp/hyp-entry.S

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ el1_sync: // Guest trapped into EL2
4040
ccmp x0, #ESR_ELx_EC_HVC32, #4, ne
4141
b.ne el1_trap
4242

43+
#ifdef __KVM_NVHE_HYPERVISOR__
4344
mrs x1, vttbr_el2 // If vttbr is valid, the guest
4445
cbnz x1, el1_hvc_guest // called HVC
4546

@@ -74,6 +75,7 @@ el1_sync: // Guest trapped into EL2
7475

7576
eret
7677
sb
78+
#endif /* __KVM_NVHE_HYPERVISOR__ */
7779

7880
el1_hvc_guest:
7981
/*

arch/arm64/kvm/hyp/nvhe/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
asflags-y := -D__KVM_NVHE_HYPERVISOR__
77
ccflags-y := -D__KVM_NVHE_HYPERVISOR__
88

9-
obj-y :=
9+
obj-y := ../hyp-entry.o
1010

1111
obj-y := $(patsubst %.o,%.hyp.o,$(obj-y))
1212
extra-y := $(patsubst %.hyp.o,%.hyp.tmp.o,$(obj-y))

arch/arm64/kvm/hyp/vhe/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
asflags-y := -D__KVM_VHE_HYPERVISOR__
77
ccflags-y := -D__KVM_VHE_HYPERVISOR__
88

9-
obj-y :=
9+
obj-y := ../hyp-entry.o
1010

1111
# KVM code is run at a different exception code with a different map, so
1212
# compiler instrumentation that inserts callbacks or checks into the code may

0 commit comments

Comments
 (0)