Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge tag 'kvmarm-for-v5.1' of git://git.kernel.org/pub/scm/linux/kernel/git/kvmarm/kvmarm into kvm-next

KVM/arm updates for Linux v5.1

- A number of pre-nested code rework
- Direct physical timer assignment on VHE systems
- kvm_call_hyp type safety enforcement
- Set/Way cache sanitisation for 32bit guests
- Build system cleanups
- A bunch of janitorial fixes

+915 -390
+7 -11
MAINTAINERS
··· 8304 8304 F: arch/x86/include/asm/svm.h 8305 8305 F: arch/x86/kvm/svm.c 8306 8306 8307 - KERNEL VIRTUAL MACHINE FOR ARM (KVM/arm) 8307 + KERNEL VIRTUAL MACHINE FOR ARM/ARM64 (KVM/arm, KVM/arm64) 8308 8308 M: Christoffer Dall <christoffer.dall@arm.com> 8309 8309 M: Marc Zyngier <marc.zyngier@arm.com> 8310 + R: James Morse <james.morse@arm.com> 8311 + R: Julien Thierry <julien.thierry@arm.com> 8312 + R: Suzuki K Pouloze <suzuki.poulose@arm.com> 8310 8313 L: linux-arm-kernel@lists.infradead.org (moderated for non-subscribers) 8311 8314 L: kvmarm@lists.cs.columbia.edu 8312 8315 W: http://systems.cs.columbia.edu/projects/kvm-arm 8313 8316 T: git git://git.kernel.org/pub/scm/linux/kernel/git/kvmarm/kvmarm.git 8314 - S: Supported 8317 + S: Maintained 8315 8318 F: arch/arm/include/uapi/asm/kvm* 8316 8319 F: arch/arm/include/asm/kvm* 8317 8320 F: arch/arm/kvm/ 8318 - F: virt/kvm/arm/ 8319 - F: include/kvm/arm_* 8320 - 8321 - KERNEL VIRTUAL MACHINE FOR ARM64 (KVM/arm64) 8322 - M: Christoffer Dall <christoffer.dall@arm.com> 8323 - M: Marc Zyngier <marc.zyngier@arm.com> 8324 - L: linux-arm-kernel@lists.infradead.org (moderated for non-subscribers) 8325 - L: kvmarm@lists.cs.columbia.edu 8326 - S: Maintained 8327 8321 F: arch/arm64/include/uapi/asm/kvm* 8328 8322 F: arch/arm64/include/asm/kvm* 8329 8323 F: arch/arm64/kvm/ 8324 + F: virt/kvm/arm/ 8325 + F: include/kvm/arm_* 8330 8326 8331 8327 KERNEL VIRTUAL MACHINE FOR MIPS (KVM/mips) 8332 8328 M: James Hogan <jhogan@kernel.org>
+2 -2
arch/arm/include/asm/arch_gicv3.h
··· 54 54 #define ICH_VTR __ACCESS_CP15(c12, 4, c11, 1) 55 55 #define ICH_MISR __ACCESS_CP15(c12, 4, c11, 2) 56 56 #define ICH_EISR __ACCESS_CP15(c12, 4, c11, 3) 57 - #define ICH_ELSR __ACCESS_CP15(c12, 4, c11, 5) 57 + #define ICH_ELRSR __ACCESS_CP15(c12, 4, c11, 5) 58 58 #define ICH_VMCR __ACCESS_CP15(c12, 4, c11, 7) 59 59 60 60 #define __LR0(x) __ACCESS_CP15(c12, 4, c12, x) ··· 151 151 CPUIF_MAP(ICH_VTR, ICH_VTR_EL2) 152 152 CPUIF_MAP(ICH_MISR, ICH_MISR_EL2) 153 153 CPUIF_MAP(ICH_EISR, ICH_EISR_EL2) 154 - CPUIF_MAP(ICH_ELSR, ICH_ELSR_EL2) 154 + CPUIF_MAP(ICH_ELRSR, ICH_ELRSR_EL2) 155 155 CPUIF_MAP(ICH_VMCR, ICH_VMCR_EL2) 156 156 CPUIF_MAP(ICH_AP0R3, ICH_AP0R3_EL2) 157 157 CPUIF_MAP(ICH_AP0R2, ICH_AP0R2_EL2)
+8
arch/arm/include/asm/kvm_emulate.h
··· 265 265 } 266 266 } 267 267 268 + static inline bool kvm_is_write_fault(struct kvm_vcpu *vcpu) 269 + { 270 + if (kvm_vcpu_trap_is_iabt(vcpu)) 271 + return false; 272 + 273 + return kvm_vcpu_dabt_iswrite(vcpu); 274 + } 275 + 268 276 static inline u32 kvm_vcpu_hvc_get_imm(struct kvm_vcpu *vcpu) 269 277 { 270 278 return kvm_vcpu_get_hsr(vcpu) & HSR_HVC_IMM_MASK;
+46 -7
arch/arm/include/asm/kvm_host.h
··· 26 26 #include <asm/kvm_asm.h> 27 27 #include <asm/kvm_mmio.h> 28 28 #include <asm/fpstate.h> 29 + #include <asm/smp_plat.h> 29 30 #include <kvm/arm_arch_timer.h> 30 31 31 32 #define __KVM_HAVE_ARCH_INTC_INITIALIZED ··· 57 56 int kvm_reset_vcpu(struct kvm_vcpu *vcpu); 58 57 void kvm_reset_coprocs(struct kvm_vcpu *vcpu); 59 58 60 - struct kvm_arch { 61 - /* VTTBR value associated with below pgd and vmid */ 62 - u64 vttbr; 59 + struct kvm_vmid { 60 + /* The VMID generation used for the virt. memory system */ 61 + u64 vmid_gen; 62 + u32 vmid; 63 + }; 63 64 65 + struct kvm_arch { 64 66 /* The last vcpu id that ran on each physical CPU */ 65 67 int __percpu *last_vcpu_ran; 66 68 ··· 73 69 */ 74 70 75 71 /* The VMID generation used for the virt. memory system */ 76 - u64 vmid_gen; 77 - u32 vmid; 72 + struct kvm_vmid vmid; 78 73 79 74 /* Stage-2 page table */ 80 75 pgd_t *pgd; 76 + phys_addr_t pgd_phys; 81 77 82 78 /* Interrupt controller */ 83 79 struct vgic_dist vgic; ··· 151 147 152 148 typedef struct kvm_cpu_context kvm_cpu_context_t; 153 149 150 + static inline void kvm_init_host_cpu_context(kvm_cpu_context_t *cpu_ctxt, 151 + int cpu) 152 + { 153 + /* The host's MPIDR is immutable, so let's set it up at boot time */ 154 + cpu_ctxt->cp15[c0_MPIDR] = cpu_logical_map(cpu); 155 + } 156 + 154 157 struct kvm_vcpu_arch { 155 158 struct kvm_cpu_context ctxt; 156 159 ··· 225 214 int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *indices); 226 215 int kvm_arm_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg); 227 216 int kvm_arm_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg); 228 - unsigned long kvm_call_hyp(void *hypfn, ...); 217 + 218 + unsigned long __kvm_call_hyp(void *hypfn, ...); 219 + 220 + /* 221 + * The has_vhe() part doesn't get emitted, but is used for type-checking. 222 + */ 223 + #define kvm_call_hyp(f, ...) \ 224 + do { \ 225 + if (has_vhe()) { \ 226 + f(__VA_ARGS__); \ 227 + } else { \ 228 + __kvm_call_hyp(kvm_ksym_ref(f), ##__VA_ARGS__); \ 229 + } \ 230 + } while(0) 231 + 232 + #define kvm_call_hyp_ret(f, ...) \ 233 + ({ \ 234 + typeof(f(__VA_ARGS__)) ret; \ 235 + \ 236 + if (has_vhe()) { \ 237 + ret = f(__VA_ARGS__); \ 238 + } else { \ 239 + ret = __kvm_call_hyp(kvm_ksym_ref(f), \ 240 + ##__VA_ARGS__); \ 241 + } \ 242 + \ 243 + ret; \ 244 + }) 245 + 229 246 void force_vm_exit(const cpumask_t *mask); 230 247 int __kvm_arm_vcpu_get_events(struct kvm_vcpu *vcpu, 231 248 struct kvm_vcpu_events *events); ··· 304 265 * compliant with the PCS!). 305 266 */ 306 267 307 - kvm_call_hyp((void*)hyp_stack_ptr, vector_ptr, pgd_ptr); 268 + __kvm_call_hyp((void*)hyp_stack_ptr, vector_ptr, pgd_ptr); 308 269 } 309 270 310 271 static inline void __cpu_init_stage2(void)
+4
arch/arm/include/asm/kvm_hyp.h
··· 40 40 #define TTBR1 __ACCESS_CP15_64(1, c2) 41 41 #define VTTBR __ACCESS_CP15_64(6, c2) 42 42 #define PAR __ACCESS_CP15_64(0, c7) 43 + #define CNTP_CVAL __ACCESS_CP15_64(2, c14) 43 44 #define CNTV_CVAL __ACCESS_CP15_64(3, c14) 44 45 #define CNTVOFF __ACCESS_CP15_64(4, c14) 45 46 ··· 86 85 #define TID_PRIV __ACCESS_CP15(c13, 0, c0, 4) 87 86 #define HTPIDR __ACCESS_CP15(c13, 4, c0, 2) 88 87 #define CNTKCTL __ACCESS_CP15(c14, 0, c1, 0) 88 + #define CNTP_CTL __ACCESS_CP15(c14, 0, c2, 1) 89 89 #define CNTV_CTL __ACCESS_CP15(c14, 0, c3, 1) 90 90 #define CNTHCTL __ACCESS_CP15(c14, 4, c1, 0) 91 91 ··· 96 94 #define read_sysreg_el0(r) read_sysreg(r##_el0) 97 95 #define write_sysreg_el0(v, r) write_sysreg(v, r##_el0) 98 96 97 + #define cntp_ctl_el0 CNTP_CTL 98 + #define cntp_cval_el0 CNTP_CVAL 99 99 #define cntv_ctl_el0 CNTV_CTL 100 100 #define cntv_cval_el0 CNTV_CVAL 101 101 #define cntvoff_el2 CNTVOFF
+7 -2
arch/arm/include/asm/kvm_mmu.h
··· 421 421 422 422 static inline void kvm_set_ipa_limit(void) {} 423 423 424 - static inline bool kvm_cpu_has_cnp(void) 424 + static __always_inline u64 kvm_get_vttbr(struct kvm *kvm) 425 425 { 426 - return false; 426 + struct kvm_vmid *vmid = &kvm->arch.vmid; 427 + u64 vmid_field, baddr; 428 + 429 + baddr = kvm->arch.pgd_phys; 430 + vmid_field = (u64)vmid->vmid << VTTBR_VMID_SHIFT; 431 + return kvm_phys_to_vttbr(baddr) | vmid_field; 427 432 } 428 433 429 434 #endif /* !__ASSEMBLY__ */
+2 -3
arch/arm/kvm/Makefile
··· 8 8 plus_virt_def := -DREQUIRES_VIRT=1 9 9 endif 10 10 11 - ccflags-y += -Iarch/arm/kvm -Ivirt/kvm/arm/vgic 12 - CFLAGS_arm.o := -I. $(plus_virt_def) 13 - CFLAGS_mmu.o := -I. 11 + ccflags-y += -I $(srctree)/$(src) -I $(srctree)/virt/kvm/arm/vgic 12 + CFLAGS_arm.o := $(plus_virt_def) 14 13 15 14 AFLAGS_init.o := -Wa,-march=armv7-a$(plus_virt) 16 15 AFLAGS_interrupts.o := -Wa,-march=armv7-a$(plus_virt)
+14 -9
arch/arm/kvm/coproc.c
··· 293 293 const struct coproc_params *p, 294 294 const struct coproc_reg *r) 295 295 { 296 - u64 now = kvm_phys_timer_read(); 297 - u64 val; 296 + u32 val; 298 297 299 298 if (p->is_write) { 300 299 val = *vcpu_reg(vcpu, p->Rt1); 301 - kvm_arm_timer_set_reg(vcpu, KVM_REG_ARM_PTIMER_CVAL, val + now); 300 + kvm_arm_timer_write_sysreg(vcpu, 301 + TIMER_PTIMER, TIMER_REG_TVAL, val); 302 302 } else { 303 - val = kvm_arm_timer_get_reg(vcpu, KVM_REG_ARM_PTIMER_CVAL); 304 - *vcpu_reg(vcpu, p->Rt1) = val - now; 303 + val = kvm_arm_timer_read_sysreg(vcpu, 304 + TIMER_PTIMER, TIMER_REG_TVAL); 305 + *vcpu_reg(vcpu, p->Rt1) = val; 305 306 } 306 307 307 308 return true; ··· 316 315 317 316 if (p->is_write) { 318 317 val = *vcpu_reg(vcpu, p->Rt1); 319 - kvm_arm_timer_set_reg(vcpu, KVM_REG_ARM_PTIMER_CTL, val); 318 + kvm_arm_timer_write_sysreg(vcpu, 319 + TIMER_PTIMER, TIMER_REG_CTL, val); 320 320 } else { 321 - val = kvm_arm_timer_get_reg(vcpu, KVM_REG_ARM_PTIMER_CTL); 321 + val = kvm_arm_timer_read_sysreg(vcpu, 322 + TIMER_PTIMER, TIMER_REG_CTL); 322 323 *vcpu_reg(vcpu, p->Rt1) = val; 323 324 } 324 325 ··· 336 333 if (p->is_write) { 337 334 val = (u64)*vcpu_reg(vcpu, p->Rt2) << 32; 338 335 val |= *vcpu_reg(vcpu, p->Rt1); 339 - kvm_arm_timer_set_reg(vcpu, KVM_REG_ARM_PTIMER_CVAL, val); 336 + kvm_arm_timer_write_sysreg(vcpu, 337 + TIMER_PTIMER, TIMER_REG_CVAL, val); 340 338 } else { 341 - val = kvm_arm_timer_get_reg(vcpu, KVM_REG_ARM_PTIMER_CVAL); 339 + val = kvm_arm_timer_read_sysreg(vcpu, 340 + TIMER_PTIMER, TIMER_REG_CVAL); 342 341 *vcpu_reg(vcpu, p->Rt1) = val; 343 342 *vcpu_reg(vcpu, p->Rt2) = val >> 32; 344 343 }
-1
arch/arm/kvm/hyp/cp15-sr.c
··· 27 27 28 28 void __hyp_text __sysreg_save_state(struct kvm_cpu_context *ctxt) 29 29 { 30 - ctxt->cp15[c0_MPIDR] = read_sysreg(VMPIDR); 31 30 ctxt->cp15[c0_CSSELR] = read_sysreg(CSSELR); 32 31 ctxt->cp15[c1_SCTLR] = read_sysreg(SCTLR); 33 32 ctxt->cp15[c1_CPACR] = read_sysreg(CPACR);
+1 -1
arch/arm/kvm/hyp/hyp-entry.S
··· 176 176 msr spsr_cxsf, lr 177 177 ldr lr, =panic 178 178 msr ELR_hyp, lr 179 - ldr lr, =kvm_call_hyp 179 + ldr lr, =__kvm_call_hyp 180 180 clrex 181 181 eret 182 182 ENDPROC(__hyp_do_panic)
+1 -1
arch/arm/kvm/hyp/switch.c
··· 77 77 static void __hyp_text __activate_vm(struct kvm_vcpu *vcpu) 78 78 { 79 79 struct kvm *kvm = kern_hyp_va(vcpu->kvm); 80 - write_sysreg(kvm->arch.vttbr, VTTBR); 80 + write_sysreg(kvm_get_vttbr(kvm), VTTBR); 81 81 write_sysreg(vcpu->arch.midr, VPIDR); 82 82 } 83 83
+2 -2
arch/arm/kvm/hyp/tlb.c
··· 41 41 42 42 /* Switch to requested VMID */ 43 43 kvm = kern_hyp_va(kvm); 44 - write_sysreg(kvm->arch.vttbr, VTTBR); 44 + write_sysreg(kvm_get_vttbr(kvm), VTTBR); 45 45 isb(); 46 46 47 47 write_sysreg(0, TLBIALLIS); ··· 61 61 struct kvm *kvm = kern_hyp_va(kern_hyp_va(vcpu)->kvm); 62 62 63 63 /* Switch to requested VMID */ 64 - write_sysreg(kvm->arch.vttbr, VTTBR); 64 + write_sysreg(kvm_get_vttbr(kvm), VTTBR); 65 65 isb(); 66 66 67 67 write_sysreg(0, TLBIALL);
+2 -2
arch/arm/kvm/interrupts.S
··· 42 42 * r12: caller save 43 43 * rest: callee save 44 44 */ 45 - ENTRY(kvm_call_hyp) 45 + ENTRY(__kvm_call_hyp) 46 46 hvc #0 47 47 bx lr 48 - ENDPROC(kvm_call_hyp) 48 + ENDPROC(__kvm_call_hyp)
+12
arch/arm64/include/asm/kvm_emulate.h
··· 77 77 */ 78 78 if (!vcpu_el1_is_32bit(vcpu)) 79 79 vcpu->arch.hcr_el2 |= HCR_TID3; 80 + 81 + if (cpus_have_const_cap(ARM64_MISMATCHED_CACHE_TYPE) || 82 + vcpu_el1_is_32bit(vcpu)) 83 + vcpu->arch.hcr_el2 |= HCR_TID2; 80 84 } 81 85 82 86 static inline unsigned long *vcpu_hcr(struct kvm_vcpu *vcpu) ··· 333 329 { 334 330 u32 esr = kvm_vcpu_get_hsr(vcpu); 335 331 return ESR_ELx_SYS64_ISS_RT(esr); 332 + } 333 + 334 + static inline bool kvm_is_write_fault(struct kvm_vcpu *vcpu) 335 + { 336 + if (kvm_vcpu_trap_is_iabt(vcpu)) 337 + return false; 338 + 339 + return kvm_vcpu_dabt_iswrite(vcpu); 336 340 } 337 341 338 342 static inline unsigned long kvm_vcpu_get_mpidr_aff(struct kvm_vcpu *vcpu)
+44 -4
arch/arm64/include/asm/kvm_host.h
··· 30 30 #include <asm/kvm.h> 31 31 #include <asm/kvm_asm.h> 32 32 #include <asm/kvm_mmio.h> 33 + #include <asm/smp_plat.h> 33 34 #include <asm/thread_info.h> 34 35 35 36 #define __KVM_HAVE_ARCH_INTC_INITIALIZED ··· 57 56 int kvm_arch_vm_ioctl_check_extension(struct kvm *kvm, long ext); 58 57 void __extended_idmap_trampoline(phys_addr_t boot_pgd, phys_addr_t idmap_start); 59 58 60 - struct kvm_arch { 59 + struct kvm_vmid { 61 60 /* The VMID generation used for the virt. memory system */ 62 61 u64 vmid_gen; 63 62 u32 vmid; 63 + }; 64 + 65 + struct kvm_arch { 66 + struct kvm_vmid vmid; 64 67 65 68 /* stage2 entry level table */ 66 69 pgd_t *pgd; 70 + phys_addr_t pgd_phys; 67 71 68 - /* VTTBR value associated with above pgd and vmid */ 69 - u64 vttbr; 70 72 /* VTCR_EL2 value for this VM */ 71 73 u64 vtcr; 72 74 ··· 374 370 void kvm_arm_resume_guest(struct kvm *kvm); 375 371 376 372 u64 __kvm_call_hyp(void *hypfn, ...); 377 - #define kvm_call_hyp(f, ...) __kvm_call_hyp(kvm_ksym_ref(f), ##__VA_ARGS__) 373 + 374 + /* 375 + * The couple of isb() below are there to guarantee the same behaviour 376 + * on VHE as on !VHE, where the eret to EL1 acts as a context 377 + * synchronization event. 378 + */ 379 + #define kvm_call_hyp(f, ...) \ 380 + do { \ 381 + if (has_vhe()) { \ 382 + f(__VA_ARGS__); \ 383 + isb(); \ 384 + } else { \ 385 + __kvm_call_hyp(kvm_ksym_ref(f), ##__VA_ARGS__); \ 386 + } \ 387 + } while(0) 388 + 389 + #define kvm_call_hyp_ret(f, ...) \ 390 + ({ \ 391 + typeof(f(__VA_ARGS__)) ret; \ 392 + \ 393 + if (has_vhe()) { \ 394 + ret = f(__VA_ARGS__); \ 395 + isb(); \ 396 + } else { \ 397 + ret = __kvm_call_hyp(kvm_ksym_ref(f), \ 398 + ##__VA_ARGS__); \ 399 + } \ 400 + \ 401 + ret; \ 402 + }) 378 403 379 404 void force_vm_exit(const cpumask_t *mask); 380 405 void kvm_mmu_wp_memory_region(struct kvm *kvm, int slot); ··· 421 388 struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr); 422 389 423 390 DECLARE_PER_CPU(kvm_cpu_context_t, kvm_host_cpu_state); 391 + 392 + static inline void kvm_init_host_cpu_context(kvm_cpu_context_t *cpu_ctxt, 393 + int cpu) 394 + { 395 + /* The host's MPIDR is immutable, so let's set it up at boot time */ 396 + cpu_ctxt->sys_regs[MPIDR_EL1] = cpu_logical_map(cpu); 397 + } 424 398 425 399 void __kvm_enable_ssbs(void); 426 400
+2 -1
arch/arm64/include/asm/kvm_hyp.h
··· 21 21 #include <linux/compiler.h> 22 22 #include <linux/kvm_host.h> 23 23 #include <asm/alternative.h> 24 + #include <asm/kvm_mmu.h> 24 25 #include <asm/sysreg.h> 25 26 26 27 #define __hyp_text __section(.hyp.text) notrace ··· 164 163 static __always_inline void __hyp_text __load_guest_stage2(struct kvm *kvm) 165 164 { 166 165 write_sysreg(kvm->arch.vtcr, vtcr_el2); 167 - write_sysreg(kvm->arch.vttbr, vttbr_el2); 166 + write_sysreg(kvm_get_vttbr(kvm), vttbr_el2); 168 167 169 168 /* 170 169 * ARM erratum 1165522 requires the actual execution of the above
+10 -3
arch/arm64/include/asm/kvm_mmu.h
··· 138 138 }) 139 139 140 140 /* 141 - * We currently only support a 40bit IPA. 141 + * We currently support using a VM-specified IPA size. For backward 142 + * compatibility, the default IPA size is fixed to 40bits. 142 143 */ 143 144 #define KVM_PHYS_SHIFT (40) 144 145 ··· 592 591 return vttbr_baddr_mask(kvm_phys_shift(kvm), kvm_stage2_levels(kvm)); 593 592 } 594 593 595 - static inline bool kvm_cpu_has_cnp(void) 594 + static __always_inline u64 kvm_get_vttbr(struct kvm *kvm) 596 595 { 597 - return system_supports_cnp(); 596 + struct kvm_vmid *vmid = &kvm->arch.vmid; 597 + u64 vmid_field, baddr; 598 + u64 cnp = system_supports_cnp() ? VTTBR_CNP_BIT : 0; 599 + 600 + baddr = kvm->arch.pgd_phys; 601 + vmid_field = (u64)vmid->vmid << VTTBR_VMID_SHIFT; 602 + return kvm_phys_to_vttbr(baddr) | vmid_field | cnp; 598 603 } 599 604 600 605 #endif /* __ASSEMBLY__ */
+6 -1
arch/arm64/include/asm/sysreg.h
··· 361 361 362 362 #define SYS_CNTKCTL_EL1 sys_reg(3, 0, 14, 1, 0) 363 363 364 + #define SYS_CCSIDR_EL1 sys_reg(3, 1, 0, 0, 0) 364 365 #define SYS_CLIDR_EL1 sys_reg(3, 1, 0, 0, 1) 365 366 #define SYS_AIDR_EL1 sys_reg(3, 1, 0, 0, 7) 366 367 ··· 392 391 #define SYS_CNTP_TVAL_EL0 sys_reg(3, 3, 14, 2, 0) 393 392 #define SYS_CNTP_CTL_EL0 sys_reg(3, 3, 14, 2, 1) 394 393 #define SYS_CNTP_CVAL_EL0 sys_reg(3, 3, 14, 2, 2) 394 + 395 + #define SYS_AARCH32_CNTP_TVAL sys_reg(0, 0, 14, 2, 0) 396 + #define SYS_AARCH32_CNTP_CTL sys_reg(0, 0, 14, 2, 1) 397 + #define SYS_AARCH32_CNTP_CVAL sys_reg(0, 2, 0, 14, 0) 395 398 396 399 #define __PMEV_op2(n) ((n) & 0x7) 397 400 #define __CNTR_CRm(n) (0x8 | (((n) >> 3) & 0x3)) ··· 431 426 #define SYS_ICH_VTR_EL2 sys_reg(3, 4, 12, 11, 1) 432 427 #define SYS_ICH_MISR_EL2 sys_reg(3, 4, 12, 11, 2) 433 428 #define SYS_ICH_EISR_EL2 sys_reg(3, 4, 12, 11, 3) 434 - #define SYS_ICH_ELSR_EL2 sys_reg(3, 4, 12, 11, 5) 429 + #define SYS_ICH_ELRSR_EL2 sys_reg(3, 4, 12, 11, 5) 435 430 #define SYS_ICH_VMCR_EL2 sys_reg(3, 4, 12, 11, 7) 436 431 437 432 #define __SYS__LR0_EL2(x) sys_reg(3, 4, 12, 12, x)
+1 -3
arch/arm64/kvm/Makefile
··· 3 3 # Makefile for Kernel-based Virtual Machine module 4 4 # 5 5 6 - ccflags-y += -Iarch/arm64/kvm -Ivirt/kvm/arm/vgic 7 - CFLAGS_arm.o := -I. 8 - CFLAGS_mmu.o := -I. 6 + ccflags-y += -I $(srctree)/$(src) -I $(srctree)/virt/kvm/arm/vgic 9 7 10 8 KVM=../../../virt/kvm 11 9
+1 -1
arch/arm64/kvm/debug.c
··· 76 76 77 77 void kvm_arm_init_debug(void) 78 78 { 79 - __this_cpu_write(mdcr_el2, kvm_call_hyp(__kvm_get_mdcr_el2)); 79 + __this_cpu_write(mdcr_el2, kvm_call_hyp_ret(__kvm_get_mdcr_el2)); 80 80 } 81 81 82 82 /**
-3
arch/arm64/kvm/hyp.S
··· 40 40 * arch/arm64/kernel/hyp_stub.S. 41 41 */ 42 42 ENTRY(__kvm_call_hyp) 43 - alternative_if_not ARM64_HAS_VIRT_HOST_EXTN 44 43 hvc #0 45 44 ret 46 - alternative_else_nop_endif 47 - b __vhe_hyp_call 48 45 ENDPROC(__kvm_call_hyp)
-12
arch/arm64/kvm/hyp/hyp-entry.S
··· 43 43 ldr lr, [sp], #16 44 44 .endm 45 45 46 - ENTRY(__vhe_hyp_call) 47 - do_el2_call 48 - /* 49 - * We used to rely on having an exception return to get 50 - * an implicit isb. In the E2H case, we don't have it anymore. 51 - * rather than changing all the leaf functions, just do it here 52 - * before returning to the rest of the kernel. 53 - */ 54 - isb 55 - ret 56 - ENDPROC(__vhe_hyp_call) 57 - 58 46 el1_sync: // Guest trapped into EL2 59 47 60 48 mrs x0, esr_el2
-1
arch/arm64/kvm/hyp/sysreg-sr.c
··· 52 52 53 53 static void __hyp_text __sysreg_save_el1_state(struct kvm_cpu_context *ctxt) 54 54 { 55 - ctxt->sys_regs[MPIDR_EL1] = read_sysreg(vmpidr_el2); 56 55 ctxt->sys_regs[CSSELR_EL1] = read_sysreg(csselr_el1); 57 56 ctxt->sys_regs[SCTLR_EL1] = read_sysreg_el1(sctlr); 58 57 ctxt->sys_regs[ACTLR_EL1] = read_sysreg(actlr_el1);
+112 -56
arch/arm64/kvm/sys_regs.c
··· 965 965 return true; 966 966 } 967 967 968 + #define reg_to_encoding(x) \ 969 + sys_reg((u32)(x)->Op0, (u32)(x)->Op1, \ 970 + (u32)(x)->CRn, (u32)(x)->CRm, (u32)(x)->Op2); 971 + 968 972 /* Silly macro to expand the DBG{BCR,BVR,WVR,WCR}n_EL1 registers in one go */ 969 973 #define DBG_BCR_BVR_WCR_WVR_EL1(n) \ 970 974 { SYS_DESC(SYS_DBGBVRn_EL1(n)), \ ··· 990 986 { SYS_DESC(SYS_PMEVTYPERn_EL0(n)), \ 991 987 access_pmu_evtyper, reset_unknown, (PMEVTYPER0_EL0 + n), } 992 988 993 - static bool access_cntp_tval(struct kvm_vcpu *vcpu, 994 - struct sys_reg_params *p, 995 - const struct sys_reg_desc *r) 989 + static bool access_arch_timer(struct kvm_vcpu *vcpu, 990 + struct sys_reg_params *p, 991 + const struct sys_reg_desc *r) 996 992 { 997 - u64 now = kvm_phys_timer_read(); 998 - u64 cval; 993 + enum kvm_arch_timers tmr; 994 + enum kvm_arch_timer_regs treg; 995 + u64 reg = reg_to_encoding(r); 999 996 1000 - if (p->is_write) { 1001 - kvm_arm_timer_set_reg(vcpu, KVM_REG_ARM_PTIMER_CVAL, 1002 - p->regval + now); 1003 - } else { 1004 - cval = kvm_arm_timer_get_reg(vcpu, KVM_REG_ARM_PTIMER_CVAL); 1005 - p->regval = cval - now; 997 + switch (reg) { 998 + case SYS_CNTP_TVAL_EL0: 999 + case SYS_AARCH32_CNTP_TVAL: 1000 + tmr = TIMER_PTIMER; 1001 + treg = TIMER_REG_TVAL; 1002 + break; 1003 + case SYS_CNTP_CTL_EL0: 1004 + case SYS_AARCH32_CNTP_CTL: 1005 + tmr = TIMER_PTIMER; 1006 + treg = TIMER_REG_CTL; 1007 + break; 1008 + case SYS_CNTP_CVAL_EL0: 1009 + case SYS_AARCH32_CNTP_CVAL: 1010 + tmr = TIMER_PTIMER; 1011 + treg = TIMER_REG_CVAL; 1012 + break; 1013 + default: 1014 + BUG(); 1006 1015 } 1007 1016 1008 - return true; 1009 - } 1010 - 1011 - static bool access_cntp_ctl(struct kvm_vcpu *vcpu, 1012 - struct sys_reg_params *p, 1013 - const struct sys_reg_desc *r) 1014 - { 1015 1017 if (p->is_write) 1016 - kvm_arm_timer_set_reg(vcpu, KVM_REG_ARM_PTIMER_CTL, p->regval); 1018 + kvm_arm_timer_write_sysreg(vcpu, tmr, treg, p->regval); 1017 1019 else 1018 - p->regval = kvm_arm_timer_get_reg(vcpu, KVM_REG_ARM_PTIMER_CTL); 1019 - 1020 - return true; 1021 - } 1022 - 1023 - static bool access_cntp_cval(struct kvm_vcpu *vcpu, 1024 - struct sys_reg_params *p, 1025 - const struct sys_reg_desc *r) 1026 - { 1027 - if (p->is_write) 1028 - kvm_arm_timer_set_reg(vcpu, KVM_REG_ARM_PTIMER_CVAL, p->regval); 1029 - else 1030 - p->regval = kvm_arm_timer_get_reg(vcpu, KVM_REG_ARM_PTIMER_CVAL); 1020 + p->regval = kvm_arm_timer_read_sysreg(vcpu, tmr, treg); 1031 1021 1032 1022 return true; 1033 1023 } ··· 1144 1146 const struct kvm_one_reg *reg, void __user *uaddr) 1145 1147 { 1146 1148 return __set_id_reg(rd, uaddr, true); 1149 + } 1150 + 1151 + static bool access_ctr(struct kvm_vcpu *vcpu, struct sys_reg_params *p, 1152 + const struct sys_reg_desc *r) 1153 + { 1154 + if (p->is_write) 1155 + return write_to_read_only(vcpu, p, r); 1156 + 1157 + p->regval = read_sanitised_ftr_reg(SYS_CTR_EL0); 1158 + return true; 1159 + } 1160 + 1161 + static bool access_clidr(struct kvm_vcpu *vcpu, struct sys_reg_params *p, 1162 + const struct sys_reg_desc *r) 1163 + { 1164 + if (p->is_write) 1165 + return write_to_read_only(vcpu, p, r); 1166 + 1167 + p->regval = read_sysreg(clidr_el1); 1168 + return true; 1169 + } 1170 + 1171 + static bool access_csselr(struct kvm_vcpu *vcpu, struct sys_reg_params *p, 1172 + const struct sys_reg_desc *r) 1173 + { 1174 + if (p->is_write) 1175 + vcpu_write_sys_reg(vcpu, p->regval, r->reg); 1176 + else 1177 + p->regval = vcpu_read_sys_reg(vcpu, r->reg); 1178 + return true; 1179 + } 1180 + 1181 + static bool access_ccsidr(struct kvm_vcpu *vcpu, struct sys_reg_params *p, 1182 + const struct sys_reg_desc *r) 1183 + { 1184 + u32 csselr; 1185 + 1186 + if (p->is_write) 1187 + return write_to_read_only(vcpu, p, r); 1188 + 1189 + csselr = vcpu_read_sys_reg(vcpu, CSSELR_EL1); 1190 + p->regval = get_ccsidr(csselr); 1191 + 1192 + /* 1193 + * Guests should not be doing cache operations by set/way at all, and 1194 + * for this reason, we trap them and attempt to infer the intent, so 1195 + * that we can flush the entire guest's address space at the appropriate 1196 + * time. 1197 + * To prevent this trapping from causing performance problems, let's 1198 + * expose the geometry of all data and unified caches (which are 1199 + * guaranteed to be PIPT and thus non-aliasing) as 1 set and 1 way. 1200 + * [If guests should attempt to infer aliasing properties from the 1201 + * geometry (which is not permitted by the architecture), they would 1202 + * only do so for virtually indexed caches.] 1203 + */ 1204 + if (!(csselr & 1)) // data or unified cache 1205 + p->regval &= ~GENMASK(27, 3); 1206 + return true; 1147 1207 } 1148 1208 1149 1209 /* sys_reg_desc initialiser for known cpufeature ID registers */ ··· 1421 1365 1422 1366 { SYS_DESC(SYS_CNTKCTL_EL1), NULL, reset_val, CNTKCTL_EL1, 0}, 1423 1367 1424 - { SYS_DESC(SYS_CSSELR_EL1), NULL, reset_unknown, CSSELR_EL1 }, 1368 + { SYS_DESC(SYS_CCSIDR_EL1), access_ccsidr }, 1369 + { SYS_DESC(SYS_CLIDR_EL1), access_clidr }, 1370 + { SYS_DESC(SYS_CSSELR_EL1), access_csselr, reset_unknown, CSSELR_EL1 }, 1371 + { SYS_DESC(SYS_CTR_EL0), access_ctr }, 1425 1372 1426 1373 { SYS_DESC(SYS_PMCR_EL0), access_pmcr, reset_pmcr, }, 1427 1374 { SYS_DESC(SYS_PMCNTENSET_EL0), access_pmcnten, reset_unknown, PMCNTENSET_EL0 }, ··· 1447 1388 { SYS_DESC(SYS_TPIDR_EL0), NULL, reset_unknown, TPIDR_EL0 }, 1448 1389 { SYS_DESC(SYS_TPIDRRO_EL0), NULL, reset_unknown, TPIDRRO_EL0 }, 1449 1390 1450 - { SYS_DESC(SYS_CNTP_TVAL_EL0), access_cntp_tval }, 1451 - { SYS_DESC(SYS_CNTP_CTL_EL0), access_cntp_ctl }, 1452 - { SYS_DESC(SYS_CNTP_CVAL_EL0), access_cntp_cval }, 1391 + { SYS_DESC(SYS_CNTP_TVAL_EL0), access_arch_timer }, 1392 + { SYS_DESC(SYS_CNTP_CTL_EL0), access_arch_timer }, 1393 + { SYS_DESC(SYS_CNTP_CVAL_EL0), access_arch_timer }, 1453 1394 1454 1395 /* PMEVCNTRn_EL0 */ 1455 1396 PMU_PMEVCNTR_EL0(0), ··· 1523 1464 1524 1465 { SYS_DESC(SYS_DACR32_EL2), NULL, reset_unknown, DACR32_EL2 }, 1525 1466 { SYS_DESC(SYS_IFSR32_EL2), NULL, reset_unknown, IFSR32_EL2 }, 1526 - { SYS_DESC(SYS_FPEXC32_EL2), NULL, reset_val, FPEXC32_EL2, 0x70 }, 1467 + { SYS_DESC(SYS_FPEXC32_EL2), NULL, reset_val, FPEXC32_EL2, 0x700 }, 1527 1468 }; 1528 1469 1529 1470 static bool trap_dbgidr(struct kvm_vcpu *vcpu, ··· 1724 1665 * register). 1725 1666 */ 1726 1667 static const struct sys_reg_desc cp15_regs[] = { 1668 + { Op1( 0), CRn( 0), CRm( 0), Op2( 1), access_ctr }, 1727 1669 { Op1( 0), CRn( 1), CRm( 0), Op2( 0), access_vm_reg, NULL, c1_SCTLR }, 1728 1670 { Op1( 0), CRn( 2), CRm( 0), Op2( 0), access_vm_reg, NULL, c2_TTBR0 }, 1729 1671 { Op1( 0), CRn( 2), CRm( 0), Op2( 1), access_vm_reg, NULL, c2_TTBR1 }, ··· 1771 1711 1772 1712 { Op1( 0), CRn(13), CRm( 0), Op2( 1), access_vm_reg, NULL, c13_CID }, 1773 1713 1774 - /* CNTP_TVAL */ 1775 - { Op1( 0), CRn(14), CRm( 2), Op2( 0), access_cntp_tval }, 1776 - /* CNTP_CTL */ 1777 - { Op1( 0), CRn(14), CRm( 2), Op2( 1), access_cntp_ctl }, 1714 + /* Arch Tmers */ 1715 + { SYS_DESC(SYS_AARCH32_CNTP_TVAL), access_arch_timer }, 1716 + { SYS_DESC(SYS_AARCH32_CNTP_CTL), access_arch_timer }, 1778 1717 1779 1718 /* PMEVCNTRn */ 1780 1719 PMU_PMEVCNTR(0), ··· 1841 1782 PMU_PMEVTYPER(30), 1842 1783 /* PMCCFILTR */ 1843 1784 { Op1(0), CRn(14), CRm(15), Op2(7), access_pmu_evtyper }, 1785 + 1786 + { Op1(1), CRn( 0), CRm( 0), Op2(0), access_ccsidr }, 1787 + { Op1(1), CRn( 0), CRm( 0), Op2(1), access_clidr }, 1788 + { Op1(2), CRn( 0), CRm( 0), Op2(0), access_csselr, NULL, c0_CSSELR }, 1844 1789 }; 1845 1790 1846 1791 static const struct sys_reg_desc cp15_64_regs[] = { ··· 1854 1791 { Op1( 1), CRn( 0), CRm( 2), Op2( 0), access_vm_reg, NULL, c2_TTBR1 }, 1855 1792 { Op1( 1), CRn( 0), CRm(12), Op2( 0), access_gic_sgi }, /* ICC_ASGI1R */ 1856 1793 { Op1( 2), CRn( 0), CRm(12), Op2( 0), access_gic_sgi }, /* ICC_SGI0R */ 1857 - { Op1( 2), CRn( 0), CRm(14), Op2( 0), access_cntp_cval }, 1794 + { SYS_DESC(SYS_AARCH32_CNTP_CVAL), access_arch_timer }, 1858 1795 }; 1859 1796 1860 1797 /* Target specific emulation tables */ ··· 1883 1820 } 1884 1821 } 1885 1822 1886 - #define reg_to_match_value(x) \ 1887 - ({ \ 1888 - unsigned long val; \ 1889 - val = (x)->Op0 << 14; \ 1890 - val |= (x)->Op1 << 11; \ 1891 - val |= (x)->CRn << 7; \ 1892 - val |= (x)->CRm << 3; \ 1893 - val |= (x)->Op2; \ 1894 - val; \ 1895 - }) 1896 - 1897 1823 static int match_sys_reg(const void *key, const void *elt) 1898 1824 { 1899 1825 const unsigned long pval = (unsigned long)key; 1900 1826 const struct sys_reg_desc *r = elt; 1901 1827 1902 - return pval - reg_to_match_value(r); 1828 + return pval - reg_to_encoding(r); 1903 1829 } 1904 1830 1905 1831 static const struct sys_reg_desc *find_reg(const struct sys_reg_params *params, 1906 1832 const struct sys_reg_desc table[], 1907 1833 unsigned int num) 1908 1834 { 1909 - unsigned long pval = reg_to_match_value(params); 1835 + unsigned long pval = reg_to_encoding(params); 1910 1836 1911 1837 return bsearch((void *)pval, table, num, sizeof(table[0]), match_sys_reg); 1912 1838 } ··· 2258 2206 } 2259 2207 2260 2208 FUNCTION_INVARIANT(midr_el1) 2261 - FUNCTION_INVARIANT(ctr_el0) 2262 2209 FUNCTION_INVARIANT(revidr_el1) 2263 2210 FUNCTION_INVARIANT(clidr_el1) 2264 2211 FUNCTION_INVARIANT(aidr_el1) 2212 + 2213 + static void get_ctr_el0(struct kvm_vcpu *v, const struct sys_reg_desc *r) 2214 + { 2215 + ((struct sys_reg_desc *)r)->val = read_sanitised_ftr_reg(SYS_CTR_EL0); 2216 + } 2265 2217 2266 2218 /* ->val is filled in by kvm_sys_reg_table_init() */ 2267 2219 static struct sys_reg_desc invariant_sys_regs[] = {
+9 -2
drivers/clocksource/arm_arch_timer.c
··· 1206 1206 return ARCH_TIMER_PHYS_SECURE_PPI; 1207 1207 } 1208 1208 1209 + static void __init arch_timer_populate_kvm_info(void) 1210 + { 1211 + arch_timer_kvm_info.virtual_irq = arch_timer_ppi[ARCH_TIMER_VIRT_PPI]; 1212 + if (is_kernel_in_hyp_mode()) 1213 + arch_timer_kvm_info.physical_irq = arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI]; 1214 + } 1215 + 1209 1216 static int __init arch_timer_of_init(struct device_node *np) 1210 1217 { 1211 1218 int i, ret; ··· 1227 1220 for (i = ARCH_TIMER_PHYS_SECURE_PPI; i < ARCH_TIMER_MAX_TIMER_PPI; i++) 1228 1221 arch_timer_ppi[i] = irq_of_parse_and_map(np, i); 1229 1222 1230 - arch_timer_kvm_info.virtual_irq = arch_timer_ppi[ARCH_TIMER_VIRT_PPI]; 1223 + arch_timer_populate_kvm_info(); 1231 1224 1232 1225 rate = arch_timer_get_cntfrq(); 1233 1226 arch_timer_of_configure_rate(rate, np); ··· 1557 1550 arch_timer_ppi[ARCH_TIMER_HYP_PPI] = 1558 1551 acpi_gtdt_map_ppi(ARCH_TIMER_HYP_PPI); 1559 1552 1560 - arch_timer_kvm_info.virtual_irq = arch_timer_ppi[ARCH_TIMER_VIRT_PPI]; 1553 + arch_timer_populate_kvm_info(); 1561 1554 1562 1555 /* 1563 1556 * When probing via ACPI, we have no mechanism to override the sysreg
+1
include/clocksource/arm_arch_timer.h
··· 74 74 struct arch_timer_kvm_info { 75 75 struct timecounter timecounter; 76 76 int virtual_irq; 77 + int physical_irq; 77 78 }; 78 79 79 80 struct arch_timer_mem_frame {
+51 -21
include/kvm/arm_arch_timer.h
··· 22 22 #include <linux/clocksource.h> 23 23 #include <linux/hrtimer.h> 24 24 25 + enum kvm_arch_timers { 26 + TIMER_PTIMER, 27 + TIMER_VTIMER, 28 + NR_KVM_TIMERS 29 + }; 30 + 31 + enum kvm_arch_timer_regs { 32 + TIMER_REG_CNT, 33 + TIMER_REG_CVAL, 34 + TIMER_REG_TVAL, 35 + TIMER_REG_CTL, 36 + }; 37 + 25 38 struct arch_timer_context { 39 + struct kvm_vcpu *vcpu; 40 + 26 41 /* Registers: control register, timer value */ 27 42 u32 cnt_ctl; 28 43 u64 cnt_cval; ··· 45 30 /* Timer IRQ */ 46 31 struct kvm_irq_level irq; 47 32 48 - /* 49 - * We have multiple paths which can save/restore the timer state 50 - * onto the hardware, so we need some way of keeping track of 51 - * where the latest state is. 52 - * 53 - * loaded == true: State is loaded on the hardware registers. 54 - * loaded == false: State is stored in memory. 55 - */ 56 - bool loaded; 57 - 58 33 /* Virtual offset */ 59 - u64 cntvoff; 34 + u64 cntvoff; 35 + 36 + /* Emulated Timer (may be unused) */ 37 + struct hrtimer hrtimer; 38 + 39 + /* 40 + * We have multiple paths which can save/restore the timer state onto 41 + * the hardware, so we need some way of keeping track of where the 42 + * latest state is. 43 + */ 44 + bool loaded; 45 + 46 + /* Duplicated state from arch_timer.c for convenience */ 47 + u32 host_timer_irq; 48 + u32 host_timer_irq_flags; 49 + }; 50 + 51 + struct timer_map { 52 + struct arch_timer_context *direct_vtimer; 53 + struct arch_timer_context *direct_ptimer; 54 + struct arch_timer_context *emul_ptimer; 60 55 }; 61 56 62 57 struct arch_timer_cpu { 63 - struct arch_timer_context vtimer; 64 - struct arch_timer_context ptimer; 58 + struct arch_timer_context timers[NR_KVM_TIMERS]; 65 59 66 60 /* Background timer used when the guest is not running */ 67 61 struct hrtimer bg_timer; 68 - 69 - /* Physical timer emulation */ 70 - struct hrtimer phys_timer; 71 62 72 63 /* Is the timer enabled */ 73 64 bool enabled; ··· 97 76 98 77 bool kvm_timer_is_pending(struct kvm_vcpu *vcpu); 99 78 100 - void kvm_timer_schedule(struct kvm_vcpu *vcpu); 101 - void kvm_timer_unschedule(struct kvm_vcpu *vcpu); 102 - 103 79 u64 kvm_phys_timer_read(void); 104 80 105 81 void kvm_timer_vcpu_load(struct kvm_vcpu *vcpu); ··· 106 88 107 89 bool kvm_arch_timer_get_input_level(int vintid); 108 90 109 - #define vcpu_vtimer(v) (&(v)->arch.timer_cpu.vtimer) 110 - #define vcpu_ptimer(v) (&(v)->arch.timer_cpu.ptimer) 91 + #define vcpu_timer(v) (&(v)->arch.timer_cpu) 92 + #define vcpu_get_timer(v,t) (&vcpu_timer(v)->timers[(t)]) 93 + #define vcpu_vtimer(v) (&(v)->arch.timer_cpu.timers[TIMER_VTIMER]) 94 + #define vcpu_ptimer(v) (&(v)->arch.timer_cpu.timers[TIMER_PTIMER]) 95 + 96 + #define arch_timer_ctx_index(ctx) ((ctx) - vcpu_timer((ctx)->vcpu)->timers) 97 + 98 + u64 kvm_arm_timer_read_sysreg(struct kvm_vcpu *vcpu, 99 + enum kvm_arch_timers tmr, 100 + enum kvm_arch_timer_regs treg); 101 + void kvm_arm_timer_write_sysreg(struct kvm_vcpu *vcpu, 102 + enum kvm_arch_timers tmr, 103 + enum kvm_arch_timer_regs treg, 104 + u64 val); 111 105 112 106 #endif
+430 -186
virt/kvm/arm/arch_timer.c
··· 25 25 26 26 #include <clocksource/arm_arch_timer.h> 27 27 #include <asm/arch_timer.h> 28 + #include <asm/kvm_emulate.h> 28 29 #include <asm/kvm_hyp.h> 29 30 30 31 #include <kvm/arm_vgic.h> ··· 35 34 36 35 static struct timecounter *timecounter; 37 36 static unsigned int host_vtimer_irq; 37 + static unsigned int host_ptimer_irq; 38 38 static u32 host_vtimer_irq_flags; 39 + static u32 host_ptimer_irq_flags; 39 40 40 41 static DEFINE_STATIC_KEY_FALSE(has_gic_active_state); 41 42 ··· 55 52 static void kvm_timer_update_irq(struct kvm_vcpu *vcpu, bool new_level, 56 53 struct arch_timer_context *timer_ctx); 57 54 static bool kvm_timer_should_fire(struct arch_timer_context *timer_ctx); 55 + static void kvm_arm_timer_write(struct kvm_vcpu *vcpu, 56 + struct arch_timer_context *timer, 57 + enum kvm_arch_timer_regs treg, 58 + u64 val); 59 + static u64 kvm_arm_timer_read(struct kvm_vcpu *vcpu, 60 + struct arch_timer_context *timer, 61 + enum kvm_arch_timer_regs treg); 58 62 59 63 u64 kvm_phys_timer_read(void) 60 64 { 61 65 return timecounter->cc->read(timecounter->cc); 66 + } 67 + 68 + static void get_timer_map(struct kvm_vcpu *vcpu, struct timer_map *map) 69 + { 70 + if (has_vhe()) { 71 + map->direct_vtimer = vcpu_vtimer(vcpu); 72 + map->direct_ptimer = vcpu_ptimer(vcpu); 73 + map->emul_ptimer = NULL; 74 + } else { 75 + map->direct_vtimer = vcpu_vtimer(vcpu); 76 + map->direct_ptimer = NULL; 77 + map->emul_ptimer = vcpu_ptimer(vcpu); 78 + } 79 + 80 + trace_kvm_get_timer_map(vcpu->vcpu_id, map); 62 81 } 63 82 64 83 static inline bool userspace_irqchip(struct kvm *kvm) ··· 103 78 static irqreturn_t kvm_arch_timer_handler(int irq, void *dev_id) 104 79 { 105 80 struct kvm_vcpu *vcpu = *(struct kvm_vcpu **)dev_id; 106 - struct arch_timer_context *vtimer; 81 + struct arch_timer_context *ctx; 82 + struct timer_map map; 107 83 108 84 /* 109 85 * We may see a timer interrupt after vcpu_put() has been called which 110 86 * sets the CPU's vcpu pointer to NULL, because even though the timer 111 - * has been disabled in vtimer_save_state(), the hardware interrupt 87 + * has been disabled in timer_save_state(), the hardware interrupt 112 88 * signal may not have been retired from the interrupt controller yet. 113 89 */ 114 90 if (!vcpu) 115 91 return IRQ_HANDLED; 116 92 117 - vtimer = vcpu_vtimer(vcpu); 118 - if (kvm_timer_should_fire(vtimer)) 119 - kvm_timer_update_irq(vcpu, true, vtimer); 93 + get_timer_map(vcpu, &map); 94 + 95 + if (irq == host_vtimer_irq) 96 + ctx = map.direct_vtimer; 97 + else 98 + ctx = map.direct_ptimer; 99 + 100 + if (kvm_timer_should_fire(ctx)) 101 + kvm_timer_update_irq(vcpu, true, ctx); 120 102 121 103 if (userspace_irqchip(vcpu->kvm) && 122 104 !static_branch_unlikely(&has_gic_active_state)) ··· 154 122 155 123 static bool kvm_timer_irq_can_fire(struct arch_timer_context *timer_ctx) 156 124 { 157 - return !(timer_ctx->cnt_ctl & ARCH_TIMER_CTRL_IT_MASK) && 125 + WARN_ON(timer_ctx && timer_ctx->loaded); 126 + return timer_ctx && 127 + !(timer_ctx->cnt_ctl & ARCH_TIMER_CTRL_IT_MASK) && 158 128 (timer_ctx->cnt_ctl & ARCH_TIMER_CTRL_ENABLE); 159 129 } 160 130 ··· 166 132 */ 167 133 static u64 kvm_timer_earliest_exp(struct kvm_vcpu *vcpu) 168 134 { 169 - u64 min_virt = ULLONG_MAX, min_phys = ULLONG_MAX; 170 - struct arch_timer_context *vtimer = vcpu_vtimer(vcpu); 171 - struct arch_timer_context *ptimer = vcpu_ptimer(vcpu); 135 + u64 min_delta = ULLONG_MAX; 136 + int i; 172 137 173 - if (kvm_timer_irq_can_fire(vtimer)) 174 - min_virt = kvm_timer_compute_delta(vtimer); 138 + for (i = 0; i < NR_KVM_TIMERS; i++) { 139 + struct arch_timer_context *ctx = &vcpu->arch.timer_cpu.timers[i]; 175 140 176 - if (kvm_timer_irq_can_fire(ptimer)) 177 - min_phys = kvm_timer_compute_delta(ptimer); 141 + WARN(ctx->loaded, "timer %d loaded\n", i); 142 + if (kvm_timer_irq_can_fire(ctx)) 143 + min_delta = min(min_delta, kvm_timer_compute_delta(ctx)); 144 + } 178 145 179 146 /* If none of timers can fire, then return 0 */ 180 - if ((min_virt == ULLONG_MAX) && (min_phys == ULLONG_MAX)) 147 + if (min_delta == ULLONG_MAX) 181 148 return 0; 182 149 183 - return min(min_virt, min_phys); 150 + return min_delta; 184 151 } 185 152 186 153 static enum hrtimer_restart kvm_bg_timer_expire(struct hrtimer *hrt) ··· 208 173 return HRTIMER_NORESTART; 209 174 } 210 175 211 - static enum hrtimer_restart kvm_phys_timer_expire(struct hrtimer *hrt) 176 + static enum hrtimer_restart kvm_hrtimer_expire(struct hrtimer *hrt) 212 177 { 213 - struct arch_timer_context *ptimer; 214 - struct arch_timer_cpu *timer; 178 + struct arch_timer_context *ctx; 215 179 struct kvm_vcpu *vcpu; 216 180 u64 ns; 217 181 218 - timer = container_of(hrt, struct arch_timer_cpu, phys_timer); 219 - vcpu = container_of(timer, struct kvm_vcpu, arch.timer_cpu); 220 - ptimer = vcpu_ptimer(vcpu); 182 + ctx = container_of(hrt, struct arch_timer_context, hrtimer); 183 + vcpu = ctx->vcpu; 184 + 185 + trace_kvm_timer_hrtimer_expire(ctx); 221 186 222 187 /* 223 188 * Check that the timer has really expired from the guest's 224 189 * PoV (NTP on the host may have forced it to expire 225 190 * early). If not ready, schedule for a later time. 226 191 */ 227 - ns = kvm_timer_compute_delta(ptimer); 192 + ns = kvm_timer_compute_delta(ctx); 228 193 if (unlikely(ns)) { 229 194 hrtimer_forward_now(hrt, ns_to_ktime(ns)); 230 195 return HRTIMER_RESTART; 231 196 } 232 197 233 - kvm_timer_update_irq(vcpu, true, ptimer); 198 + kvm_timer_update_irq(vcpu, true, ctx); 234 199 return HRTIMER_NORESTART; 235 200 } 236 201 237 202 static bool kvm_timer_should_fire(struct arch_timer_context *timer_ctx) 238 203 { 204 + enum kvm_arch_timers index; 239 205 u64 cval, now; 240 206 241 - if (timer_ctx->loaded) { 242 - u32 cnt_ctl; 207 + if (!timer_ctx) 208 + return false; 243 209 244 - /* Only the virtual timer can be loaded so far */ 245 - cnt_ctl = read_sysreg_el0(cntv_ctl); 210 + index = arch_timer_ctx_index(timer_ctx); 211 + 212 + if (timer_ctx->loaded) { 213 + u32 cnt_ctl = 0; 214 + 215 + switch (index) { 216 + case TIMER_VTIMER: 217 + cnt_ctl = read_sysreg_el0(cntv_ctl); 218 + break; 219 + case TIMER_PTIMER: 220 + cnt_ctl = read_sysreg_el0(cntp_ctl); 221 + break; 222 + case NR_KVM_TIMERS: 223 + /* GCC is braindead */ 224 + cnt_ctl = 0; 225 + break; 226 + } 227 + 246 228 return (cnt_ctl & ARCH_TIMER_CTRL_ENABLE) && 247 229 (cnt_ctl & ARCH_TIMER_CTRL_IT_STAT) && 248 230 !(cnt_ctl & ARCH_TIMER_CTRL_IT_MASK); ··· 276 224 277 225 bool kvm_timer_is_pending(struct kvm_vcpu *vcpu) 278 226 { 279 - struct arch_timer_context *vtimer = vcpu_vtimer(vcpu); 280 - struct arch_timer_context *ptimer = vcpu_ptimer(vcpu); 227 + struct timer_map map; 281 228 282 - if (kvm_timer_should_fire(vtimer)) 283 - return true; 229 + get_timer_map(vcpu, &map); 284 230 285 - return kvm_timer_should_fire(ptimer); 231 + return kvm_timer_should_fire(map.direct_vtimer) || 232 + kvm_timer_should_fire(map.direct_ptimer) || 233 + kvm_timer_should_fire(map.emul_ptimer); 286 234 } 287 235 288 236 /* ··· 321 269 } 322 270 } 323 271 324 - /* Schedule the background timer for the emulated timer. */ 325 - static void phys_timer_emulate(struct kvm_vcpu *vcpu) 272 + static void timer_emulate(struct arch_timer_context *ctx) 326 273 { 327 - struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; 328 - struct arch_timer_context *ptimer = vcpu_ptimer(vcpu); 274 + bool should_fire = kvm_timer_should_fire(ctx); 275 + 276 + trace_kvm_timer_emulate(ctx, should_fire); 277 + 278 + if (should_fire) { 279 + kvm_timer_update_irq(ctx->vcpu, true, ctx); 280 + return; 281 + } 329 282 330 283 /* 331 284 * If the timer can fire now, we don't need to have a soft timer 332 285 * scheduled for the future. If the timer cannot fire at all, 333 286 * then we also don't need a soft timer. 334 287 */ 335 - if (kvm_timer_should_fire(ptimer) || !kvm_timer_irq_can_fire(ptimer)) { 336 - soft_timer_cancel(&timer->phys_timer); 288 + if (!kvm_timer_irq_can_fire(ctx)) { 289 + soft_timer_cancel(&ctx->hrtimer); 337 290 return; 338 291 } 339 292 340 - soft_timer_start(&timer->phys_timer, kvm_timer_compute_delta(ptimer)); 293 + soft_timer_start(&ctx->hrtimer, kvm_timer_compute_delta(ctx)); 341 294 } 342 295 343 - /* 344 - * Check if there was a change in the timer state, so that we should either 345 - * raise or lower the line level to the GIC or schedule a background timer to 346 - * emulate the physical timer. 347 - */ 348 - static void kvm_timer_update_state(struct kvm_vcpu *vcpu) 296 + static void timer_save_state(struct arch_timer_context *ctx) 349 297 { 350 - struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; 351 - struct arch_timer_context *vtimer = vcpu_vtimer(vcpu); 352 - struct arch_timer_context *ptimer = vcpu_ptimer(vcpu); 353 - bool level; 354 - 355 - if (unlikely(!timer->enabled)) 356 - return; 357 - 358 - /* 359 - * The vtimer virtual interrupt is a 'mapped' interrupt, meaning part 360 - * of its lifecycle is offloaded to the hardware, and we therefore may 361 - * not have lowered the irq.level value before having to signal a new 362 - * interrupt, but have to signal an interrupt every time the level is 363 - * asserted. 364 - */ 365 - level = kvm_timer_should_fire(vtimer); 366 - kvm_timer_update_irq(vcpu, level, vtimer); 367 - 368 - phys_timer_emulate(vcpu); 369 - 370 - if (kvm_timer_should_fire(ptimer) != ptimer->irq.level) 371 - kvm_timer_update_irq(vcpu, !ptimer->irq.level, ptimer); 372 - } 373 - 374 - static void vtimer_save_state(struct kvm_vcpu *vcpu) 375 - { 376 - struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; 377 - struct arch_timer_context *vtimer = vcpu_vtimer(vcpu); 298 + struct arch_timer_cpu *timer = vcpu_timer(ctx->vcpu); 299 + enum kvm_arch_timers index = arch_timer_ctx_index(ctx); 378 300 unsigned long flags; 301 + 302 + if (!timer->enabled) 303 + return; 379 304 380 305 local_irq_save(flags); 381 306 382 - if (!vtimer->loaded) 307 + if (!ctx->loaded) 383 308 goto out; 384 309 385 - if (timer->enabled) { 386 - vtimer->cnt_ctl = read_sysreg_el0(cntv_ctl); 387 - vtimer->cnt_cval = read_sysreg_el0(cntv_cval); 310 + switch (index) { 311 + case TIMER_VTIMER: 312 + ctx->cnt_ctl = read_sysreg_el0(cntv_ctl); 313 + ctx->cnt_cval = read_sysreg_el0(cntv_cval); 314 + 315 + /* Disable the timer */ 316 + write_sysreg_el0(0, cntv_ctl); 317 + isb(); 318 + 319 + break; 320 + case TIMER_PTIMER: 321 + ctx->cnt_ctl = read_sysreg_el0(cntp_ctl); 322 + ctx->cnt_cval = read_sysreg_el0(cntp_cval); 323 + 324 + /* Disable the timer */ 325 + write_sysreg_el0(0, cntp_ctl); 326 + isb(); 327 + 328 + break; 329 + case NR_KVM_TIMERS: 330 + BUG(); 388 331 } 389 332 390 - /* Disable the virtual timer */ 391 - write_sysreg_el0(0, cntv_ctl); 392 - isb(); 333 + trace_kvm_timer_save_state(ctx); 393 334 394 - vtimer->loaded = false; 335 + ctx->loaded = false; 395 336 out: 396 337 local_irq_restore(flags); 397 338 } ··· 394 349 * thread is removed from its waitqueue and made runnable when there's a timer 395 350 * interrupt to handle. 396 351 */ 397 - void kvm_timer_schedule(struct kvm_vcpu *vcpu) 352 + static void kvm_timer_blocking(struct kvm_vcpu *vcpu) 398 353 { 399 - struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; 400 - struct arch_timer_context *vtimer = vcpu_vtimer(vcpu); 401 - struct arch_timer_context *ptimer = vcpu_ptimer(vcpu); 354 + struct arch_timer_cpu *timer = vcpu_timer(vcpu); 355 + struct timer_map map; 402 356 403 - vtimer_save_state(vcpu); 404 - 405 - /* 406 - * No need to schedule a background timer if any guest timer has 407 - * already expired, because kvm_vcpu_block will return before putting 408 - * the thread to sleep. 409 - */ 410 - if (kvm_timer_should_fire(vtimer) || kvm_timer_should_fire(ptimer)) 411 - return; 357 + get_timer_map(vcpu, &map); 412 358 413 359 /* 414 - * If both timers are not capable of raising interrupts (disabled or 360 + * If no timers are capable of raising interrupts (disabled or 415 361 * masked), then there's no more work for us to do. 416 362 */ 417 - if (!kvm_timer_irq_can_fire(vtimer) && !kvm_timer_irq_can_fire(ptimer)) 363 + if (!kvm_timer_irq_can_fire(map.direct_vtimer) && 364 + !kvm_timer_irq_can_fire(map.direct_ptimer) && 365 + !kvm_timer_irq_can_fire(map.emul_ptimer)) 418 366 return; 419 367 420 368 /* 421 - * The guest timers have not yet expired, schedule a background timer. 369 + * At least one guest time will expire. Schedule a background timer. 422 370 * Set the earliest expiration time among the guest timers. 423 371 */ 424 372 soft_timer_start(&timer->bg_timer, kvm_timer_earliest_exp(vcpu)); 425 373 } 426 374 427 - static void vtimer_restore_state(struct kvm_vcpu *vcpu) 375 + static void kvm_timer_unblocking(struct kvm_vcpu *vcpu) 428 376 { 429 - struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; 430 - struct arch_timer_context *vtimer = vcpu_vtimer(vcpu); 377 + struct arch_timer_cpu *timer = vcpu_timer(vcpu); 378 + 379 + soft_timer_cancel(&timer->bg_timer); 380 + } 381 + 382 + static void timer_restore_state(struct arch_timer_context *ctx) 383 + { 384 + struct arch_timer_cpu *timer = vcpu_timer(ctx->vcpu); 385 + enum kvm_arch_timers index = arch_timer_ctx_index(ctx); 431 386 unsigned long flags; 387 + 388 + if (!timer->enabled) 389 + return; 432 390 433 391 local_irq_save(flags); 434 392 435 - if (vtimer->loaded) 393 + if (ctx->loaded) 436 394 goto out; 437 395 438 - if (timer->enabled) { 439 - write_sysreg_el0(vtimer->cnt_cval, cntv_cval); 396 + switch (index) { 397 + case TIMER_VTIMER: 398 + write_sysreg_el0(ctx->cnt_cval, cntv_cval); 440 399 isb(); 441 - write_sysreg_el0(vtimer->cnt_ctl, cntv_ctl); 400 + write_sysreg_el0(ctx->cnt_ctl, cntv_ctl); 401 + break; 402 + case TIMER_PTIMER: 403 + write_sysreg_el0(ctx->cnt_cval, cntp_cval); 404 + isb(); 405 + write_sysreg_el0(ctx->cnt_ctl, cntp_ctl); 406 + break; 407 + case NR_KVM_TIMERS: 408 + BUG(); 442 409 } 443 410 444 - vtimer->loaded = true; 411 + trace_kvm_timer_restore_state(ctx); 412 + 413 + ctx->loaded = true; 445 414 out: 446 415 local_irq_restore(flags); 447 - } 448 - 449 - void kvm_timer_unschedule(struct kvm_vcpu *vcpu) 450 - { 451 - struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; 452 - 453 - vtimer_restore_state(vcpu); 454 - 455 - soft_timer_cancel(&timer->bg_timer); 456 416 } 457 417 458 418 static void set_cntvoff(u64 cntvoff) ··· 475 425 kvm_call_hyp(__kvm_timer_set_cntvoff, low, high); 476 426 } 477 427 478 - static inline void set_vtimer_irq_phys_active(struct kvm_vcpu *vcpu, bool active) 428 + static inline void set_timer_irq_phys_active(struct arch_timer_context *ctx, bool active) 479 429 { 480 430 int r; 481 - r = irq_set_irqchip_state(host_vtimer_irq, IRQCHIP_STATE_ACTIVE, active); 431 + r = irq_set_irqchip_state(ctx->host_timer_irq, IRQCHIP_STATE_ACTIVE, active); 482 432 WARN_ON(r); 483 433 } 484 434 485 - static void kvm_timer_vcpu_load_gic(struct kvm_vcpu *vcpu) 435 + static void kvm_timer_vcpu_load_gic(struct arch_timer_context *ctx) 486 436 { 487 - struct arch_timer_context *vtimer = vcpu_vtimer(vcpu); 488 - bool phys_active; 437 + struct kvm_vcpu *vcpu = ctx->vcpu; 438 + bool phys_active = false; 439 + 440 + /* 441 + * Update the timer output so that it is likely to match the 442 + * state we're about to restore. If the timer expires between 443 + * this point and the register restoration, we'll take the 444 + * interrupt anyway. 445 + */ 446 + kvm_timer_update_irq(ctx->vcpu, kvm_timer_should_fire(ctx), ctx); 489 447 490 448 if (irqchip_in_kernel(vcpu->kvm)) 491 - phys_active = kvm_vgic_map_is_active(vcpu, vtimer->irq.irq); 492 - else 493 - phys_active = vtimer->irq.level; 494 - set_vtimer_irq_phys_active(vcpu, phys_active); 449 + phys_active = kvm_vgic_map_is_active(vcpu, ctx->irq.irq); 450 + 451 + phys_active |= ctx->irq.level; 452 + 453 + set_timer_irq_phys_active(ctx, phys_active); 495 454 } 496 455 497 456 static void kvm_timer_vcpu_load_nogic(struct kvm_vcpu *vcpu) ··· 525 466 526 467 void kvm_timer_vcpu_load(struct kvm_vcpu *vcpu) 527 468 { 528 - struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; 529 - struct arch_timer_context *vtimer = vcpu_vtimer(vcpu); 530 - struct arch_timer_context *ptimer = vcpu_ptimer(vcpu); 469 + struct arch_timer_cpu *timer = vcpu_timer(vcpu); 470 + struct timer_map map; 531 471 532 472 if (unlikely(!timer->enabled)) 533 473 return; 534 474 535 - if (static_branch_likely(&has_gic_active_state)) 536 - kvm_timer_vcpu_load_gic(vcpu); 537 - else 475 + get_timer_map(vcpu, &map); 476 + 477 + if (static_branch_likely(&has_gic_active_state)) { 478 + kvm_timer_vcpu_load_gic(map.direct_vtimer); 479 + if (map.direct_ptimer) 480 + kvm_timer_vcpu_load_gic(map.direct_ptimer); 481 + } else { 538 482 kvm_timer_vcpu_load_nogic(vcpu); 483 + } 539 484 540 - set_cntvoff(vtimer->cntvoff); 485 + set_cntvoff(map.direct_vtimer->cntvoff); 541 486 542 - vtimer_restore_state(vcpu); 487 + kvm_timer_unblocking(vcpu); 543 488 544 - /* Set the background timer for the physical timer emulation. */ 545 - phys_timer_emulate(vcpu); 489 + timer_restore_state(map.direct_vtimer); 490 + if (map.direct_ptimer) 491 + timer_restore_state(map.direct_ptimer); 546 492 547 - /* If the timer fired while we weren't running, inject it now */ 548 - if (kvm_timer_should_fire(ptimer) != ptimer->irq.level) 549 - kvm_timer_update_irq(vcpu, !ptimer->irq.level, ptimer); 493 + if (map.emul_ptimer) 494 + timer_emulate(map.emul_ptimer); 550 495 } 551 496 552 497 bool kvm_timer_should_notify_user(struct kvm_vcpu *vcpu) ··· 572 509 573 510 void kvm_timer_vcpu_put(struct kvm_vcpu *vcpu) 574 511 { 575 - struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; 512 + struct arch_timer_cpu *timer = vcpu_timer(vcpu); 513 + struct timer_map map; 576 514 577 515 if (unlikely(!timer->enabled)) 578 516 return; 579 517 580 - vtimer_save_state(vcpu); 518 + get_timer_map(vcpu, &map); 519 + 520 + timer_save_state(map.direct_vtimer); 521 + if (map.direct_ptimer) 522 + timer_save_state(map.direct_ptimer); 581 523 582 524 /* 583 - * Cancel the physical timer emulation, because the only case where we 525 + * Cancel soft timer emulation, because the only case where we 584 526 * need it after a vcpu_put is in the context of a sleeping VCPU, and 585 527 * in that case we already factor in the deadline for the physical 586 528 * timer when scheduling the bg_timer. ··· 593 525 * In any case, we re-schedule the hrtimer for the physical timer when 594 526 * coming back to the VCPU thread in kvm_timer_vcpu_load(). 595 527 */ 596 - soft_timer_cancel(&timer->phys_timer); 528 + if (map.emul_ptimer) 529 + soft_timer_cancel(&map.emul_ptimer->hrtimer); 530 + 531 + if (swait_active(kvm_arch_vcpu_wq(vcpu))) 532 + kvm_timer_blocking(vcpu); 597 533 598 534 /* 599 535 * The kernel may decide to run userspace after calling vcpu_put, so ··· 606 534 * counter of non-VHE case. For VHE, the virtual counter uses a fixed 607 535 * virtual offset of zero, so no need to zero CNTVOFF_EL2 register. 608 536 */ 609 - if (!has_vhe()) 610 - set_cntvoff(0); 537 + set_cntvoff(0); 611 538 } 612 539 613 540 /* ··· 621 550 if (!kvm_timer_should_fire(vtimer)) { 622 551 kvm_timer_update_irq(vcpu, false, vtimer); 623 552 if (static_branch_likely(&has_gic_active_state)) 624 - set_vtimer_irq_phys_active(vcpu, false); 553 + set_timer_irq_phys_active(vtimer, false); 625 554 else 626 555 enable_percpu_irq(host_vtimer_irq, host_vtimer_irq_flags); 627 556 } ··· 629 558 630 559 void kvm_timer_sync_hwstate(struct kvm_vcpu *vcpu) 631 560 { 632 - struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; 561 + struct arch_timer_cpu *timer = vcpu_timer(vcpu); 633 562 634 563 if (unlikely(!timer->enabled)) 635 564 return; ··· 640 569 641 570 int kvm_timer_vcpu_reset(struct kvm_vcpu *vcpu) 642 571 { 643 - struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; 644 - struct arch_timer_context *vtimer = vcpu_vtimer(vcpu); 645 - struct arch_timer_context *ptimer = vcpu_ptimer(vcpu); 572 + struct arch_timer_cpu *timer = vcpu_timer(vcpu); 573 + struct timer_map map; 574 + 575 + get_timer_map(vcpu, &map); 646 576 647 577 /* 648 578 * The bits in CNTV_CTL are architecturally reset to UNKNOWN for ARMv8 ··· 651 579 * resets the timer to be disabled and unmasked and is compliant with 652 580 * the ARMv7 architecture. 653 581 */ 654 - vtimer->cnt_ctl = 0; 655 - ptimer->cnt_ctl = 0; 656 - kvm_timer_update_state(vcpu); 582 + vcpu_vtimer(vcpu)->cnt_ctl = 0; 583 + vcpu_ptimer(vcpu)->cnt_ctl = 0; 657 584 658 - if (timer->enabled && irqchip_in_kernel(vcpu->kvm)) 659 - kvm_vgic_reset_mapped_irq(vcpu, vtimer->irq.irq); 585 + if (timer->enabled) { 586 + kvm_timer_update_irq(vcpu, false, vcpu_vtimer(vcpu)); 587 + kvm_timer_update_irq(vcpu, false, vcpu_ptimer(vcpu)); 588 + 589 + if (irqchip_in_kernel(vcpu->kvm)) { 590 + kvm_vgic_reset_mapped_irq(vcpu, map.direct_vtimer->irq.irq); 591 + if (map.direct_ptimer) 592 + kvm_vgic_reset_mapped_irq(vcpu, map.direct_ptimer->irq.irq); 593 + } 594 + } 595 + 596 + if (map.emul_ptimer) 597 + soft_timer_cancel(&map.emul_ptimer->hrtimer); 660 598 661 599 return 0; 662 600 } ··· 692 610 693 611 void kvm_timer_vcpu_init(struct kvm_vcpu *vcpu) 694 612 { 695 - struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; 613 + struct arch_timer_cpu *timer = vcpu_timer(vcpu); 696 614 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu); 697 615 struct arch_timer_context *ptimer = vcpu_ptimer(vcpu); 698 616 699 617 /* Synchronize cntvoff across all vtimers of a VM. */ 700 618 update_vtimer_cntvoff(vcpu, kvm_phys_timer_read()); 701 - vcpu_ptimer(vcpu)->cntvoff = 0; 619 + ptimer->cntvoff = 0; 702 620 703 621 hrtimer_init(&timer->bg_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS); 704 622 timer->bg_timer.function = kvm_bg_timer_expire; 705 623 706 - hrtimer_init(&timer->phys_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS); 707 - timer->phys_timer.function = kvm_phys_timer_expire; 624 + hrtimer_init(&vtimer->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS); 625 + hrtimer_init(&ptimer->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS); 626 + vtimer->hrtimer.function = kvm_hrtimer_expire; 627 + ptimer->hrtimer.function = kvm_hrtimer_expire; 708 628 709 629 vtimer->irq.irq = default_vtimer_irq.irq; 710 630 ptimer->irq.irq = default_ptimer_irq.irq; 631 + 632 + vtimer->host_timer_irq = host_vtimer_irq; 633 + ptimer->host_timer_irq = host_ptimer_irq; 634 + 635 + vtimer->host_timer_irq_flags = host_vtimer_irq_flags; 636 + ptimer->host_timer_irq_flags = host_ptimer_irq_flags; 637 + 638 + vtimer->vcpu = vcpu; 639 + ptimer->vcpu = vcpu; 711 640 } 712 641 713 642 static void kvm_timer_init_interrupt(void *info) 714 643 { 715 644 enable_percpu_irq(host_vtimer_irq, host_vtimer_irq_flags); 645 + enable_percpu_irq(host_ptimer_irq, host_ptimer_irq_flags); 716 646 } 717 647 718 648 int kvm_arm_timer_set_reg(struct kvm_vcpu *vcpu, u64 regid, u64 value) 719 649 { 720 - struct arch_timer_context *vtimer = vcpu_vtimer(vcpu); 721 - struct arch_timer_context *ptimer = vcpu_ptimer(vcpu); 650 + struct arch_timer_context *timer; 651 + bool level; 722 652 723 653 switch (regid) { 724 654 case KVM_REG_ARM_TIMER_CTL: 725 - vtimer->cnt_ctl = value & ~ARCH_TIMER_CTRL_IT_STAT; 655 + timer = vcpu_vtimer(vcpu); 656 + kvm_arm_timer_write(vcpu, timer, TIMER_REG_CTL, value); 726 657 break; 727 658 case KVM_REG_ARM_TIMER_CNT: 659 + timer = vcpu_vtimer(vcpu); 728 660 update_vtimer_cntvoff(vcpu, kvm_phys_timer_read() - value); 729 661 break; 730 662 case KVM_REG_ARM_TIMER_CVAL: 731 - vtimer->cnt_cval = value; 663 + timer = vcpu_vtimer(vcpu); 664 + kvm_arm_timer_write(vcpu, timer, TIMER_REG_CVAL, value); 732 665 break; 733 666 case KVM_REG_ARM_PTIMER_CTL: 734 - ptimer->cnt_ctl = value & ~ARCH_TIMER_CTRL_IT_STAT; 667 + timer = vcpu_ptimer(vcpu); 668 + kvm_arm_timer_write(vcpu, timer, TIMER_REG_CTL, value); 735 669 break; 736 670 case KVM_REG_ARM_PTIMER_CVAL: 737 - ptimer->cnt_cval = value; 671 + timer = vcpu_ptimer(vcpu); 672 + kvm_arm_timer_write(vcpu, timer, TIMER_REG_CVAL, value); 738 673 break; 739 674 740 675 default: 741 676 return -1; 742 677 } 743 678 744 - kvm_timer_update_state(vcpu); 679 + level = kvm_timer_should_fire(timer); 680 + kvm_timer_update_irq(vcpu, level, timer); 681 + timer_emulate(timer); 682 + 745 683 return 0; 746 684 } 747 685 ··· 781 679 782 680 u64 kvm_arm_timer_get_reg(struct kvm_vcpu *vcpu, u64 regid) 783 681 { 784 - struct arch_timer_context *ptimer = vcpu_ptimer(vcpu); 785 - struct arch_timer_context *vtimer = vcpu_vtimer(vcpu); 786 - 787 682 switch (regid) { 788 683 case KVM_REG_ARM_TIMER_CTL: 789 - return read_timer_ctl(vtimer); 684 + return kvm_arm_timer_read(vcpu, 685 + vcpu_vtimer(vcpu), TIMER_REG_CTL); 790 686 case KVM_REG_ARM_TIMER_CNT: 791 - return kvm_phys_timer_read() - vtimer->cntvoff; 687 + return kvm_arm_timer_read(vcpu, 688 + vcpu_vtimer(vcpu), TIMER_REG_CNT); 792 689 case KVM_REG_ARM_TIMER_CVAL: 793 - return vtimer->cnt_cval; 690 + return kvm_arm_timer_read(vcpu, 691 + vcpu_vtimer(vcpu), TIMER_REG_CVAL); 794 692 case KVM_REG_ARM_PTIMER_CTL: 795 - return read_timer_ctl(ptimer); 796 - case KVM_REG_ARM_PTIMER_CVAL: 797 - return ptimer->cnt_cval; 693 + return kvm_arm_timer_read(vcpu, 694 + vcpu_ptimer(vcpu), TIMER_REG_CTL); 798 695 case KVM_REG_ARM_PTIMER_CNT: 799 - return kvm_phys_timer_read(); 696 + return kvm_arm_timer_read(vcpu, 697 + vcpu_vtimer(vcpu), TIMER_REG_CNT); 698 + case KVM_REG_ARM_PTIMER_CVAL: 699 + return kvm_arm_timer_read(vcpu, 700 + vcpu_ptimer(vcpu), TIMER_REG_CVAL); 800 701 } 801 702 return (u64)-1; 703 + } 704 + 705 + static u64 kvm_arm_timer_read(struct kvm_vcpu *vcpu, 706 + struct arch_timer_context *timer, 707 + enum kvm_arch_timer_regs treg) 708 + { 709 + u64 val; 710 + 711 + switch (treg) { 712 + case TIMER_REG_TVAL: 713 + val = kvm_phys_timer_read() - timer->cntvoff - timer->cnt_cval; 714 + break; 715 + 716 + case TIMER_REG_CTL: 717 + val = read_timer_ctl(timer); 718 + break; 719 + 720 + case TIMER_REG_CVAL: 721 + val = timer->cnt_cval; 722 + break; 723 + 724 + case TIMER_REG_CNT: 725 + val = kvm_phys_timer_read() - timer->cntvoff; 726 + break; 727 + 728 + default: 729 + BUG(); 730 + } 731 + 732 + return val; 733 + } 734 + 735 + u64 kvm_arm_timer_read_sysreg(struct kvm_vcpu *vcpu, 736 + enum kvm_arch_timers tmr, 737 + enum kvm_arch_timer_regs treg) 738 + { 739 + u64 val; 740 + 741 + preempt_disable(); 742 + kvm_timer_vcpu_put(vcpu); 743 + 744 + val = kvm_arm_timer_read(vcpu, vcpu_get_timer(vcpu, tmr), treg); 745 + 746 + kvm_timer_vcpu_load(vcpu); 747 + preempt_enable(); 748 + 749 + return val; 750 + } 751 + 752 + static void kvm_arm_timer_write(struct kvm_vcpu *vcpu, 753 + struct arch_timer_context *timer, 754 + enum kvm_arch_timer_regs treg, 755 + u64 val) 756 + { 757 + switch (treg) { 758 + case TIMER_REG_TVAL: 759 + timer->cnt_cval = val - kvm_phys_timer_read() - timer->cntvoff; 760 + break; 761 + 762 + case TIMER_REG_CTL: 763 + timer->cnt_ctl = val & ~ARCH_TIMER_CTRL_IT_STAT; 764 + break; 765 + 766 + case TIMER_REG_CVAL: 767 + timer->cnt_cval = val; 768 + break; 769 + 770 + default: 771 + BUG(); 772 + } 773 + } 774 + 775 + void kvm_arm_timer_write_sysreg(struct kvm_vcpu *vcpu, 776 + enum kvm_arch_timers tmr, 777 + enum kvm_arch_timer_regs treg, 778 + u64 val) 779 + { 780 + preempt_disable(); 781 + kvm_timer_vcpu_put(vcpu); 782 + 783 + kvm_arm_timer_write(vcpu, vcpu_get_timer(vcpu, tmr), treg, val); 784 + 785 + kvm_timer_vcpu_load(vcpu); 786 + preempt_enable(); 802 787 } 803 788 804 789 static int kvm_timer_starting_cpu(unsigned int cpu) ··· 913 724 return -ENODEV; 914 725 } 915 726 727 + /* First, do the virtual EL1 timer irq */ 728 + 916 729 if (info->virtual_irq <= 0) { 917 730 kvm_err("kvm_arch_timer: invalid virtual timer IRQ: %d\n", 918 731 info->virtual_irq); ··· 925 734 host_vtimer_irq_flags = irq_get_trigger_type(host_vtimer_irq); 926 735 if (host_vtimer_irq_flags != IRQF_TRIGGER_HIGH && 927 736 host_vtimer_irq_flags != IRQF_TRIGGER_LOW) { 928 - kvm_err("Invalid trigger for IRQ%d, assuming level low\n", 737 + kvm_err("Invalid trigger for vtimer IRQ%d, assuming level low\n", 929 738 host_vtimer_irq); 930 739 host_vtimer_irq_flags = IRQF_TRIGGER_LOW; 931 740 } 932 741 933 742 err = request_percpu_irq(host_vtimer_irq, kvm_arch_timer_handler, 934 - "kvm guest timer", kvm_get_running_vcpus()); 743 + "kvm guest vtimer", kvm_get_running_vcpus()); 935 744 if (err) { 936 - kvm_err("kvm_arch_timer: can't request interrupt %d (%d)\n", 745 + kvm_err("kvm_arch_timer: can't request vtimer interrupt %d (%d)\n", 937 746 host_vtimer_irq, err); 938 747 return err; 939 748 } ··· 951 760 952 761 kvm_debug("virtual timer IRQ%d\n", host_vtimer_irq); 953 762 763 + /* Now let's do the physical EL1 timer irq */ 764 + 765 + if (info->physical_irq > 0) { 766 + host_ptimer_irq = info->physical_irq; 767 + host_ptimer_irq_flags = irq_get_trigger_type(host_ptimer_irq); 768 + if (host_ptimer_irq_flags != IRQF_TRIGGER_HIGH && 769 + host_ptimer_irq_flags != IRQF_TRIGGER_LOW) { 770 + kvm_err("Invalid trigger for ptimer IRQ%d, assuming level low\n", 771 + host_ptimer_irq); 772 + host_ptimer_irq_flags = IRQF_TRIGGER_LOW; 773 + } 774 + 775 + err = request_percpu_irq(host_ptimer_irq, kvm_arch_timer_handler, 776 + "kvm guest ptimer", kvm_get_running_vcpus()); 777 + if (err) { 778 + kvm_err("kvm_arch_timer: can't request ptimer interrupt %d (%d)\n", 779 + host_ptimer_irq, err); 780 + return err; 781 + } 782 + 783 + if (has_gic) { 784 + err = irq_set_vcpu_affinity(host_ptimer_irq, 785 + kvm_get_running_vcpus()); 786 + if (err) { 787 + kvm_err("kvm_arch_timer: error setting vcpu affinity\n"); 788 + goto out_free_irq; 789 + } 790 + } 791 + 792 + kvm_debug("physical timer IRQ%d\n", host_ptimer_irq); 793 + } else if (has_vhe()) { 794 + kvm_err("kvm_arch_timer: invalid physical timer IRQ: %d\n", 795 + info->physical_irq); 796 + err = -ENODEV; 797 + goto out_free_irq; 798 + } 799 + 954 800 cpuhp_setup_state(CPUHP_AP_KVM_ARM_TIMER_STARTING, 955 801 "kvm/arm/timer:starting", kvm_timer_starting_cpu, 956 802 kvm_timer_dying_cpu); ··· 999 771 1000 772 void kvm_timer_vcpu_terminate(struct kvm_vcpu *vcpu) 1001 773 { 1002 - struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; 774 + struct arch_timer_cpu *timer = vcpu_timer(vcpu); 1003 775 1004 776 soft_timer_cancel(&timer->bg_timer); 1005 777 } ··· 1035 807 1036 808 if (vintid == vcpu_vtimer(vcpu)->irq.irq) 1037 809 timer = vcpu_vtimer(vcpu); 810 + else if (vintid == vcpu_ptimer(vcpu)->irq.irq) 811 + timer = vcpu_ptimer(vcpu); 1038 812 else 1039 - BUG(); /* We only map the vtimer so far */ 813 + BUG(); 1040 814 1041 815 return kvm_timer_should_fire(timer); 1042 816 } 1043 817 1044 818 int kvm_timer_enable(struct kvm_vcpu *vcpu) 1045 819 { 1046 - struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; 1047 - struct arch_timer_context *vtimer = vcpu_vtimer(vcpu); 820 + struct arch_timer_cpu *timer = vcpu_timer(vcpu); 821 + struct timer_map map; 1048 822 int ret; 1049 823 1050 824 if (timer->enabled) ··· 1064 834 return -EINVAL; 1065 835 } 1066 836 1067 - ret = kvm_vgic_map_phys_irq(vcpu, host_vtimer_irq, vtimer->irq.irq, 837 + get_timer_map(vcpu, &map); 838 + 839 + ret = kvm_vgic_map_phys_irq(vcpu, 840 + map.direct_vtimer->host_timer_irq, 841 + map.direct_vtimer->irq.irq, 1068 842 kvm_arch_timer_get_input_level); 843 + if (ret) 844 + return ret; 845 + 846 + if (map.direct_ptimer) { 847 + ret = kvm_vgic_map_phys_irq(vcpu, 848 + map.direct_ptimer->host_timer_irq, 849 + map.direct_ptimer->irq.irq, 850 + kvm_arch_timer_get_input_level); 851 + } 852 + 1069 853 if (ret) 1070 854 return ret; 1071 855 ··· 1089 845 } 1090 846 1091 847 /* 1092 - * On VHE system, we only need to configure trap on physical timer and counter 1093 - * accesses in EL0 and EL1 once, not for every world switch. 848 + * On VHE system, we only need to configure the EL2 timer trap register once, 849 + * not for every world switch. 1094 850 * The host kernel runs at EL2 with HCR_EL2.TGE == 1, 1095 851 * and this makes those bits have no effect for the host kernel execution. 1096 852 */ ··· 1101 857 u64 val; 1102 858 1103 859 /* 1104 - * Disallow physical timer access for the guest. 1105 - * Physical counter access is allowed. 860 + * VHE systems allow the guest direct access to the EL1 physical 861 + * timer/counter. 1106 862 */ 1107 863 val = read_sysreg(cnthctl_el2); 1108 - val &= ~(CNTHCTL_EL1PCEN << cnthctl_shift); 864 + val |= (CNTHCTL_EL1PCEN << cnthctl_shift); 1109 865 val |= (CNTHCTL_EL1PCTEN << cnthctl_shift); 1110 866 write_sysreg(val, cnthctl_el2); 1111 867 }
+23 -41
virt/kvm/arm/arm.c
··· 65 65 /* The VMID used in the VTTBR */ 66 66 static atomic64_t kvm_vmid_gen = ATOMIC64_INIT(1); 67 67 static u32 kvm_next_vmid; 68 - static unsigned int kvm_vmid_bits __read_mostly; 69 68 static DEFINE_SPINLOCK(kvm_vmid_lock); 70 69 71 70 static bool vgic_present; ··· 141 142 kvm_vgic_early_init(kvm); 142 143 143 144 /* Mark the initial VMID generation invalid */ 144 - kvm->arch.vmid_gen = 0; 145 + kvm->arch.vmid.vmid_gen = 0; 145 146 146 147 /* The maximum number of VCPUs is limited by the host's GIC model */ 147 148 kvm->arch.max_vcpus = vgic_present ? ··· 335 336 336 337 void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu) 337 338 { 338 - kvm_timer_schedule(vcpu); 339 339 kvm_vgic_v4_enable_doorbell(vcpu); 340 340 } 341 341 342 342 void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu) 343 343 { 344 - kvm_timer_unschedule(vcpu); 345 344 kvm_vgic_v4_disable_doorbell(vcpu); 346 345 } 347 346 ··· 469 472 470 473 /** 471 474 * need_new_vmid_gen - check that the VMID is still valid 472 - * @kvm: The VM's VMID to check 475 + * @vmid: The VMID to check 473 476 * 474 477 * return true if there is a new generation of VMIDs being used 475 478 * 476 - * The hardware supports only 256 values with the value zero reserved for the 477 - * host, so we check if an assigned value belongs to a previous generation, 478 - * which which requires us to assign a new value. If we're the first to use a 479 - * VMID for the new generation, we must flush necessary caches and TLBs on all 480 - * CPUs. 479 + * The hardware supports a limited set of values with the value zero reserved 480 + * for the host, so we check if an assigned value belongs to a previous 481 + * generation, which which requires us to assign a new value. If we're the 482 + * first to use a VMID for the new generation, we must flush necessary caches 483 + * and TLBs on all CPUs. 481 484 */ 482 - static bool need_new_vmid_gen(struct kvm *kvm) 485 + static bool need_new_vmid_gen(struct kvm_vmid *vmid) 483 486 { 484 487 u64 current_vmid_gen = atomic64_read(&kvm_vmid_gen); 485 488 smp_rmb(); /* Orders read of kvm_vmid_gen and kvm->arch.vmid */ 486 - return unlikely(READ_ONCE(kvm->arch.vmid_gen) != current_vmid_gen); 489 + return unlikely(READ_ONCE(vmid->vmid_gen) != current_vmid_gen); 487 490 } 488 491 489 492 /** 490 - * update_vttbr - Update the VTTBR with a valid VMID before the guest runs 491 - * @kvm The guest that we are about to run 492 - * 493 - * Called from kvm_arch_vcpu_ioctl_run before entering the guest to ensure the 494 - * VM has a valid VMID, otherwise assigns a new one and flushes corresponding 495 - * caches and TLBs. 493 + * update_vmid - Update the vmid with a valid VMID for the current generation 494 + * @kvm: The guest that struct vmid belongs to 495 + * @vmid: The stage-2 VMID information struct 496 496 */ 497 - static void update_vttbr(struct kvm *kvm) 497 + static void update_vmid(struct kvm_vmid *vmid) 498 498 { 499 - phys_addr_t pgd_phys; 500 - u64 vmid, cnp = kvm_cpu_has_cnp() ? VTTBR_CNP_BIT : 0; 501 - 502 - if (!need_new_vmid_gen(kvm)) 499 + if (!need_new_vmid_gen(vmid)) 503 500 return; 504 501 505 502 spin_lock(&kvm_vmid_lock); ··· 503 512 * already allocated a valid vmid for this vm, then this vcpu should 504 513 * use the same vmid. 505 514 */ 506 - if (!need_new_vmid_gen(kvm)) { 515 + if (!need_new_vmid_gen(vmid)) { 507 516 spin_unlock(&kvm_vmid_lock); 508 517 return; 509 518 } ··· 527 536 kvm_call_hyp(__kvm_flush_vm_context); 528 537 } 529 538 530 - kvm->arch.vmid = kvm_next_vmid; 539 + vmid->vmid = kvm_next_vmid; 531 540 kvm_next_vmid++; 532 - kvm_next_vmid &= (1 << kvm_vmid_bits) - 1; 533 - 534 - /* update vttbr to be used with the new vmid */ 535 - pgd_phys = virt_to_phys(kvm->arch.pgd); 536 - BUG_ON(pgd_phys & ~kvm_vttbr_baddr_mask(kvm)); 537 - vmid = ((u64)(kvm->arch.vmid) << VTTBR_VMID_SHIFT) & VTTBR_VMID_MASK(kvm_vmid_bits); 538 - kvm->arch.vttbr = kvm_phys_to_vttbr(pgd_phys) | vmid | cnp; 541 + kvm_next_vmid &= (1 << kvm_get_vmid_bits()) - 1; 539 542 540 543 smp_wmb(); 541 - WRITE_ONCE(kvm->arch.vmid_gen, atomic64_read(&kvm_vmid_gen)); 544 + WRITE_ONCE(vmid->vmid_gen, atomic64_read(&kvm_vmid_gen)); 542 545 543 546 spin_unlock(&kvm_vmid_lock); 544 547 } ··· 675 690 */ 676 691 cond_resched(); 677 692 678 - update_vttbr(vcpu->kvm); 693 + update_vmid(&vcpu->kvm->arch.vmid); 679 694 680 695 check_vcpu_requests(vcpu); 681 696 ··· 724 739 */ 725 740 smp_store_mb(vcpu->mode, IN_GUEST_MODE); 726 741 727 - if (ret <= 0 || need_new_vmid_gen(vcpu->kvm) || 742 + if (ret <= 0 || need_new_vmid_gen(&vcpu->kvm->arch.vmid) || 728 743 kvm_request_pending(vcpu)) { 729 744 vcpu->mode = OUTSIDE_GUEST_MODE; 730 745 isb(); /* Ensure work in x_flush_hwstate is committed */ ··· 750 765 ret = kvm_vcpu_run_vhe(vcpu); 751 766 kvm_arm_vhe_guest_exit(); 752 767 } else { 753 - ret = kvm_call_hyp(__kvm_vcpu_run_nvhe, vcpu); 768 + ret = kvm_call_hyp_ret(__kvm_vcpu_run_nvhe, vcpu); 754 769 } 755 770 756 771 vcpu->mode = OUTSIDE_GUEST_MODE; ··· 1402 1417 1403 1418 static int init_common_resources(void) 1404 1419 { 1405 - /* set size of VMID supported by CPU */ 1406 - kvm_vmid_bits = kvm_get_vmid_bits(); 1407 - kvm_info("%d-bit VMID\n", kvm_vmid_bits); 1408 - 1409 1420 kvm_set_ipa_limit(); 1410 1421 1411 1422 return 0; ··· 1542 1561 kvm_cpu_context_t *cpu_ctxt; 1543 1562 1544 1563 cpu_ctxt = per_cpu_ptr(&kvm_host_cpu_state, cpu); 1564 + kvm_init_host_cpu_context(cpu_ctxt, cpu); 1545 1565 err = create_hyp_mappings(cpu_ctxt, cpu_ctxt + 1, PAGE_HYP); 1546 1566 1547 1567 if (err) { ··· 1553 1571 1554 1572 err = hyp_map_aux_data(); 1555 1573 if (err) 1556 - kvm_err("Cannot map host auxilary data: %d\n", err); 1574 + kvm_err("Cannot map host auxiliary data: %d\n", err); 1557 1575 1558 1576 return 0; 1559 1577
+1 -1
virt/kvm/arm/hyp/vgic-v3-sr.c
··· 226 226 int i; 227 227 u32 elrsr; 228 228 229 - elrsr = read_gicreg(ICH_ELSR_EL2); 229 + elrsr = read_gicreg(ICH_ELRSR_EL2); 230 230 231 231 write_gicreg(cpu_if->vgic_hcr & ~ICH_HCR_EN, ICH_HCR_EL2); 232 232
+8 -10
virt/kvm/arm/mmu.c
··· 908 908 */ 909 909 int kvm_alloc_stage2_pgd(struct kvm *kvm) 910 910 { 911 + phys_addr_t pgd_phys; 911 912 pgd_t *pgd; 912 913 913 914 if (kvm->arch.pgd != NULL) { ··· 921 920 if (!pgd) 922 921 return -ENOMEM; 923 922 923 + pgd_phys = virt_to_phys(pgd); 924 + if (WARN_ON(pgd_phys & ~kvm_vttbr_baddr_mask(kvm))) 925 + return -EINVAL; 926 + 924 927 kvm->arch.pgd = pgd; 928 + kvm->arch.pgd_phys = pgd_phys; 925 929 return 0; 926 930 } 927 931 ··· 1014 1008 unmap_stage2_range(kvm, 0, kvm_phys_size(kvm)); 1015 1009 pgd = READ_ONCE(kvm->arch.pgd); 1016 1010 kvm->arch.pgd = NULL; 1011 + kvm->arch.pgd_phys = 0; 1017 1012 } 1018 1013 spin_unlock(&kvm->mmu_lock); 1019 1014 ··· 1403 1396 return false; 1404 1397 } 1405 1398 1406 - static bool kvm_is_write_fault(struct kvm_vcpu *vcpu) 1407 - { 1408 - if (kvm_vcpu_trap_is_iabt(vcpu)) 1409 - return false; 1410 - 1411 - return kvm_vcpu_dabt_iswrite(vcpu); 1412 - } 1413 - 1414 1399 /** 1415 1400 * stage2_wp_ptes - write protect PMD range 1416 1401 * @pmd: pointer to pmd entry ··· 1597 1598 static bool fault_supports_stage2_pmd_mappings(struct kvm_memory_slot *memslot, 1598 1599 unsigned long hva) 1599 1600 { 1600 - gpa_t gpa_start, gpa_end; 1601 + gpa_t gpa_start; 1601 1602 hva_t uaddr_start, uaddr_end; 1602 1603 size_t size; 1603 1604 1604 1605 size = memslot->npages * PAGE_SIZE; 1605 1606 1606 1607 gpa_start = memslot->base_gfn << PAGE_SHIFT; 1607 - gpa_end = gpa_start + size; 1608 1608 1609 1609 uaddr_start = memslot->userspace_addr; 1610 1610 uaddr_end = uaddr_start + size;
+106 -1
virt/kvm/arm/trace.h
··· 2 2 #if !defined(_TRACE_KVM_H) || defined(TRACE_HEADER_MULTI_READ) 3 3 #define _TRACE_KVM_H 4 4 5 + #include <kvm/arm_arch_timer.h> 5 6 #include <linux/tracepoint.h> 6 7 7 8 #undef TRACE_SYSTEM ··· 263 262 __entry->vcpu_id, __entry->irq, __entry->level) 264 263 ); 265 264 265 + TRACE_EVENT(kvm_get_timer_map, 266 + TP_PROTO(unsigned long vcpu_id, struct timer_map *map), 267 + TP_ARGS(vcpu_id, map), 268 + 269 + TP_STRUCT__entry( 270 + __field( unsigned long, vcpu_id ) 271 + __field( int, direct_vtimer ) 272 + __field( int, direct_ptimer ) 273 + __field( int, emul_ptimer ) 274 + ), 275 + 276 + TP_fast_assign( 277 + __entry->vcpu_id = vcpu_id; 278 + __entry->direct_vtimer = arch_timer_ctx_index(map->direct_vtimer); 279 + __entry->direct_ptimer = 280 + (map->direct_ptimer) ? arch_timer_ctx_index(map->direct_ptimer) : -1; 281 + __entry->emul_ptimer = 282 + (map->emul_ptimer) ? arch_timer_ctx_index(map->emul_ptimer) : -1; 283 + ), 284 + 285 + TP_printk("VCPU: %ld, dv: %d, dp: %d, ep: %d", 286 + __entry->vcpu_id, 287 + __entry->direct_vtimer, 288 + __entry->direct_ptimer, 289 + __entry->emul_ptimer) 290 + ); 291 + 292 + TRACE_EVENT(kvm_timer_save_state, 293 + TP_PROTO(struct arch_timer_context *ctx), 294 + TP_ARGS(ctx), 295 + 296 + TP_STRUCT__entry( 297 + __field( unsigned long, ctl ) 298 + __field( unsigned long long, cval ) 299 + __field( int, timer_idx ) 300 + ), 301 + 302 + TP_fast_assign( 303 + __entry->ctl = ctx->cnt_ctl; 304 + __entry->cval = ctx->cnt_cval; 305 + __entry->timer_idx = arch_timer_ctx_index(ctx); 306 + ), 307 + 308 + TP_printk(" CTL: %#08lx CVAL: %#16llx arch_timer_ctx_index: %d", 309 + __entry->ctl, 310 + __entry->cval, 311 + __entry->timer_idx) 312 + ); 313 + 314 + TRACE_EVENT(kvm_timer_restore_state, 315 + TP_PROTO(struct arch_timer_context *ctx), 316 + TP_ARGS(ctx), 317 + 318 + TP_STRUCT__entry( 319 + __field( unsigned long, ctl ) 320 + __field( unsigned long long, cval ) 321 + __field( int, timer_idx ) 322 + ), 323 + 324 + TP_fast_assign( 325 + __entry->ctl = ctx->cnt_ctl; 326 + __entry->cval = ctx->cnt_cval; 327 + __entry->timer_idx = arch_timer_ctx_index(ctx); 328 + ), 329 + 330 + TP_printk("CTL: %#08lx CVAL: %#16llx arch_timer_ctx_index: %d", 331 + __entry->ctl, 332 + __entry->cval, 333 + __entry->timer_idx) 334 + ); 335 + 336 + TRACE_EVENT(kvm_timer_hrtimer_expire, 337 + TP_PROTO(struct arch_timer_context *ctx), 338 + TP_ARGS(ctx), 339 + 340 + TP_STRUCT__entry( 341 + __field( int, timer_idx ) 342 + ), 343 + 344 + TP_fast_assign( 345 + __entry->timer_idx = arch_timer_ctx_index(ctx); 346 + ), 347 + 348 + TP_printk("arch_timer_ctx_index: %d", __entry->timer_idx) 349 + ); 350 + 351 + TRACE_EVENT(kvm_timer_emulate, 352 + TP_PROTO(struct arch_timer_context *ctx, bool should_fire), 353 + TP_ARGS(ctx, should_fire), 354 + 355 + TP_STRUCT__entry( 356 + __field( int, timer_idx ) 357 + __field( bool, should_fire ) 358 + ), 359 + 360 + TP_fast_assign( 361 + __entry->timer_idx = arch_timer_ctx_index(ctx); 362 + __entry->should_fire = should_fire; 363 + ), 364 + 365 + TP_printk("arch_timer_ctx_index: %d (should_fire: %d)", 366 + __entry->timer_idx, __entry->should_fire) 367 + ); 368 + 266 369 #endif /* _TRACE_KVM_H */ 267 370 268 371 #undef TRACE_INCLUDE_PATH 269 - #define TRACE_INCLUDE_PATH ../../../virt/kvm/arm 372 + #define TRACE_INCLUDE_PATH ../../virt/kvm/arm 270 373 #undef TRACE_INCLUDE_FILE 271 374 #define TRACE_INCLUDE_FILE trace 272 375
+2 -2
virt/kvm/arm/vgic/vgic-v3.c
··· 589 589 */ 590 590 int vgic_v3_probe(const struct gic_kvm_info *info) 591 591 { 592 - u32 ich_vtr_el2 = kvm_call_hyp(__vgic_v3_get_ich_vtr_el2); 592 + u32 ich_vtr_el2 = kvm_call_hyp_ret(__vgic_v3_get_ich_vtr_el2); 593 593 int ret; 594 594 595 595 /* ··· 679 679 struct vgic_v3_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v3; 680 680 681 681 if (likely(cpu_if->vgic_sre)) 682 - cpu_if->vgic_vmcr = kvm_call_hyp(__vgic_v3_read_vmcr); 682 + cpu_if->vgic_vmcr = kvm_call_hyp_ret(__vgic_v3_read_vmcr); 683 683 684 684 kvm_call_hyp(__vgic_v3_save_aprs, vcpu); 685 685