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