| 1 | // SPDX-License-Identifier: GPL-2.0-only |
|---|---|
| 2 | /* |
| 3 | * Copyright (C) 2012-2015 - ARM Ltd |
| 4 | * Author: Marc Zyngier <marc.zyngier@arm.com> |
| 5 | */ |
| 6 | |
| 7 | #include <hyp/adjust_pc.h> |
| 8 | |
| 9 | #include <linux/compiler.h> |
| 10 | #include <linux/irqchip/arm-gic.h> |
| 11 | #include <linux/kvm_host.h> |
| 12 | #include <linux/swab.h> |
| 13 | |
| 14 | #include <asm/kvm_emulate.h> |
| 15 | #include <asm/kvm_hyp.h> |
| 16 | #include <asm/kvm_mmu.h> |
| 17 | |
| 18 | static bool __is_be(struct kvm_vcpu *vcpu) |
| 19 | { |
| 20 | if (vcpu_mode_is_32bit(vcpu)) |
| 21 | return !!(read_sysreg_el2(SYS_SPSR) & PSR_AA32_E_BIT); |
| 22 | |
| 23 | return !!(read_sysreg_el1(SYS_SCTLR) & SCTLR_ELx_EE); |
| 24 | } |
| 25 | |
| 26 | /* |
| 27 | * __vgic_v2_perform_cpuif_access -- perform a GICV access on behalf of the |
| 28 | * guest. |
| 29 | * |
| 30 | * @vcpu: the offending vcpu |
| 31 | * |
| 32 | * Returns: |
| 33 | * 1: GICV access successfully performed |
| 34 | * 0: Not a GICV access |
| 35 | * -1: Illegal GICV access successfully performed |
| 36 | */ |
| 37 | int __vgic_v2_perform_cpuif_access(struct kvm_vcpu *vcpu) |
| 38 | { |
| 39 | struct kvm *kvm = kern_hyp_va(vcpu->kvm); |
| 40 | struct vgic_dist *vgic = &kvm->arch.vgic; |
| 41 | phys_addr_t fault_ipa; |
| 42 | void __iomem *addr; |
| 43 | int rd; |
| 44 | |
| 45 | /* Build the full address */ |
| 46 | fault_ipa = kvm_vcpu_get_fault_ipa(vcpu); |
| 47 | fault_ipa |= kvm_vcpu_get_hfar(vcpu) & GENMASK(11, 0); |
| 48 | |
| 49 | /* If not for GICV, move on */ |
| 50 | if (fault_ipa < vgic->vgic_cpu_base || |
| 51 | fault_ipa >= (vgic->vgic_cpu_base + KVM_VGIC_V2_CPU_SIZE)) |
| 52 | return 0; |
| 53 | |
| 54 | /* Reject anything but a 32bit access */ |
| 55 | if (kvm_vcpu_dabt_get_as(vcpu) != sizeof(u32)) { |
| 56 | __kvm_skip_instr(vcpu); |
| 57 | return -1; |
| 58 | } |
| 59 | |
| 60 | /* Not aligned? Don't bother */ |
| 61 | if (fault_ipa & 3) { |
| 62 | __kvm_skip_instr(vcpu); |
| 63 | return -1; |
| 64 | } |
| 65 | |
| 66 | /* Handle deactivation as a normal exit */ |
| 67 | if ((fault_ipa - vgic->vgic_cpu_base) >= GIC_CPU_DEACTIVATE) |
| 68 | return 0; |
| 69 | |
| 70 | rd = kvm_vcpu_dabt_get_rd(vcpu); |
| 71 | addr = kvm_vgic_global_state.vcpu_hyp_va; |
| 72 | addr += fault_ipa - vgic->vgic_cpu_base; |
| 73 | |
| 74 | if (kvm_vcpu_dabt_iswrite(vcpu)) { |
| 75 | u32 data = vcpu_get_reg(vcpu, rd); |
| 76 | if (__is_be(vcpu)) { |
| 77 | /* guest pre-swabbed data, undo this for writel() */ |
| 78 | data = __kvm_swab32(data); |
| 79 | } |
| 80 | writel_relaxed(data, addr); |
| 81 | } else { |
| 82 | u32 data = readl_relaxed(addr); |
| 83 | if (__is_be(vcpu)) { |
| 84 | /* guest expects swabbed data */ |
| 85 | data = __kvm_swab32(data); |
| 86 | } |
| 87 | vcpu_set_reg(vcpu, rd, data); |
| 88 | } |
| 89 | |
| 90 | __kvm_skip_instr(vcpu); |
| 91 | |
| 92 | return 1; |
| 93 | } |
| 94 |
