Marc Zyngier | 06282fd | 2015-10-19 15:50:37 +0100 | [diff] [blame] | 1 | /* |
| 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> |
James Morse | b220244 | 2018-05-04 16:19:24 +0100 | [diff] [blame] | 21 | #include <linux/swab.h> |
Marc Zyngier | 06282fd | 2015-10-19 15:50:37 +0100 | [diff] [blame] | 22 | |
Marc Zyngier | bf8feb3 | 2016-09-06 09:28:46 +0100 | [diff] [blame] | 23 | #include <asm/kvm_emulate.h> |
Marc Zyngier | 13720a5 | 2016-01-28 13:44:07 +0000 | [diff] [blame] | 24 | #include <asm/kvm_hyp.h> |
Marc Zyngier | d681198 | 2017-10-23 17:11:14 +0100 | [diff] [blame] | 25 | #include <asm/kvm_mmu.h> |
Marc Zyngier | 06282fd | 2015-10-19 15:50:37 +0100 | [diff] [blame] | 26 | |
James Morse | b220244 | 2018-05-04 16:19:24 +0100 | [diff] [blame] | 27 | static bool __hyp_text __is_be(struct kvm_vcpu *vcpu) |
| 28 | { |
| 29 | if (vcpu_mode_is_32bit(vcpu)) |
Mark Rutland | 256c096 | 2018-07-05 15:16:53 +0100 | [diff] [blame^] | 30 | return !!(read_sysreg_el2(spsr) & PSR_AA32_E_BIT); |
James Morse | b220244 | 2018-05-04 16:19:24 +0100 | [diff] [blame] | 31 | |
| 32 | return !!(read_sysreg(SCTLR_EL1) & SCTLR_ELx_EE); |
| 33 | } |
| 34 | |
Marc Zyngier | 3272f0d | 2016-09-06 14:02:17 +0100 | [diff] [blame] | 35 | /* |
| 36 | * __vgic_v2_perform_cpuif_access -- perform a GICV access on behalf of the |
| 37 | * guest. |
| 38 | * |
| 39 | * @vcpu: the offending vcpu |
| 40 | * |
| 41 | * Returns: |
| 42 | * 1: GICV access successfully performed |
| 43 | * 0: Not a GICV access |
| 44 | * -1: Illegal GICV access |
| 45 | */ |
| 46 | int __hyp_text __vgic_v2_perform_cpuif_access(struct kvm_vcpu *vcpu) |
Marc Zyngier | fb5ee36 | 2016-09-06 09:28:45 +0100 | [diff] [blame] | 47 | { |
Marc Zyngier | bf8feb3 | 2016-09-06 09:28:46 +0100 | [diff] [blame] | 48 | struct kvm *kvm = kern_hyp_va(vcpu->kvm); |
| 49 | struct vgic_dist *vgic = &kvm->arch.vgic; |
| 50 | phys_addr_t fault_ipa; |
| 51 | void __iomem *addr; |
| 52 | int rd; |
| 53 | |
| 54 | /* Build the full address */ |
| 55 | fault_ipa = kvm_vcpu_get_fault_ipa(vcpu); |
| 56 | fault_ipa |= kvm_vcpu_get_hfar(vcpu) & GENMASK(11, 0); |
| 57 | |
| 58 | /* If not for GICV, move on */ |
| 59 | if (fault_ipa < vgic->vgic_cpu_base || |
| 60 | fault_ipa >= (vgic->vgic_cpu_base + KVM_VGIC_V2_CPU_SIZE)) |
Marc Zyngier | 3272f0d | 2016-09-06 14:02:17 +0100 | [diff] [blame] | 61 | return 0; |
Marc Zyngier | bf8feb3 | 2016-09-06 09:28:46 +0100 | [diff] [blame] | 62 | |
| 63 | /* Reject anything but a 32bit access */ |
| 64 | if (kvm_vcpu_dabt_get_as(vcpu) != sizeof(u32)) |
Marc Zyngier | 3272f0d | 2016-09-06 14:02:17 +0100 | [diff] [blame] | 65 | return -1; |
Marc Zyngier | bf8feb3 | 2016-09-06 09:28:46 +0100 | [diff] [blame] | 66 | |
| 67 | /* Not aligned? Don't bother */ |
| 68 | if (fault_ipa & 3) |
Marc Zyngier | 3272f0d | 2016-09-06 14:02:17 +0100 | [diff] [blame] | 69 | return -1; |
Marc Zyngier | bf8feb3 | 2016-09-06 09:28:46 +0100 | [diff] [blame] | 70 | |
| 71 | rd = kvm_vcpu_dabt_get_rd(vcpu); |
Marc Zyngier | 1bb32a4 | 2017-12-04 16:43:23 +0000 | [diff] [blame] | 72 | addr = hyp_symbol_addr(kvm_vgic_global_state)->vcpu_hyp_va; |
Marc Zyngier | bf8feb3 | 2016-09-06 09:28:46 +0100 | [diff] [blame] | 73 | addr += fault_ipa - vgic->vgic_cpu_base; |
| 74 | |
| 75 | if (kvm_vcpu_dabt_iswrite(vcpu)) { |
James Morse | b220244 | 2018-05-04 16:19:24 +0100 | [diff] [blame] | 76 | u32 data = vcpu_get_reg(vcpu, rd); |
| 77 | if (__is_be(vcpu)) { |
| 78 | /* guest pre-swabbed data, undo this for writel() */ |
| 79 | data = swab32(data); |
| 80 | } |
Marc Zyngier | bf8feb3 | 2016-09-06 09:28:46 +0100 | [diff] [blame] | 81 | writel_relaxed(data, addr); |
| 82 | } else { |
| 83 | u32 data = readl_relaxed(addr); |
James Morse | b220244 | 2018-05-04 16:19:24 +0100 | [diff] [blame] | 84 | if (__is_be(vcpu)) { |
| 85 | /* guest expects swabbed data */ |
| 86 | data = swab32(data); |
| 87 | } |
| 88 | vcpu_set_reg(vcpu, rd, data); |
Marc Zyngier | bf8feb3 | 2016-09-06 09:28:46 +0100 | [diff] [blame] | 89 | } |
| 90 | |
Marc Zyngier | 3272f0d | 2016-09-06 14:02:17 +0100 | [diff] [blame] | 91 | return 1; |
Marc Zyngier | fb5ee36 | 2016-09-06 09:28:45 +0100 | [diff] [blame] | 92 | } |