xref: /linux/arch/arm64/include/asm/kvm_hyp.h (revision 20bed0d6ff62d985fe8222958825504b8aad4e88)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (C) 2015 - ARM Ltd
4  * Author: Marc Zyngier <marc.zyngier@arm.com>
5  */
6 
7 #ifndef __ARM64_KVM_HYP_H__
8 #define __ARM64_KVM_HYP_H__
9 
10 #include <linux/compiler.h>
11 #include <linux/kvm_host.h>
12 #include <asm/alternative.h>
13 #include <asm/spectre.h>
14 #include <asm/sysreg.h>
15 
16 DECLARE_PER_CPU(struct kvm_cpu_context, kvm_hyp_ctxt);
17 DECLARE_PER_CPU(unsigned long, kvm_hyp_vector);
18 DECLARE_PER_CPU(struct kvm_nvhe_init_params, kvm_init_params);
19 
20 /*
21  * Unified accessors for registers that have a different encoding
22  * between VHE and non-VHE. They must be specified without their "ELx"
23  * encoding, but with the SYS_ prefix, as defined in asm/sysreg.h.
24  */
25 
26 #if defined(__KVM_VHE_HYPERVISOR__)
27 
28 #define read_sysreg_el0(r)	read_sysreg_s(r##_EL02)
29 #define write_sysreg_el0(v,r)	write_sysreg_s(v, r##_EL02)
30 #define read_sysreg_el1(r)	read_sysreg_s(r##_EL12)
31 #define write_sysreg_el1(v,r)	write_sysreg_s(v, r##_EL12)
32 #define read_sysreg_el2(r)	read_sysreg_s(r##_EL1)
33 #define write_sysreg_el2(v,r)	write_sysreg_s(v, r##_EL1)
34 
35 #else // !__KVM_VHE_HYPERVISOR__
36 
37 #if defined(__KVM_NVHE_HYPERVISOR__)
38 #define VHE_ALT_KEY	ARM64_KVM_HVHE
39 #else
40 #define VHE_ALT_KEY	ARM64_HAS_VIRT_HOST_EXTN
41 #endif
42 
43 #define read_sysreg_elx(r,nvh,vh)					\
44 	({								\
45 		u64 reg;						\
46 		asm volatile(ALTERNATIVE(__mrs_s("%0", r##nvh),		\
47 					 __mrs_s("%0", r##vh),		\
48 					 VHE_ALT_KEY)			\
49 			     : "=r" (reg));				\
50 		reg;							\
51 	})
52 
53 #define write_sysreg_elx(v,r,nvh,vh)					\
54 	do {								\
55 		u64 __val = (u64)(v);					\
56 		asm volatile(ALTERNATIVE(__msr_s(r##nvh, "%x0"),	\
57 					 __msr_s(r##vh, "%x0"),		\
58 					 VHE_ALT_KEY)			\
59 					 : : "rZ" (__val));		\
60 	} while (0)
61 
62 #define read_sysreg_el0(r)	read_sysreg_elx(r, _EL0, _EL02)
63 #define write_sysreg_el0(v,r)	write_sysreg_elx(v, r, _EL0, _EL02)
64 #define read_sysreg_el1(r)	read_sysreg_elx(r, _EL1, _EL12)
65 #define write_sysreg_el1(v,r)	write_sysreg_elx(v, r, _EL1, _EL12)
66 #define read_sysreg_el2(r)	read_sysreg_elx(r, _EL2, _EL1)
67 #define write_sysreg_el2(v,r)	write_sysreg_elx(v, r, _EL2, _EL1)
68 
69 #endif	// __KVM_VHE_HYPERVISOR__
70 
71 /*
72  * Without an __arch_swab32(), we fall back to ___constant_swab32(), but the
73  * static inline can allow the compiler to out-of-line this. KVM always wants
74  * the macro version as it's always inlined.
75  */
76 #define __kvm_swab32(x)	___constant_swab32(x)
77 
78 int __vgic_v2_perform_cpuif_access(struct kvm_vcpu *vcpu);
79 
80 u64 __gic_v3_get_lr(unsigned int lr);
81 void __gic_v3_set_lr(u64 val, int lr);
82 
83 void __vgic_v3_save_state(struct vgic_v3_cpu_if *cpu_if);
84 void __vgic_v3_restore_state(struct vgic_v3_cpu_if *cpu_if);
85 void __vgic_v3_activate_traps(struct vgic_v3_cpu_if *cpu_if);
86 void __vgic_v3_deactivate_traps(struct vgic_v3_cpu_if *cpu_if);
87 void __vgic_v3_save_aprs(struct vgic_v3_cpu_if *cpu_if);
88 void __vgic_v3_restore_vmcr_aprs(struct vgic_v3_cpu_if *cpu_if);
89 int __vgic_v3_perform_cpuif_access(struct kvm_vcpu *vcpu);
90 
91 /* GICv5 */
92 void __vgic_v5_make_resident(struct vgic_v5_cpu_if *cpu_if);
93 void __vgic_v5_make_non_resident(struct vgic_v5_cpu_if *cpu_if);
94 void __vgic_v5_save_apr(struct vgic_v5_cpu_if *cpu_if);
95 void __vgic_v5_restore_vmcr_apr(struct vgic_v5_cpu_if *cpu_if);
96 /* No hypercalls for the following */
97 void __vgic_v5_save_ppi_state(struct vgic_v5_cpu_if *cpu_if);
98 void __vgic_v5_restore_ppi_state(struct vgic_v5_cpu_if *cpu_if);
99 void __vgic_v5_save_state(struct vgic_v5_cpu_if *cpu_if);
100 void __vgic_v5_restore_state(struct vgic_v5_cpu_if *cpu_if);
101 void __vgic_v5_vdpend(u32 intid, bool pending, u16 vm);
102 
103 #ifdef __KVM_NVHE_HYPERVISOR__
104 void __timer_enable_traps(struct kvm_vcpu *vcpu);
105 void __timer_disable_traps(struct kvm_vcpu *vcpu);
106 #endif
107 
108 #ifdef __KVM_NVHE_HYPERVISOR__
109 void __sysreg_save_state_nvhe(struct kvm_cpu_context *ctxt);
110 void __sysreg_restore_state_nvhe(struct kvm_cpu_context *ctxt);
111 #else
112 void __vcpu_load_switch_sysregs(struct kvm_vcpu *vcpu);
113 void __vcpu_put_switch_sysregs(struct kvm_vcpu *vcpu);
114 void sysreg_save_host_state_vhe(struct kvm_cpu_context *ctxt);
115 void sysreg_restore_host_state_vhe(struct kvm_cpu_context *ctxt);
116 void sysreg_save_guest_state_vhe(struct kvm_cpu_context *ctxt);
117 void sysreg_restore_guest_state_vhe(struct kvm_cpu_context *ctxt);
118 #endif
119 
120 void __debug_switch_to_guest(struct kvm_vcpu *vcpu);
121 void __debug_switch_to_host(struct kvm_vcpu *vcpu);
122 
123 #ifdef __KVM_NVHE_HYPERVISOR__
124 void __debug_save_host_buffers_nvhe(struct kvm_vcpu *vcpu);
125 void __debug_restore_host_buffers_nvhe(struct kvm_vcpu *vcpu);
126 #endif
127 
128 u64 __guest_enter(struct kvm_vcpu *vcpu);
129 
130 bool kvm_host_psci_handler(struct kvm_cpu_context *host_ctxt, u32 func_id);
131 
132 #ifdef __KVM_NVHE_HYPERVISOR__
133 void __noreturn __hyp_do_panic(struct kvm_cpu_context *host_ctxt, u64 spsr,
134 			       u64 elr, u64 par);
135 #endif
136 
137 #ifdef __KVM_NVHE_HYPERVISOR__
138 void __pkvm_init_switch_pgd(phys_addr_t pgd, unsigned long sp,
139 		void (*fn)(void));
140 int __pkvm_init(phys_addr_t phys, unsigned long size, unsigned long *per_cpu_base, u32 hyp_va_bits);
141 void __noreturn __host_enter(struct kvm_cpu_context *host_ctxt);
142 #endif
143 
144 extern u64 kvm_nvhe_sym(id_aa64pfr0_el1_sys_val);
145 extern u64 kvm_nvhe_sym(id_aa64pfr1_el1_sys_val);
146 extern u64 kvm_nvhe_sym(id_aa64pfr2_el1_sys_val);
147 extern u64 kvm_nvhe_sym(id_aa64isar0_el1_sys_val);
148 extern u64 kvm_nvhe_sym(id_aa64isar1_el1_sys_val);
149 extern u64 kvm_nvhe_sym(id_aa64isar2_el1_sys_val);
150 extern u64 kvm_nvhe_sym(id_aa64mmfr0_el1_sys_val);
151 extern u64 kvm_nvhe_sym(id_aa64mmfr1_el1_sys_val);
152 extern u64 kvm_nvhe_sym(id_aa64mmfr2_el1_sys_val);
153 extern u64 kvm_nvhe_sym(id_aa64smfr0_el1_sys_val);
154 
155 extern unsigned long kvm_nvhe_sym(__icache_flags);
156 extern unsigned int kvm_nvhe_sym(kvm_arm_vmid_bits);
157 extern enum mitigation_state kvm_nvhe_sym(spectre_v2_state);
158 extern enum mitigation_state kvm_nvhe_sym(spectre_v4_state);
159 extern enum mitigation_state kvm_nvhe_sym(spectre_bhb_state);
160 extern unsigned int kvm_nvhe_sym(kvm_host_sve_max_vl);
161 extern unsigned long kvm_nvhe_sym(hyp_nr_cpus);
162 extern unsigned int kvm_nvhe_sym(hyp_gicv3_nr_lr);
163 
164 #endif /* __ARM64_KVM_HYP_H__ */
165