14f8d6632SMarc Zyngier /* 24f8d6632SMarc Zyngier * Copyright (C) 2012,2013 - ARM Ltd 34f8d6632SMarc Zyngier * Author: Marc Zyngier <marc.zyngier@arm.com> 44f8d6632SMarc Zyngier * 54f8d6632SMarc Zyngier * Derived from arch/arm/include/asm/kvm_host.h: 64f8d6632SMarc Zyngier * Copyright (C) 2012 - Virtual Open Systems and Columbia University 74f8d6632SMarc Zyngier * Author: Christoffer Dall <c.dall@virtualopensystems.com> 84f8d6632SMarc Zyngier * 94f8d6632SMarc Zyngier * This program is free software; you can redistribute it and/or modify 104f8d6632SMarc Zyngier * it under the terms of the GNU General Public License version 2 as 114f8d6632SMarc Zyngier * published by the Free Software Foundation. 124f8d6632SMarc Zyngier * 134f8d6632SMarc Zyngier * This program is distributed in the hope that it will be useful, 144f8d6632SMarc Zyngier * but WITHOUT ANY WARRANTY; without even the implied warranty of 154f8d6632SMarc Zyngier * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 164f8d6632SMarc Zyngier * GNU General Public License for more details. 174f8d6632SMarc Zyngier * 184f8d6632SMarc Zyngier * You should have received a copy of the GNU General Public License 194f8d6632SMarc Zyngier * along with this program. If not, see <http://www.gnu.org/licenses/>. 204f8d6632SMarc Zyngier */ 214f8d6632SMarc Zyngier 224f8d6632SMarc Zyngier #ifndef __ARM64_KVM_HOST_H__ 234f8d6632SMarc Zyngier #define __ARM64_KVM_HOST_H__ 244f8d6632SMarc Zyngier 2565647300SPaolo Bonzini #include <linux/types.h> 2665647300SPaolo Bonzini #include <linux/kvm_types.h> 274f8d6632SMarc Zyngier #include <asm/kvm.h> 284f8d6632SMarc Zyngier #include <asm/kvm_asm.h> 294f8d6632SMarc Zyngier #include <asm/kvm_mmio.h> 304f8d6632SMarc Zyngier 31da781470SAnup Patel #if defined(CONFIG_KVM_ARM_MAX_VCPUS) 32da781470SAnup Patel #define KVM_MAX_VCPUS CONFIG_KVM_ARM_MAX_VCPUS 33da781470SAnup Patel #else 34da781470SAnup Patel #define KVM_MAX_VCPUS 0 35da781470SAnup Patel #endif 36da781470SAnup Patel 374f8d6632SMarc Zyngier #define KVM_USER_MEM_SLOTS 32 384f8d6632SMarc Zyngier #define KVM_PRIVATE_MEM_SLOTS 4 394f8d6632SMarc Zyngier #define KVM_COALESCED_MMIO_PAGE_OFFSET 1 404f8d6632SMarc Zyngier 414f8d6632SMarc Zyngier #include <kvm/arm_vgic.h> 424f8d6632SMarc Zyngier #include <kvm/arm_arch_timer.h> 434f8d6632SMarc Zyngier 447d0f84aaSAnup Patel #define KVM_VCPU_MAX_FEATURES 3 454f8d6632SMarc Zyngier 464f8d6632SMarc Zyngier int kvm_target_cpu(void); 474f8d6632SMarc Zyngier int kvm_reset_vcpu(struct kvm_vcpu *vcpu); 484f8d6632SMarc Zyngier int kvm_arch_dev_ioctl_check_extension(long ext); 494f8d6632SMarc Zyngier 504f8d6632SMarc Zyngier struct kvm_arch { 514f8d6632SMarc Zyngier /* The VMID generation used for the virt. memory system */ 524f8d6632SMarc Zyngier u64 vmid_gen; 534f8d6632SMarc Zyngier u32 vmid; 544f8d6632SMarc Zyngier 554f8d6632SMarc Zyngier /* 1-level 2nd stage table and lock */ 564f8d6632SMarc Zyngier spinlock_t pgd_lock; 574f8d6632SMarc Zyngier pgd_t *pgd; 584f8d6632SMarc Zyngier 594f8d6632SMarc Zyngier /* VTTBR value associated with above pgd and vmid */ 604f8d6632SMarc Zyngier u64 vttbr; 614f8d6632SMarc Zyngier 624f8d6632SMarc Zyngier /* Interrupt controller */ 634f8d6632SMarc Zyngier struct vgic_dist vgic; 644f8d6632SMarc Zyngier 654f8d6632SMarc Zyngier /* Timer */ 664f8d6632SMarc Zyngier struct arch_timer_kvm timer; 674f8d6632SMarc Zyngier }; 684f8d6632SMarc Zyngier 694f8d6632SMarc Zyngier #define KVM_NR_MEM_OBJS 40 704f8d6632SMarc Zyngier 714f8d6632SMarc Zyngier /* 724f8d6632SMarc Zyngier * We don't want allocation failures within the mmu code, so we preallocate 734f8d6632SMarc Zyngier * enough memory for a single page fault in a cache. 744f8d6632SMarc Zyngier */ 754f8d6632SMarc Zyngier struct kvm_mmu_memory_cache { 764f8d6632SMarc Zyngier int nobjs; 774f8d6632SMarc Zyngier void *objects[KVM_NR_MEM_OBJS]; 784f8d6632SMarc Zyngier }; 794f8d6632SMarc Zyngier 804f8d6632SMarc Zyngier struct kvm_vcpu_fault_info { 814f8d6632SMarc Zyngier u32 esr_el2; /* Hyp Syndrom Register */ 824f8d6632SMarc Zyngier u64 far_el2; /* Hyp Fault Address Register */ 834f8d6632SMarc Zyngier u64 hpfar_el2; /* Hyp IPA Fault Address Register */ 844f8d6632SMarc Zyngier }; 854f8d6632SMarc Zyngier 864f8d6632SMarc Zyngier struct kvm_cpu_context { 874f8d6632SMarc Zyngier struct kvm_regs gp_regs; 8840033a61SMarc Zyngier union { 894f8d6632SMarc Zyngier u64 sys_regs[NR_SYS_REGS]; 9072564016SMarc Zyngier u32 copro[NR_COPRO_REGS]; 9140033a61SMarc Zyngier }; 924f8d6632SMarc Zyngier }; 934f8d6632SMarc Zyngier 944f8d6632SMarc Zyngier typedef struct kvm_cpu_context kvm_cpu_context_t; 954f8d6632SMarc Zyngier 964f8d6632SMarc Zyngier struct kvm_vcpu_arch { 974f8d6632SMarc Zyngier struct kvm_cpu_context ctxt; 984f8d6632SMarc Zyngier 994f8d6632SMarc Zyngier /* HYP configuration */ 1004f8d6632SMarc Zyngier u64 hcr_el2; 1014f8d6632SMarc Zyngier 1024f8d6632SMarc Zyngier /* Exception Information */ 1034f8d6632SMarc Zyngier struct kvm_vcpu_fault_info fault; 1044f8d6632SMarc Zyngier 1050c557ed4SMarc Zyngier /* Debug state */ 1060c557ed4SMarc Zyngier u64 debug_flags; 1070c557ed4SMarc Zyngier 1084f8d6632SMarc Zyngier /* Pointer to host CPU context */ 1094f8d6632SMarc Zyngier kvm_cpu_context_t *host_cpu_context; 1104f8d6632SMarc Zyngier 1114f8d6632SMarc Zyngier /* VGIC state */ 1124f8d6632SMarc Zyngier struct vgic_cpu vgic_cpu; 1134f8d6632SMarc Zyngier struct arch_timer_cpu timer_cpu; 1144f8d6632SMarc Zyngier 1154f8d6632SMarc Zyngier /* 1164f8d6632SMarc Zyngier * Anything that is not used directly from assembly code goes 1174f8d6632SMarc Zyngier * here. 1184f8d6632SMarc Zyngier */ 1194f8d6632SMarc Zyngier /* dcache set/way operation pending */ 1204f8d6632SMarc Zyngier int last_pcpu; 1214f8d6632SMarc Zyngier cpumask_t require_dcache_flush; 1224f8d6632SMarc Zyngier 1234f8d6632SMarc Zyngier /* Don't run the guest */ 1244f8d6632SMarc Zyngier bool pause; 1254f8d6632SMarc Zyngier 1264f8d6632SMarc Zyngier /* IO related fields */ 1274f8d6632SMarc Zyngier struct kvm_decode mmio_decode; 1284f8d6632SMarc Zyngier 1294f8d6632SMarc Zyngier /* Interrupt related fields */ 1304f8d6632SMarc Zyngier u64 irq_lines; /* IRQ and FIQ levels */ 1314f8d6632SMarc Zyngier 1324f8d6632SMarc Zyngier /* Cache some mmu pages needed inside spinlock regions */ 1334f8d6632SMarc Zyngier struct kvm_mmu_memory_cache mmu_page_cache; 1344f8d6632SMarc Zyngier 1354f8d6632SMarc Zyngier /* Target CPU and feature flags */ 1366c8c0c4dSChen Gang int target; 1374f8d6632SMarc Zyngier DECLARE_BITMAP(features, KVM_VCPU_MAX_FEATURES); 1384f8d6632SMarc Zyngier 1394f8d6632SMarc Zyngier /* Detect first run of a vcpu */ 1404f8d6632SMarc Zyngier bool has_run_once; 1414f8d6632SMarc Zyngier }; 1424f8d6632SMarc Zyngier 1434f8d6632SMarc Zyngier #define vcpu_gp_regs(v) (&(v)->arch.ctxt.gp_regs) 1444f8d6632SMarc Zyngier #define vcpu_sys_reg(v,r) ((v)->arch.ctxt.sys_regs[(r)]) 14572564016SMarc Zyngier /* 14672564016SMarc Zyngier * CP14 and CP15 live in the same array, as they are backed by the 14772564016SMarc Zyngier * same system registers. 14872564016SMarc Zyngier */ 14972564016SMarc Zyngier #define vcpu_cp14(v,r) ((v)->arch.ctxt.copro[(r)]) 15072564016SMarc Zyngier #define vcpu_cp15(v,r) ((v)->arch.ctxt.copro[(r)]) 1514f8d6632SMarc Zyngier 152f0a3eaffSVictor Kamensky #ifdef CONFIG_CPU_BIG_ENDIAN 153dedf97e8SMarc Zyngier #define vcpu_cp15_64_high(v,r) vcpu_cp15((v),(r)) 154dedf97e8SMarc Zyngier #define vcpu_cp15_64_low(v,r) vcpu_cp15((v),(r) + 1) 155f0a3eaffSVictor Kamensky #else 156dedf97e8SMarc Zyngier #define vcpu_cp15_64_high(v,r) vcpu_cp15((v),(r) + 1) 157dedf97e8SMarc Zyngier #define vcpu_cp15_64_low(v,r) vcpu_cp15((v),(r)) 158f0a3eaffSVictor Kamensky #endif 159f0a3eaffSVictor Kamensky 1604f8d6632SMarc Zyngier struct kvm_vm_stat { 1614f8d6632SMarc Zyngier u32 remote_tlb_flush; 1624f8d6632SMarc Zyngier }; 1634f8d6632SMarc Zyngier 1644f8d6632SMarc Zyngier struct kvm_vcpu_stat { 1654f8d6632SMarc Zyngier u32 halt_wakeup; 1664f8d6632SMarc Zyngier }; 1674f8d6632SMarc Zyngier 1684f8d6632SMarc Zyngier int kvm_vcpu_set_target(struct kvm_vcpu *vcpu, 1694f8d6632SMarc Zyngier const struct kvm_vcpu_init *init); 170473bdc0eSAnup Patel int kvm_vcpu_preferred_target(struct kvm_vcpu_init *init); 1714f8d6632SMarc Zyngier unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu); 1724f8d6632SMarc Zyngier int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *indices); 1734f8d6632SMarc Zyngier int kvm_arm_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg); 1744f8d6632SMarc Zyngier int kvm_arm_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg); 1754f8d6632SMarc Zyngier 1764f8d6632SMarc Zyngier #define KVM_ARCH_WANT_MMU_NOTIFIER 1774f8d6632SMarc Zyngier int kvm_unmap_hva(struct kvm *kvm, unsigned long hva); 1784f8d6632SMarc Zyngier int kvm_unmap_hva_range(struct kvm *kvm, 1794f8d6632SMarc Zyngier unsigned long start, unsigned long end); 1804f8d6632SMarc Zyngier void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte); 1814f8d6632SMarc Zyngier 1824f8d6632SMarc Zyngier /* We do not have shadow page tables, hence the empty hooks */ 18357128468SAndres Lagar-Cavilla static inline int kvm_age_hva(struct kvm *kvm, unsigned long start, 18457128468SAndres Lagar-Cavilla unsigned long end) 1854f8d6632SMarc Zyngier { 1864f8d6632SMarc Zyngier return 0; 1874f8d6632SMarc Zyngier } 1884f8d6632SMarc Zyngier 1894f8d6632SMarc Zyngier static inline int kvm_test_age_hva(struct kvm *kvm, unsigned long hva) 1904f8d6632SMarc Zyngier { 1914f8d6632SMarc Zyngier return 0; 1924f8d6632SMarc Zyngier } 1934f8d6632SMarc Zyngier 1944f8d6632SMarc Zyngier struct kvm_vcpu *kvm_arm_get_running_vcpu(void); 1954f8d6632SMarc Zyngier struct kvm_vcpu __percpu **kvm_get_running_vcpus(void); 1964f8d6632SMarc Zyngier 1974f8d6632SMarc Zyngier u64 kvm_call_hyp(void *hypfn, ...); 1984f8d6632SMarc Zyngier 1994f8d6632SMarc Zyngier int handle_exit(struct kvm_vcpu *vcpu, struct kvm_run *run, 2004f8d6632SMarc Zyngier int exception_index); 2014f8d6632SMarc Zyngier 2024f8d6632SMarc Zyngier int kvm_perf_init(void); 2034f8d6632SMarc Zyngier int kvm_perf_teardown(void); 2044f8d6632SMarc Zyngier 205092bd143SMarc Zyngier static inline void __cpu_init_hyp_mode(phys_addr_t boot_pgd_ptr, 206092bd143SMarc Zyngier phys_addr_t pgd_ptr, 207092bd143SMarc Zyngier unsigned long hyp_stack_ptr, 208092bd143SMarc Zyngier unsigned long vector_ptr) 209092bd143SMarc Zyngier { 210092bd143SMarc Zyngier /* 211092bd143SMarc Zyngier * Call initialization code, and switch to the full blown 212092bd143SMarc Zyngier * HYP code. 213092bd143SMarc Zyngier */ 214092bd143SMarc Zyngier kvm_call_hyp((void *)boot_pgd_ptr, pgd_ptr, 215092bd143SMarc Zyngier hyp_stack_ptr, vector_ptr); 216092bd143SMarc Zyngier } 217092bd143SMarc Zyngier 2181a9b1305SMarc Zyngier struct vgic_sr_vectors { 2191a9b1305SMarc Zyngier void *save_vgic; 2201a9b1305SMarc Zyngier void *restore_vgic; 2211a9b1305SMarc Zyngier }; 2221a9b1305SMarc Zyngier 2231a9b1305SMarc Zyngier static inline void vgic_arch_setup(const struct vgic_params *vgic) 2241a9b1305SMarc Zyngier { 2251a9b1305SMarc Zyngier extern struct vgic_sr_vectors __vgic_sr_vectors; 2261a9b1305SMarc Zyngier 2271a9b1305SMarc Zyngier switch(vgic->type) 2281a9b1305SMarc Zyngier { 2291a9b1305SMarc Zyngier case VGIC_V2: 2301a9b1305SMarc Zyngier __vgic_sr_vectors.save_vgic = __save_vgic_v2_state; 2311a9b1305SMarc Zyngier __vgic_sr_vectors.restore_vgic = __restore_vgic_v2_state; 2321a9b1305SMarc Zyngier break; 2331a9b1305SMarc Zyngier 23467b2abfeSMarc Zyngier #ifdef CONFIG_ARM_GIC_V3 23567b2abfeSMarc Zyngier case VGIC_V3: 23667b2abfeSMarc Zyngier __vgic_sr_vectors.save_vgic = __save_vgic_v3_state; 23767b2abfeSMarc Zyngier __vgic_sr_vectors.restore_vgic = __restore_vgic_v3_state; 23867b2abfeSMarc Zyngier break; 23967b2abfeSMarc Zyngier #endif 24067b2abfeSMarc Zyngier 2411a9b1305SMarc Zyngier default: 2421a9b1305SMarc Zyngier BUG(); 2431a9b1305SMarc Zyngier } 2441a9b1305SMarc Zyngier } 2451a9b1305SMarc Zyngier 24613a34e06SRadim Krčmář static inline void kvm_arch_hardware_disable(void) {} 2470865e636SRadim Krčmář static inline void kvm_arch_hardware_unsetup(void) {} 2480865e636SRadim Krčmář static inline void kvm_arch_sync_events(struct kvm *kvm) {} 2490865e636SRadim Krčmář static inline void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu) {} 2500865e636SRadim Krčmář static inline void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu) {} 2510865e636SRadim Krčmář 2524f8d6632SMarc Zyngier #endif /* __ARM64_KVM_HOST_H__ */ 253