18a43a2b3SChristoffer Dall /* 28a43a2b3SChristoffer Dall * Copyright (C) 2012-2015 - ARM Ltd 38a43a2b3SChristoffer Dall * Author: Marc Zyngier <marc.zyngier@arm.com> 48a43a2b3SChristoffer Dall * 58a43a2b3SChristoffer Dall * This program is free software; you can redistribute it and/or modify 68a43a2b3SChristoffer Dall * it under the terms of the GNU General Public License version 2 as 78a43a2b3SChristoffer Dall * published by the Free Software Foundation. 88a43a2b3SChristoffer Dall * 98a43a2b3SChristoffer Dall * This program is distributed in the hope that it will be useful, 108a43a2b3SChristoffer Dall * but WITHOUT ANY WARRANTY; without even the implied warranty of 118a43a2b3SChristoffer Dall * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 128a43a2b3SChristoffer Dall * GNU General Public License for more details. 138a43a2b3SChristoffer Dall * 148a43a2b3SChristoffer Dall * You should have received a copy of the GNU General Public License 158a43a2b3SChristoffer Dall * along with this program. If not, see <http://www.gnu.org/licenses/>. 168a43a2b3SChristoffer Dall */ 178a43a2b3SChristoffer Dall 188a43a2b3SChristoffer Dall #include <linux/compiler.h> 198a43a2b3SChristoffer Dall #include <linux/irqchip/arm-gic.h> 208a43a2b3SChristoffer Dall #include <linux/kvm_host.h> 21b220244dSJames Morse #include <linux/swab.h> 228a43a2b3SChristoffer Dall 238a43a2b3SChristoffer Dall #include <asm/kvm_emulate.h> 248a43a2b3SChristoffer Dall #include <asm/kvm_hyp.h> 258a43a2b3SChristoffer Dall #include <asm/kvm_mmu.h> 268a43a2b3SChristoffer Dall 27b220244dSJames Morse static bool __hyp_text __is_be(struct kvm_vcpu *vcpu) 28b220244dSJames Morse { 29b220244dSJames Morse if (vcpu_mode_is_32bit(vcpu)) 30256c0960SMark Rutland return !!(read_sysreg_el2(spsr) & PSR_AA32_E_BIT); 31b220244dSJames Morse 32b220244dSJames Morse return !!(read_sysreg(SCTLR_EL1) & SCTLR_ELx_EE); 33b220244dSJames Morse } 34b220244dSJames Morse 358a43a2b3SChristoffer Dall /* 368a43a2b3SChristoffer Dall * __vgic_v2_perform_cpuif_access -- perform a GICV access on behalf of the 378a43a2b3SChristoffer Dall * guest. 388a43a2b3SChristoffer Dall * 398a43a2b3SChristoffer Dall * @vcpu: the offending vcpu 408a43a2b3SChristoffer Dall * 418a43a2b3SChristoffer Dall * Returns: 428a43a2b3SChristoffer Dall * 1: GICV access successfully performed 438a43a2b3SChristoffer Dall * 0: Not a GICV access 44*bd7d95caSMark Rutland * -1: Illegal GICV access successfully performed 458a43a2b3SChristoffer Dall */ 468a43a2b3SChristoffer Dall int __hyp_text __vgic_v2_perform_cpuif_access(struct kvm_vcpu *vcpu) 478a43a2b3SChristoffer Dall { 488a43a2b3SChristoffer Dall struct kvm *kvm = kern_hyp_va(vcpu->kvm); 498a43a2b3SChristoffer Dall struct vgic_dist *vgic = &kvm->arch.vgic; 508a43a2b3SChristoffer Dall phys_addr_t fault_ipa; 518a43a2b3SChristoffer Dall void __iomem *addr; 528a43a2b3SChristoffer Dall int rd; 538a43a2b3SChristoffer Dall 548a43a2b3SChristoffer Dall /* Build the full address */ 558a43a2b3SChristoffer Dall fault_ipa = kvm_vcpu_get_fault_ipa(vcpu); 568a43a2b3SChristoffer Dall fault_ipa |= kvm_vcpu_get_hfar(vcpu) & GENMASK(11, 0); 578a43a2b3SChristoffer Dall 588a43a2b3SChristoffer Dall /* If not for GICV, move on */ 598a43a2b3SChristoffer Dall if (fault_ipa < vgic->vgic_cpu_base || 608a43a2b3SChristoffer Dall fault_ipa >= (vgic->vgic_cpu_base + KVM_VGIC_V2_CPU_SIZE)) 618a43a2b3SChristoffer Dall return 0; 628a43a2b3SChristoffer Dall 638a43a2b3SChristoffer Dall /* Reject anything but a 32bit access */ 64*bd7d95caSMark Rutland if (kvm_vcpu_dabt_get_as(vcpu) != sizeof(u32)) { 65*bd7d95caSMark Rutland __kvm_skip_instr(vcpu); 668a43a2b3SChristoffer Dall return -1; 67*bd7d95caSMark Rutland } 688a43a2b3SChristoffer Dall 698a43a2b3SChristoffer Dall /* Not aligned? Don't bother */ 70*bd7d95caSMark Rutland if (fault_ipa & 3) { 71*bd7d95caSMark Rutland __kvm_skip_instr(vcpu); 728a43a2b3SChristoffer Dall return -1; 73*bd7d95caSMark Rutland } 748a43a2b3SChristoffer Dall 758a43a2b3SChristoffer Dall rd = kvm_vcpu_dabt_get_rd(vcpu); 761bb32a44SMarc Zyngier addr = hyp_symbol_addr(kvm_vgic_global_state)->vcpu_hyp_va; 778a43a2b3SChristoffer Dall addr += fault_ipa - vgic->vgic_cpu_base; 788a43a2b3SChristoffer Dall 798a43a2b3SChristoffer Dall if (kvm_vcpu_dabt_iswrite(vcpu)) { 80b220244dSJames Morse u32 data = vcpu_get_reg(vcpu, rd); 81b220244dSJames Morse if (__is_be(vcpu)) { 82b220244dSJames Morse /* guest pre-swabbed data, undo this for writel() */ 83b220244dSJames Morse data = swab32(data); 84b220244dSJames Morse } 858a43a2b3SChristoffer Dall writel_relaxed(data, addr); 868a43a2b3SChristoffer Dall } else { 878a43a2b3SChristoffer Dall u32 data = readl_relaxed(addr); 88b220244dSJames Morse if (__is_be(vcpu)) { 89b220244dSJames Morse /* guest expects swabbed data */ 90b220244dSJames Morse data = swab32(data); 91b220244dSJames Morse } 92b220244dSJames Morse vcpu_set_reg(vcpu, rd, data); 938a43a2b3SChristoffer Dall } 948a43a2b3SChristoffer Dall 95*bd7d95caSMark Rutland __kvm_skip_instr(vcpu); 96*bd7d95caSMark Rutland 978a43a2b3SChristoffer Dall return 1; 988a43a2b3SChristoffer Dall } 99