From patchwork Wed Nov 23 13:01:53 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vijay Kilari X-Patchwork-Id: 83658 Delivered-To: patch@linaro.org Received: by 10.140.97.165 with SMTP id m34csp2624981qge; Wed, 23 Nov 2016 05:06:06 -0800 (PST) X-Received: by 10.99.178.89 with SMTP id t25mr4922943pgo.131.1479906365986; Wed, 23 Nov 2016 05:06:05 -0800 (PST) Return-Path: Received: from bombadil.infradead.org (bombadil.infradead.org. [2001:1868:205::9]) by mx.google.com with ESMTPS id 15si33709513pfk.17.2016.11.23.05.06.05 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 23 Nov 2016 05:06:05 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-arm-kernel-bounces+patch=linaro.org@lists.infradead.org designates 2001:1868:205::9 as permitted sender) client-ip=2001:1868:205::9; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com; spf=pass (google.com: best guess record for domain of linux-arm-kernel-bounces+patch=linaro.org@lists.infradead.org designates 2001:1868:205::9 as permitted sender) smtp.mailfrom=linux-arm-kernel-bounces+patch=linaro.org@lists.infradead.org; dmarc=fail (p=NONE dis=NONE) header.from=gmail.com Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.85_2 #1 (Red Hat Linux)) id 1c9XEF-00051h-7e; Wed, 23 Nov 2016 13:04:47 +0000 Received: from mail-pf0-x243.google.com ([2607:f8b0:400e:c00::243]) by bombadil.infradead.org with esmtps (Exim 4.85_2 #1 (Red Hat Linux)) id 1c9XCI-0003zc-LO for linux-arm-kernel@lists.infradead.org; Wed, 23 Nov 2016 13:02:53 +0000 Received: by mail-pf0-x243.google.com with SMTP id y68so703964pfb.1 for ; Wed, 23 Nov 2016 05:02:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=xjLdc/MTf/239SJkRrXph+xwu3vHggayaHnsS+/kXns=; b=JLcnD9cwIZcbsf96CEA0zS2p6gadTE1sK8E5DOXvYZA/GHarY1lNpi/Wec2zw9xCJ6 Cuj6QwZARn3cY1TQOhUExeJqTQSzmPRWqKTx7l7zjv5ptWqz1q1x2wRIs9aTz7CVJynT xyJ/mP6aOTkSeFf3VTQyTH5OCO6VNOuFCSMAluRkciKR5NSyXolQOXai36P4CApwchgP EfgI2sw5N83tEj5N9Ra5bpVLHqS9S5HHquKy4MFsUfq6OacUyhmGUs/DiH+tarytZz2r xOj9Fxqd2m40lkZguqJXZXhN529GD1wfX9WaIVnjeFyl6D/p816+r8LL9sDoKTBkP/FC kz4A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=xjLdc/MTf/239SJkRrXph+xwu3vHggayaHnsS+/kXns=; b=VjGOx33WNNVeelHDlejLqGtwDU9c9lSWnQJJ/h/Zxk+HTIc/W0HF8NbxPu5vyw7AhS NJqA7D/G+Rn17HHBAutD1ZlrF4hAwTLYPGrIk+NMviochLldF6C2URDvVMKtoOSWEXtf wq7wWHFBmV7U6WPUpKVkUu5rsGBEnOLAL9SNiJWfjdkrjYWqBW+cFNgIExujXAWurDYE MJNavy9SpB4pzPkD1OWSWiNABEQgXRKR8OIYO23FVEP9ZQF2J7PNKaWTtHk6QHrGYarU fUPWbAXRsLmdjLr3uj/EXKzvt833/xriaSkNNN6vPWONjZhfumwuHoJhr9DLN+AVRbkU E7yA== X-Gm-Message-State: AKaTC02iwN085rL1ObAzoZd6BDcYNng02/ZLwHX8/39RMDbgw81V7T7QDEr5zNU3kZqnOg== X-Received: by 10.84.217.199 with SMTP id d7mr6400987plj.165.1479906147474; Wed, 23 Nov 2016 05:02:27 -0800 (PST) Received: from cavium-Vostro-2520.caveonetworks.com ([111.93.218.67]) by smtp.gmail.com with ESMTPSA id a24sm36838849pfh.57.2016.11.23.05.02.24 (version=TLS1_1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 23 Nov 2016 05:02:27 -0800 (PST) From: vijay.kilari@gmail.com To: marc.zyngier@arm.com, christoffer.dall@linaro.org, peter.maydell@linaro.org Subject: [PATCH v9 06/11] arm/arm64: vgic: Implement VGICv3 CPU interface access Date: Wed, 23 Nov 2016 18:31:53 +0530 Message-Id: <1479906118-15832-7-git-send-email-vijay.kilari@gmail.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1479906118-15832-1-git-send-email-vijay.kilari@gmail.com> References: <1479906118-15832-1-git-send-email-vijay.kilari@gmail.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20161123_050246_879002_1EFD66E0 X-CRM114-Status: GOOD ( 24.47 ) X-Spam-Score: -0.6 (/) X-Spam-Report: SpamAssassin version 3.4.0 on bombadil.infradead.org summary: Content analysis details: (-0.6 points) pts rule name description ---- ---------------------- -------------------------------------------------- 0.8 RCVD_IN_SORBS_WEB RBL: SORBS: sender is an abusable web server [111.93.218.67 listed in dnsbl.sorbs.net] -0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at http://www.dnswl.org/, low trust [2607:f8b0:400e:c00:0:0:0:243 listed in] [list.dnswl.org] 1.3 RCVD_IN_BL_SPAMCOP_NET RBL: Received via a relay in bl.spamcop.net [Blocked - see ] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider (vijay.kilari[at]gmail.com) -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: p.fedin@samsung.com, kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org, Vijaya Kumar K MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patch=linaro.org@lists.infradead.org From: Vijaya Kumar K VGICv3 CPU interface registers are accessed using KVM_DEV_ARM_VGIC_CPU_SYSREGS ioctl. These registers are accessed as 64-bit. The cpu MPIDR value is passed along with register id. is used to identify the cpu for registers access. The VM that supports SEIs expect it on destination machine to handle guest aborts and hence checked for ICC_CTLR_EL1.SEIS compatibility. Similarly, VM that supports Affinity Level 3 that is required for AArch64 mode, is required to be supported on destination machine. Hence checked for ICC_CTLR_EL1.A3V compatibility. The CPU system register handling is spitted into two files vgic-sys-reg-common.c and vgic-sys-reg-v3.c. The vgic-sys-reg-common.c handles read and write of VGIC CPU registers for both AArch64 and AArch32 mode. The vgic-sys-reg-v3.c handles AArch64 mode and is compiled only for AArch64 mode. Updated arch/arm/include/uapi/asm/kvm.h with new definitions required to compile for AArch32. The version of VGIC v3 specification is define here Documentation/virtual/kvm/devices/arm-vgic-v3.txt Signed-off-by: Pavel Fedin Signed-off-by: Vijaya Kumar K --- arch/arm/include/uapi/asm/kvm.h | 2 + arch/arm64/include/uapi/asm/kvm.h | 3 + arch/arm64/kvm/Makefile | 2 + include/kvm/arm_vgic.h | 9 ++ virt/kvm/arm/vgic/vgic-kvm-device.c | 28 ++++ virt/kvm/arm/vgic/vgic-mmio-v3.c | 18 +++ virt/kvm/arm/vgic/vgic-sys-reg-common.c | 258 ++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic-sys-reg-v3.c | 142 ++++++++++++++++++ virt/kvm/arm/vgic/vgic-v3.c | 8 + virt/kvm/arm/vgic/vgic.h | 22 +++ 10 files changed, 492 insertions(+) -- 1.9.1 _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel diff --git a/arch/arm/include/uapi/asm/kvm.h b/arch/arm/include/uapi/asm/kvm.h index 0ae6035..98658d9d 100644 --- a/arch/arm/include/uapi/asm/kvm.h +++ b/arch/arm/include/uapi/asm/kvm.h @@ -186,9 +186,11 @@ struct kvm_arch_memory_slot { (0xffffffffULL << KVM_DEV_ARM_VGIC_V3_MPIDR_SHIFT) #define KVM_DEV_ARM_VGIC_OFFSET_SHIFT 0 #define KVM_DEV_ARM_VGIC_OFFSET_MASK (0xffffffffULL << KVM_DEV_ARM_VGIC_OFFSET_SHIFT) +#define KVM_DEV_ARM_VGIC_SYSREG_INSTR_MASK (0xffff) #define KVM_DEV_ARM_VGIC_GRP_NR_IRQS 3 #define KVM_DEV_ARM_VGIC_GRP_CTRL 4 #define KVM_DEV_ARM_VGIC_GRP_REDIST_REGS 5 +#define KVM_DEV_ARM_VGIC_CPU_SYSREGS 6 #define KVM_DEV_ARM_VGIC_CTRL_INIT 0 /* KVM_IRQ_LINE irq field index values */ diff --git a/arch/arm64/include/uapi/asm/kvm.h b/arch/arm64/include/uapi/asm/kvm.h index 56dc08d..91c7137 100644 --- a/arch/arm64/include/uapi/asm/kvm.h +++ b/arch/arm64/include/uapi/asm/kvm.h @@ -206,9 +206,12 @@ struct kvm_arch_memory_slot { (0xffffffffULL << KVM_DEV_ARM_VGIC_V3_MPIDR_SHIFT) #define KVM_DEV_ARM_VGIC_OFFSET_SHIFT 0 #define KVM_DEV_ARM_VGIC_OFFSET_MASK (0xffffffffULL << KVM_DEV_ARM_VGIC_OFFSET_SHIFT) +#define KVM_DEV_ARM_VGIC_SYSREG_INSTR_MASK (0xffff) #define KVM_DEV_ARM_VGIC_GRP_NR_IRQS 3 #define KVM_DEV_ARM_VGIC_GRP_CTRL 4 #define KVM_DEV_ARM_VGIC_GRP_REDIST_REGS 5 +#define KVM_DEV_ARM_VGIC_CPU_SYSREGS 6 + #define KVM_DEV_ARM_VGIC_CTRL_INIT 0 /* Device Control API on vcpu fd */ diff --git a/arch/arm64/kvm/Makefile b/arch/arm64/kvm/Makefile index d50a82a..5c8580e 100644 --- a/arch/arm64/kvm/Makefile +++ b/arch/arm64/kvm/Makefile @@ -32,5 +32,7 @@ kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/vgic/vgic-mmio-v3.o kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/vgic/vgic-kvm-device.o kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/vgic/vgic-its.o kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/irqchip.o +kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/vgic/vgic-sys-reg-common.o +kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/vgic/vgic-sys-reg-v3.o kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/arch_timer.o kvm-$(CONFIG_KVM_ARM_PMU) += $(KVM)/arm/pmu.o diff --git a/include/kvm/arm_vgic.h b/include/kvm/arm_vgic.h index 002f092..730a18a 100644 --- a/include/kvm/arm_vgic.h +++ b/include/kvm/arm_vgic.h @@ -71,6 +71,9 @@ struct vgic_global { /* GIC system register CPU interface */ struct static_key_false gicv3_cpuif; + + /* Cache ICH_VTR_EL2 reg value */ + u32 ich_vtr_el2; }; extern struct vgic_global kvm_vgic_global_state; @@ -269,6 +272,12 @@ struct vgic_cpu { u64 pendbaser; bool lpis_enabled; + + /* Cache guest priority bits */ + u32 num_pri_bits; + + /* Cache guest interrupt ID bits */ + u32 num_id_bits; }; extern struct static_key_false vgic_v2_cpuif_trap; diff --git a/virt/kvm/arm/vgic/vgic-kvm-device.c b/virt/kvm/arm/vgic/vgic-kvm-device.c index bc7de95..b6266fe 100644 --- a/virt/kvm/arm/vgic/vgic-kvm-device.c +++ b/virt/kvm/arm/vgic/vgic-kvm-device.c @@ -16,6 +16,7 @@ #include #include #include +#include #include #include "vgic.h" @@ -501,6 +502,14 @@ static int vgic_v3_attr_regs_access(struct kvm_device *dev, if (!is_write) *reg = tmp32; break; + case KVM_DEV_ARM_VGIC_CPU_SYSREGS: { + u64 regid; + + regid = (attr->attr & KVM_DEV_ARM_VGIC_SYSREG_INSTR_MASK); + ret = vgic_v3_cpu_sysregs_uaccess(vcpu, is_write, + regid, reg); + break; + } default: ret = -EINVAL; break; @@ -534,6 +543,15 @@ static int vgic_v3_set_attr(struct kvm_device *dev, reg = tmp32; return vgic_v3_attr_regs_access(dev, attr, ®, true); } + case KVM_DEV_ARM_VGIC_CPU_SYSREGS: { + u64 __user *uaddr = (u64 __user *)(long)attr->addr; + u64 reg; + + if (get_user(reg, uaddr)) + return -EFAULT; + + return vgic_v3_attr_regs_access(dev, attr, ®, true); + } } return -ENXIO; } @@ -560,6 +578,15 @@ static int vgic_v3_get_attr(struct kvm_device *dev, tmp32 = reg; return put_user(tmp32, uaddr); } + case KVM_DEV_ARM_VGIC_CPU_SYSREGS: { + u64 __user *uaddr = (u64 __user *)(long)attr->addr; + u64 reg; + + ret = vgic_v3_attr_regs_access(dev, attr, ®, false); + if (ret) + return ret; + return put_user(reg, uaddr); + } } return -ENXIO; @@ -578,6 +605,7 @@ static int vgic_v3_has_attr(struct kvm_device *dev, break; case KVM_DEV_ARM_VGIC_GRP_DIST_REGS: case KVM_DEV_ARM_VGIC_GRP_REDIST_REGS: + case KVM_DEV_ARM_VGIC_CPU_SYSREGS: return vgic_v3_has_attr_regs(dev, attr); case KVM_DEV_ARM_VGIC_GRP_NR_IRQS: return 0; diff --git a/virt/kvm/arm/vgic/vgic-mmio-v3.c b/virt/kvm/arm/vgic/vgic-mmio-v3.c index 2a7cd62..2f7b4ed 100644 --- a/virt/kvm/arm/vgic/vgic-mmio-v3.c +++ b/virt/kvm/arm/vgic/vgic-mmio-v3.c @@ -641,6 +641,24 @@ int vgic_v3_has_attr_regs(struct kvm_device *dev, struct kvm_device_attr *attr) nr_regions = ARRAY_SIZE(vgic_v3_rdbase_registers); break; } + case KVM_DEV_ARM_VGIC_CPU_SYSREGS: { + u64 reg, id; + unsigned long vgic_mpidr, mpidr_reg; + struct kvm_vcpu *vcpu; + + vgic_mpidr = (attr->attr & KVM_DEV_ARM_VGIC_V3_MPIDR_MASK) >> + KVM_DEV_ARM_VGIC_V3_MPIDR_SHIFT; + + /* Convert plain mpidr value to MPIDR reg format */ + mpidr_reg = VGIC_TO_MPIDR(vgic_mpidr); + + vcpu = kvm_mpidr_to_vcpu(dev->kvm, mpidr_reg); + if (!vcpu) + return -EINVAL; + + id = (attr->attr & KVM_DEV_ARM_VGIC_SYSREG_INSTR_MASK); + return vgic_v3_has_cpu_sysregs_attr(vcpu, 0, id, ®); + } default: return -ENXIO; } diff --git a/virt/kvm/arm/vgic/vgic-sys-reg-common.c b/virt/kvm/arm/vgic/vgic-sys-reg-common.c new file mode 100644 index 0000000..a1fc370c --- /dev/null +++ b/virt/kvm/arm/vgic/vgic-sys-reg-common.c @@ -0,0 +1,258 @@ +/* + * VGIC system registers handling functions + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#include +#include +#include +#include +#include "vgic.h" + +bool access_gic_ctlr_reg(struct kvm_vcpu *vcpu, bool is_write, + unsigned long *reg) +{ + struct vgic_cpu *vgic_v3_cpu = &vcpu->arch.vgic_cpu; + struct vgic_vmcr vmcr; + u64 val; + u32 valid_bits, seis, a3v; + + vgic_get_vmcr(vcpu, &vmcr); + if (is_write) { + val = *reg; + + /* + * Disallow restoring VM state if not supported by this + * hardware. + */ + valid_bits = ((val & ICC_CTLR_EL1_PRI_BITS_MASK) >> + ICC_CTLR_EL1_PRI_BITS_SHIFT) + 1; + if (valid_bits > vgic_v3_cpu->num_pri_bits) + return false; + + vgic_v3_cpu->num_pri_bits = valid_bits; + + valid_bits = (val & ICC_CTLR_EL1_ID_BITS_MASK) >> + ICC_CTLR_EL1_ID_BITS_SHIFT; + if (valid_bits > vgic_v3_cpu->num_id_bits) + return false; + + vgic_v3_cpu->num_id_bits = valid_bits; + + valid_bits = ((kvm_vgic_global_state.ich_vtr_el2 & + ICH_VTR_SEIS_MASK) >> ICH_VTR_SEIS_SHIFT); + seis = (val & ICC_CTLR_EL1_SEIS_MASK) >> + ICC_CTLR_EL1_SEIS_SHIFT; + if (valid_bits != seis) + return false; + + valid_bits = ((kvm_vgic_global_state.ich_vtr_el2 & + ICH_VTR_A3V_MASK) >> ICH_VTR_A3V_SHIFT); + a3v = (val & ICC_CTLR_EL1_A3V_MASK) >> + ICC_CTLR_EL1_A3V_SHIFT; + if (valid_bits != a3v) + return false; + + vmcr.ctlr = (val & ICC_CTLR_EL1_CBPR_MASK); + vmcr.ctlr |= (val & ICC_CTLR_EL1_EOImode_MASK); + vgic_set_vmcr(vcpu, &vmcr); + } else { + val = 0; + val |= (vgic_v3_cpu->num_pri_bits - 1) << + ICC_CTLR_EL1_PRI_BITS_SHIFT; + val |= vgic_v3_cpu->num_id_bits << + ICC_CTLR_EL1_ID_BITS_SHIFT; + val |= ((kvm_vgic_global_state.ich_vtr_el2 & + ICH_VTR_SEIS_MASK) >> ICH_VTR_SEIS_SHIFT) << + ICC_CTLR_EL1_SEIS_SHIFT; + val |= ((kvm_vgic_global_state.ich_vtr_el2 & + ICH_VTR_A3V_MASK) >> ICH_VTR_A3V_SHIFT) << + ICC_CTLR_EL1_A3V_SHIFT; + val |= (vmcr.ctlr & ICC_CTLR_EL1_CBPR_MASK); + val |= (vmcr.ctlr & ICC_CTLR_EL1_EOImode_MASK); + + *reg = val; + } + + return true; +} + +bool access_gic_pmr_reg(struct kvm_vcpu *vcpu, bool is_write, + unsigned long *reg) +{ + struct vgic_vmcr vmcr; + + vgic_get_vmcr(vcpu, &vmcr); + if (is_write) { + vmcr.pmr = (*reg & ICC_PMR_EL1_MASK) >> ICC_PMR_EL1_SHIFT; + vgic_set_vmcr(vcpu, &vmcr); + } else { + *reg = (vmcr.pmr << ICC_PMR_EL1_SHIFT) & ICC_PMR_EL1_MASK; + } + + return true; +} + +bool access_gic_bpr0_reg(struct kvm_vcpu *vcpu, bool is_write, + unsigned long *reg) +{ + struct vgic_vmcr vmcr; + + vgic_get_vmcr(vcpu, &vmcr); + if (is_write) { + vmcr.bpr = (*reg & ICC_BPR0_EL1_MASK) >> + ICC_BPR0_EL1_SHIFT; + vgic_set_vmcr(vcpu, &vmcr); + } else { + *reg = (vmcr.bpr << ICC_BPR0_EL1_SHIFT) & ICC_BPR0_EL1_MASK; + } + + return true; +} + +bool access_gic_bpr1_reg(struct kvm_vcpu *vcpu, bool is_write, + unsigned long *reg) +{ + struct vgic_vmcr vmcr; + + if (!is_write) + *reg = 0; + + vgic_get_vmcr(vcpu, &vmcr); + if (!((vmcr.ctlr & ICH_VMCR_CBPR_MASK) >> ICH_VMCR_CBPR_SHIFT)) { + if (is_write) { + vmcr.abpr = (*reg & ICC_BPR1_EL1_MASK) >> + ICC_BPR1_EL1_SHIFT; + vgic_set_vmcr(vcpu, &vmcr); + } else { + *reg = (vmcr.abpr << ICC_BPR1_EL1_SHIFT) & + ICC_BPR1_EL1_MASK; + } + } else { + if (!is_write) + *reg = min((vmcr.bpr + 1), 7U); + } + + return true; +} + +bool access_gic_grpen0_reg(struct kvm_vcpu *vcpu, bool is_write, + unsigned long *reg) +{ + struct vgic_vmcr vmcr; + + vgic_get_vmcr(vcpu, &vmcr); + if (is_write) { + vmcr.grpen0 = (*reg & ICC_IGRPEN0_EL1_MASK) >> + ICC_IGRPEN0_EL1_SHIFT; + vgic_set_vmcr(vcpu, &vmcr); + } else { + *reg = (vmcr.grpen0 << ICC_IGRPEN0_EL1_SHIFT) & + ICC_IGRPEN0_EL1_MASK; + } + + return true; +} + +bool access_gic_grpen1_reg(struct kvm_vcpu *vcpu, bool is_write, + unsigned long *reg) +{ + struct vgic_vmcr vmcr; + + vgic_get_vmcr(vcpu, &vmcr); + if (is_write) { + vmcr.grpen1 = (*reg & ICC_IGRPEN1_EL1_MASK) >> + ICC_IGRPEN1_EL1_SHIFT; + vgic_set_vmcr(vcpu, &vmcr); + } else { + *reg = (vmcr.grpen1 << ICC_IGRPEN1_EL1_SHIFT) & + ICC_IGRPEN1_EL1_MASK; + } + + return true; +} + +static void vgic_v3_access_apr_reg(struct kvm_vcpu *vcpu, bool is_write, + u8 apr, u8 idx, unsigned long *reg) +{ + struct vgic_v3_cpu_if *vgicv3 = &vcpu->arch.vgic_cpu.vgic_v3; + uint32_t *ap_reg; + + if (apr) + ap_reg = &vgicv3->vgic_ap1r[idx]; + else + ap_reg = &vgicv3->vgic_ap0r[idx]; + + if (is_write) + *ap_reg = *reg; + else + *reg = *ap_reg; +} + +static bool access_gic_aprn(struct kvm_vcpu *vcpu, bool is_write, u8 apr, + u8 idx, unsigned long *reg) +{ + struct vgic_cpu *vgic_v3_cpu = &vcpu->arch.vgic_cpu; + + /* num_pri_bits are initialized with HW supported values. + * We can rely safely on num_pri_bits even if VM has not + * restored ICC_CTLR_EL1 before restoring APnR registers. + */ + switch (vgic_v3_cpu->num_pri_bits) { + case 7: + vgic_v3_access_apr_reg(vcpu, is_write, apr, idx, reg); + break; + case 6: + if (idx > 1) + goto err; + vgic_v3_access_apr_reg(vcpu, is_write, apr, idx, reg); + break; + default: + if (idx > 0) + goto err; + vgic_v3_access_apr_reg(vcpu, is_write, apr, idx, reg); + } + + return true; +err: + if (!is_write) + *reg = 0; + + return false; +} + +bool access_gic_ap0r_reg(struct kvm_vcpu *vcpu, bool is_write, u8 idx, + unsigned long *reg) +{ + return access_gic_aprn(vcpu, is_write, 0, idx, reg); +} + +bool access_gic_ap1r_reg(struct kvm_vcpu *vcpu, bool is_write, u8 idx, + unsigned long *reg) +{ + return access_gic_aprn(vcpu, is_write, 1, idx, reg); +} + +bool access_gic_sre_reg(struct kvm_vcpu *vcpu, bool is_write, + unsigned long *reg) +{ + struct vgic_v3_cpu_if *vgicv3 = &vcpu->arch.vgic_cpu.vgic_v3; + + /* Validate SRE bit */ + if (is_write) { + if (!(*reg & ICC_SRE_EL1_SRE)) + return false; + } else { + *reg = vgicv3->vgic_sre; + } + + return true; +} diff --git a/virt/kvm/arm/vgic/vgic-sys-reg-v3.c b/virt/kvm/arm/vgic/vgic-sys-reg-v3.c new file mode 100644 index 0000000..82c2f02 --- /dev/null +++ b/virt/kvm/arm/vgic/vgic-sys-reg-v3.c @@ -0,0 +1,142 @@ +/* + * VGIC system registers handling functions + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#include +#include +#include +#include "vgic.h" +#include "sys_regs.h" + +#define ACCESS_SYS_REG(REG) \ +static bool access_gic_##REG##_sys_reg(struct kvm_vcpu *vcpu, \ + struct sys_reg_params *p, \ + const struct sys_reg_desc *r) \ +{ \ + unsigned long tmp; \ + bool ret; \ + \ + if (p->is_write) \ + tmp = p->regval; \ + ret = access_gic_##REG##_reg(vcpu, p->is_write, &tmp); \ + if (!p->is_write) \ + p->regval = tmp; \ + \ + return ret; \ +} + +ACCESS_SYS_REG(ctlr) +ACCESS_SYS_REG(pmr) +ACCESS_SYS_REG(bpr0) +ACCESS_SYS_REG(bpr1) +ACCESS_SYS_REG(sre) +ACCESS_SYS_REG(grpen0) +ACCESS_SYS_REG(grpen1) + +#define ACCESS_APNR_SYS_REG(REG) \ +static bool access_gic_##REG##_sys_reg(struct kvm_vcpu *vcpu, \ + struct sys_reg_params *p, \ + const struct sys_reg_desc *r) \ +{ \ + unsigned long tmp; \ + u8 idx = p->Op2 & 3; \ + bool ret; \ + \ + if (p->is_write) \ + tmp = p->regval; \ + ret = access_gic_##REG##_reg(vcpu, p->is_write, idx, &tmp); \ + if (!p->is_write) \ + p->regval = tmp; \ + \ + return ret; \ +} + +ACCESS_APNR_SYS_REG(ap0r) +ACCESS_APNR_SYS_REG(ap1r) + +static const struct sys_reg_desc gic_v3_icc_reg_descs[] = { + /* ICC_PMR_EL1 */ + { Op0(3), Op1(0), CRn(4), CRm(6), Op2(0), access_gic_pmr_sys_reg }, + /* ICC_BPR0_EL1 */ + { Op0(3), Op1(0), CRn(12), CRm(8), Op2(3), access_gic_bpr0_sys_reg }, + /* ICC_AP0R0_EL1 */ + { Op0(3), Op1(0), CRn(12), CRm(8), Op2(4), access_gic_ap0r_sys_reg }, + /* ICC_AP0R1_EL1 */ + { Op0(3), Op1(0), CRn(12), CRm(8), Op2(5), access_gic_ap0r_sys_reg }, + /* ICC_AP0R2_EL1 */ + { Op0(3), Op1(0), CRn(12), CRm(8), Op2(6), access_gic_ap0r_sys_reg }, + /* ICC_AP0R3_EL1 */ + { Op0(3), Op1(0), CRn(12), CRm(8), Op2(7), access_gic_ap0r_sys_reg }, + /* ICC_AP1R0_EL1 */ + { Op0(3), Op1(0), CRn(12), CRm(9), Op2(0), access_gic_ap1r_sys_reg }, + /* ICC_AP1R1_EL1 */ + { Op0(3), Op1(0), CRn(12), CRm(9), Op2(1), access_gic_ap1r_sys_reg }, + /* ICC_AP1R2_EL1 */ + { Op0(3), Op1(0), CRn(12), CRm(9), Op2(2), access_gic_ap1r_sys_reg }, + /* ICC_AP1R3_EL1 */ + { Op0(3), Op1(0), CRn(12), CRm(9), Op2(3), access_gic_ap1r_sys_reg }, + /* ICC_BPR1_EL1 */ + { Op0(3), Op1(0), CRn(12), CRm(12), Op2(3), access_gic_bpr1_sys_reg }, + /* ICC_CTLR_EL1 */ + { Op0(3), Op1(0), CRn(12), CRm(12), Op2(4), access_gic_ctlr_sys_reg }, + /* ICC_SRE_EL1 */ + { Op0(3), Op1(0), CRn(12), CRm(12), Op2(5), access_gic_sre_sys_reg }, + /* ICC_IGRPEN0_EL1 */ + { Op0(3), Op1(0), CRn(12), CRm(12), Op2(6), access_gic_grpen0_sys_reg }, + /* ICC_GRPEN1_EL1 */ + { Op0(3), Op1(0), CRn(12), CRm(12), Op2(7), access_gic_grpen1_sys_reg }, +}; + +int vgic_v3_has_cpu_sysregs_attr(struct kvm_vcpu *vcpu, bool is_write, u64 id, + u64 *reg) +{ + struct sys_reg_params params; + u64 sysreg = (id & KVM_DEV_ARM_VGIC_SYSREG_MASK) | KVM_REG_SIZE_U64; + + params.regval = *reg; + params.is_write = is_write; + params.is_aarch32 = false; + params.is_32bit = false; + + if (find_reg_by_id(sysreg, ¶ms, gic_v3_icc_reg_descs, + ARRAY_SIZE(gic_v3_icc_reg_descs))) + return 0; + + return -ENXIO; +} + +int vgic_v3_cpu_sysregs_uaccess(struct kvm_vcpu *vcpu, bool is_write, u64 id, + u64 *reg) +{ + struct sys_reg_params params; + const struct sys_reg_desc *r; + u64 sysreg = (id & KVM_DEV_ARM_VGIC_SYSREG_MASK) | KVM_REG_SIZE_U64; + + if (is_write) + params.regval = *reg; + params.is_write = is_write; + params.is_aarch32 = false; + params.is_32bit = false; + + r = find_reg_by_id(sysreg, ¶ms, gic_v3_icc_reg_descs, + ARRAY_SIZE(gic_v3_icc_reg_descs)); + if (!r) + return -ENXIO; + + if (!r->access(vcpu, ¶ms, r)) + return -EINVAL; + + if (!is_write) + *reg = params.regval; + + return 0; +} diff --git a/virt/kvm/arm/vgic/vgic-v3.c b/virt/kvm/arm/vgic/vgic-v3.c index a3ff04b..6e7400e 100644 --- a/virt/kvm/arm/vgic/vgic-v3.c +++ b/virt/kvm/arm/vgic/vgic-v3.c @@ -240,6 +240,13 @@ void vgic_v3_enable(struct kvm_vcpu *vcpu) vgic_v3->vgic_sre = 0; } + vcpu->arch.vgic_cpu.num_id_bits = (kvm_vgic_global_state.ich_vtr_el2 & + ICH_VTR_ID_BITS_MASK) >> + ICH_VTR_ID_BITS_SHIFT; + vcpu->arch.vgic_cpu.num_pri_bits = ((kvm_vgic_global_state.ich_vtr_el2 & + ICH_VTR_PRI_BITS_MASK) >> + ICH_VTR_PRI_BITS_SHIFT) + 1; + /* Get the show on the road... */ vgic_v3->vgic_hcr = ICH_HCR_EN; } @@ -340,6 +347,7 @@ int vgic_v3_probe(const struct gic_kvm_info *info) */ kvm_vgic_global_state.nr_lr = (ich_vtr_el2 & 0xf) + 1; kvm_vgic_global_state.can_emulate_gicv2 = false; + kvm_vgic_global_state.ich_vtr_el2 = ich_vtr_el2; if (!info->vcpu.start) { kvm_info("GICv3: no GICV resource entry\n"); diff --git a/virt/kvm/arm/vgic/vgic.h b/virt/kvm/arm/vgic/vgic.h index 9232791..af23399 100644 --- a/virt/kvm/arm/vgic/vgic.h +++ b/virt/kvm/arm/vgic/vgic.h @@ -140,6 +140,28 @@ int vgic_v3_dist_uaccess(struct kvm_vcpu *vcpu, bool is_write, int offset, u32 *val); int vgic_v3_redist_uaccess(struct kvm_vcpu *vcpu, bool is_write, int offset, u32 *val); +int vgic_v3_cpu_sysregs_uaccess(struct kvm_vcpu *vcpu, bool is_write, + u64 id, u64 *val); +int vgic_v3_has_cpu_sysregs_attr(struct kvm_vcpu *vcpu, bool is_write, u64 id, + u64 *reg); +bool access_gic_ctlr_reg(struct kvm_vcpu *vcpu, bool is_write, + unsigned long *reg); +bool access_gic_pmr_reg(struct kvm_vcpu *vcpu, bool is_write, + unsigned long *reg); +bool access_gic_bpr0_reg(struct kvm_vcpu *vcpu, bool is_write, + unsigned long *reg); +bool access_gic_bpr1_reg(struct kvm_vcpu *vcpu, bool is_write, + unsigned long *reg); +bool access_gic_grpen0_reg(struct kvm_vcpu *vcpu, bool is_write, + unsigned long *reg); +bool access_gic_grpen1_reg(struct kvm_vcpu *vcpu, bool is_write, + unsigned long *reg); +bool access_gic_ap0r_reg(struct kvm_vcpu *vcpu, bool is_write, + u8 idx, unsigned long *reg); +bool access_gic_ap1r_reg(struct kvm_vcpu *vcpu, bool is_write, + u8 idx, unsigned long *reg); +bool access_gic_sre_reg(struct kvm_vcpu *vcpu, bool is_write, + unsigned long *reg); int kvm_register_vgic_device(unsigned long type); void vgic_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr); void vgic_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr);