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/sysreg.h> 14 15 DECLARE_PER_CPU(struct kvm_cpu_context, kvm_hyp_ctxt); 16 DECLARE_PER_CPU(unsigned long, kvm_hyp_vector); 17 DECLARE_PER_CPU(struct kvm_nvhe_init_params, kvm_init_params); 18 DECLARE_PER_CPU(int, hyp_cpu_number); 19 20 #define hyp_smp_processor_id() (__this_cpu_read(hyp_cpu_number)) 21 22 /* 23 * Unified accessors for registers that have a different encoding 24 * between VHE and non-VHE. They must be specified without their "ELx" 25 * encoding, but with the SYS_ prefix, as defined in asm/sysreg.h. 26 */ 27 28 #if defined(__KVM_VHE_HYPERVISOR__) 29 30 #define read_sysreg_el0(r) read_sysreg_s(r##_EL02) 31 #define write_sysreg_el0(v,r) write_sysreg_s(v, r##_EL02) 32 #define read_sysreg_el1(r) read_sysreg_s(r##_EL12) 33 #define write_sysreg_el1(v,r) write_sysreg_s(v, r##_EL12) 34 #define read_sysreg_el2(r) read_sysreg_s(r##_EL1) 35 #define write_sysreg_el2(v,r) write_sysreg_s(v, r##_EL1) 36 37 #else // !__KVM_VHE_HYPERVISOR__ 38 39 #if defined(__KVM_NVHE_HYPERVISOR__) 40 #define VHE_ALT_KEY ARM64_KVM_HVHE 41 #else 42 #define VHE_ALT_KEY ARM64_HAS_VIRT_HOST_EXTN 43 #endif 44 45 #define read_sysreg_elx(r,nvh,vh) \ 46 ({ \ 47 u64 reg; \ 48 asm volatile(ALTERNATIVE(__mrs_s("%0", r##nvh), \ 49 __mrs_s("%0", r##vh), \ 50 VHE_ALT_KEY) \ 51 : "=r" (reg)); \ 52 reg; \ 53 }) 54 55 #define write_sysreg_elx(v,r,nvh,vh) \ 56 do { \ 57 u64 __val = (u64)(v); \ 58 asm volatile(ALTERNATIVE(__msr_s(r##nvh, "%x0"), \ 59 __msr_s(r##vh, "%x0"), \ 60 VHE_ALT_KEY) \ 61 : : "rZ" (__val)); \ 62 } while (0) 63 64 #define read_sysreg_el0(r) read_sysreg_elx(r, _EL0, _EL02) 65 #define write_sysreg_el0(v,r) write_sysreg_elx(v, r, _EL0, _EL02) 66 #define read_sysreg_el1(r) read_sysreg_elx(r, _EL1, _EL12) 67 #define write_sysreg_el1(v,r) write_sysreg_elx(v, r, _EL1, _EL12) 68 #define read_sysreg_el2(r) read_sysreg_elx(r, _EL2, _EL1) 69 #define write_sysreg_el2(v,r) write_sysreg_elx(v, r, _EL2, _EL1) 70 71 #endif // __KVM_VHE_HYPERVISOR__ 72 73 /* 74 * Without an __arch_swab32(), we fall back to ___constant_swab32(), but the 75 * static inline can allow the compiler to out-of-line this. KVM always wants 76 * the macro version as its always inlined. 77 */ 78 #define __kvm_swab32(x) ___constant_swab32(x) 79 80 int __vgic_v2_perform_cpuif_access(struct kvm_vcpu *vcpu); 81 82 void __vgic_v3_save_state(struct vgic_v3_cpu_if *cpu_if); 83 void __vgic_v3_restore_state(struct vgic_v3_cpu_if *cpu_if); 84 void __vgic_v3_activate_traps(struct vgic_v3_cpu_if *cpu_if); 85 void __vgic_v3_deactivate_traps(struct vgic_v3_cpu_if *cpu_if); 86 void __vgic_v3_save_vmcr_aprs(struct vgic_v3_cpu_if *cpu_if); 87 void __vgic_v3_restore_vmcr_aprs(struct vgic_v3_cpu_if *cpu_if); 88 int __vgic_v3_perform_cpuif_access(struct kvm_vcpu *vcpu); 89 90 #ifdef __KVM_NVHE_HYPERVISOR__ 91 void __timer_enable_traps(struct kvm_vcpu *vcpu); 92 void __timer_disable_traps(struct kvm_vcpu *vcpu); 93 #endif 94 95 #ifdef __KVM_NVHE_HYPERVISOR__ 96 void __sysreg_save_state_nvhe(struct kvm_cpu_context *ctxt); 97 void __sysreg_restore_state_nvhe(struct kvm_cpu_context *ctxt); 98 #else 99 void sysreg_save_host_state_vhe(struct kvm_cpu_context *ctxt); 100 void sysreg_restore_host_state_vhe(struct kvm_cpu_context *ctxt); 101 void sysreg_save_guest_state_vhe(struct kvm_cpu_context *ctxt); 102 void sysreg_restore_guest_state_vhe(struct kvm_cpu_context *ctxt); 103 #endif 104 105 void __debug_switch_to_guest(struct kvm_vcpu *vcpu); 106 void __debug_switch_to_host(struct kvm_vcpu *vcpu); 107 108 #ifdef __KVM_NVHE_HYPERVISOR__ 109 void __debug_save_host_buffers_nvhe(struct kvm_vcpu *vcpu); 110 void __debug_restore_host_buffers_nvhe(struct kvm_vcpu *vcpu); 111 #endif 112 113 void __fpsimd_save_state(struct user_fpsimd_state *fp_regs); 114 void __fpsimd_restore_state(struct user_fpsimd_state *fp_regs); 115 void __sve_save_state(void *sve_pffr, u32 *fpsr); 116 void __sve_restore_state(void *sve_pffr, u32 *fpsr); 117 118 #ifndef __KVM_NVHE_HYPERVISOR__ 119 void activate_traps_vhe_load(struct kvm_vcpu *vcpu); 120 void deactivate_traps_vhe_put(struct kvm_vcpu *vcpu); 121 #endif 122 123 u64 __guest_enter(struct kvm_vcpu *vcpu); 124 125 bool kvm_host_psci_handler(struct kvm_cpu_context *host_ctxt, u32 func_id); 126 127 #ifdef __KVM_NVHE_HYPERVISOR__ 128 void __noreturn __hyp_do_panic(struct kvm_cpu_context *host_ctxt, u64 spsr, 129 u64 elr, u64 par); 130 #endif 131 132 #ifdef __KVM_NVHE_HYPERVISOR__ 133 void __pkvm_init_switch_pgd(phys_addr_t pgd, unsigned long sp, 134 void (*fn)(void)); 135 int __pkvm_init(phys_addr_t phys, unsigned long size, unsigned long nr_cpus, 136 unsigned long *per_cpu_base, u32 hyp_va_bits); 137 void __noreturn __host_enter(struct kvm_cpu_context *host_ctxt); 138 void __hyp_enter(void); 139 void __hyp_exit(void); 140 #endif 141 142 #ifdef __KVM_NVHE_HYPERVISOR__ 143 struct user_fpsimd_state *get_host_fpsimd_state(struct kvm_vcpu *vcpu); 144 struct kvm_host_sve_state *get_host_sve_state(struct kvm_vcpu *vcpu); 145 #endif 146 147 extern u64 kvm_nvhe_sym(id_aa64pfr0_el1_sys_val); 148 extern u64 kvm_nvhe_sym(id_aa64pfr1_el1_sys_val); 149 extern u64 kvm_nvhe_sym(id_aa64zfr0_el1_sys_val); 150 extern u64 kvm_nvhe_sym(id_aa64isar0_el1_sys_val); 151 extern u64 kvm_nvhe_sym(id_aa64isar1_el1_sys_val); 152 extern u64 kvm_nvhe_sym(id_aa64isar2_el1_sys_val); 153 extern u64 kvm_nvhe_sym(id_aa64mmfr0_el1_sys_val); 154 extern u64 kvm_nvhe_sym(id_aa64mmfr1_el1_sys_val); 155 extern u64 kvm_nvhe_sym(id_aa64mmfr2_el1_sys_val); 156 extern u64 kvm_nvhe_sym(id_aa64smfr0_el1_sys_val); 157 158 extern unsigned long kvm_nvhe_sym(__icache_flags); 159 extern unsigned int kvm_nvhe_sym(kvm_arm_vmid_bits); 160 extern bool kvm_nvhe_sym(smccc_trng_available); 161 extern unsigned int kvm_nvhe_sym(kvm_sve_max_vl); 162 extern unsigned int kvm_nvhe_sym(kvm_host_sve_max_vl); 163 164 struct kvm_nvhe_clock_data { 165 u32 mult; 166 u32 shift; 167 u64 epoch_ns; 168 u64 epoch_cyc; 169 }; 170 171 #endif /* __ARM64_KVM_HYP_H__ */ 172