blob: 29ee1feba4eb7de12d55d8c8a99f64e2577e73e5 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-only
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * Copyright (C) 2012-2015 - ARM Ltd
4 * Author: Marc Zyngier <marc.zyngier@arm.com>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005 */
6
7#include <linux/compiler.h>
8#include <linux/irqchip/arm-gic.h>
9#include <linux/kvm_host.h>
10#include <linux/swab.h>
11
12#include <asm/kvm_emulate.h>
13#include <asm/kvm_hyp.h>
14#include <asm/kvm_mmu.h>
15
16static bool __hyp_text __is_be(struct kvm_vcpu *vcpu)
17{
18 if (vcpu_mode_is_32bit(vcpu))
David Brazdil0f672f62019-12-10 10:32:29 +000019 return !!(read_sysreg_el2(SYS_SPSR) & PSR_AA32_E_BIT);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000020
21 return !!(read_sysreg(SCTLR_EL1) & SCTLR_ELx_EE);
22}
23
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
David Brazdil0f672f62019-12-10 10:32:29 +000033 * -1: Illegal GICV access successfully performed
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000034 */
35int __hyp_text __vgic_v2_perform_cpuif_access(struct kvm_vcpu *vcpu)
36{
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))
50 return 0;
51
52 /* Reject anything but a 32bit access */
David Brazdil0f672f62019-12-10 10:32:29 +000053 if (kvm_vcpu_dabt_get_as(vcpu) != sizeof(u32)) {
54 __kvm_skip_instr(vcpu);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000055 return -1;
David Brazdil0f672f62019-12-10 10:32:29 +000056 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000057
58 /* Not aligned? Don't bother */
David Brazdil0f672f62019-12-10 10:32:29 +000059 if (fault_ipa & 3) {
60 __kvm_skip_instr(vcpu);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000061 return -1;
David Brazdil0f672f62019-12-10 10:32:29 +000062 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000063
64 rd = kvm_vcpu_dabt_get_rd(vcpu);
65 addr = hyp_symbol_addr(kvm_vgic_global_state)->vcpu_hyp_va;
66 addr += fault_ipa - vgic->vgic_cpu_base;
67
68 if (kvm_vcpu_dabt_iswrite(vcpu)) {
69 u32 data = vcpu_get_reg(vcpu, rd);
70 if (__is_be(vcpu)) {
71 /* guest pre-swabbed data, undo this for writel() */
72 data = swab32(data);
73 }
74 writel_relaxed(data, addr);
75 } else {
76 u32 data = readl_relaxed(addr);
77 if (__is_be(vcpu)) {
78 /* guest expects swabbed data */
79 data = swab32(data);
80 }
81 vcpu_set_reg(vcpu, rd, data);
82 }
83
David Brazdil0f672f62019-12-10 10:32:29 +000084 __kvm_skip_instr(vcpu);
85
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000086 return 1;
87}