1caab277bSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */ 213720a56SMarc Zyngier /* 313720a56SMarc Zyngier * Copyright (C) 2015 - ARM Ltd 413720a56SMarc Zyngier * Author: Marc Zyngier <marc.zyngier@arm.com> 513720a56SMarc Zyngier */ 613720a56SMarc Zyngier 713720a56SMarc Zyngier #ifndef __ARM64_KVM_HYP_H__ 813720a56SMarc Zyngier #define __ARM64_KVM_HYP_H__ 913720a56SMarc Zyngier 1013720a56SMarc Zyngier #include <linux/compiler.h> 1113720a56SMarc Zyngier #include <linux/kvm_host.h> 121e4448c5SMarc Zyngier #include <asm/alternative.h> 1313720a56SMarc Zyngier #include <asm/sysreg.h> 1413720a56SMarc Zyngier 15b619d9aaSAndrew Scull DECLARE_PER_CPU(struct kvm_cpu_context, kvm_hyp_ctxt); 16a0e47952SAndrew Scull DECLARE_PER_CPU(unsigned long, kvm_hyp_vector); 1763fec243SDavid Brazdil DECLARE_PER_CPU(struct kvm_nvhe_init_params, kvm_init_params); 18a0e47952SAndrew Scull 1913720a56SMarc Zyngier #define read_sysreg_elx(r,nvh,vh) \ 2013720a56SMarc Zyngier ({ \ 2113720a56SMarc Zyngier u64 reg; \ 22fdec2a9eSDave Martin asm volatile(ALTERNATIVE(__mrs_s("%0", r##nvh), \ 23be604c61SKees Cook __mrs_s("%0", r##vh), \ 2413720a56SMarc Zyngier ARM64_HAS_VIRT_HOST_EXTN) \ 2513720a56SMarc Zyngier : "=r" (reg)); \ 2613720a56SMarc Zyngier reg; \ 2713720a56SMarc Zyngier }) 2813720a56SMarc Zyngier 2913720a56SMarc Zyngier #define write_sysreg_elx(v,r,nvh,vh) \ 3013720a56SMarc Zyngier do { \ 3113720a56SMarc Zyngier u64 __val = (u64)(v); \ 32fdec2a9eSDave Martin asm volatile(ALTERNATIVE(__msr_s(r##nvh, "%x0"), \ 33be604c61SKees Cook __msr_s(r##vh, "%x0"), \ 3413720a56SMarc Zyngier ARM64_HAS_VIRT_HOST_EXTN) \ 3513720a56SMarc Zyngier : : "rZ" (__val)); \ 3613720a56SMarc Zyngier } while (0) 3713720a56SMarc Zyngier 3813720a56SMarc Zyngier /* 3913720a56SMarc Zyngier * Unified accessors for registers that have a different encoding 4013720a56SMarc Zyngier * between VHE and non-VHE. They must be specified without their "ELx" 41fdec2a9eSDave Martin * encoding, but with the SYS_ prefix, as defined in asm/sysreg.h. 4213720a56SMarc Zyngier */ 4313720a56SMarc Zyngier 4413720a56SMarc Zyngier #define read_sysreg_el0(r) read_sysreg_elx(r, _EL0, _EL02) 4513720a56SMarc Zyngier #define write_sysreg_el0(v,r) write_sysreg_elx(v, r, _EL0, _EL02) 4613720a56SMarc Zyngier #define read_sysreg_el1(r) read_sysreg_elx(r, _EL1, _EL12) 4713720a56SMarc Zyngier #define write_sysreg_el1(v,r) write_sysreg_elx(v, r, _EL1, _EL12) 48fdec2a9eSDave Martin #define read_sysreg_el2(r) read_sysreg_elx(r, _EL2, _EL1) 49fdec2a9eSDave Martin #define write_sysreg_el2(v,r) write_sysreg_elx(v, r, _EL2, _EL1) 5013720a56SMarc Zyngier 518c2d146eSJames Morse /* 528c2d146eSJames Morse * Without an __arch_swab32(), we fall back to ___constant_swab32(), but the 538c2d146eSJames Morse * static inline can allow the compiler to out-of-line this. KVM always wants 548c2d146eSJames Morse * the macro version as its always inlined. 558c2d146eSJames Morse */ 568c2d146eSJames Morse #define __kvm_swab32(x) ___constant_swab32(x) 578c2d146eSJames Morse 583272f0d0SMarc Zyngier int __vgic_v2_perform_cpuif_access(struct kvm_vcpu *vcpu); 5913720a56SMarc Zyngier 60fc5d1f1aSChristoffer Dall void __vgic_v3_save_state(struct vgic_v3_cpu_if *cpu_if); 61fc5d1f1aSChristoffer Dall void __vgic_v3_restore_state(struct vgic_v3_cpu_if *cpu_if); 62fc5d1f1aSChristoffer Dall void __vgic_v3_activate_traps(struct vgic_v3_cpu_if *cpu_if); 63fc5d1f1aSChristoffer Dall void __vgic_v3_deactivate_traps(struct vgic_v3_cpu_if *cpu_if); 64fc5d1f1aSChristoffer Dall void __vgic_v3_save_aprs(struct vgic_v3_cpu_if *cpu_if); 65fc5d1f1aSChristoffer Dall void __vgic_v3_restore_aprs(struct vgic_v3_cpu_if *cpu_if); 6659da1cbfSMarc Zyngier int __vgic_v3_perform_cpuif_access(struct kvm_vcpu *vcpu); 6713720a56SMarc Zyngier 689aebdea4SDavid Brazdil #ifdef __KVM_NVHE_HYPERVISOR__ 69688c50aaSChristoffer Dall void __timer_enable_traps(struct kvm_vcpu *vcpu); 70688c50aaSChristoffer Dall void __timer_disable_traps(struct kvm_vcpu *vcpu); 719aebdea4SDavid Brazdil #endif 7213720a56SMarc Zyngier 7313aeb9b4SDavid Brazdil #ifdef __KVM_NVHE_HYPERVISOR__ 744cdecabaSChristoffer Dall void __sysreg_save_state_nvhe(struct kvm_cpu_context *ctxt); 754cdecabaSChristoffer Dall void __sysreg_restore_state_nvhe(struct kvm_cpu_context *ctxt); 7613aeb9b4SDavid Brazdil #else 77f837453dSChristoffer Dall void sysreg_save_host_state_vhe(struct kvm_cpu_context *ctxt); 78f837453dSChristoffer Dall void sysreg_restore_host_state_vhe(struct kvm_cpu_context *ctxt); 79f837453dSChristoffer Dall void sysreg_save_guest_state_vhe(struct kvm_cpu_context *ctxt); 80f837453dSChristoffer Dall void sysreg_restore_guest_state_vhe(struct kvm_cpu_context *ctxt); 8113aeb9b4SDavid Brazdil #endif 8213720a56SMarc Zyngier 83014c4c77SChristoffer Dall void __debug_switch_to_guest(struct kvm_vcpu *vcpu); 84014c4c77SChristoffer Dall void __debug_switch_to_host(struct kvm_vcpu *vcpu); 8513720a56SMarc Zyngier 86b96b0c5dSSuzuki K Poulose #ifdef __KVM_NVHE_HYPERVISOR__ 87b96b0c5dSSuzuki K Poulose void __debug_save_host_buffers_nvhe(struct kvm_vcpu *vcpu); 88b96b0c5dSSuzuki K Poulose void __debug_restore_host_buffers_nvhe(struct kvm_vcpu *vcpu); 89b96b0c5dSSuzuki K Poulose #endif 90b96b0c5dSSuzuki K Poulose 9113720a56SMarc Zyngier void __fpsimd_save_state(struct user_fpsimd_state *fp_regs); 9213720a56SMarc Zyngier void __fpsimd_restore_state(struct user_fpsimd_state *fp_regs); 9352029198SMarc Zyngier void __sve_restore_state(void *sve_pffr, u32 *fpsr); 9413720a56SMarc Zyngier 9509cf57ebSDavid Brazdil #ifndef __KVM_NVHE_HYPERVISOR__ 96a2465629SChristoffer Dall void activate_traps_vhe_load(struct kvm_vcpu *vcpu); 971460b4b2SFuad Tabba void deactivate_traps_vhe_put(struct kvm_vcpu *vcpu); 9809cf57ebSDavid Brazdil #endif 99a2465629SChristoffer Dall 100b619d9aaSAndrew Scull u64 __guest_enter(struct kvm_vcpu *vcpu); 10109cf57ebSDavid Brazdil 102eeeee719SDavid Brazdil bool kvm_host_psci_handler(struct kvm_cpu_context *host_ctxt); 103eeeee719SDavid Brazdil 10409cf57ebSDavid Brazdil #ifdef __KVM_NVHE_HYPERVISOR__ 105c4b000c3SAndrew Scull void __noreturn __hyp_do_panic(struct kvm_cpu_context *host_ctxt, u64 spsr, 106c4b000c3SAndrew Scull u64 elr, u64 par); 10709cf57ebSDavid Brazdil #endif 10813720a56SMarc Zyngier 109f320bc74SQuentin Perret #ifdef __KVM_NVHE_HYPERVISOR__ 110f320bc74SQuentin Perret void __pkvm_init_switch_pgd(phys_addr_t phys, unsigned long size, 111f320bc74SQuentin Perret phys_addr_t pgd, void *sp, void *cont_fn); 112f320bc74SQuentin Perret int __pkvm_init(phys_addr_t phys, unsigned long size, unsigned long nr_cpus, 113f320bc74SQuentin Perret unsigned long *per_cpu_base, u32 hyp_va_bits); 114f320bc74SQuentin Perret void __noreturn __host_enter(struct kvm_cpu_context *host_ctxt); 115f320bc74SQuentin Perret #endif 116f320bc74SQuentin Perret 1176c30bfb1SFuad Tabba extern u64 kvm_nvhe_sym(id_aa64pfr0_el1_sys_val); 1186c30bfb1SFuad Tabba extern u64 kvm_nvhe_sym(id_aa64pfr1_el1_sys_val); 1196c30bfb1SFuad Tabba extern u64 kvm_nvhe_sym(id_aa64isar0_el1_sys_val); 1206c30bfb1SFuad Tabba extern u64 kvm_nvhe_sym(id_aa64isar1_el1_sys_val); 121def8c222SVladimir Murzin extern u64 kvm_nvhe_sym(id_aa64isar2_el1_sys_val); 1227c419937SMarc Zyngier extern u64 kvm_nvhe_sym(id_aa64mmfr0_el1_sys_val); 1237c419937SMarc Zyngier extern u64 kvm_nvhe_sym(id_aa64mmfr1_el1_sys_val); 1246c30bfb1SFuad Tabba extern u64 kvm_nvhe_sym(id_aa64mmfr2_el1_sys_val); 1257c419937SMarc Zyngier 12613e248aaSWill Deacon extern unsigned long kvm_nvhe_sym(__icache_flags); 127*73f38ef2SWill Deacon extern unsigned int kvm_nvhe_sym(kvm_arm_vmid_bits); 128*73f38ef2SWill Deacon 12913720a56SMarc Zyngier #endif /* __ARM64_KVM_HYP_H__ */ 130