blob: 86801b6055d6dc714a1bb98640caee9985965092 [file] [log] [blame]
Marc Zyngier06282fd2015-10-19 15:50:37 +01001/*
2 * Copyright (C) 2012-2015 - ARM Ltd
3 * Author: Marc Zyngier <marc.zyngier@arm.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include <linux/compiler.h>
19#include <linux/irqchip/arm-gic.h>
20#include <linux/kvm_host.h>
21
Marc Zyngierbf8feb32016-09-06 09:28:46 +010022#include <asm/kvm_emulate.h>
Marc Zyngier13720a52016-01-28 13:44:07 +000023#include <asm/kvm_hyp.h>
Marc Zyngierd6811982017-10-23 17:11:14 +010024#include <asm/kvm_mmu.h>
Marc Zyngier06282fd2015-10-19 15:50:37 +010025
Marc Zyngier3272f0d2016-09-06 14:02:17 +010026/*
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
36 */
37int __hyp_text __vgic_v2_perform_cpuif_access(struct kvm_vcpu *vcpu)
Marc Zyngierfb5ee362016-09-06 09:28:45 +010038{
Marc Zyngierbf8feb32016-09-06 09:28:46 +010039 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))
Marc Zyngier3272f0d2016-09-06 14:02:17 +010052 return 0;
Marc Zyngierbf8feb32016-09-06 09:28:46 +010053
54 /* Reject anything but a 32bit access */
55 if (kvm_vcpu_dabt_get_as(vcpu) != sizeof(u32))
Marc Zyngier3272f0d2016-09-06 14:02:17 +010056 return -1;
Marc Zyngierbf8feb32016-09-06 09:28:46 +010057
58 /* Not aligned? Don't bother */
59 if (fault_ipa & 3)
Marc Zyngier3272f0d2016-09-06 14:02:17 +010060 return -1;
Marc Zyngierbf8feb32016-09-06 09:28:46 +010061
62 rd = kvm_vcpu_dabt_get_rd(vcpu);
Marc Zyngier1bb32a42017-12-04 16:43:23 +000063 addr = hyp_symbol_addr(kvm_vgic_global_state)->vcpu_hyp_va;
Marc Zyngierbf8feb32016-09-06 09:28:46 +010064 addr += fault_ipa - vgic->vgic_cpu_base;
65
66 if (kvm_vcpu_dabt_iswrite(vcpu)) {
67 u32 data = vcpu_data_guest_to_host(vcpu,
68 vcpu_get_reg(vcpu, rd),
69 sizeof(u32));
70 writel_relaxed(data, addr);
71 } else {
72 u32 data = readl_relaxed(addr);
73 vcpu_set_reg(vcpu, rd, vcpu_data_host_to_guest(vcpu, data,
74 sizeof(u32)));
75 }
76
Marc Zyngier3272f0d2016-09-06 14:02:17 +010077 return 1;
Marc Zyngierfb5ee362016-09-06 09:28:45 +010078}