Skip to contentSkip to conversation

arm/arm64: vgic: Implement API for vGICv3 live migration

PATCH Superseded [email protected] 5 participants 34 messages Dec 1, 2016 – Jan 23, 2017

Subsystems (5)

v10 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

v10 cover arm/arm64: vgic: Implement API for vGICv3 live migration

From: Vijaya Kumar K <[email protected]>

This patchset adds API for saving and restoring
of VGICv3 registers to support live migration with new vgic feature. This API definition is as per version of VGICv3 specification Documentation/virtual/kvm/devices/arm-vgic-v3.txt

The patch 3 & 4 are picked from the Pavel's previous implementation.
http://www.spinics.net/lists/kvm/msg122040.html

NOTE: Only compilation tested for AArch32. No hardware to test.

v9 => v10:
- Dropped support for AArch32 mode.
- Fixed line level update
- Updated documentation
- Moved vgic-sys-reg-v3.c to arch/arm64/kvm/ and added vgic-v3-coproc.c to arch/arm/kvm for AArch32
- Fixed nits

v8 => v9:
- Rebased to kvmarm/next branch
- Introduce support for save and restore of CPU interface registers for AArch32 mode (9,10 and 11 patches). Only compilation tested.
- Fixed vmcr.ctlr format
- Updated error code for invalid CPU REG value in Documentation
- Updated commit messages and added comments required
- Queued IRQ when irq_line is set.
- Compatibility check on ICC_CTLR_EL1.SEIS and A3V

v7 => v8:
- Rebased to 4.9-rc3
- Fixed wrong parameter to VGIC_TO_MPIDR
v6 => v7:
- Rename all patches heading from vgic-new to vgic
- Moved caching of priority and ID bits from vgic global struct to vgic_cpu struct.

v5 => v6:
- Collated all register definitions to single patch (4)
- Introduce macro to convert userspace MPIDR format to MPIDR reg format
- Check on ICC_CTLR_EL1.CBPR value is made while accessing ICC_BPR1_EL1
- Cached ich_vtr_el2 and guests priority and ID bits
- Check on number of priority and ID bits when ICC_CTRL_EL1 write is made
- Check is made on SRE bit for ICC_SRE_EL1 write

v4 => v5:
- ICC_CTLR_EL1 access is updated to reflect HW values
- Updated ICC reg access mask and shift macros
- Introduced patch 4 for VMCR changes
- Other minor fixes.
v3 => v4:
- Rebased to latest code base
- Moved vgic_uaccess() from vgic-mmio-v2.c to vgic-mmio.c
- Dropped macro REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED_UACCESS
- Dropped LE conversion for userspace access
- Introduced vgic_uaccess_write_pending() for ISPENDR write
- Change macro KVM_DEV_ARM_VGIC_V3_CPUID_MASK to KVM_DEV_ARM_VGIC_V3_MIDR_MASK
- Refactored some code as common code.
- Changed handing of ICC_* registers
- Allowed ICC_SRE_EL1 read by userspace
- Fixed KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_* macros

v2 => v3:
- Implemented separate API for ISPENDR and ICPENDR to
read soft_pending instead of pending for level triggerred interrupts
- Implemented ioctl KVM_DEV_ARM_VGIC_GRP_LEVEL_INFO to access line level
- Rebased on top of Christoffer's patch set
http://www.spinics.net/lists/kvm/msg136840.html

NOTE: GICD_STATUSR and GICR_STATUSR are implemented as RAZ/WI.

v1 => v2:
- The init sequence change patch is no more required.
Fixed in patch 2 by using static vgic_io_dev regions structure instead of using dynamic allocation pointer.
- Updated commit message of patch 4.
- Dropped usage of union to manage 32-bit and 64-bit access in patch 1. Used local variable for 32-bit access.
- Updated macro __ARM64_SYS_REG and ARM64_SYS_REG in arch/arm64/include/uapi/asm/kvm.h as per qemu requirements.

Vijaya Kumar K (8):
arm/arm64: vgic: Implement support for userspace access arm/arm64: vgic: Add distributor and redistributor access arm/arm64: vgic: Introduce find_reg_by_id() irqchip/gic-v3: Add missing system register definitions arm/arm64: vgic: Introduce VENG0 and VENG1 fields to vmcr struct arm/arm64: vgic: Implement VGICv3 CPU interface access arm/arm64: vgic: Implement KVM_DEV_ARM_VGIC_GRP_LEVEL_INFO ioctl arm/arm64: Documentation: Update arm-vgic-v3.txt

 Documentation/virtual/kvm/devices/arm-vgic-v3.txt |   9 +-
 arch/arm/include/uapi/asm/kvm.h                   |  13 +
 arch/arm/kvm/Makefile                             |   4 +-
 arch/arm/kvm/vgic-v3-coproc.c                     |  35 +++
 arch/arm64/include/uapi/asm/kvm.h                 |  13 +
 arch/arm64/kvm/Makefile                           |   3 +-
 arch/arm64/kvm/sys_regs.c                         |  22 +-
 arch/arm64/kvm/sys_regs.h                         |   4 +
 arch/arm64/kvm/vgic-sys-reg-v3.c                  | 338 ++++++++++++++++++++++
 include/kvm/arm_vgic.h                            |   9 +
 include/linux/irqchip/arm-gic-v3.h                |  45 ++-
 virt/kvm/arm/vgic/vgic-kvm-device.c               | 215 +++++++++++++-
 virt/kvm/arm/vgic/vgic-mmio-v2.c                  |  57 +---
 virt/kvm/arm/vgic/vgic-mmio-v3.c                  | 204 +++++++++++--
 virt/kvm/arm/vgic/vgic-mmio.c                     | 162 ++++++++++-
 virt/kvm/arm/vgic/vgic-mmio.h                     |  28 ++
 virt/kvm/arm/vgic/vgic-v3.c                       |  28 +-
 virt/kvm/arm/vgic/vgic.h                          |  60 +++-
 18 files changed, 1144 insertions(+), 105 deletions(-)
 create mode 100644 arch/arm/kvm/vgic-v3-coproc.c
 create mode 100644 arch/arm64/kvm/vgic-sys-reg-v3.c
Signature · 1 line
1.9.1
#1 lore raw

PATCH v10 1/8 arm/arm64: vgic: Implement support for userspace access

From: Vijaya Kumar K <[email protected]>

Read and write of some registers like ISPENDR and ICPENDR from userspace requires special handling when compared to guest access for these registers.

Refer to Documentation/virtual/kvm/devices/arm-vgic-v3.txt for handling of ISPENDR, ICPENDR registers handling.

Add infrastructure to support guest and userspace read and write for the required registers
Also moved vgic_uaccess from vgic-mmio-v2.c to vgic-mmio.c

---
 virt/kvm/arm/vgic/vgic-mmio-v2.c |  25 ----------
 virt/kvm/arm/vgic/vgic-mmio-v3.c | 103 ++++++++++++++++++++++++++++++++-------
 virt/kvm/arm/vgic/vgic-mmio.c    |  78 ++++++++++++++++++++++++++---
 virt/kvm/arm/vgic/vgic-mmio.h    |  19 ++++++++
 4 files changed, 176 insertions(+), 49 deletions(-)
virt/kvm/arm/vgic/vgic-mmio-v2.c +0 -25
index b44b359..0b32f40 100644
@@ -406,31 +406,6 @@ int vgic_v2_has_attr_regs(struct kvm_device *dev, struct kvm_device_attr *attr)
 	return -ENXIO;
 }
 
-/*
- * When userland tries to access the VGIC register handlers, we need to
- * create a usable struct vgic_io_device to be passed to the handlers and we
- * have to set up a buffer similar to what would have happened if a guest MMIO
- * access occurred, including doing endian conversions on BE systems.
- */
-static int vgic_uaccess(struct kvm_vcpu *vcpu, struct vgic_io_device *dev,
-			bool is_write, int offset, u32 *val)
-{
-	unsigned int len = 4;
-	u8 buf[4];
-	int ret;
-
-	if (is_write) {
-		vgic_data_host_to_mmio_bus(buf, len, *val);
-		ret = kvm_io_gic_ops.write(vcpu, &dev->dev, offset, len, buf);
-	} else {
-		ret = kvm_io_gic_ops.read(vcpu, &dev->dev, offset, len, buf);
-		if (!ret)
-			*val = vgic_data_mmio_bus_to_host(buf, len);
-	}
-
-	return ret;
-}
-
 int vgic_v2_cpuif_uaccess(struct kvm_vcpu *vcpu, bool is_write,
 			  int offset, u32 *val)
 {
virt/kvm/arm/vgic/vgic-mmio-v3.c +85 -18

155 lines not on this page · raw message

virt/kvm/arm/vgic/vgic-mmio.c +72 -6

96 lines not on this page · raw message

virt/kvm/arm/vgic/vgic-mmio.h +19 -0
index 84961b4..7b30296 100644
@@ -34,6 +34,10 @@ struct vgic_register_region {
 				  gpa_t addr, unsigned int len,
 				  unsigned long val);
 	};
+	unsigned long (*uaccess_read)(struct kvm_vcpu *vcpu, gpa_t addr,
+				      unsigned int len);
+	void (*uaccess_write)(struct kvm_vcpu *vcpu, gpa_t addr,
+			      unsigned int len, unsigned long val);
 };
 
 extern struct kvm_io_device_ops kvm_io_gic_ops;
@@ -86,6 +90,18 @@ struct vgic_register_region {
 		.write = wr,						\
 	}
 
+#define REGISTER_DESC_WITH_LENGTH_UACCESS(off, rd, wr, urd, uwr, length, acc) \
+	{								\
+		.reg_offset = off,					\
+		.bits_per_irq = 0,					\
+		.len = length,						\
+		.access_flags = acc,					\
+		.read = rd,						\
+		.write = wr,						\
+		.uaccess_read = urd,					\
+		.uaccess_write = uwr,					\
+	}
+
 int kvm_vgic_register_mmio_region(struct kvm *kvm, struct kvm_vcpu *vcpu,
 				  struct vgic_register_region *reg_desc,
 				  struct vgic_io_device *region,
@@ -158,6 +174,9 @@ void vgic_mmio_write_config(struct kvm_vcpu *vcpu,
 			    gpa_t addr, unsigned int len,
 			    unsigned long val);
 
+int vgic_uaccess(struct kvm_vcpu *vcpu, struct vgic_io_device *dev,
+		 bool is_write, int offset, u32 *val);
+
 unsigned int vgic_v2_init_dist_iodev(struct vgic_io_device *dev);
 
 unsigned int vgic_v3_init_dist_iodev(struct vgic_io_device *dev);
Signature · 1 line
1.9.1
#2 lore raw

PATCH v10 2/8 arm/arm64: vgic: Add distributor and redistributor access

In reply to [email protected]

From: Vijaya Kumar K <[email protected]>

VGICv3 Distributor and Redistributor registers are accessed using KVM_DEV_ARM_VGIC_GRP_DIST_REGS and KVM_DEV_ARM_VGIC_GRP_REDIST_REGS with KVM_SET_DEVICE_ATTR and KVM_GET_DEVICE_ATTR ioctls. These registers are accessed as 32-bit and cpu mpidr value passed along with register offset is used to identify the cpu for redistributor registers access.

The version of VGIC v3 specification is define here Documentation/virtual/kvm/devices/arm-vgic-v3.txt

Also update arch/arm/include/uapi/asm/kvm.h to compile for AArch32 mode.

---
 arch/arm/include/uapi/asm/kvm.h     |   4 +
 arch/arm64/include/uapi/asm/kvm.h   |   4 +
 virt/kvm/arm/vgic/vgic-kvm-device.c | 144 ++++++++++++++++++++++++++++++++++--
 virt/kvm/arm/vgic/vgic-mmio-v2.c    |  16 +---
 virt/kvm/arm/vgic/vgic-mmio-v3.c    |  72 ++++++++++++++++++
 virt/kvm/arm/vgic/vgic-mmio.c       |  22 ++++++
 virt/kvm/arm/vgic/vgic-mmio.h       |   4 +
 virt/kvm/arm/vgic/vgic.h            |  49 +++++++++++-
 8 files changed, 292 insertions(+), 23 deletions(-)
arch/arm/include/uapi/asm/kvm.h +4 -0
index af05f8e..0ae6035 100644
@@ -181,10 +181,14 @@ struct kvm_arch_memory_slot {
 #define KVM_DEV_ARM_VGIC_GRP_CPU_REGS	2
 #define   KVM_DEV_ARM_VGIC_CPUID_SHIFT	32
 #define   KVM_DEV_ARM_VGIC_CPUID_MASK	(0xffULL << KVM_DEV_ARM_VGIC_CPUID_SHIFT)
+#define   KVM_DEV_ARM_VGIC_V3_MPIDR_SHIFT 32
+#define   KVM_DEV_ARM_VGIC_V3_MPIDR_MASK \
+			(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_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_CTRL_INIT    0
 
 /* KVM_IRQ_LINE irq field index values */
arch/arm64/include/uapi/asm/kvm.h +4 -0
index 3051f86..56dc08d 100644
@@ -201,10 +201,14 @@ struct kvm_arch_memory_slot {
 #define KVM_DEV_ARM_VGIC_GRP_CPU_REGS	2
 #define   KVM_DEV_ARM_VGIC_CPUID_SHIFT	32
 #define   KVM_DEV_ARM_VGIC_CPUID_MASK	(0xffULL << KVM_DEV_ARM_VGIC_CPUID_SHIFT)
+#define   KVM_DEV_ARM_VGIC_V3_MPIDR_SHIFT 32
+#define   KVM_DEV_ARM_VGIC_V3_MPIDR_MASK \
+			(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_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_CTRL_INIT	0
 
 /* Device Control API on vcpu fd */
virt/kvm/arm/vgic/vgic-kvm-device.c +136 -8

200 lines not on this page · raw message

virt/kvm/arm/vgic/vgic-mmio-v2.c +2 -14
index 0b32f40..2cb04b7 100644
@@ -368,10 +368,9 @@ unsigned int vgic_v2_init_dist_iodev(struct vgic_io_device *dev)
 
 int vgic_v2_has_attr_regs(struct kvm_device *dev, struct kvm_device_attr *attr)
 {
-	int nr_irqs = dev->kvm->arch.vgic.nr_spis + VGIC_NR_PRIVATE_IRQS;
 	const struct vgic_register_region *regions;
 	gpa_t addr;
-	int nr_regions, i, len;
+	int nr_regions;
 
 	addr = attr->attr & KVM_DEV_ARM_VGIC_OFFSET_MASK;
 
@@ -392,18 +391,7 @@ int vgic_v2_has_attr_regs(struct kvm_device *dev, struct kvm_device_attr *attr)
 	if (addr & 3)
 		return -ENXIO;
 
-	for (i = 0; i < nr_regions; i++) {
-		if (regions[i].bits_per_irq)
-			len = (regions[i].bits_per_irq * nr_irqs) / 8;
-		else
-			len = regions[i].len;
-
-		if (regions[i].reg_offset <= addr &&
-		    regions[i].reg_offset + len > addr)
-			return 0;
-	}
-
-	return -ENXIO;
+	return vgic_validate_mmio_region_addr(dev, regions, nr_regions, addr);
 }
 
 int vgic_v2_cpuif_uaccess(struct kvm_vcpu *vcpu, bool is_write,
virt/kvm/arm/vgic/vgic-mmio-v3.c +72 -0

105 lines not on this page · raw message

virt/kvm/arm/vgic/vgic-mmio.c +22 -0
index d5f3ee2..0d1bc98 100644
@@ -394,6 +394,28 @@ static int match_region(const void *key, const void *elt)
 		       sizeof(region[0]), match_region);
 }
 
+/* Check if address falls within the region */
+int vgic_validate_mmio_region_addr(struct kvm_device *dev,
+				   const struct vgic_register_region *regions,
+				   int nr_regions, gpa_t addr)
+{
+	int i, len;
+	int nr_irqs = dev->kvm->arch.vgic.nr_spis + VGIC_NR_PRIVATE_IRQS;
+
+	for (i = 0; i < nr_regions; i++) {
+		if (regions[i].bits_per_irq)
+			len = (regions[i].bits_per_irq * nr_irqs) / 8;
+		else
+			len = regions[i].len;
+
+		if (regions[i].reg_offset <= addr &&
+		    regions[i].reg_offset + len > addr)
+			return 0;
+	}
+
+	return -ENXIO;
+}
+
 /*
  * kvm_mmio_read_buf() returns a value in a format where it can be converted
  * to a byte array and be directly observed as the guest wanted it to appear
virt/kvm/arm/vgic/vgic-mmio.h +4 -0
index 7b30296..1cc7faf 100644
@@ -177,6 +177,10 @@ void vgic_mmio_write_config(struct kvm_vcpu *vcpu,
 int vgic_uaccess(struct kvm_vcpu *vcpu, struct vgic_io_device *dev,
 		 bool is_write, int offset, u32 *val);
 
+int vgic_validate_mmio_region_addr(struct kvm_device *dev,
+				   const struct vgic_register_region *regions,
+				   int nr_regions, gpa_t addr);
+
 unsigned int vgic_v2_init_dist_iodev(struct vgic_io_device *dev);
 
 unsigned int vgic_v3_init_dist_iodev(struct vgic_io_device *dev);
virt/kvm/arm/vgic/vgic.h +48 -1

61 lines not on this page · raw message

Signature · 1 line
1.9.1
#3 lore raw

PATCH v10 3/8 arm/arm64: vgic: Introduce find_reg_by_id()

In reply to [email protected]

From: Vijaya Kumar K <[email protected]>

In order to implement vGICv3 CPU interface access, we will need to perform table lookup of system registers. We would need both index_to_params() and find_reg() exported for that purpose, but instead we export a single function which combines them both.

---
 arch/arm64/kvm/sys_regs.c | 22 +++++++++++++++-------
 arch/arm64/kvm/sys_regs.h |  4 ++++
 2 files changed, 19 insertions(+), 7 deletions(-)
arch/arm64/kvm/sys_regs.c +15 -7
index f302fdb..1330d4c 100644
@@ -1789,6 +1789,17 @@ static bool index_to_params(u64 id, struct sys_reg_params *params)
 	}
 }
 
+const struct sys_reg_desc *find_reg_by_id(u64 id,
+					  struct sys_reg_params *params,
+					  const struct sys_reg_desc table[],
+					  unsigned int num)
+{
+	if (!index_to_params(id, params))
+		return NULL;
+
+	return find_reg(params, table, num);
+}
+
 /* Decode an index value, and find the sys_reg_desc entry. */
 static const struct sys_reg_desc *index_to_sys_reg_desc(struct kvm_vcpu *vcpu,
 						    u64 id)
@@ -1912,10 +1923,8 @@ static int get_invariant_sys_reg(u64 id, void __user *uaddr)
 	struct sys_reg_params params;
 	const struct sys_reg_desc *r;
 
-	if (!index_to_params(id, &params))
-		return -ENOENT;
-
-	r = find_reg(&params, invariant_sys_regs, ARRAY_SIZE(invariant_sys_regs));
+	r = find_reg_by_id(id, &params, invariant_sys_regs,
+			   ARRAY_SIZE(invariant_sys_regs));
 	if (!r)
 		return -ENOENT;
 
@@ -1929,9 +1938,8 @@ static int set_invariant_sys_reg(u64 id, void __user *uaddr)
 	int err;
 	u64 val = 0; /* Make sure high bits are 0 for 32-bit regs */
 
-	if (!index_to_params(id, &params))
-		return -ENOENT;
-	r = find_reg(&params, invariant_sys_regs, ARRAY_SIZE(invariant_sys_regs));
+	r = find_reg_by_id(id, &params, invariant_sys_regs,
+			   ARRAY_SIZE(invariant_sys_regs));
 	if (!r)
 		return -ENOENT;
 
arch/arm64/kvm/sys_regs.h +4 -0
index dbbb01c..9c6ffd0 100644
@@ -136,6 +136,10 @@ static inline int cmp_sys_reg(const struct sys_reg_desc *i1,
 	return i1->Op2 - i2->Op2;
 }
 
+const struct sys_reg_desc *find_reg_by_id(u64 id,
+					  struct sys_reg_params *params,
+					  const struct sys_reg_desc table[],
+					  unsigned int num);
 
 #define Op0(_x) 	.Op0 = _x
 #define Op1(_x) 	.Op1 = _x
Signature · 1 line
1.9.1
#4 lore raw

PATCH v10 4/8 irqchip/gic-v3: Add missing system register definitions

In reply to [email protected]

From: Vijaya Kumar K <[email protected]>

Define register definitions for ICH_VMCR_EL2, ICC_CTLR_EL1 and ICH_VTR_EL2, ICC_BPR0_EL1, ICC_BPR1_EL1 registers.

---
 include/linux/irqchip/arm-gic-v3.h | 43 ++++++++++++++++++++++++++++++++++++--
 1 file changed, 41 insertions(+), 2 deletions(-)
include/linux/irqchip/arm-gic-v3.h +41 -2
index 0deea34..b4f8287 100644
@@ -352,8 +352,30 @@
 /*
  * CPU interface registers
  */
-#define ICC_CTLR_EL1_EOImode_drop_dir	(0U << 1)
-#define ICC_CTLR_EL1_EOImode_drop	(1U << 1)
+#define ICC_CTLR_EL1_EOImode_SHIFT	(1)
+#define ICC_CTLR_EL1_EOImode_drop_dir	(0U << ICC_CTLR_EL1_EOImode_SHIFT)
+#define ICC_CTLR_EL1_EOImode_drop	(1U << ICC_CTLR_EL1_EOImode_SHIFT)
+#define ICC_CTLR_EL1_EOImode_MASK	(1 << ICC_CTLR_EL1_EOImode_SHIFT)
+#define ICC_CTLR_EL1_CBPR_SHIFT		0
+#define ICC_CTLR_EL1_CBPR_MASK		(1 << ICC_CTLR_EL1_CBPR_SHIFT)
+#define ICC_CTLR_EL1_PRI_BITS_SHIFT	8
+#define ICC_CTLR_EL1_PRI_BITS_MASK	(0x7 << ICC_CTLR_EL1_PRI_BITS_SHIFT)
+#define ICC_CTLR_EL1_ID_BITS_SHIFT	11
+#define ICC_CTLR_EL1_ID_BITS_MASK	(0x7 << ICC_CTLR_EL1_ID_BITS_SHIFT)
+#define ICC_CTLR_EL1_SEIS_SHIFT		14
+#define ICC_CTLR_EL1_SEIS_MASK		(0x1 << ICC_CTLR_EL1_SEIS_SHIFT)
+#define ICC_CTLR_EL1_A3V_SHIFT		15
+#define ICC_CTLR_EL1_A3V_MASK		(0x1 << ICC_CTLR_EL1_A3V_SHIFT)
+#define ICC_PMR_EL1_SHIFT		0
+#define ICC_PMR_EL1_MASK		(0xff << ICC_PMR_EL1_SHIFT)
+#define ICC_BPR0_EL1_SHIFT		0
+#define ICC_BPR0_EL1_MASK		(0x7 << ICC_BPR0_EL1_SHIFT)
+#define ICC_BPR1_EL1_SHIFT		0
+#define ICC_BPR1_EL1_MASK		(0x7 << ICC_BPR1_EL1_SHIFT)
+#define ICC_IGRPEN0_EL1_SHIFT		0
+#define ICC_IGRPEN0_EL1_MASK		(1 << ICC_IGRPEN0_EL1_SHIFT)
+#define ICC_IGRPEN1_EL1_SHIFT		0
+#define ICC_IGRPEN1_EL1_MASK		(1 << ICC_IGRPEN1_EL1_SHIFT)
 #define ICC_SRE_EL1_SRE			(1U << 0)
 
 /*
@@ -384,12 +406,29 @@
 
 #define ICH_VMCR_CTLR_SHIFT		0
 #define ICH_VMCR_CTLR_MASK		(0x21f << ICH_VMCR_CTLR_SHIFT)
+#define ICH_VMCR_CBPR_SHIFT		4
+#define ICH_VMCR_CBPR_MASK		(1 << ICH_VMCR_CBPR_SHIFT)
+#define ICH_VMCR_EOIM_SHIFT		9
+#define ICH_VMCR_EOIM_MASK		(1 << ICH_VMCR_EOIM_SHIFT)
 #define ICH_VMCR_BPR1_SHIFT		18
 #define ICH_VMCR_BPR1_MASK		(7 << ICH_VMCR_BPR1_SHIFT)
 #define ICH_VMCR_BPR0_SHIFT		21
 #define ICH_VMCR_BPR0_MASK		(7 << ICH_VMCR_BPR0_SHIFT)
 #define ICH_VMCR_PMR_SHIFT		24
 #define ICH_VMCR_PMR_MASK		(0xffUL << ICH_VMCR_PMR_SHIFT)
+#define ICH_VMCR_ENG0_SHIFT		0
+#define ICH_VMCR_ENG0_MASK		(1 << ICH_VMCR_ENG0_SHIFT)
+#define ICH_VMCR_ENG1_SHIFT		1
+#define ICH_VMCR_ENG1_MASK		(1 << ICH_VMCR_ENG1_SHIFT)
+
+#define ICH_VTR_PRI_BITS_SHIFT		29
+#define ICH_VTR_PRI_BITS_MASK		(7 << ICH_VTR_PRI_BITS_SHIFT)
+#define ICH_VTR_ID_BITS_SHIFT		23
+#define ICH_VTR_ID_BITS_MASK		(7 << ICH_VTR_ID_BITS_SHIFT)
+#define ICH_VTR_SEIS_SHIFT		22
+#define ICH_VTR_SEIS_MASK		(1 << ICH_VTR_SEIS_SHIFT)
+#define ICH_VTR_A3V_SHIFT		21
+#define ICH_VTR_A3V_MASK		(1 << ICH_VTR_A3V_SHIFT)
 
 #define ICC_IAR1_EL1_SPURIOUS		0x3ff
 
Signature · 1 line
1.9.1
#5 lore raw

PATCH v10 5/8 arm/arm64: vgic: Introduce VENG0 and VENG1 fields to vmcr struct

In reply to [email protected]

From: Vijaya Kumar K <[email protected]>

ICC_VMCR_EL2 supports virtual access to ICC_IGRPEN1_EL1.Enable and ICC_IGRPEN0_EL1.Enable fields. Add grpen0 and grpen1 member variables to struct vmcr to support read and write of these fields.

Also refactor vgic_set_vmcr and vgic_get_vmcr() code. Drop ICH_VMCR_CTLR_SHIFT and ICH_VMCR_CTLR_MASK macros and instead use ICH_VMCR_EOI* and ICH_VMCR_CBPR* macros.

---
 include/linux/irqchip/arm-gic-v3.h |  2 --
 virt/kvm/arm/vgic/vgic-mmio-v2.c   | 16 ----------------
 virt/kvm/arm/vgic/vgic-mmio.c      | 16 ++++++++++++++++
 virt/kvm/arm/vgic/vgic-v3.c        | 20 ++++++++++++++++++--
 virt/kvm/arm/vgic/vgic.h           |  5 +++++
 5 files changed, 39 insertions(+), 20 deletions(-)
include/linux/irqchip/arm-gic-v3.h +0 -2
index b4f8287..406fc3e 100644
@@ -404,8 +404,6 @@
 #define ICH_HCR_EN			(1 << 0)
 #define ICH_HCR_UIE			(1 << 1)
 
-#define ICH_VMCR_CTLR_SHIFT		0
-#define ICH_VMCR_CTLR_MASK		(0x21f << ICH_VMCR_CTLR_SHIFT)
 #define ICH_VMCR_CBPR_SHIFT		4
 #define ICH_VMCR_CBPR_MASK		(1 << ICH_VMCR_CBPR_SHIFT)
 #define ICH_VMCR_EOIM_SHIFT		9
virt/kvm/arm/vgic/vgic-mmio-v2.c +0 -16
index 2cb04b7..ad353b5 100644
@@ -212,22 +212,6 @@ static void vgic_mmio_write_sgipends(struct kvm_vcpu *vcpu,
 	}
 }
 
-static void vgic_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr)
-{
-	if (kvm_vgic_global_state.type == VGIC_V2)
-		vgic_v2_set_vmcr(vcpu, vmcr);
-	else
-		vgic_v3_set_vmcr(vcpu, vmcr);
-}
-
-static void vgic_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr)
-{
-	if (kvm_vgic_global_state.type == VGIC_V2)
-		vgic_v2_get_vmcr(vcpu, vmcr);
-	else
-		vgic_v3_get_vmcr(vcpu, vmcr);
-}
-
 #define GICC_ARCH_VERSION_V2	0x2
 
 /* These are for userland accesses only, there is no guest-facing emulation. */
virt/kvm/arm/vgic/vgic-mmio.c +16 -0
index 0d1bc98..f81e0e5 100644
@@ -416,6 +416,22 @@ int vgic_validate_mmio_region_addr(struct kvm_device *dev,
 	return -ENXIO;
 }
 
+void vgic_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr)
+{
+	if (kvm_vgic_global_state.type == VGIC_V2)
+		vgic_v2_set_vmcr(vcpu, vmcr);
+	else
+		vgic_v3_set_vmcr(vcpu, vmcr);
+}
+
+void vgic_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr)
+{
+	if (kvm_vgic_global_state.type == VGIC_V2)
+		vgic_v2_get_vmcr(vcpu, vmcr);
+	else
+		vgic_v3_get_vmcr(vcpu, vmcr);
+}
+
 /*
  * kvm_mmio_read_buf() returns a value in a format where it can be converted
  * to a byte array and be directly observed as the guest wanted it to appear
virt/kvm/arm/vgic/vgic-v3.c +18 -2
index 9f0dae3..c21968b 100644
@@ -175,10 +175,18 @@ void vgic_v3_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcrp)
 {
 	u32 vmcr;
 
-	vmcr  = (vmcrp->ctlr << ICH_VMCR_CTLR_SHIFT) & ICH_VMCR_CTLR_MASK;
+	/*
+	 * Ignore the FIQen bit, because GIC emulation always implies
+	 * SRE=1 which means the vFIQEn bit is also RES1.
+	 */
+	vmcr = ((vmcrp->ctlr >> ICC_CTLR_EL1_EOImode_SHIFT) <<
+		 ICH_VMCR_EOIM_SHIFT) & ICH_VMCR_EOIM_MASK;
+	vmcr |= (vmcrp->ctlr << ICH_VMCR_CBPR_SHIFT) & ICH_VMCR_CBPR_MASK;
 	vmcr |= (vmcrp->abpr << ICH_VMCR_BPR1_SHIFT) & ICH_VMCR_BPR1_MASK;
 	vmcr |= (vmcrp->bpr << ICH_VMCR_BPR0_SHIFT) & ICH_VMCR_BPR0_MASK;
 	vmcr |= (vmcrp->pmr << ICH_VMCR_PMR_SHIFT) & ICH_VMCR_PMR_MASK;
+	vmcr |= (vmcrp->grpen0 << ICH_VMCR_ENG0_SHIFT) & ICH_VMCR_ENG0_MASK;
+	vmcr |= (vmcrp->grpen1 << ICH_VMCR_ENG1_SHIFT) & ICH_VMCR_ENG1_MASK;
 
 	vcpu->arch.vgic_cpu.vgic_v3.vgic_vmcr = vmcr;
 }
@@ -187,10 +195,18 @@ void vgic_v3_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcrp)
 {
 	u32 vmcr = vcpu->arch.vgic_cpu.vgic_v3.vgic_vmcr;
 
-	vmcrp->ctlr = (vmcr & ICH_VMCR_CTLR_MASK) >> ICH_VMCR_CTLR_SHIFT;
+	/*
+	 * Ignore the FIQen bit, because GIC emulation always implies
+	 * SRE=1 which means the vFIQEn bit is also RES1.
+	 */
+	vmcrp->ctlr = ((vmcr >> ICH_VMCR_EOIM_SHIFT) <<
+			ICC_CTLR_EL1_EOImode_SHIFT) & ICC_CTLR_EL1_EOImode_MASK;
+	vmcrp->ctlr |= (vmcr & ICH_VMCR_CBPR_MASK) >> ICH_VMCR_CBPR_SHIFT;
 	vmcrp->abpr = (vmcr & ICH_VMCR_BPR1_MASK) >> ICH_VMCR_BPR1_SHIFT;
 	vmcrp->bpr  = (vmcr & ICH_VMCR_BPR0_MASK) >> ICH_VMCR_BPR0_SHIFT;
 	vmcrp->pmr  = (vmcr & ICH_VMCR_PMR_MASK) >> ICH_VMCR_PMR_SHIFT;
+	vmcrp->grpen0 = (vmcr & ICH_VMCR_ENG0_MASK) >> ICH_VMCR_ENG0_SHIFT;
+	vmcrp->grpen1 = (vmcr & ICH_VMCR_ENG1_MASK) >> ICH_VMCR_ENG1_SHIFT;
 }
 
 #define INITIAL_PENDBASER_VALUE						  \
virt/kvm/arm/vgic/vgic.h +5 -0
index 91f58b2..9232791 100644
@@ -78,6 +78,9 @@ struct vgic_vmcr {
 	u32	abpr;
 	u32	bpr;
 	u32	pmr;
+	/* Below member variable are valid only for GICv3 */
+	u32	grpen0;
+	u32	grpen1;
 };
 
 struct vgic_irq *vgic_get_irq(struct kvm *kvm, struct kvm_vcpu *vcpu,
@@ -138,6 +141,8 @@ int vgic_v3_dist_uaccess(struct kvm_vcpu *vcpu, bool is_write,
 int vgic_v3_redist_uaccess(struct kvm_vcpu *vcpu, bool is_write,
 			 int offset, u32 *val);
 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);
 int vgic_lazy_init(struct kvm *kvm);
 int vgic_init(struct kvm *kvm);
 
Signature · 1 line
1.9.1
#6 lore raw

PATCH v10 6/8 arm/arm64: vgic: Implement VGICv3 CPU interface access

In reply to [email protected]

From: Vijaya Kumar K <[email protected]>

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 arch/arm64/kvm/vgic-sys-reg-v3.c handles read and write of VGIC CPU registers for AArch64.

For AArch32 mode, arch/arm/kvm/vgic-v3-coproc.c file is created but APIs are not implemented.

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

---
 arch/arm/include/uapi/asm/kvm.h     |   2 +
 arch/arm/kvm/Makefile               |   4 +-
 arch/arm/kvm/vgic-v3-coproc.c       |  35 ++++
 arch/arm64/include/uapi/asm/kvm.h   |   3 +
 arch/arm64/kvm/Makefile             |   3 +-
 arch/arm64/kvm/vgic-sys-reg-v3.c    | 338 ++++++++++++++++++++++++++++++++++++
 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-v3.c         |   8 +
 virt/kvm/arm/vgic/vgic.h            |   4 +
 11 files changed, 449 insertions(+), 3 deletions(-)
arch/arm/include/uapi/asm/kvm.h +2 -0
index 0ae6035..98658d9d 100644
@@ -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 */
arch/arm/kvm/Makefile +2 -2
index d571243..68fb023 100644
@@ -7,7 +7,7 @@ ifeq ($(plus_virt),+virt)
 	plus_virt_def := -DREQUIRES_VIRT=1
 endif
 
-ccflags-y += -Iarch/arm/kvm
+ccflags-y += -Iarch/arm/kvm -Ivirt/kvm/arm/vgic
 CFLAGS_arm.o := -I. $(plus_virt_def)
 CFLAGS_mmu.o := -I.
 
@@ -20,7 +20,7 @@ kvm-arm-y = $(KVM)/kvm_main.o $(KVM)/coalesced_mmio.o $(KVM)/eventfd.o $(KVM)/vf
 obj-$(CONFIG_KVM_ARM_HOST) += hyp/
 obj-y += kvm-arm.o init.o interrupts.o
 obj-y += arm.o handle_exit.o guest.o mmu.o emulate.o reset.o
-obj-y += coproc.o coproc_a15.o coproc_a7.o mmio.o psci.o perf.o
+obj-y += coproc.o coproc_a15.o coproc_a7.o mmio.o psci.o perf.o vgic-v3-coproc.o
 obj-y += $(KVM)/arm/aarch32.o
 
 obj-y += $(KVM)/arm/vgic/vgic.o
arch/arm/kvm/vgic-v3-coproc.c new file +35 -0
new file mode 100644
index 0000000..f41abf7
@@ -0,0 +1,35 @@
+/*
+ * VGIC system registers handling functions for AArch32 mode
+ *
+ * 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 <linux/kvm.h>
+#include <linux/kvm_host.h>
+#include <asm/kvm_emulate.h>
+#include "vgic.h"
+
+int vgic_v3_has_cpu_sysregs_attr(struct kvm_vcpu *vcpu, bool is_write, u64 id,
+				 u64 *reg)
+{
+	/*
+	 * TODO: Implement for AArch32
+	 */
+	return -ENXIO;
+}
+
+int vgic_v3_cpu_sysregs_uaccess(struct kvm_vcpu *vcpu, bool is_write, u64 id,
+				u64 *reg)
+{
+	/*
+	 * TODO: Implement for AArch32
+	 */
+	return -ENXIO;
+}
arch/arm64/include/uapi/asm/kvm.h +3 -0
index 56dc08d..91c7137 100644
@@ -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 */
arch/arm64/kvm/Makefile +2 -1
index d50a82a..d89aa50 100644
@@ -2,7 +2,7 @@
 # Makefile for Kernel-based Virtual Machine module
 #
 
-ccflags-y += -Iarch/arm64/kvm
+ccflags-y += -Iarch/arm64/kvm -Ivirt/kvm/arm/vgic
 CFLAGS_arm.o := -I.
 CFLAGS_mmu.o := -I.
 
@@ -19,6 +19,7 @@ kvm-$(CONFIG_KVM_ARM_HOST) += $(ARM)/psci.o $(ARM)/perf.o
 kvm-$(CONFIG_KVM_ARM_HOST) += inject_fault.o regmap.o
 kvm-$(CONFIG_KVM_ARM_HOST) += hyp.o hyp-init.o handle_exit.o
 kvm-$(CONFIG_KVM_ARM_HOST) += guest.o debug.o reset.o sys_regs.o sys_regs_generic_v8.o
+kvm-$(CONFIG_KVM_ARM_HOST) += vgic-sys-reg-v3.o
 kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/aarch32.o
 
 kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/vgic/vgic.o
arch/arm64/kvm/vgic-sys-reg-v3.c new file +338 -0

338 lines not on this page · raw message

include/kvm/arm_vgic.h +9 -0
index 002f092..730a18a 100644
@@ -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;
virt/kvm/arm/vgic/vgic-kvm-device.c +28 -0
index bc7de95..b6266fe 100644
@@ -16,6 +16,7 @@
 #include <linux/kvm_host.h>
 #include <kvm/arm_vgic.h>
 #include <linux/uaccess.h>
+#include <asm/kvm_emulate.h>
 #include <asm/kvm_mmu.h>
 #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, &reg, 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, &reg, 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, &reg, 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;
virt/kvm/arm/vgic/vgic-mmio-v3.c +18 -0
index e4799ae..51439c9 100644
@@ -642,6 +642,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, &reg);
+	}
 	default:
 		return -ENXIO;
 	}
virt/kvm/arm/vgic/vgic-v3.c +8 -0
index c21968b..c98a1c5 100644
@@ -238,6 +238,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;
 }
@@ -338,6 +345,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");
virt/kvm/arm/vgic/vgic.h +4 -0
index 9232791..eac272c 100644
@@ -140,6 +140,10 @@ 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);
 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);
Signature · 1 line
1.9.1
#7 lore raw

Re: [PATCH v10 6/8] arm/arm64: vgic: Implement VGICv3 CPU interface access

Hi Vijaya,

Quoted 6 lines · On 01/12/2016 08:09, [email protected] wrote:

From: Vijaya Kumar K <[email protected]>

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.

s/is/It is

Quoted 6 lines

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.

We make sure ICC_CTLR_EL1 SEIS and A3V are compatible on source and target?

Quoted 11 lines

The arch/arm64/kvm/vgic-sys-reg-v3.c handles read and write of VGIC CPU registers for AArch64.

For AArch32 mode, arch/arm/kvm/vgic-v3-coproc.c file is created but APIs are not implemented.

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

s/define/defined

Quoted 169 lines

Documentation/virtual/kvm/devices/arm-vgic-v3.txt

---
 arch/arm/include/uapi/asm/kvm.h     |   2 +
 arch/arm/kvm/Makefile               |   4 +-
 arch/arm/kvm/vgic-v3-coproc.c       |  35 ++++
 arch/arm64/include/uapi/asm/kvm.h   |   3 +
 arch/arm64/kvm/Makefile             |   3 +-
 arch/arm64/kvm/vgic-sys-reg-v3.c    | 338 ++++++++++++++++++++++++++++++++++++
 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-v3.c         |   8 +
 virt/kvm/arm/vgic/vgic.h            |   4 +
 11 files changed, 449 insertions(+), 3 deletions(-)
arch/arm/include/uapi/asm/kvm.h +2 -0
index 0ae6035..98658d9d 100644
@@ -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 */
arch/arm/kvm/Makefile +2 -2
index d571243..68fb023 100644
@@ -7,7 +7,7 @@ ifeq ($(plus_virt),+virt)
 	plus_virt_def := -DREQUIRES_VIRT=1
 endif
 
-ccflags-y += -Iarch/arm/kvm
+ccflags-y += -Iarch/arm/kvm -Ivirt/kvm/arm/vgic
 CFLAGS_arm.o := -I. $(plus_virt_def)
 CFLAGS_mmu.o := -I.
 
@@ -20,7 +20,7 @@ kvm-arm-y = $(KVM)/kvm_main.o $(KVM)/coalesced_mmio.o $(KVM)/eventfd.o $(KVM)/vf
 obj-$(CONFIG_KVM_ARM_HOST) += hyp/
 obj-y += kvm-arm.o init.o interrupts.o
 obj-y += arm.o handle_exit.o guest.o mmu.o emulate.o reset.o
-obj-y += coproc.o coproc_a15.o coproc_a7.o mmio.o psci.o perf.o
+obj-y += coproc.o coproc_a15.o coproc_a7.o mmio.o psci.o perf.o vgic-v3-coproc.o
 obj-y += $(KVM)/arm/aarch32.o
 
 obj-y += $(KVM)/arm/vgic/vgic.o
arch/arm/kvm/vgic-v3-coproc.c new file +35 -0
new file mode 100644
index 0000000..f41abf7
@@ -0,0 +1,35 @@
+/*
+ * VGIC system registers handling functions for AArch32 mode
+ *
+ * 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 <linux/kvm.h>
+#include <linux/kvm_host.h>
+#include <asm/kvm_emulate.h>
+#include "vgic.h"
+
+int vgic_v3_has_cpu_sysregs_attr(struct kvm_vcpu *vcpu, bool is_write, u64 id,
+				 u64 *reg)
+{
+	/*
+	 * TODO: Implement for AArch32
+	 */
+	return -ENXIO;
+}
+
+int vgic_v3_cpu_sysregs_uaccess(struct kvm_vcpu *vcpu, bool is_write, u64 id,
+				u64 *reg)
+{
+	/*
+	 * TODO: Implement for AArch32
+	 */
+	return -ENXIO;
+}
arch/arm64/include/uapi/asm/kvm.h +3 -0
index 56dc08d..91c7137 100644
@@ -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 */
arch/arm64/kvm/Makefile +2 -1
index d50a82a..d89aa50 100644
@@ -2,7 +2,7 @@
 # Makefile for Kernel-based Virtual Machine module
 #
 
-ccflags-y += -Iarch/arm64/kvm
+ccflags-y += -Iarch/arm64/kvm -Ivirt/kvm/arm/vgic
 CFLAGS_arm.o := -I.
 CFLAGS_mmu.o := -I.
 
@@ -19,6 +19,7 @@ kvm-$(CONFIG_KVM_ARM_HOST) += $(ARM)/psci.o $(ARM)/perf.o
 kvm-$(CONFIG_KVM_ARM_HOST) += inject_fault.o regmap.o
 kvm-$(CONFIG_KVM_ARM_HOST) += hyp.o hyp-init.o handle_exit.o
 kvm-$(CONFIG_KVM_ARM_HOST) += guest.o debug.o reset.o sys_regs.o sys_regs_generic_v8.o
+kvm-$(CONFIG_KVM_ARM_HOST) += vgic-sys-reg-v3.o
 kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/aarch32.o
 
 kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/vgic/vgic.o
arch/arm64/kvm/vgic-sys-reg-v3.c new file +28 -0
new file mode 100644
index 0000000..76663f9
@@ -0,0 +1,338 @@
+/*
+ * VGIC system registers handling functions for AArch64 mode
+ *
+ * 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 <linux/irqchip/arm-gic-v3.h>
+#include <linux/kvm.h>
+#include <linux/kvm_host.h>
+#include <asm/kvm_emulate.h>
+#include "vgic.h"
+#include "sys_regs.h"
+
+static bool access_gic_ctlr(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
+			    const struct sys_reg_desc *r)
+{
+	u32 host_pri_bits, host_id_bits, host_seis, host_a3v, seis, a3v;
+	struct vgic_cpu *vgic_v3_cpu = &vcpu->arch.vgic_cpu;
+	struct vgic_vmcr vmcr;
+	u64 val;
+

arch/arm64/kvm/vgic-sys-reg-v3.c line 38

Quoted 10 lines
arch/arm64/kvm/vgic-sys-reg-v3.c new file +10 -0
+	vgic_get_vmcr(vcpu, &vmcr);
+	if (p->is_write) {
+		val = p->regval;
+
+		/*
+		 * Disallow restoring VM state if not supported by this
+		 * hardware.
+		 */
+		host_pri_bits = ((val & ICC_CTLR_EL1_PRI_BITS_MASK) >>
+				 ICC_CTLR_EL1_PRI_BITS_SHIFT) + 1;

I am confused by the "host" terminology. Those are the "source" values we want to restore and we compare with the destination current value, right?

arch/arm64/kvm/vgic-sys-reg-v3.c line 40

Quoted 2 lines
arch/arm64/kvm/vgic-sys-reg-v3.c new file +2 -0
+		if (host_pri_bits > vgic_v3_cpu->num_pri_bits)
+			return false;

I am lost. Who did set num_pri_bits and num_id_bits we compare with? Seis and a3v I get this is computed on probe

Quoted 15 lines
arch/arm64/kvm/vgic-sys-reg-v3.c new file +15 -0
+
+		vgic_v3_cpu->num_pri_bits = host_pri_bits;
+
+		host_id_bits = (val & ICC_CTLR_EL1_ID_BITS_MASK) >>
+				ICC_CTLR_EL1_ID_BITS_SHIFT;
+		if (host_id_bits > vgic_v3_cpu->num_id_bits)
+			return false;
+
+		vgic_v3_cpu->num_id_bits = host_id_bits;
+
+		host_seis = ((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 (host_seis != seis)

arch/arm64/kvm/vgic-sys-reg-v3.c line 65

Quoted 10 lines
arch/arm64/kvm/vgic-sys-reg-v3.c new file +10 -0
+			return false;
+
+		host_a3v = ((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 (host_a3v != a3v)
+			return false;
+
+		vmcr.ctlr = val & ICC_CTLR_EL1_CBPR_MASK;
+		vmcr.ctlr |= val & ICC_CTLR_EL1_EOImode_MASK;

nit: I still don't get why the vmcr has CPBR and EOImode set with the ICC_CTLR_EL1 layout and then this gets transformed in the proper vmcr format by vgic_set_vmcr. This is confusing to me and would at least deserve a comment attached to struct vgic_vmcr definition.

Why don't we set the vmcr.ctlr directly in its ICH_VMCR format? In set/get_vmcr all the other struct vgic_vmcr fields are handled in ICH_VMCR native layout except the ctrl field.

Quoted 224 lines
arch/arm64/kvm/vgic-sys-reg-v3.c new file +224 -0
+		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;
+
+		p->regval = val;
+	}
+
+	return true;
+}
+
+static bool access_gic_pmr(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
+			   const struct sys_reg_desc *r)
+{
+	struct vgic_vmcr vmcr;
+
+	vgic_get_vmcr(vcpu, &vmcr);
+	if (p->is_write) {
+		vmcr.pmr = (p->regval & ICC_PMR_EL1_MASK) >> ICC_PMR_EL1_SHIFT;
+		vgic_set_vmcr(vcpu, &vmcr);
+	} else {
+		p->regval = (vmcr.pmr << ICC_PMR_EL1_SHIFT) & ICC_PMR_EL1_MASK;
+	}
+
+	return true;
+}
+
+static bool access_gic_bpr0(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
+			    const struct sys_reg_desc *r)
+{
+	struct vgic_vmcr vmcr;
+
+	vgic_get_vmcr(vcpu, &vmcr);
+	if (p->is_write) {
+		vmcr.bpr = (p->regval & ICC_BPR0_EL1_MASK) >>
+			    ICC_BPR0_EL1_SHIFT;
+		vgic_set_vmcr(vcpu, &vmcr);
+	} else {
+		p->regval = (vmcr.bpr << ICC_BPR0_EL1_SHIFT) &
+			     ICC_BPR0_EL1_MASK;
+	}
+
+	return true;
+}
+
+static bool access_gic_bpr1(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
+			    const struct sys_reg_desc *r)
+{
+	struct vgic_vmcr vmcr;
+
+	if (!p->is_write)
+		p->regval = 0;
+
+	vgic_get_vmcr(vcpu, &vmcr);
+	if (!((vmcr.ctlr & ICH_VMCR_CBPR_MASK) >> ICH_VMCR_CBPR_SHIFT)) {
+		if (p->is_write) {
+			vmcr.abpr = (p->regval & ICC_BPR1_EL1_MASK) >>
+				     ICC_BPR1_EL1_SHIFT;
+			vgic_set_vmcr(vcpu, &vmcr);
+		} else {
+			p->regval = (vmcr.abpr << ICC_BPR1_EL1_SHIFT) &
+				     ICC_BPR1_EL1_MASK;
+		}
+	} else {
+		if (!p->is_write)
+			p->regval = min((vmcr.bpr + 1), 7U);
+	}
+
+	return true;
+}
+
+static bool access_gic_grpen0(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
+			      const struct sys_reg_desc *r)
+{
+	struct vgic_vmcr vmcr;
+
+	vgic_get_vmcr(vcpu, &vmcr);
+	if (p->is_write) {
+		vmcr.grpen0 = (p->regval & ICC_IGRPEN0_EL1_MASK) >>
+			       ICC_IGRPEN0_EL1_SHIFT;
+		vgic_set_vmcr(vcpu, &vmcr);
+	} else {
+		p->regval = (vmcr.grpen0 << ICC_IGRPEN0_EL1_SHIFT) &
+			     ICC_IGRPEN0_EL1_MASK;
+	}
+
+	return true;
+}
+
+static bool access_gic_grpen1(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
+			      const struct sys_reg_desc *r)
+{
+	struct vgic_vmcr vmcr;
+
+	vgic_get_vmcr(vcpu, &vmcr);
+	if (p->is_write) {
+		vmcr.grpen1 = (p->regval & ICC_IGRPEN1_EL1_MASK) >>
+			       ICC_IGRPEN1_EL1_SHIFT;
+		vgic_set_vmcr(vcpu, &vmcr);
+	} else {
+		p->regval = (vmcr.grpen1 << ICC_IGRPEN1_EL1_SHIFT) &
+			     ICC_IGRPEN1_EL1_MASK;
+	}
+
+	return true;
+}
+
+static void vgic_v3_access_apr_reg(struct kvm_vcpu *vcpu,
+				   struct sys_reg_params *p, u8 apr, u8 idx)
+{
+	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 (p->is_write)
+		*ap_reg = p->regval;
+	else
+		p->regval = *ap_reg;
+}
+
+static bool access_gic_aprn(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
+			    const struct sys_reg_desc *r, u8 apr)
+{
+	struct vgic_cpu *vgic_v3_cpu = &vcpu->arch.vgic_cpu;
+	u8 idx = r->Op2 & 3;
+
+	/*
+	 * 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, p, apr, idx);
+		break;
+	case 6:
+		if (idx > 1)
+			goto err;
+		vgic_v3_access_apr_reg(vcpu, p, apr, idx);
+		break;
+	default:
+		if (idx > 0)
+			goto err;
+		vgic_v3_access_apr_reg(vcpu, p, apr, idx);
+	}
+
+	return true;
+err:
+	if (!p->is_write)
+		p->regval = 0;
+
+	return false;
+}
+
+static bool access_gic_ap0r(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
+			    const struct sys_reg_desc *r)
+
+{
+	return access_gic_aprn(vcpu, p, r, 0);
+}
+
+static bool access_gic_ap1r(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
+			    const struct sys_reg_desc *r)
+{
+	return access_gic_aprn(vcpu, p, r, 1);
+}
+
+static bool access_gic_sre(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
+			   const struct sys_reg_desc *r)
+{
+	struct vgic_v3_cpu_if *vgicv3 = &vcpu->arch.vgic_cpu.vgic_v3;
+
+	/* Validate SRE bit */
+	if (p->is_write) {
+		if (!(p->regval & ICC_SRE_EL1_SRE))
+			return false;
+	} else {
+		p->regval = vgicv3->vgic_sre;
+	}
+
+	return true;
+}
+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 },
+	/* ICC_BPR0_EL1 */
+	{ Op0(3), Op1(0), CRn(12), CRm(8), Op2(3), access_gic_bpr0 },
+	/* ICC_AP0R0_EL1 */
+	{ Op0(3), Op1(0), CRn(12), CRm(8), Op2(4), access_gic_ap0r },
+	/* ICC_AP0R1_EL1 */
+	{ Op0(3), Op1(0), CRn(12), CRm(8), Op2(5), access_gic_ap0r },
+	/* ICC_AP0R2_EL1 */
+	{ Op0(3), Op1(0), CRn(12), CRm(8), Op2(6), access_gic_ap0r },
+	/* ICC_AP0R3_EL1 */
+	{ Op0(3), Op1(0), CRn(12), CRm(8), Op2(7), access_gic_ap0r },
+	/* ICC_AP1R0_EL1 */
+	{ Op0(3), Op1(0), CRn(12), CRm(9), Op2(0), access_gic_ap1r },
+	/* ICC_AP1R1_EL1 */
+	{ Op0(3), Op1(0), CRn(12), CRm(9), Op2(1), access_gic_ap1r },
+	/* ICC_AP1R2_EL1 */
+	{ Op0(3), Op1(0), CRn(12), CRm(9), Op2(2), access_gic_ap1r },
+	/* ICC_AP1R3_EL1 */
+	{ Op0(3), Op1(0), CRn(12), CRm(9), Op2(3), access_gic_ap1r },
+	/* ICC_BPR1_EL1 */
+	{ Op0(3), Op1(0), CRn(12), CRm(12), Op2(3), access_gic_bpr1 },
+	/* ICC_CTLR_EL1 */
+	{ Op0(3), Op1(0), CRn(12), CRm(12), Op2(4), access_gic_ctlr },
+	/* ICC_SRE_EL1 */
+	{ Op0(3), Op1(0), CRn(12), CRm(12), Op2(5), access_gic_sre },
+	/* ICC_IGRPEN0_EL1 */

arch/arm64/kvm/vgic-sys-reg-v3.c line 299

Quoted 10 lines
arch/arm64/kvm/vgic-sys-reg-v3.c new file +10 -0
+	{ Op0(3), Op1(0), CRn(12), CRm(12), Op2(6), access_gic_grpen0 },
+	/* ICC_GRPEN1_EL1 */
+	{ Op0(3), Op1(0), CRn(12), CRm(12), Op2(7), access_gic_grpen1 },
+};
+
+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;

At the beginning I asked myself why we did not OR with KVM_REG_ARM64 as stated in include/uapi/linux/kvm.h but then looking at index_to_params() implementation it looks it is not used.

Quoted 44 lines
arch/arm64/kvm/vgic-sys-reg-v3.c new file +39 -0
+
+	params.regval = *reg;
+	params.is_write = is_write;
+	params.is_aarch32 = false;
+	params.is_32bit = false;
+
+	if (find_reg_by_id(sysreg, &params, 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, &params, gic_v3_icc_reg_descs,
+			   ARRAY_SIZE(gic_v3_icc_reg_descs));
+	if (!r)
+		return -ENXIO;
+
+	if (!r->access(vcpu, &params, r))
+		return -EINVAL;
+
+	if (!is_write)
+		*reg = params.regval;
+
+	return 0;
+}
include/kvm/arm_vgic.h +0 -0
index 002f092..730a18a 100644

include/kvm/arm_vgic.h struct vgic_global { · line 75

Quoted 5 lines
include/kvm/arm_vgic.h +2 -0
@@ -71,6 +71,9 @@ struct vgic_global {
 
 	/* GIC system register CPU interface */
 	struct static_key_false gicv3_cpuif;
+
+	/* Cache ICH_VTR_EL2 reg value */

nit: comment does not bring much value I think

Quoted 22 lines
include/kvm/arm_vgic.h +7 -0
+	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;
virt/kvm/arm/vgic/vgic-kvm-device.c +0 -0
index bc7de95..b6266fe 100644

virt/kvm/arm/vgic/vgic-kvm-device.c line 19

Quoted 4 lines
virt/kvm/arm/vgic/vgic-kvm-device.c +1 -0
@@ -16,6 +16,7 @@
 #include <linux/kvm_host.h>
 #include <kvm/arm_vgic.h>
 #include <linux/uaccess.h>
+#include <asm/kvm_emulate.h>

not needed I think

Thanks

Eric

Quoted 129 lines
virt/kvm/arm/vgic/vgic-kvm-device.c +27 -0
 #include <asm/kvm_mmu.h>
 #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, &reg, 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, &reg, 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, &reg, 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;
virt/kvm/arm/vgic/vgic-mmio-v3.c +18 -0
index e4799ae..51439c9 100644
@@ -642,6 +642,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, &reg);
+	}
 	default:
 		return -ENXIO;
 	}
virt/kvm/arm/vgic/vgic-v3.c +8 -0
index c21968b..c98a1c5 100644
@@ -238,6 +238,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;
 }
@@ -338,6 +345,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");
virt/kvm/arm/vgic/vgic.h +4 -0
index 9232791..eac272c 100644
@@ -140,6 +140,10 @@ 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);
 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);
#8 lore raw

Re: [PATCH v10 6/8] arm/arm64: vgic: Implement VGICv3 CPU interface access

Quoted 69 lines · Auger Eric

Hi Vijaya,

Quoted 6 lines · On 01/12/2016 08:09, [email protected] wrote:

From: Vijaya Kumar K <[email protected]>

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.

s/is/It is

Quoted 6 lines

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.

We make sure ICC_CTLR_EL1 SEIS and A3V are compatible on source and target?

Quoted 11 lines

The arch/arm64/kvm/vgic-sys-reg-v3.c handles read and write of VGIC CPU registers for AArch64.

For AArch32 mode, arch/arm/kvm/vgic-v3-coproc.c file is created but APIs are not implemented.

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

s/define/defined

Quoted 40 lines

Documentation/virtual/kvm/devices/arm-vgic-v3.txt

---

arch/arm64/kvm/vgic-sys-reg-v3.c new file +32 -0
@@ -0,0 +1,338 @@
+/*
+ * VGIC system registers handling functions for AArch64 mode
+ *
+ * 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 <linux/irqchip/arm-gic-v3.h>
+#include <linux/kvm.h>
+#include <linux/kvm_host.h>
+#include <asm/kvm_emulate.h>
+#include "vgic.h"
+#include "sys_regs.h"
+
+static bool access_gic_ctlr(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
+                         const struct sys_reg_desc *r)
+{
+     u32 host_pri_bits, host_id_bits, host_seis, host_a3v, seis, a3v;
+     struct vgic_cpu *vgic_v3_cpu = &vcpu->arch.vgic_cpu;
+     struct vgic_vmcr vmcr;
+     u64 val;
+
+     vgic_get_vmcr(vcpu, &vmcr);
+     if (p->is_write) {
+             val = p->regval;
+
Quoted 8 lines

arch/arm64/kvm/vgic-sys-reg-v3.c line 38

Quoted 6 lines
arch/arm64/kvm/vgic-sys-reg-v3.c new file +6 -0
+             /*
+              * Disallow restoring VM state if not supported by this
+              * hardware.
+              */
+             host_pri_bits = ((val & ICC_CTLR_EL1_PRI_BITS_MASK) >>
+                              ICC_CTLR_EL1_PRI_BITS_SHIFT) + 1;

I am confused by the "host" terminology. Those are the "source" values we want to restore and we compare with the destination current value, right?

yes

Quoted 3 lines

arch/arm64/kvm/vgic-sys-reg-v3.c line 40

Quoted 2 lines
arch/arm64/kvm/vgic-sys-reg-v3.c new file +2 -0
+             if (host_pri_bits > vgic_v3_cpu->num_pri_bits)
+                     return false;

I am lost. Who did set num_pri_bits and num_id_bits we compare with?

In vgic_v3_enable() these values are computed

Quoted 22 lines

Seis and a3v I get this is computed on probe

Quoted 21 lines
arch/arm64/kvm/vgic-sys-reg-v3.c new file +21 -0
+
+             vgic_v3_cpu->num_pri_bits = host_pri_bits;
+
+             host_id_bits = (val & ICC_CTLR_EL1_ID_BITS_MASK) >>
+                             ICC_CTLR_EL1_ID_BITS_SHIFT;
+             if (host_id_bits > vgic_v3_cpu->num_id_bits)
+                     return false;
+
+             vgic_v3_cpu->num_id_bits = host_id_bits;
+
+             host_seis = ((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 (host_seis != seis)
+                     return false;
+
+             host_a3v = ((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 (host_a3v != a3v)
Quoted 8 lines

arch/arm64/kvm/vgic-sys-reg-v3.c line 65

Quoted 4 lines
arch/arm64/kvm/vgic-sys-reg-v3.c new file +4 -0
+                     return false;
+
+             vmcr.ctlr = val & ICC_CTLR_EL1_CBPR_MASK;
+             vmcr.ctlr |= val & ICC_CTLR_EL1_EOImode_MASK;

nit: I still don't get why the vmcr has CPBR and EOImode set with the ICC_CTLR_EL1 layout and then this gets transformed in the proper vmcr format by vgic_set_vmcr. This is confusing to me and would at least deserve a comment attached to struct vgic_vmcr definition.

I will add a comment

Quoted 4 lines

Why don't we set the vmcr.ctlr directly in its ICH_VMCR format? In set/get_vmcr all the other struct vgic_vmcr fields are handled in ICH_VMCR native layout except the ctrl field.

None of the fields of struct vgic_vmcr are in ICH_VMCR native layout. Except ctlr all the other fields are registers having single field value.
Ex: pmr, bpr0 etc.,

Quoted 304 lines
Quoted 225 lines
arch/arm64/kvm/vgic-sys-reg-v3.c new file +224 -0
+             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;
+
+             p->regval = val;
+     }
+
+     return true;
+}
+
+static bool access_gic_pmr(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
+                        const struct sys_reg_desc *r)
+{
+     struct vgic_vmcr vmcr;
+
+     vgic_get_vmcr(vcpu, &vmcr);
+     if (p->is_write) {
+             vmcr.pmr = (p->regval & ICC_PMR_EL1_MASK) >> ICC_PMR_EL1_SHIFT;
+             vgic_set_vmcr(vcpu, &vmcr);
+     } else {
+             p->regval = (vmcr.pmr << ICC_PMR_EL1_SHIFT) & ICC_PMR_EL1_MASK;
+     }
+
+     return true;
+}
+
+static bool access_gic_bpr0(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
+                         const struct sys_reg_desc *r)
+{
+     struct vgic_vmcr vmcr;
+
+     vgic_get_vmcr(vcpu, &vmcr);
+     if (p->is_write) {
+             vmcr.bpr = (p->regval & ICC_BPR0_EL1_MASK) >>
+                         ICC_BPR0_EL1_SHIFT;
+             vgic_set_vmcr(vcpu, &vmcr);
+     } else {
+             p->regval = (vmcr.bpr << ICC_BPR0_EL1_SHIFT) &
+                          ICC_BPR0_EL1_MASK;
+     }
+
+     return true;
+}
+
+static bool access_gic_bpr1(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
+                         const struct sys_reg_desc *r)
+{
+     struct vgic_vmcr vmcr;
+
+     if (!p->is_write)
+             p->regval = 0;
+
+     vgic_get_vmcr(vcpu, &vmcr);
+     if (!((vmcr.ctlr & ICH_VMCR_CBPR_MASK) >> ICH_VMCR_CBPR_SHIFT)) {
+             if (p->is_write) {
+                     vmcr.abpr = (p->regval & ICC_BPR1_EL1_MASK) >>
+                                  ICC_BPR1_EL1_SHIFT;
+                     vgic_set_vmcr(vcpu, &vmcr);
+             } else {
+                     p->regval = (vmcr.abpr << ICC_BPR1_EL1_SHIFT) &
+                                  ICC_BPR1_EL1_MASK;
+             }
+     } else {
+             if (!p->is_write)
+                     p->regval = min((vmcr.bpr + 1), 7U);
+     }
+
+     return true;
+}
+
+static bool access_gic_grpen0(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
+                           const struct sys_reg_desc *r)
+{
+     struct vgic_vmcr vmcr;
+
+     vgic_get_vmcr(vcpu, &vmcr);
+     if (p->is_write) {
+             vmcr.grpen0 = (p->regval & ICC_IGRPEN0_EL1_MASK) >>
+                            ICC_IGRPEN0_EL1_SHIFT;
+             vgic_set_vmcr(vcpu, &vmcr);
+     } else {
+             p->regval = (vmcr.grpen0 << ICC_IGRPEN0_EL1_SHIFT) &
+                          ICC_IGRPEN0_EL1_MASK;
+     }
+
+     return true;
+}
+
+static bool access_gic_grpen1(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
+                           const struct sys_reg_desc *r)
+{
+     struct vgic_vmcr vmcr;
+
+     vgic_get_vmcr(vcpu, &vmcr);
+     if (p->is_write) {
+             vmcr.grpen1 = (p->regval & ICC_IGRPEN1_EL1_MASK) >>
+                            ICC_IGRPEN1_EL1_SHIFT;
+             vgic_set_vmcr(vcpu, &vmcr);
+     } else {
+             p->regval = (vmcr.grpen1 << ICC_IGRPEN1_EL1_SHIFT) &
+                          ICC_IGRPEN1_EL1_MASK;
+     }
+
+     return true;
+}
+
+static void vgic_v3_access_apr_reg(struct kvm_vcpu *vcpu,
+                                struct sys_reg_params *p, u8 apr, u8 idx)
+{
+     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 (p->is_write)
+             *ap_reg = p->regval;
+     else
+             p->regval = *ap_reg;
+}
+
+static bool access_gic_aprn(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
+                         const struct sys_reg_desc *r, u8 apr)
+{
+     struct vgic_cpu *vgic_v3_cpu = &vcpu->arch.vgic_cpu;
+     u8 idx = r->Op2 & 3;
+
+     /*
+      * 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, p, apr, idx);
+             break;
+     case 6:
+             if (idx > 1)
+                     goto err;
+             vgic_v3_access_apr_reg(vcpu, p, apr, idx);
+             break;
+     default:
+             if (idx > 0)
+                     goto err;
+             vgic_v3_access_apr_reg(vcpu, p, apr, idx);
+     }
+
+     return true;
+err:
+     if (!p->is_write)
+             p->regval = 0;
+
+     return false;
+}
+
+static bool access_gic_ap0r(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
+                         const struct sys_reg_desc *r)
+
+{
+     return access_gic_aprn(vcpu, p, r, 0);
+}
+
+static bool access_gic_ap1r(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
+                         const struct sys_reg_desc *r)
+{
+     return access_gic_aprn(vcpu, p, r, 1);
+}
+
+static bool access_gic_sre(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
+                        const struct sys_reg_desc *r)
+{
+     struct vgic_v3_cpu_if *vgicv3 = &vcpu->arch.vgic_cpu.vgic_v3;
+
+     /* Validate SRE bit */
+     if (p->is_write) {
+             if (!(p->regval & ICC_SRE_EL1_SRE))
+                     return false;
+     } else {
+             p->regval = vgicv3->vgic_sre;
+     }
+
+     return true;
+}
+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 },
+     /* ICC_BPR0_EL1 */
+     { Op0(3), Op1(0), CRn(12), CRm(8), Op2(3), access_gic_bpr0 },
+     /* ICC_AP0R0_EL1 */
+     { Op0(3), Op1(0), CRn(12), CRm(8), Op2(4), access_gic_ap0r },
+     /* ICC_AP0R1_EL1 */
+     { Op0(3), Op1(0), CRn(12), CRm(8), Op2(5), access_gic_ap0r },
+     /* ICC_AP0R2_EL1 */
+     { Op0(3), Op1(0), CRn(12), CRm(8), Op2(6), access_gic_ap0r },
+     /* ICC_AP0R3_EL1 */
+     { Op0(3), Op1(0), CRn(12), CRm(8), Op2(7), access_gic_ap0r },
+     /* ICC_AP1R0_EL1 */
+     { Op0(3), Op1(0), CRn(12), CRm(9), Op2(0), access_gic_ap1r },
+     /* ICC_AP1R1_EL1 */
+     { Op0(3), Op1(0), CRn(12), CRm(9), Op2(1), access_gic_ap1r },
+     /* ICC_AP1R2_EL1 */
+     { Op0(3), Op1(0), CRn(12), CRm(9), Op2(2), access_gic_ap1r },
+     /* ICC_AP1R3_EL1 */
+     { Op0(3), Op1(0), CRn(12), CRm(9), Op2(3), access_gic_ap1r },
+     /* ICC_BPR1_EL1 */
+     { Op0(3), Op1(0), CRn(12), CRm(12), Op2(3), access_gic_bpr1 },
+     /* ICC_CTLR_EL1 */
+     { Op0(3), Op1(0), CRn(12), CRm(12), Op2(4), access_gic_ctlr },
+     /* ICC_SRE_EL1 */
+     { Op0(3), Op1(0), CRn(12), CRm(12), Op2(5), access_gic_sre },
+     /* ICC_IGRPEN0_EL1 */

arch/arm64/kvm/vgic-sys-reg-v3.c line 299

Quoted 10 lines
arch/arm64/kvm/vgic-sys-reg-v3.c new file +10 -0
+     { Op0(3), Op1(0), CRn(12), CRm(12), Op2(6), access_gic_grpen0 },
+     /* ICC_GRPEN1_EL1 */
+     { Op0(3), Op1(0), CRn(12), CRm(12), Op2(7), access_gic_grpen1 },
+};
+
+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;

At the beginning I asked myself why we did not OR with KVM_REG_ARM64 as stated in include/uapi/linux/kvm.h but then looking at index_to_params() implementation it looks it is not used.

Quoted 44 lines
arch/arm64/kvm/vgic-sys-reg-v3.c new file +39 -0
+
+     params.regval = *reg;
+     params.is_write = is_write;
+     params.is_aarch32 = false;
+     params.is_32bit = false;
+
+     if (find_reg_by_id(sysreg, &params, 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, &params, gic_v3_icc_reg_descs,
+                        ARRAY_SIZE(gic_v3_icc_reg_descs));
+     if (!r)
+             return -ENXIO;
+
+     if (!r->access(vcpu, &params, r))
+             return -EINVAL;
+
+     if (!is_write)
+             *reg = params.regval;
+
+     return 0;
+}
include/kvm/arm_vgic.h +0 -0
index 002f092..730a18a 100644

include/kvm/arm_vgic.h struct vgic_global { · line 75

Quoted 5 lines
include/kvm/arm_vgic.h +2 -0
@@ -71,6 +71,9 @@ struct vgic_global {
 
      /* GIC system register CPU interface */
      struct static_key_false gicv3_cpuif;
+
+     /* Cache ICH_VTR_EL2 reg value */

nit: comment does not bring much value I think

Quoted 16 lines
include/kvm/arm_vgic.h +7 -0
+     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;
 };
 
Quoted 11 lines

virt/kvm/arm/vgic/vgic-kvm-device.c line 19

Quoted 10 lines
include/kvm/arm_vgic.h +0 -0
 extern struct static_key_false vgic_v2_cpuif_trap;
virt/kvm/arm/vgic/vgic-kvm-device.c +1 -0
index bc7de95..b6266fe 100644
@@ -16,6 +16,7 @@
 #include <linux/kvm_host.h>
 #include <kvm/arm_vgic.h>
 #include <linux/uaccess.h>
+#include <asm/kvm_emulate.h>

not needed I think

OK.

#9 lore raw

Re: [PATCH v10 6/8] arm/arm64: vgic: Implement VGICv3 CPU interface access

Hi Vijaya,

Quoted 79 lines · Vijay Kilari
Quoted 69 lines · Auger Eric

Hi Vijaya,

Quoted 6 lines · On 01/12/2016 08:09, [email protected] wrote:

From: Vijaya Kumar K <[email protected]>

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.

s/is/It is

Quoted 6 lines

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.

We make sure ICC_CTLR_EL1 SEIS and A3V are compatible on source and target?

Quoted 11 lines

The arch/arm64/kvm/vgic-sys-reg-v3.c handles read and write of VGIC CPU registers for AArch64.

For AArch32 mode, arch/arm/kvm/vgic-v3-coproc.c file is created but APIs are not implemented.

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

s/define/defined

Quoted 40 lines

Documentation/virtual/kvm/devices/arm-vgic-v3.txt

---

arch/arm64/kvm/vgic-sys-reg-v3.c new file +32 -0
@@ -0,0 +1,338 @@
+/*
+ * VGIC system registers handling functions for AArch64 mode
+ *
+ * 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 <linux/irqchip/arm-gic-v3.h>
+#include <linux/kvm.h>
+#include <linux/kvm_host.h>
+#include <asm/kvm_emulate.h>
+#include "vgic.h"
+#include "sys_regs.h"
+
+static bool access_gic_ctlr(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
+                         const struct sys_reg_desc *r)
+{
+     u32 host_pri_bits, host_id_bits, host_seis, host_a3v, seis, a3v;
+     struct vgic_cpu *vgic_v3_cpu = &vcpu->arch.vgic_cpu;
+     struct vgic_vmcr vmcr;
+     u64 val;
+
+     vgic_get_vmcr(vcpu, &vmcr);
+     if (p->is_write) {
+             val = p->regval;
+
Quoted 8 lines

arch/arm64/kvm/vgic-sys-reg-v3.c line 38

Quoted 6 lines
arch/arm64/kvm/vgic-sys-reg-v3.c new file +6 -0
+             /*
+              * Disallow restoring VM state if not supported by this
+              * hardware.
+              */
+             host_pri_bits = ((val & ICC_CTLR_EL1_PRI_BITS_MASK) >>
+                              ICC_CTLR_EL1_PRI_BITS_SHIFT) + 1;

I am confused by the "host" terminology. Those are the "source" values we want to restore and we compare with the destination current value, right?

Quoted 7 lines

yes

Quoted 3 lines

arch/arm64/kvm/vgic-sys-reg-v3.c line 40

Quoted 2 lines
arch/arm64/kvm/vgic-sys-reg-v3.c new file +2 -0
+             if (host_pri_bits > vgic_v3_cpu->num_pri_bits)
+                     return false;

I am lost. Who did set num_pri_bits and num_id_bits we compare with?

In vgic_v3_enable() these values are computed

OK I missed that.

Quoted 34 lines
Quoted 23 lines

Seis and a3v I get this is computed on probe

Quoted 21 lines
arch/arm64/kvm/vgic-sys-reg-v3.c new file +21 -0
+
+             vgic_v3_cpu->num_pri_bits = host_pri_bits;
+
+             host_id_bits = (val & ICC_CTLR_EL1_ID_BITS_MASK) >>
+                             ICC_CTLR_EL1_ID_BITS_SHIFT;
+             if (host_id_bits > vgic_v3_cpu->num_id_bits)
+                     return false;
+
+             vgic_v3_cpu->num_id_bits = host_id_bits;
+
+             host_seis = ((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 (host_seis != seis)
+                     return false;
+
+             host_a3v = ((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 (host_a3v != a3v)
Quoted 8 lines

arch/arm64/kvm/vgic-sys-reg-v3.c line 65

Quoted 4 lines
arch/arm64/kvm/vgic-sys-reg-v3.c new file +4 -0
+                     return false;
+
+             vmcr.ctlr = val & ICC_CTLR_EL1_CBPR_MASK;
+             vmcr.ctlr |= val & ICC_CTLR_EL1_EOImode_MASK;

nit: I still don't get why the vmcr has CPBR and EOImode set with the ICC_CTLR_EL1 layout and then this gets transformed in the proper vmcr format by vgic_set_vmcr. This is confusing to me and would at least deserve a comment attached to struct vgic_vmcr definition.

I will add a comment

Quoted 1 line
Quoted 7 lines
Quoted 3 lines

Why don't we set the vmcr.ctlr directly in its ICH_VMCR format? In set/get_vmcr all the other struct vgic_vmcr fields are handled in ICH_VMCR native layout except the ctrl field.

None of the fields of struct vgic_vmcr are in ICH_VMCR native layout. Except ctlr all the other fields are registers having single field value.
Ex: pmr, bpr0 etc.,

OK but to me would be more natural to keep vmcr.ctlr in original format but well that's just my pov. If you add a comment that helps already.

Thanks

Eric

Quoted 323 lines
Quoted 305 lines
Quoted 226 lines
arch/arm64/kvm/vgic-sys-reg-v3.c new file +224 -0
+             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;
+
+             p->regval = val;
+     }
+
+     return true;
+}
+
+static bool access_gic_pmr(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
+                        const struct sys_reg_desc *r)
+{
+     struct vgic_vmcr vmcr;
+
+     vgic_get_vmcr(vcpu, &vmcr);
+     if (p->is_write) {
+             vmcr.pmr = (p->regval & ICC_PMR_EL1_MASK) >> ICC_PMR_EL1_SHIFT;
+             vgic_set_vmcr(vcpu, &vmcr);
+     } else {
+             p->regval = (vmcr.pmr << ICC_PMR_EL1_SHIFT) & ICC_PMR_EL1_MASK;
+     }
+
+     return true;
+}
+
+static bool access_gic_bpr0(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
+                         const struct sys_reg_desc *r)
+{
+     struct vgic_vmcr vmcr;
+
+     vgic_get_vmcr(vcpu, &vmcr);
+     if (p->is_write) {
+             vmcr.bpr = (p->regval & ICC_BPR0_EL1_MASK) >>
+                         ICC_BPR0_EL1_SHIFT;
+             vgic_set_vmcr(vcpu, &vmcr);
+     } else {
+             p->regval = (vmcr.bpr << ICC_BPR0_EL1_SHIFT) &
+                          ICC_BPR0_EL1_MASK;
+     }
+
+     return true;
+}
+
+static bool access_gic_bpr1(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
+                         const struct sys_reg_desc *r)
+{
+     struct vgic_vmcr vmcr;
+
+     if (!p->is_write)
+             p->regval = 0;
+
+     vgic_get_vmcr(vcpu, &vmcr);
+     if (!((vmcr.ctlr & ICH_VMCR_CBPR_MASK) >> ICH_VMCR_CBPR_SHIFT)) {
+             if (p->is_write) {
+                     vmcr.abpr = (p->regval & ICC_BPR1_EL1_MASK) >>
+                                  ICC_BPR1_EL1_SHIFT;
+                     vgic_set_vmcr(vcpu, &vmcr);
+             } else {
+                     p->regval = (vmcr.abpr << ICC_BPR1_EL1_SHIFT) &
+                                  ICC_BPR1_EL1_MASK;
+             }
+     } else {
+             if (!p->is_write)
+                     p->regval = min((vmcr.bpr + 1), 7U);
+     }
+
+     return true;
+}
+
+static bool access_gic_grpen0(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
+                           const struct sys_reg_desc *r)
+{
+     struct vgic_vmcr vmcr;
+
+     vgic_get_vmcr(vcpu, &vmcr);
+     if (p->is_write) {
+             vmcr.grpen0 = (p->regval & ICC_IGRPEN0_EL1_MASK) >>
+                            ICC_IGRPEN0_EL1_SHIFT;
+             vgic_set_vmcr(vcpu, &vmcr);
+     } else {
+             p->regval = (vmcr.grpen0 << ICC_IGRPEN0_EL1_SHIFT) &
+                          ICC_IGRPEN0_EL1_MASK;
+     }
+
+     return true;
+}
+
+static bool access_gic_grpen1(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
+                           const struct sys_reg_desc *r)
+{
+     struct vgic_vmcr vmcr;
+
+     vgic_get_vmcr(vcpu, &vmcr);
+     if (p->is_write) {
+             vmcr.grpen1 = (p->regval & ICC_IGRPEN1_EL1_MASK) >>
+                            ICC_IGRPEN1_EL1_SHIFT;
+             vgic_set_vmcr(vcpu, &vmcr);
+     } else {
+             p->regval = (vmcr.grpen1 << ICC_IGRPEN1_EL1_SHIFT) &
+                          ICC_IGRPEN1_EL1_MASK;
+     }
+
+     return true;
+}
+
+static void vgic_v3_access_apr_reg(struct kvm_vcpu *vcpu,
+                                struct sys_reg_params *p, u8 apr, u8 idx)
+{
+     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 (p->is_write)
+             *ap_reg = p->regval;
+     else
+             p->regval = *ap_reg;
+}
+
+static bool access_gic_aprn(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
+                         const struct sys_reg_desc *r, u8 apr)
+{
+     struct vgic_cpu *vgic_v3_cpu = &vcpu->arch.vgic_cpu;
+     u8 idx = r->Op2 & 3;
+
+     /*
+      * 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, p, apr, idx);
+             break;
+     case 6:
+             if (idx > 1)
+                     goto err;
+             vgic_v3_access_apr_reg(vcpu, p, apr, idx);
+             break;
+     default:
+             if (idx > 0)
+                     goto err;
+             vgic_v3_access_apr_reg(vcpu, p, apr, idx);
+     }
+
+     return true;
+err:
+     if (!p->is_write)
+             p->regval = 0;
+
+     return false;
+}
+
+static bool access_gic_ap0r(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
+                         const struct sys_reg_desc *r)
+
+{
+     return access_gic_aprn(vcpu, p, r, 0);
+}
+
+static bool access_gic_ap1r(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
+                         const struct sys_reg_desc *r)
+{
+     return access_gic_aprn(vcpu, p, r, 1);
+}
+
+static bool access_gic_sre(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
+                        const struct sys_reg_desc *r)
+{
+     struct vgic_v3_cpu_if *vgicv3 = &vcpu->arch.vgic_cpu.vgic_v3;
+
+     /* Validate SRE bit */
+     if (p->is_write) {
+             if (!(p->regval & ICC_SRE_EL1_SRE))
+                     return false;
+     } else {
+             p->regval = vgicv3->vgic_sre;
+     }
+
+     return true;
+}
+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 },
+     /* ICC_BPR0_EL1 */
+     { Op0(3), Op1(0), CRn(12), CRm(8), Op2(3), access_gic_bpr0 },
+     /* ICC_AP0R0_EL1 */
+     { Op0(3), Op1(0), CRn(12), CRm(8), Op2(4), access_gic_ap0r },
+     /* ICC_AP0R1_EL1 */
+     { Op0(3), Op1(0), CRn(12), CRm(8), Op2(5), access_gic_ap0r },
+     /* ICC_AP0R2_EL1 */
+     { Op0(3), Op1(0), CRn(12), CRm(8), Op2(6), access_gic_ap0r },
+     /* ICC_AP0R3_EL1 */
+     { Op0(3), Op1(0), CRn(12), CRm(8), Op2(7), access_gic_ap0r },
+     /* ICC_AP1R0_EL1 */
+     { Op0(3), Op1(0), CRn(12), CRm(9), Op2(0), access_gic_ap1r },
+     /* ICC_AP1R1_EL1 */
+     { Op0(3), Op1(0), CRn(12), CRm(9), Op2(1), access_gic_ap1r },
+     /* ICC_AP1R2_EL1 */
+     { Op0(3), Op1(0), CRn(12), CRm(9), Op2(2), access_gic_ap1r },
+     /* ICC_AP1R3_EL1 */
+     { Op0(3), Op1(0), CRn(12), CRm(9), Op2(3), access_gic_ap1r },
+     /* ICC_BPR1_EL1 */
+     { Op0(3), Op1(0), CRn(12), CRm(12), Op2(3), access_gic_bpr1 },
+     /* ICC_CTLR_EL1 */
+     { Op0(3), Op1(0), CRn(12), CRm(12), Op2(4), access_gic_ctlr },
+     /* ICC_SRE_EL1 */
+     { Op0(3), Op1(0), CRn(12), CRm(12), Op2(5), access_gic_sre },
+     /* ICC_IGRPEN0_EL1 */

arch/arm64/kvm/vgic-sys-reg-v3.c line 299

Quoted 10 lines
arch/arm64/kvm/vgic-sys-reg-v3.c new file +10 -0
+     { Op0(3), Op1(0), CRn(12), CRm(12), Op2(6), access_gic_grpen0 },
+     /* ICC_GRPEN1_EL1 */
+     { Op0(3), Op1(0), CRn(12), CRm(12), Op2(7), access_gic_grpen1 },
+};
+
+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;

At the beginning I asked myself why we did not OR with KVM_REG_ARM64 as stated in include/uapi/linux/kvm.h but then looking at index_to_params() implementation it looks it is not used.

Quoted 44 lines
arch/arm64/kvm/vgic-sys-reg-v3.c new file +39 -0
+
+     params.regval = *reg;
+     params.is_write = is_write;
+     params.is_aarch32 = false;
+     params.is_32bit = false;
+
+     if (find_reg_by_id(sysreg, &params, 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, &params, gic_v3_icc_reg_descs,
+                        ARRAY_SIZE(gic_v3_icc_reg_descs));
+     if (!r)
+             return -ENXIO;
+
+     if (!r->access(vcpu, &params, r))
+             return -EINVAL;
+
+     if (!is_write)
+             *reg = params.regval;
+
+     return 0;
+}
include/kvm/arm_vgic.h +0 -0
index 002f092..730a18a 100644

include/kvm/arm_vgic.h struct vgic_global { · line 75

Quoted 5 lines
include/kvm/arm_vgic.h +2 -0
@@ -71,6 +71,9 @@ struct vgic_global {
 
      /* GIC system register CPU interface */
      struct static_key_false gicv3_cpuif;
+
+     /* Cache ICH_VTR_EL2 reg value */

nit: comment does not bring much value I think

Quoted 16 lines
include/kvm/arm_vgic.h +7 -0
+     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;
 };
 
Quoted 11 lines

virt/kvm/arm/vgic/vgic-kvm-device.c line 19

Quoted 10 lines
include/kvm/arm_vgic.h +0 -0
 extern struct static_key_false vgic_v2_cpuif_trap;
virt/kvm/arm/vgic/vgic-kvm-device.c +1 -0
index bc7de95..b6266fe 100644
@@ -16,6 +16,7 @@
 #include <linux/kvm_host.h>
 #include <kvm/arm_vgic.h>
 #include <linux/uaccess.h>
+#include <asm/kvm_emulate.h>

not needed I think

OK.

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#10 lore raw

Re: [PATCH v10 6/8] arm/arm64: vgic: Implement VGICv3 CPU interface access

Quoted 128 lines · Auger Eric

Hi Vijaya,

Quoted 79 lines · Vijay Kilari
Quoted 69 lines · Auger Eric

Hi Vijaya,

Quoted 6 lines · vijay.kilari at gmail.com

From: Vijaya Kumar K <[email protected]>

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.

s/is/It is

Quoted 6 lines

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.

We make sure ICC_CTLR_EL1 SEIS and A3V are compatible on source and target?

Quoted 11 lines

The arch/arm64/kvm/vgic-sys-reg-v3.c handles read and write of VGIC CPU registers for AArch64.

For AArch32 mode, arch/arm/kvm/vgic-v3-coproc.c file is created but APIs are not implemented.

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

s/define/defined

Quoted 40 lines

Documentation/virtual/kvm/devices/arm-vgic-v3.txt

---

arch/arm64/kvm/vgic-sys-reg-v3.c new file +32 -0
@@ -0,0 +1,338 @@
+/*
+ * VGIC system registers handling functions for AArch64 mode
+ *
+ * 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 <linux/irqchip/arm-gic-v3.h>
+#include <linux/kvm.h>
+#include <linux/kvm_host.h>
+#include <asm/kvm_emulate.h>
+#include "vgic.h"
+#include "sys_regs.h"
+
+static bool access_gic_ctlr(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
+                         const struct sys_reg_desc *r)
+{
+     u32 host_pri_bits, host_id_bits, host_seis, host_a3v, seis, a3v;
+     struct vgic_cpu *vgic_v3_cpu = &vcpu->arch.vgic_cpu;
+     struct vgic_vmcr vmcr;
+     u64 val;
+
+     vgic_get_vmcr(vcpu, &vmcr);
+     if (p->is_write) {
+             val = p->regval;
+
Quoted 8 lines

arch/arm64/kvm/vgic-sys-reg-v3.c line 38

Quoted 6 lines
arch/arm64/kvm/vgic-sys-reg-v3.c new file +6 -0
+             /*
+              * Disallow restoring VM state if not supported by this
+              * hardware.
+              */
+             host_pri_bits = ((val & ICC_CTLR_EL1_PRI_BITS_MASK) >>
+                              ICC_CTLR_EL1_PRI_BITS_SHIFT) + 1;

I am confused by the "host" terminology. Those are the "source" values we want to restore and we compare with the destination current value, right?

Quoted 7 lines

yes

Quoted 3 lines

arch/arm64/kvm/vgic-sys-reg-v3.c line 40

Quoted 2 lines
arch/arm64/kvm/vgic-sys-reg-v3.c new file +2 -0
+             if (host_pri_bits > vgic_v3_cpu->num_pri_bits)
+                     return false;

I am lost. Who did set num_pri_bits and num_id_bits we compare with?

In vgic_v3_enable() these values are computed

OK I missed that.

Quoted 38 lines
Quoted 23 lines

Seis and a3v I get this is computed on probe

Quoted 21 lines
arch/arm64/kvm/vgic-sys-reg-v3.c new file +21 -0
+
+             vgic_v3_cpu->num_pri_bits = host_pri_bits;
+
+             host_id_bits = (val & ICC_CTLR_EL1_ID_BITS_MASK) >>
+                             ICC_CTLR_EL1_ID_BITS_SHIFT;
+             if (host_id_bits > vgic_v3_cpu->num_id_bits)
+                     return false;
+
+             vgic_v3_cpu->num_id_bits = host_id_bits;
+
+             host_seis = ((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 (host_seis != seis)
+                     return false;
+
+             host_a3v = ((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 (host_a3v != a3v)
Quoted 8 lines

arch/arm64/kvm/vgic-sys-reg-v3.c line 65

Quoted 4 lines
arch/arm64/kvm/vgic-sys-reg-v3.c new file +4 -0
+                     return false;
+
+             vmcr.ctlr = val & ICC_CTLR_EL1_CBPR_MASK;
+             vmcr.ctlr |= val & ICC_CTLR_EL1_EOImode_MASK;

nit: I still don't get why the vmcr has CPBR and EOImode set with the ICC_CTLR_EL1 layout and then this gets transformed in the proper vmcr format by vgic_set_vmcr. This is confusing to me and would at least deserve a comment attached to struct vgic_vmcr definition.

I will add a comment

Quoted 4 lines

Why don't we set the vmcr.ctlr directly in its ICH_VMCR format? In set/get_vmcr all the other struct vgic_vmcr fields are handled in ICH_VMCR native layout except the ctrl field.

Quoted 6 lines
Quoted 3 lines

None of the fields of struct vgic_vmcr are in ICH_VMCR native layout. Except ctlr all the other fields are registers having single field value.
Ex: pmr, bpr0 etc.,

OK but to me would be more natural to keep vmcr.ctlr in original format but well that's just my pov. If you add a comment that helps already.

Honestly this has been confusing and I'm forgetting the history, but I think it currently serves the purpose that code shared between v2 and v3 can look at vgic_vmcr.bpr in some canonical format that works for both v2 and v3. Originally I think the ctlr field only contained the bits that then had the common bit position between v2 and v3.

A comment will help, and someone taking a look at reworking it to be more clear would also be welcome.

But the currently code (incl this patch) looks right to me at this point.

Thanks,
-Christoffer

#11 lore raw

Re: [PATCH v10 6/8] arm/arm64: vgic: Implement VGICv3 CPU interface access

In reply to [email protected]

Hi Vijaya, Christoffer,

Quoted 46 lines · On 01/12/2016 08:09, [email protected] wrote:

From: Vijaya Kumar K <[email protected]>

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 arch/arm64/kvm/vgic-sys-reg-v3.c handles read and write of VGIC CPU registers for AArch64.

For AArch32 mode, arch/arm/kvm/vgic-v3-coproc.c file is created but APIs are not implemented.

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

---
 arch/arm/include/uapi/asm/kvm.h     |   2 +
 arch/arm/kvm/Makefile               |   4 +-
 arch/arm/kvm/vgic-v3-coproc.c       |  35 ++++
 arch/arm64/include/uapi/asm/kvm.h   |   3 +
 arch/arm64/kvm/Makefile             |   3 +-
 arch/arm64/kvm/vgic-sys-reg-v3.c    | 338 ++++++++++++++++++++++++++++++++++++
 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-v3.c         |   8 +
 virt/kvm/arm/vgic/vgic.h            |   4 +
 11 files changed, 449 insertions(+), 3 deletions(-)
arch/arm/include/uapi/asm/kvm.h +0 -0
index 0ae6035..98658d9d 100644

arch/arm/include/uapi/asm/kvm.h struct kvm_arch_memory_slot { · line 193

Quoted 8 lines
arch/arm/include/uapi/asm/kvm.h +2 -0
@@ -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

nit: arm-vgic-v3.txt spec uses that name. To be homogeneous with other groups, shouldn't we correct this into KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS?

Thanks

Eric

Quoted 613 lines
arch/arm/include/uapi/asm/kvm.h +0 -0
 #define   KVM_DEV_ARM_VGIC_CTRL_INIT    0
 
 /* KVM_IRQ_LINE irq field index values */
arch/arm/kvm/Makefile +2 -2
index d571243..68fb023 100644
@@ -7,7 +7,7 @@ ifeq ($(plus_virt),+virt)
 	plus_virt_def := -DREQUIRES_VIRT=1
 endif
 
-ccflags-y += -Iarch/arm/kvm
+ccflags-y += -Iarch/arm/kvm -Ivirt/kvm/arm/vgic
 CFLAGS_arm.o := -I. $(plus_virt_def)
 CFLAGS_mmu.o := -I.
 
@@ -20,7 +20,7 @@ kvm-arm-y = $(KVM)/kvm_main.o $(KVM)/coalesced_mmio.o $(KVM)/eventfd.o $(KVM)/vf
 obj-$(CONFIG_KVM_ARM_HOST) += hyp/
 obj-y += kvm-arm.o init.o interrupts.o
 obj-y += arm.o handle_exit.o guest.o mmu.o emulate.o reset.o
-obj-y += coproc.o coproc_a15.o coproc_a7.o mmio.o psci.o perf.o
+obj-y += coproc.o coproc_a15.o coproc_a7.o mmio.o psci.o perf.o vgic-v3-coproc.o
 obj-y += $(KVM)/arm/aarch32.o
 
 obj-y += $(KVM)/arm/vgic/vgic.o
arch/arm/kvm/vgic-v3-coproc.c new file +35 -0
new file mode 100644
index 0000000..f41abf7
@@ -0,0 +1,35 @@
+/*
+ * VGIC system registers handling functions for AArch32 mode
+ *
+ * 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 <linux/kvm.h>
+#include <linux/kvm_host.h>
+#include <asm/kvm_emulate.h>
+#include "vgic.h"
+
+int vgic_v3_has_cpu_sysregs_attr(struct kvm_vcpu *vcpu, bool is_write, u64 id,
+				 u64 *reg)
+{
+	/*
+	 * TODO: Implement for AArch32
+	 */
+	return -ENXIO;
+}
+
+int vgic_v3_cpu_sysregs_uaccess(struct kvm_vcpu *vcpu, bool is_write, u64 id,
+				u64 *reg)
+{
+	/*
+	 * TODO: Implement for AArch32
+	 */
+	return -ENXIO;
+}
arch/arm64/include/uapi/asm/kvm.h +3 -0
index 56dc08d..91c7137 100644
@@ -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 */
arch/arm64/kvm/Makefile +2 -1
index d50a82a..d89aa50 100644
@@ -2,7 +2,7 @@
 # Makefile for Kernel-based Virtual Machine module
 #
 
-ccflags-y += -Iarch/arm64/kvm
+ccflags-y += -Iarch/arm64/kvm -Ivirt/kvm/arm/vgic
 CFLAGS_arm.o := -I.
 CFLAGS_mmu.o := -I.
 
@@ -19,6 +19,7 @@ kvm-$(CONFIG_KVM_ARM_HOST) += $(ARM)/psci.o $(ARM)/perf.o
 kvm-$(CONFIG_KVM_ARM_HOST) += inject_fault.o regmap.o
 kvm-$(CONFIG_KVM_ARM_HOST) += hyp.o hyp-init.o handle_exit.o
 kvm-$(CONFIG_KVM_ARM_HOST) += guest.o debug.o reset.o sys_regs.o sys_regs_generic_v8.o
+kvm-$(CONFIG_KVM_ARM_HOST) += vgic-sys-reg-v3.o
 kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/aarch32.o
 
 kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/vgic/vgic.o
arch/arm64/kvm/vgic-sys-reg-v3.c new file +338 -0
new file mode 100644
index 0000000..76663f9
@@ -0,0 +1,338 @@
+/*
+ * VGIC system registers handling functions for AArch64 mode
+ *
+ * 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 <linux/irqchip/arm-gic-v3.h>
+#include <linux/kvm.h>
+#include <linux/kvm_host.h>
+#include <asm/kvm_emulate.h>
+#include "vgic.h"
+#include "sys_regs.h"
+
+static bool access_gic_ctlr(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
+			    const struct sys_reg_desc *r)
+{
+	u32 host_pri_bits, host_id_bits, host_seis, host_a3v, seis, a3v;
+	struct vgic_cpu *vgic_v3_cpu = &vcpu->arch.vgic_cpu;
+	struct vgic_vmcr vmcr;
+	u64 val;
+
+	vgic_get_vmcr(vcpu, &vmcr);
+	if (p->is_write) {
+		val = p->regval;
+
+		/*
+		 * Disallow restoring VM state if not supported by this
+		 * hardware.
+		 */
+		host_pri_bits = ((val & ICC_CTLR_EL1_PRI_BITS_MASK) >>
+				 ICC_CTLR_EL1_PRI_BITS_SHIFT) + 1;
+		if (host_pri_bits > vgic_v3_cpu->num_pri_bits)
+			return false;
+
+		vgic_v3_cpu->num_pri_bits = host_pri_bits;
+
+		host_id_bits = (val & ICC_CTLR_EL1_ID_BITS_MASK) >>
+				ICC_CTLR_EL1_ID_BITS_SHIFT;
+		if (host_id_bits > vgic_v3_cpu->num_id_bits)
+			return false;
+
+		vgic_v3_cpu->num_id_bits = host_id_bits;
+
+		host_seis = ((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 (host_seis != seis)
+			return false;
+
+		host_a3v = ((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 (host_a3v != 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;
+
+		p->regval = val;
+	}
+
+	return true;
+}
+
+static bool access_gic_pmr(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
+			   const struct sys_reg_desc *r)
+{
+	struct vgic_vmcr vmcr;
+
+	vgic_get_vmcr(vcpu, &vmcr);
+	if (p->is_write) {
+		vmcr.pmr = (p->regval & ICC_PMR_EL1_MASK) >> ICC_PMR_EL1_SHIFT;
+		vgic_set_vmcr(vcpu, &vmcr);
+	} else {
+		p->regval = (vmcr.pmr << ICC_PMR_EL1_SHIFT) & ICC_PMR_EL1_MASK;
+	}
+
+	return true;
+}
+
+static bool access_gic_bpr0(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
+			    const struct sys_reg_desc *r)
+{
+	struct vgic_vmcr vmcr;
+
+	vgic_get_vmcr(vcpu, &vmcr);
+	if (p->is_write) {
+		vmcr.bpr = (p->regval & ICC_BPR0_EL1_MASK) >>
+			    ICC_BPR0_EL1_SHIFT;
+		vgic_set_vmcr(vcpu, &vmcr);
+	} else {
+		p->regval = (vmcr.bpr << ICC_BPR0_EL1_SHIFT) &
+			     ICC_BPR0_EL1_MASK;
+	}
+
+	return true;
+}
+
+static bool access_gic_bpr1(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
+			    const struct sys_reg_desc *r)
+{
+	struct vgic_vmcr vmcr;
+
+	if (!p->is_write)
+		p->regval = 0;
+
+	vgic_get_vmcr(vcpu, &vmcr);
+	if (!((vmcr.ctlr & ICH_VMCR_CBPR_MASK) >> ICH_VMCR_CBPR_SHIFT)) {
+		if (p->is_write) {
+			vmcr.abpr = (p->regval & ICC_BPR1_EL1_MASK) >>
+				     ICC_BPR1_EL1_SHIFT;
+			vgic_set_vmcr(vcpu, &vmcr);
+		} else {
+			p->regval = (vmcr.abpr << ICC_BPR1_EL1_SHIFT) &
+				     ICC_BPR1_EL1_MASK;
+		}
+	} else {
+		if (!p->is_write)
+			p->regval = min((vmcr.bpr + 1), 7U);
+	}
+
+	return true;
+}
+
+static bool access_gic_grpen0(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
+			      const struct sys_reg_desc *r)
+{
+	struct vgic_vmcr vmcr;
+
+	vgic_get_vmcr(vcpu, &vmcr);
+	if (p->is_write) {
+		vmcr.grpen0 = (p->regval & ICC_IGRPEN0_EL1_MASK) >>
+			       ICC_IGRPEN0_EL1_SHIFT;
+		vgic_set_vmcr(vcpu, &vmcr);
+	} else {
+		p->regval = (vmcr.grpen0 << ICC_IGRPEN0_EL1_SHIFT) &
+			     ICC_IGRPEN0_EL1_MASK;
+	}
+
+	return true;
+}
+
+static bool access_gic_grpen1(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
+			      const struct sys_reg_desc *r)
+{
+	struct vgic_vmcr vmcr;
+
+	vgic_get_vmcr(vcpu, &vmcr);
+	if (p->is_write) {
+		vmcr.grpen1 = (p->regval & ICC_IGRPEN1_EL1_MASK) >>
+			       ICC_IGRPEN1_EL1_SHIFT;
+		vgic_set_vmcr(vcpu, &vmcr);
+	} else {
+		p->regval = (vmcr.grpen1 << ICC_IGRPEN1_EL1_SHIFT) &
+			     ICC_IGRPEN1_EL1_MASK;
+	}
+
+	return true;
+}
+
+static void vgic_v3_access_apr_reg(struct kvm_vcpu *vcpu,
+				   struct sys_reg_params *p, u8 apr, u8 idx)
+{
+	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 (p->is_write)
+		*ap_reg = p->regval;
+	else
+		p->regval = *ap_reg;
+}
+
+static bool access_gic_aprn(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
+			    const struct sys_reg_desc *r, u8 apr)
+{
+	struct vgic_cpu *vgic_v3_cpu = &vcpu->arch.vgic_cpu;
+	u8 idx = r->Op2 & 3;
+
+	/*
+	 * 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, p, apr, idx);
+		break;
+	case 6:
+		if (idx > 1)
+			goto err;
+		vgic_v3_access_apr_reg(vcpu, p, apr, idx);
+		break;
+	default:
+		if (idx > 0)
+			goto err;
+		vgic_v3_access_apr_reg(vcpu, p, apr, idx);
+	}
+
+	return true;
+err:
+	if (!p->is_write)
+		p->regval = 0;
+
+	return false;
+}
+
+static bool access_gic_ap0r(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
+			    const struct sys_reg_desc *r)
+
+{
+	return access_gic_aprn(vcpu, p, r, 0);
+}
+
+static bool access_gic_ap1r(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
+			    const struct sys_reg_desc *r)
+{
+	return access_gic_aprn(vcpu, p, r, 1);
+}
+
+static bool access_gic_sre(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
+			   const struct sys_reg_desc *r)
+{
+	struct vgic_v3_cpu_if *vgicv3 = &vcpu->arch.vgic_cpu.vgic_v3;
+
+	/* Validate SRE bit */
+	if (p->is_write) {
+		if (!(p->regval & ICC_SRE_EL1_SRE))
+			return false;
+	} else {
+		p->regval = vgicv3->vgic_sre;
+	}
+
+	return true;
+}
+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 },
+	/* ICC_BPR0_EL1 */
+	{ Op0(3), Op1(0), CRn(12), CRm(8), Op2(3), access_gic_bpr0 },
+	/* ICC_AP0R0_EL1 */
+	{ Op0(3), Op1(0), CRn(12), CRm(8), Op2(4), access_gic_ap0r },
+	/* ICC_AP0R1_EL1 */
+	{ Op0(3), Op1(0), CRn(12), CRm(8), Op2(5), access_gic_ap0r },
+	/* ICC_AP0R2_EL1 */
+	{ Op0(3), Op1(0), CRn(12), CRm(8), Op2(6), access_gic_ap0r },
+	/* ICC_AP0R3_EL1 */
+	{ Op0(3), Op1(0), CRn(12), CRm(8), Op2(7), access_gic_ap0r },
+	/* ICC_AP1R0_EL1 */
+	{ Op0(3), Op1(0), CRn(12), CRm(9), Op2(0), access_gic_ap1r },
+	/* ICC_AP1R1_EL1 */
+	{ Op0(3), Op1(0), CRn(12), CRm(9), Op2(1), access_gic_ap1r },
+	/* ICC_AP1R2_EL1 */
+	{ Op0(3), Op1(0), CRn(12), CRm(9), Op2(2), access_gic_ap1r },
+	/* ICC_AP1R3_EL1 */
+	{ Op0(3), Op1(0), CRn(12), CRm(9), Op2(3), access_gic_ap1r },
+	/* ICC_BPR1_EL1 */
+	{ Op0(3), Op1(0), CRn(12), CRm(12), Op2(3), access_gic_bpr1 },
+	/* ICC_CTLR_EL1 */
+	{ Op0(3), Op1(0), CRn(12), CRm(12), Op2(4), access_gic_ctlr },
+	/* ICC_SRE_EL1 */
+	{ Op0(3), Op1(0), CRn(12), CRm(12), Op2(5), access_gic_sre },
+	/* ICC_IGRPEN0_EL1 */
+	{ Op0(3), Op1(0), CRn(12), CRm(12), Op2(6), access_gic_grpen0 },
+	/* ICC_GRPEN1_EL1 */
+	{ Op0(3), Op1(0), CRn(12), CRm(12), Op2(7), access_gic_grpen1 },
+};
+
+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, &params, 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, &params, gic_v3_icc_reg_descs,
+			   ARRAY_SIZE(gic_v3_icc_reg_descs));
+	if (!r)
+		return -ENXIO;
+
+	if (!r->access(vcpu, &params, r))
+		return -EINVAL;
+
+	if (!is_write)
+		*reg = params.regval;
+
+	return 0;
+}
include/kvm/arm_vgic.h +9 -0
index 002f092..730a18a 100644
@@ -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;
virt/kvm/arm/vgic/vgic-kvm-device.c +28 -0
index bc7de95..b6266fe 100644
@@ -16,6 +16,7 @@
 #include <linux/kvm_host.h>
 #include <kvm/arm_vgic.h>
 #include <linux/uaccess.h>
+#include <asm/kvm_emulate.h>
 #include <asm/kvm_mmu.h>
 #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, &reg, 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, &reg, 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, &reg, 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;
virt/kvm/arm/vgic/vgic-mmio-v3.c +18 -0
index e4799ae..51439c9 100644
@@ -642,6 +642,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, &reg);
+	}
 	default:
 		return -ENXIO;
 	}
virt/kvm/arm/vgic/vgic-v3.c +8 -0
index c21968b..c98a1c5 100644
@@ -238,6 +238,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;
 }
@@ -338,6 +345,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");
virt/kvm/arm/vgic/vgic.h +4 -0
index 9232791..eac272c 100644
@@ -140,6 +140,10 @@ 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);
 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);
#12 lore raw

Re: [PATCH v10 6/8] arm/arm64: vgic: Implement VGICv3 CPU interface access

Quoted 44 lines · Auger Eric

Hi Vijaya, Christoffer,

Quoted 41 lines · vijay.kilari at gmail.com

From: Vijaya Kumar K <[email protected]>

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 arch/arm64/kvm/vgic-sys-reg-v3.c handles read and write of VGIC CPU registers for AArch64.

For AArch32 mode, arch/arm/kvm/vgic-v3-coproc.c file is created but APIs are not implemented.

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

---
 arch/arm/include/uapi/asm/kvm.h     |   2 +
 arch/arm/kvm/Makefile               |   4 +-
 arch/arm/kvm/vgic-v3-coproc.c       |  35 ++++
 arch/arm64/include/uapi/asm/kvm.h   |   3 +
 arch/arm64/kvm/Makefile             |   3 +-
 arch/arm64/kvm/vgic-sys-reg-v3.c    | 338 ++++++++++++++++++++++++++++++++++++
 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-v3.c         |   8 +
 virt/kvm/arm/vgic/vgic.h            |   4 +
 11 files changed, 449 insertions(+), 3 deletions(-)
Quoted 15 lines

arch/arm/include/uapi/asm/kvm.h struct kvm_arch_memory_slot { · line 193

Quoted 13 lines
arch/arm/include/uapi/asm/kvm.h +2 -0
index 0ae6035..98658d9d 100644
@@ -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

nit: arm-vgic-v3.txt spec uses that name. To be homogeneous with other groups, shouldn't we correct this into KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS?

Yeah, probably, as long as we use the same naming scheme and have the docs and code in sync, I'm fine with this.

Thanks,
-Christoffer

#13 lore raw

Re: [PATCH v10 6/8] arm/arm64: vgic: Implement VGICv3 CPU interface access

In reply to [email protected]
Quoted 22 lines · vijay.kilari at gmail.com

From: Vijaya Kumar K <[email protected]>

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 arch/arm64/kvm/vgic-sys-reg-v3.c handles read and write of VGIC CPU registers for AArch64.

For AArch32 mode, arch/arm/kvm/vgic-v3-coproc.c file is created but APIs are not implemented.

Updated arch/arm/include/uapi/asm/kvm.h with new definitions required to compile for AArch32.

Quoted 6 lines

The version of VGIC v3 specification is define here Documentation/virtual/kvm/devices/arm-vgic-v3.txt

---

With Eric's comments addressed on this patch, it now looks good to me.

-Christoffer

#14 lore raw

PATCH v10 7/8 arm/arm64: vgic: Implement KVM_DEV_ARM_VGIC_GRP_LEVEL_INFO ioctl

In reply to [email protected]

From: Vijaya Kumar K <[email protected]>

Userspace requires to store and restore of line_level for level triggered interrupts using ioctl KVM_DEV_ARM_VGIC_GRP_LEVEL_INFO.

---
 arch/arm/include/uapi/asm/kvm.h     |  7 ++++++
 arch/arm64/include/uapi/asm/kvm.h   |  6 +++++
 virt/kvm/arm/vgic/vgic-kvm-device.c | 45 +++++++++++++++++++++++++++++++++++-
 virt/kvm/arm/vgic/vgic-mmio-v3.c    | 11 +++++++++
 virt/kvm/arm/vgic/vgic-mmio.c       | 46 +++++++++++++++++++++++++++++++++++++
 virt/kvm/arm/vgic/vgic-mmio.h       |  5 ++++
 virt/kvm/arm/vgic/vgic.h            |  2 ++
 7 files changed, 121 insertions(+), 1 deletion(-)
arch/arm/include/uapi/asm/kvm.h +7 -0
index 98658d9d..f347779 100644
@@ -191,6 +191,13 @@ struct kvm_arch_memory_slot {
 #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_GRP_LEVEL_INFO 7
+#define KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_SHIFT	10
+#define KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_MASK \
+			(0x3fffffULL << KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_SHIFT)
+#define KVM_DEV_ARM_VGIC_LINE_LEVEL_INTID_MASK	0x3ff
+#define VGIC_LEVEL_INFO_LINE_LEVEL	0
+
 #define   KVM_DEV_ARM_VGIC_CTRL_INIT    0
 
 /* KVM_IRQ_LINE irq field index values */
arch/arm64/include/uapi/asm/kvm.h +6 -0
index 91c7137..4100f8c 100644
@@ -211,6 +211,12 @@ struct kvm_arch_memory_slot {
 #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_GRP_LEVEL_INFO 7
+#define KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_SHIFT	10
+#define KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_MASK \
+			(0x3fffffULL << KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_SHIFT)
+#define KVM_DEV_ARM_VGIC_LINE_LEVEL_INTID_MASK	0x3ff
+#define VGIC_LEVEL_INFO_LINE_LEVEL	0
 
 #define   KVM_DEV_ARM_VGIC_CTRL_INIT	0
 
virt/kvm/arm/vgic/vgic-kvm-device.c +44 -1

70 lines not on this page · raw message

virt/kvm/arm/vgic/vgic-mmio-v3.c +11 -0
index 51439c9..9491d72 100644
@@ -809,3 +809,14 @@ int vgic_v3_redist_uaccess(struct kvm_vcpu *vcpu, bool is_write,
 		return vgic_uaccess(vcpu, &rd_dev, is_write,
 				    offset, val);
 }
+
+int vgic_v3_line_level_info_uaccess(struct kvm_vcpu *vcpu, bool is_write,
+				    u32 intid, u64 *val)
+{
+	if (is_write)
+		vgic_write_irq_line_level_info(vcpu, intid, *val);
+	else
+		*val = vgic_read_irq_line_level_info(vcpu, intid);
+
+	return 0;
+}
virt/kvm/arm/vgic/vgic-mmio.c +46 -0

52 lines not on this page · raw message

virt/kvm/arm/vgic/vgic-mmio.h +5 -0
index 1cc7faf..b9423b4 100644
@@ -181,6 +181,11 @@ int vgic_validate_mmio_region_addr(struct kvm_device *dev,
 				   const struct vgic_register_region *regions,
 				   int nr_regions, gpa_t addr);
 
+u64 vgic_read_irq_line_level_info(struct kvm_vcpu *vcpu, u32 intid);
+
+void vgic_write_irq_line_level_info(struct kvm_vcpu *vcpu, u32 intid,
+				    const u64 val);
+
 unsigned int vgic_v2_init_dist_iodev(struct vgic_io_device *dev);
 
 unsigned int vgic_v3_init_dist_iodev(struct vgic_io_device *dev);
virt/kvm/arm/vgic/vgic.h +2 -0
index eac272c..60d4350 100644
@@ -144,6 +144,8 @@ 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);
+int vgic_v3_line_level_info_uaccess(struct kvm_vcpu *vcpu, bool is_write,
+				    u32 intid, u64 *val);
 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);
Signature · 1 line
1.9.1
#15 lore raw

Re: [PATCH v10 7/8] arm/arm64: vgic: Implement KVM_DEV_ARM_VGIC_GRP_LEVEL_INFO ioctl

Hi Vijaya,

Quoted 134 lines · On 01/12/2016 08:09, [email protected] wrote:

From: Vijaya Kumar K <[email protected]>

Userspace requires to store and restore of line_level for level triggered interrupts using ioctl KVM_DEV_ARM_VGIC_GRP_LEVEL_INFO.

---
 arch/arm/include/uapi/asm/kvm.h     |  7 ++++++
 arch/arm64/include/uapi/asm/kvm.h   |  6 +++++
 virt/kvm/arm/vgic/vgic-kvm-device.c | 45 +++++++++++++++++++++++++++++++++++-
 virt/kvm/arm/vgic/vgic-mmio-v3.c    | 11 +++++++++
 virt/kvm/arm/vgic/vgic-mmio.c       | 46 +++++++++++++++++++++++++++++++++++++
 virt/kvm/arm/vgic/vgic-mmio.h       |  5 ++++
 virt/kvm/arm/vgic/vgic.h            |  2 ++
 7 files changed, 121 insertions(+), 1 deletion(-)
arch/arm/include/uapi/asm/kvm.h +7 -0
index 98658d9d..f347779 100644
@@ -191,6 +191,13 @@ struct kvm_arch_memory_slot {
 #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_GRP_LEVEL_INFO 7
+#define KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_SHIFT	10
+#define KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_MASK \
+			(0x3fffffULL << KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_SHIFT)
+#define KVM_DEV_ARM_VGIC_LINE_LEVEL_INTID_MASK	0x3ff
+#define VGIC_LEVEL_INFO_LINE_LEVEL	0
+
 #define   KVM_DEV_ARM_VGIC_CTRL_INIT    0
 
 /* KVM_IRQ_LINE irq field index values */
arch/arm64/include/uapi/asm/kvm.h +6 -0
index 91c7137..4100f8c 100644
@@ -211,6 +211,12 @@ struct kvm_arch_memory_slot {
 #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_GRP_LEVEL_INFO 7
+#define KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_SHIFT	10
+#define KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_MASK \
+			(0x3fffffULL << KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_SHIFT)
+#define KVM_DEV_ARM_VGIC_LINE_LEVEL_INTID_MASK	0x3ff
+#define VGIC_LEVEL_INFO_LINE_LEVEL	0
 
 #define   KVM_DEV_ARM_VGIC_CTRL_INIT	0
 
virt/kvm/arm/vgic/vgic-kvm-device.c +44 -1
index b6266fe..d5f7197 100644
@@ -510,6 +510,21 @@ static int vgic_v3_attr_regs_access(struct kvm_device *dev,
 						  regid, reg);
 		break;
 	}
+	case KVM_DEV_ARM_VGIC_GRP_LEVEL_INFO: {
+		unsigned int info, intid;
+
+		info = (attr->attr & KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_MASK) >>
+			KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_SHIFT;
+		if (info == VGIC_LEVEL_INFO_LINE_LEVEL) {
+			intid = attr->attr &
+				KVM_DEV_ARM_VGIC_LINE_LEVEL_INTID_MASK;
+			ret = vgic_v3_line_level_info_uaccess(vcpu, is_write,
+							      intid, reg);
+		} else {
+			ret = -EINVAL;
+		}
+		break;
+	}
 	default:
 		ret = -EINVAL;
 		break;
@@ -552,6 +567,17 @@ static int vgic_v3_set_attr(struct kvm_device *dev,
 
 		return vgic_v3_attr_regs_access(dev, attr, &reg, true);
 	}
+	case KVM_DEV_ARM_VGIC_GRP_LEVEL_INFO: {
+		u32 __user *uaddr = (u32 __user *)(long)attr->addr;
+		u64 reg;
+		u32 tmp32;
+
+		if (get_user(tmp32, uaddr))
+			return -EFAULT;
+
+		reg = tmp32;
+		return vgic_v3_attr_regs_access(dev, attr, &reg, true);
+	}
 	}
 	return -ENXIO;
 }
@@ -587,8 +613,18 @@ static int vgic_v3_get_attr(struct kvm_device *dev,
 			return ret;
 		return put_user(reg, uaddr);
 	}
-	}
+	case KVM_DEV_ARM_VGIC_GRP_LEVEL_INFO: {
+		u32 __user *uaddr = (u32 __user *)(long)attr->addr;
+		u64 reg;
+		u32 tmp32;
 
+		ret = vgic_v3_attr_regs_access(dev, attr, &reg, false);
+		if (ret)
+			return ret;
+		tmp32 = reg;
+		return put_user(tmp32, uaddr);
+	}
+	}
 	return -ENXIO;
 }
 
@@ -609,6 +645,13 @@ static int vgic_v3_has_attr(struct kvm_device *dev,
 		return vgic_v3_has_attr_regs(dev, attr);
 	case KVM_DEV_ARM_VGIC_GRP_NR_IRQS:
 		return 0;
+	case KVM_DEV_ARM_VGIC_GRP_LEVEL_INFO: {
+		if (((attr->attr & KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_MASK) >>
+		      KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_SHIFT) ==
+		      VGIC_LEVEL_INFO_LINE_LEVEL)
+			return 0;
+		break;
+	}
 	case KVM_DEV_ARM_VGIC_GRP_CTRL:
 		switch (attr->attr) {
 		case KVM_DEV_ARM_VGIC_CTRL_INIT:
virt/kvm/arm/vgic/vgic-mmio-v3.c +0 -0
index 51439c9..9491d72 100644

virt/kvm/arm/vgic/vgic-mmio-v3.c int vgic_v3_redist_uaccess(struct kvm_vcpu *vcpu, bool is_write, · line 815

Quoted 7 lines
virt/kvm/arm/vgic/vgic-mmio-v3.c +4 -0
@@ -809,3 +809,14 @@ int vgic_v3_redist_uaccess(struct kvm_vcpu *vcpu, bool is_write,
 		return vgic_uaccess(vcpu, &rd_dev, is_write,
 				    offset, val);
 }
+
+int vgic_v3_line_level_info_uaccess(struct kvm_vcpu *vcpu, bool is_write,
+				    u32 intid, u64 *val)
+{

I don't see anyone checking vINTID is multiple of 32 as emphasized in Documentation/virtual/kvm/devices/arm-vgic-v3.txt

Quoted 15 lines
virt/kvm/arm/vgic/vgic-mmio-v3.c +7 -0
+	if (is_write)
+		vgic_write_irq_line_level_info(vcpu, intid, *val);
+	else
+		*val = vgic_read_irq_line_level_info(vcpu, intid);
+
+	return 0;
+}
virt/kvm/arm/vgic/vgic-mmio.c +0 -0
index f81e0e5..28fef92b 100644
@@ -371,6 +371,52 @@ void vgic_mmio_write_config(struct kvm_vcpu *vcpu,
 	}
 }
 

virt/kvm/arm/vgic/vgic-mmio.c void vgic_mmio_write_config(struct kvm_vcpu *vcpu, · line 383

Quoted 10 lines
virt/kvm/arm/vgic/vgic-mmio.c +10 -0
+u64 vgic_read_irq_line_level_info(struct kvm_vcpu *vcpu, u32 intid)
+{
+	int i;
+	u64 val = 0;
+
+	for (i = 0; i < 32; i++) {
+		struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
+
+		if (irq->line_level)
+			val |= (1U << i);

what about SGIs and higher ID than the number of interrupts supported. Spec says RAZ/WI.

Quoted 5 lines
virt/kvm/arm/vgic/vgic-mmio.c +5 -0
+
+		vgic_put_irq(vcpu->kvm, irq);
+	}
+
+	return val;

virt/kvm/arm/vgic/vgic-mmio.c void vgic_mmio_write_config(struct kvm_vcpu *vcpu, · line 398

Quoted 10 lines
virt/kvm/arm/vgic/vgic-mmio.c +10 -0
+}
+
+void vgic_write_irq_line_level_info(struct kvm_vcpu *vcpu, u32 intid,
+				    const u64 val)
+{
+	int i;
+
+	for (i = 0; i < 32; i++) {
+		struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
+

same as above.

virt/kvm/arm/vgic/vgic-mmio.c void vgic_mmio_write_config(struct kvm_vcpu *vcpu, · line 409

Quoted 11 lines
virt/kvm/arm/vgic/vgic-mmio.c +11 -0
+		spin_lock(&irq->irq_lock);
+		if (val & (1U << i)) {
+			if (irq->config == VGIC_CONFIG_LEVEL) {
+				irq->line_level = true;
+				irq->pending = true;
+				vgic_queue_irq_unlock(vcpu->kvm, irq);
+			} else {
+				spin_unlock(&irq->irq_lock);
+			}
+		} else {
+			if (irq->config == VGIC_CONFIG_EDGE ||

can't we just ignore VGIC_CONFIG_EDGE case for which line_level is not modeled?

virt/kvm/arm/vgic/vgic-mmio.c void vgic_mmio_write_config(struct kvm_vcpu *vcpu, · line 412

Quoted 3 lines
virt/kvm/arm/vgic/vgic-mmio.c +3 -0
+			    (irq->config == VGIC_CONFIG_LEVEL &&
+			    !irq->soft_pending))
+				irq->line_level = false;

To me the line level does not depend on the soft_pending bit. The pending state depends on both.

Thanks

Eric

Quoted 40 lines
virt/kvm/arm/vgic/vgic-mmio.c +7 -0
+			spin_unlock(&irq->irq_lock);
+		}
+
+		vgic_put_irq(vcpu->kvm, irq);
+	}
+}
+
 static int match_region(const void *key, const void *elt)
 {
 	const unsigned int offset = (unsigned long)key;
virt/kvm/arm/vgic/vgic-mmio.h +5 -0
index 1cc7faf..b9423b4 100644
@@ -181,6 +181,11 @@ int vgic_validate_mmio_region_addr(struct kvm_device *dev,
 				   const struct vgic_register_region *regions,
 				   int nr_regions, gpa_t addr);
 
+u64 vgic_read_irq_line_level_info(struct kvm_vcpu *vcpu, u32 intid);
+
+void vgic_write_irq_line_level_info(struct kvm_vcpu *vcpu, u32 intid,
+				    const u64 val);
+
 unsigned int vgic_v2_init_dist_iodev(struct vgic_io_device *dev);
 
 unsigned int vgic_v3_init_dist_iodev(struct vgic_io_device *dev);
virt/kvm/arm/vgic/vgic.h +2 -0
index eac272c..60d4350 100644
@@ -144,6 +144,8 @@ 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);
+int vgic_v3_line_level_info_uaccess(struct kvm_vcpu *vcpu, bool is_write,
+				    u32 intid, u64 *val);
 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);
#16 lore raw

Re: [PATCH v10 7/8] arm/arm64: vgic: Implement KVM_DEV_ARM_VGIC_GRP_LEVEL_INFO ioctl

Quoted 23 lines · Auger Eric

Hi Vijaya,

virt/kvm/arm/vgic/vgic-mmio.c void vgic_mmio_write_config(struct kvm_vcpu *vcpu, · line 398

Quoted 8 lines · On 01/12/2016 08:09, [email protected] wrote:
virt/kvm/arm/vgic/vgic-mmio.c +8 -0
+void vgic_write_irq_line_level_info(struct kvm_vcpu *vcpu, u32 intid,
+                                 const u64 val)
+{
+     int i;
+
+     for (i = 0; i < 32; i++) {
+             struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
+

same as above.

virt/kvm/arm/vgic/vgic-mmio.c void vgic_mmio_write_config(struct kvm_vcpu *vcpu, · line 409

Quoted 11 lines
virt/kvm/arm/vgic/vgic-mmio.c +11 -0
+             spin_lock(&irq->irq_lock);
+             if (val & (1U << i)) {
+                     if (irq->config == VGIC_CONFIG_LEVEL) {
+                             irq->line_level = true;
+                             irq->pending = true;
+                             vgic_queue_irq_unlock(vcpu->kvm, irq);
+                     } else {
+                             spin_unlock(&irq->irq_lock);
+                     }
+             } else {
+                     if (irq->config == VGIC_CONFIG_EDGE ||
Quoted 7 lines

can't we just ignore VGIC_CONFIG_EDGE case for which line_level is not modeled?

virt/kvm/arm/vgic/vgic-mmio.c void vgic_mmio_write_config(struct kvm_vcpu *vcpu, · line 412

Quoted 3 lines
virt/kvm/arm/vgic/vgic-mmio.c +3 -0
+                         (irq->config == VGIC_CONFIG_LEVEL &&
+                         !irq->soft_pending))
+                             irq->line_level = false;

To me the line level does not depend on the soft_pending bit. The pending state depends on both.

Without having looked at the details, it seems surprising to me that the implementation of "set line level to X" is not "set irq->line_level to X; figure out consequences and update other state as necessary"...

thanks
-- PMM

#17 lore raw

Re: [PATCH v10 7/8] arm/arm64: vgic: Implement KVM_DEV_ARM_VGIC_GRP_LEVEL_INFO ioctl

Quoted 29 lines · Peter Maydell
Quoted 28 lines · Auger Eric

Hi Vijaya,

virt/kvm/arm/vgic/vgic-mmio.c void vgic_mmio_write_config(struct kvm_vcpu *vcpu, · line 398

Quoted 8 lines · vijay.kilari at gmail.com
virt/kvm/arm/vgic/vgic-mmio.c +8 -0
+void vgic_write_irq_line_level_info(struct kvm_vcpu *vcpu, u32 intid,
+                                 const u64 val)
+{
+     int i;
+
+     for (i = 0; i < 32; i++) {
+             struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
+

same as above.

virt/kvm/arm/vgic/vgic-mmio.c void vgic_mmio_write_config(struct kvm_vcpu *vcpu, · line 409

Quoted 11 lines
virt/kvm/arm/vgic/vgic-mmio.c +11 -0
+             spin_lock(&irq->irq_lock);
+             if (val & (1U << i)) {
+                     if (irq->config == VGIC_CONFIG_LEVEL) {
+                             irq->line_level = true;
+                             irq->pending = true;
+                             vgic_queue_irq_unlock(vcpu->kvm, irq);
+                     } else {
+                             spin_unlock(&irq->irq_lock);
+                     }
+             } else {
+                     if (irq->config == VGIC_CONFIG_EDGE ||

can't we just ignore VGIC_CONFIG_EDGE case for which line_level is not modeled?

Quoted 3 lines
virt/kvm/arm/vgic/vgic-mmio.c +3 -0
+                         (irq->config == VGIC_CONFIG_LEVEL &&
+                         !irq->soft_pending))
+                             irq->line_level = false;
Quoted 8 lines
Quoted 2 lines

To me the line level does not depend on the soft_pending bit. The pending state depends on both.

Without having looked at the details, it seems surprising to me that the implementation of "set line level to X" is not "set irq->line_level to X; figure out consequences and update other state as necessary"...

Indeed. Assuming you're ok with the API requiring config state to be restored before the line level state, I think my suggestion in the separate reply should work.

Of course, we need to clarify the ordering requirement in the API.

Thanks,
-Christoffer

#18 lore raw

Re: [PATCH v10 7/8] arm/arm64: vgic: Implement KVM_DEV_ARM_VGIC_GRP_LEVEL_INFO ioctl

In reply to [email protected]
Quoted 187 lines · vijay.kilari at gmail.com

From: Vijaya Kumar K <[email protected]>

Userspace requires to store and restore of line_level for level triggered interrupts using ioctl KVM_DEV_ARM_VGIC_GRP_LEVEL_INFO.

---
 arch/arm/include/uapi/asm/kvm.h     |  7 ++++++
 arch/arm64/include/uapi/asm/kvm.h   |  6 +++++
 virt/kvm/arm/vgic/vgic-kvm-device.c | 45 +++++++++++++++++++++++++++++++++++-
 virt/kvm/arm/vgic/vgic-mmio-v3.c    | 11 +++++++++
 virt/kvm/arm/vgic/vgic-mmio.c       | 46 +++++++++++++++++++++++++++++++++++++
 virt/kvm/arm/vgic/vgic-mmio.h       |  5 ++++
 virt/kvm/arm/vgic/vgic.h            |  2 ++
 7 files changed, 121 insertions(+), 1 deletion(-)
arch/arm/include/uapi/asm/kvm.h +7 -0
index 98658d9d..f347779 100644
@@ -191,6 +191,13 @@ struct kvm_arch_memory_slot {
 #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_GRP_LEVEL_INFO 7
+#define KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_SHIFT	10
+#define KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_MASK \
+			(0x3fffffULL << KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_SHIFT)
+#define KVM_DEV_ARM_VGIC_LINE_LEVEL_INTID_MASK	0x3ff
+#define VGIC_LEVEL_INFO_LINE_LEVEL	0
+
 #define   KVM_DEV_ARM_VGIC_CTRL_INIT    0
 
 /* KVM_IRQ_LINE irq field index values */
arch/arm64/include/uapi/asm/kvm.h +6 -0
index 91c7137..4100f8c 100644
@@ -211,6 +211,12 @@ struct kvm_arch_memory_slot {
 #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_GRP_LEVEL_INFO 7
+#define KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_SHIFT	10
+#define KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_MASK \
+			(0x3fffffULL << KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_SHIFT)
+#define KVM_DEV_ARM_VGIC_LINE_LEVEL_INTID_MASK	0x3ff
+#define VGIC_LEVEL_INFO_LINE_LEVEL	0
 
 #define   KVM_DEV_ARM_VGIC_CTRL_INIT	0
 
virt/kvm/arm/vgic/vgic-kvm-device.c +44 -1
index b6266fe..d5f7197 100644
@@ -510,6 +510,21 @@ static int vgic_v3_attr_regs_access(struct kvm_device *dev,
 						  regid, reg);
 		break;
 	}
+	case KVM_DEV_ARM_VGIC_GRP_LEVEL_INFO: {
+		unsigned int info, intid;
+
+		info = (attr->attr & KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_MASK) >>
+			KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_SHIFT;
+		if (info == VGIC_LEVEL_INFO_LINE_LEVEL) {
+			intid = attr->attr &
+				KVM_DEV_ARM_VGIC_LINE_LEVEL_INTID_MASK;
+			ret = vgic_v3_line_level_info_uaccess(vcpu, is_write,
+							      intid, reg);
+		} else {
+			ret = -EINVAL;
+		}
+		break;
+	}
 	default:
 		ret = -EINVAL;
 		break;
@@ -552,6 +567,17 @@ static int vgic_v3_set_attr(struct kvm_device *dev,
 
 		return vgic_v3_attr_regs_access(dev, attr, &reg, true);
 	}
+	case KVM_DEV_ARM_VGIC_GRP_LEVEL_INFO: {
+		u32 __user *uaddr = (u32 __user *)(long)attr->addr;
+		u64 reg;
+		u32 tmp32;
+
+		if (get_user(tmp32, uaddr))
+			return -EFAULT;
+
+		reg = tmp32;
+		return vgic_v3_attr_regs_access(dev, attr, &reg, true);
+	}
 	}
 	return -ENXIO;
 }
@@ -587,8 +613,18 @@ static int vgic_v3_get_attr(struct kvm_device *dev,
 			return ret;
 		return put_user(reg, uaddr);
 	}
-	}
+	case KVM_DEV_ARM_VGIC_GRP_LEVEL_INFO: {
+		u32 __user *uaddr = (u32 __user *)(long)attr->addr;
+		u64 reg;
+		u32 tmp32;
 
+		ret = vgic_v3_attr_regs_access(dev, attr, &reg, false);
+		if (ret)
+			return ret;
+		tmp32 = reg;
+		return put_user(tmp32, uaddr);
+	}
+	}
 	return -ENXIO;
 }
 
@@ -609,6 +645,13 @@ static int vgic_v3_has_attr(struct kvm_device *dev,
 		return vgic_v3_has_attr_regs(dev, attr);
 	case KVM_DEV_ARM_VGIC_GRP_NR_IRQS:
 		return 0;
+	case KVM_DEV_ARM_VGIC_GRP_LEVEL_INFO: {
+		if (((attr->attr & KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_MASK) >>
+		      KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_SHIFT) ==
+		      VGIC_LEVEL_INFO_LINE_LEVEL)
+			return 0;
+		break;
+	}
 	case KVM_DEV_ARM_VGIC_GRP_CTRL:
 		switch (attr->attr) {
 		case KVM_DEV_ARM_VGIC_CTRL_INIT:
virt/kvm/arm/vgic/vgic-mmio-v3.c +11 -0
index 51439c9..9491d72 100644
@@ -809,3 +809,14 @@ int vgic_v3_redist_uaccess(struct kvm_vcpu *vcpu, bool is_write,
 		return vgic_uaccess(vcpu, &rd_dev, is_write,
 				    offset, val);
 }
+
+int vgic_v3_line_level_info_uaccess(struct kvm_vcpu *vcpu, bool is_write,
+				    u32 intid, u64 *val)
+{
+	if (is_write)
+		vgic_write_irq_line_level_info(vcpu, intid, *val);
+	else
+		*val = vgic_read_irq_line_level_info(vcpu, intid);
+
+	return 0;
+}
virt/kvm/arm/vgic/vgic-mmio.c +31 -0
index f81e0e5..28fef92b 100644
@@ -371,6 +371,52 @@ void vgic_mmio_write_config(struct kvm_vcpu *vcpu,
 	}
 }
 
+u64 vgic_read_irq_line_level_info(struct kvm_vcpu *vcpu, u32 intid)
+{
+	int i;
+	u64 val = 0;
+
+	for (i = 0; i < 32; i++) {
+		struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
+
+		if (irq->line_level)
+			val |= (1U << i);
+
+		vgic_put_irq(vcpu->kvm, irq);
+	}
+
+	return val;
+}
+
+void vgic_write_irq_line_level_info(struct kvm_vcpu *vcpu, u32 intid,
+				    const u64 val)
+{
+	int i;
+
+	for (i = 0; i < 32; i++) {
+		struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
+
+		spin_lock(&irq->irq_lock);
+		if (val & (1U << i)) {
+			if (irq->config == VGIC_CONFIG_LEVEL) {
+				irq->line_level = true;
+				irq->pending = true;
+				vgic_queue_irq_unlock(vcpu->kvm, irq);

virt/kvm/arm/vgic/vgic-mmio.c void vgic_mmio_write_config(struct kvm_vcpu *vcpu, · line 414

Quoted 10 lines
virt/kvm/arm/vgic/vgic-mmio.c +10 -0
+			} else {
+				spin_unlock(&irq->irq_lock);
+			}
+		} else {
+			if (irq->config == VGIC_CONFIG_EDGE ||
+			    (irq->config == VGIC_CONFIG_LEVEL &&
+			    !irq->soft_pending))
+				irq->line_level = false;
+			spin_unlock(&irq->irq_lock);
+		}

So last time we had a discussion about whether or not the API should support any random order of restoring the registers, but I cannot see how we can support that, because how can we tell the difference between the following two scenarios without knowing if an interrupt is edge-triggered or level triggered:

(1) Clearing the line_level for an edge-triggered interrupt after having set it to pending, which means it should stay pending.

(2) Clearing the line_level for a level-triggered interrupt when the state is already pending for some reason, but the soft_pending (latch) state is not set, in which case the pending state should be removed.

This leads me to conclude that userspace must restore the configuration state of the interrupt before at least the line_level, in which case the implementation becomes:

	for (i = 0; i < 32; i++) {
		struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
		bool new_level = !!(val & (1U << i));

		if (irq->config == VGIC_CONFIG_EDGE)
			continue;

		spin_lock(&irq->irq_lock);
		irq->line_level = new_level;
		if (new_level) {
			irq->pending = true;
			vgic_queue_irq_unlock(vcpu->kvm, irq);
		} else {
			if (!irq->soft_pending)
				irq->pending = false;
			spin_unlock(&irq->irq_lock);
		}

		vgic_put_irq(vcpu->kvm, irq);
	}

Thanks,
-Christoffer

#19 lore raw

Re: [PATCH v10 7/8] arm/arm64: vgic: Implement KVM_DEV_ARM_VGIC_GRP_LEVEL_INFO ioctl

Quoted 8 lines · Christoffer Dall

So last time we had a discussion about whether or not the API should support any random order of restoring the registers, but I cannot see how we can support that, because how can we tell the difference between the following two scenarios without knowing if an interrupt is edge-triggered or level triggered:

(1) Clearing the line_level for an edge-triggered interrupt after having set it to pending, which means it should stay pending.

This is userspace doing:
* set pending-latch to 1
* set linelevel to 0

right? The pending state is pending-latch | (linelevel & ~edge_trigger), and you can recalculate that when userspace updates either of the pending-latch state or linelevel. (It will be temporarily wrong before userspace has told the kernel about all the state, but that's fine because we won't try to run the VM until we've finished loading everything.)

Quoted 4 lines

(2) Clearing the line_level for a level-triggered interrupt when the state is already pending for some reason, but the soft_pending (latch) state is not set, in which case the pending state should be removed.

This is userspace doing
* set pending-latch to 0
* set line_level to 0

and thus the pending state goes to 0 (same calculation as above).

pending is always a pure logical function of pending-latch, linelevel and edge-trigger bits, so as long as you recalculate it at the right time then it shouldn't matter which order userspace tells you about the three things in.

thanks
-- PMM

#20 lore raw

Re: [PATCH v10 7/8] arm/arm64: vgic: Implement KVM_DEV_ARM_VGIC_GRP_LEVEL_INFO ioctl

Quoted 31 lines · Peter Maydell
Quoted 8 lines · Christoffer Dall

So last time we had a discussion about whether or not the API should support any random order of restoring the registers, but I cannot see how we can support that, because how can we tell the difference between the following two scenarios without knowing if an interrupt is edge-triggered or level triggered:

(1) Clearing the line_level for an edge-triggered interrupt after having set it to pending, which means it should stay pending.

This is userspace doing:
* set pending-latch to 1
* set linelevel to 0

right? The pending state is pending-latch | (linelevel & ~edge_trigger), and you can recalculate that when userspace updates either of the pending-latch state or linelevel. (It will be temporarily wrong before userspace has told the kernel about all the state, but that's fine because we won't try to run the VM until we've finished loading everything.)

Quoted 4 lines

(2) Clearing the line_level for a level-triggered interrupt when the state is already pending for some reason, but the soft_pending (latch) state is not set, in which case the pending state should be removed.

This is userspace doing
* set pending-latch to 0
* set line_level to 0

Quoted 7 lines

and thus the pending state goes to 0 (same calculation as above).

pending is always a pure logical function of pending-latch, linelevel and edge-trigger bits, so as long as you recalculate it at the right time then it shouldn't matter which order userspace tells you about the three things in.

ok, I think you're right that it can be done this way, but it has the unfortunate consequence of having to change a lot of the implementation.

The reason is that we store the latch state in two different variables, depening on whether it's an edge- or level-triggered interrupts.

We use the irq->pending field for the computed result (using above calculaiton) for level interrupts based on irq->line_level and irq->soft_pending. soft_pending is the latch state for level interrupts only.

For edge triggered interrupts the computed result and the latch state are alway the same (right?) so we we only use the irq->pending field for that.

But unless I didn't have enough coffee this morning, this only works if you have a-priori knowledge of which interrupts are level and which are edge. When this is not the case, as in the case of order-free save/restore, then I think the only solution is to rework the logic so that the soft_pending field is used for the latch state for both edge and level interrupts, and the pending field is just the internal computed value.

Thoughts?

Andre, Marc, Eric, do you agree with the above?

(I wonder how we could go through an entire redesign of the vgic and still have issues like this, but I do remember we all felt tired when we finally got to design the soft_pending stuff.)

Thanks,
-Christoffer

#21 lore raw

Re: [PATCH v10 7/8] arm/arm64: vgic: Implement KVM_DEV_ARM_VGIC_GRP_LEVEL_INFO ioctl

Quoted 15 lines · Christoffer Dall

ok, I think you're right that it can be done this way, but it has the unfortunate consequence of having to change a lot of the implementation.

The reason is that we store the latch state in two different variables, depening on whether it's an edge- or level-triggered interrupts.

We use the irq->pending field for the computed result (using above calculaiton) for level interrupts based on irq->line_level and irq->soft_pending. soft_pending is the latch state for level interrupts only.

For edge triggered interrupts the computed result and the latch state are alway the same (right?) so we we only use the irq->pending field for that.

Quoted 7 lines

But unless I didn't have enough coffee this morning, this only works if you have a-priori knowledge of which interrupts are level and which are edge. When this is not the case, as in the case of order-free save/restore, then I think the only solution is to rework the logic so that the soft_pending field is used for the latch state for both edge and level interrupts, and the pending field is just the internal computed value.

I *think* you could fudge it by saying "when the config changes from edge to level, copy the current irq->pending into irq->soft_pending". Then you can always read the latch state (it's soft_pending
if level, otherwise pending), and writing it is "set both if level, just irq->pending if edge". That in turn gives you enough information I think to cope with restores of all of (config, level, pending-latch) in any order. It feels a bit fragile, though.

thanks
-- PMM

#22 lore raw

Re: [PATCH v10 7/8] arm/arm64: vgic: Implement KVM_DEV_ARM_VGIC_GRP_LEVEL_INFO ioctl

Quoted 25 lines · Peter Maydell
Quoted 15 lines · Christoffer Dall

ok, I think you're right that it can be done this way, but it has the unfortunate consequence of having to change a lot of the implementation.

The reason is that we store the latch state in two different variables, depening on whether it's an edge- or level-triggered interrupts.

We use the irq->pending field for the computed result (using above calculaiton) for level interrupts based on irq->line_level and irq->soft_pending. soft_pending is the latch state for level interrupts only.

For edge triggered interrupts the computed result and the latch state are alway the same (right?) so we we only use the irq->pending field for that.

Quoted 7 lines

But unless I didn't have enough coffee this morning, this only works if you have a-priori knowledge of which interrupts are level and which are edge. When this is not the case, as in the case of order-free save/restore, then I think the only solution is to rework the logic so that the soft_pending field is used for the latch state for both edge and level interrupts, and the pending field is just the internal computed value.

Quoted 7 lines

I *think* you could fudge it by saying "when the config changes from edge to level, copy the current irq->pending into irq->soft_pending". Then you can always read the latch state (it's soft_pending
if level, otherwise pending), and writing it is "set both if level, just irq->pending if edge". That in turn gives you enough information I think to cope with restores of all of (config, level, pending-latch) in any order. It feels a bit fragile, though.

Right, thanks for working this out. I agree it's fragile and I cannot seem to easily convince myself it would be correct, so I sent out a patch to get rid of the pending cached state which should simplify the save/restore patches as well.

Assuming the other VGIC suspects don't object to that, I suggest we base these patches on top of that one and see if we can convince ourselves that it's correct then.

Thanks,
-Christoffer

#23 lore raw

PATCH v10 8/8 arm/arm64: Documentation: Update arm-vgic-v3.txt

In reply to [email protected]

From: Vijaya Kumar K <[email protected]>

Update error code returned for Invalid CPU interface register value and access in AArch32 mode.

---
 Documentation/virtual/kvm/devices/arm-vgic-v3.txt | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
Documentation/virtual/kvm/devices/arm-vgic-v3.txt +8 -1
index 9348b3c..0f29850 100644
@@ -142,10 +142,12 @@ Groups:
     KVM_DEV_ARM_VGIC_CPU_SYSREGS accesses the CPU interface registers for the
     CPU specified by the mpidr field.
 
+    CPU interface registers access is not implemented for AArch32 mode.
+    Error -ENXIO is returned when accessed in AArch32 mode.
   Errors:
     -ENXIO: Getting or setting this register is not yet supported
     -EBUSY: VCPU is running
-    -EINVAL: Invalid mpidr supplied
+    -EINVAL: Invalid mpidr or register value supplied
 
 
   KVM_DEV_ARM_VGIC_GRP_NR_IRQS
@@ -193,6 +195,11 @@ Groups:
 
 	Bit[n] indicates the status for interrupt vINTID + n.
 
+	Getting or setting the level info for an edge-triggered interrupt is
+	not guaranteed to work.  To restore the complete state of the VGIC, the
+	configuration (edge/level) of interrupts must be restored before
+	restoring the level.
+
     SGIs and any interrupt with a higher ID than the number of interrupts
     supported, will be RAZ/WI.  LPIs are always edge-triggered and are
     therefore not supported by this interface.
Signature · 1 line
1.9.1
#24 lore raw

Re: [PATCH v10 8/8] arm/arm64: Documentation: Update arm-vgic-v3.txt

Hi Vijaya,

Quoted 29 lines · On 01/12/2016 08:09, [email protected] wrote:

From: Vijaya Kumar K <[email protected]>

Update error code returned for Invalid CPU interface register value and access in AArch32 mode.

---
 Documentation/virtual/kvm/devices/arm-vgic-v3.txt | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
Documentation/virtual/kvm/devices/arm-vgic-v3.txt +3 -1
index 9348b3c..0f29850 100644
@@ -142,10 +142,12 @@ Groups:
     KVM_DEV_ARM_VGIC_CPU_SYSREGS accesses the CPU interface registers for the
     CPU specified by the mpidr field.
 
+    CPU interface registers access is not implemented for AArch32 mode.
+    Error -ENXIO is returned when accessed in AArch32 mode.
   Errors:
     -ENXIO: Getting or setting this register is not yet supported
     -EBUSY: VCPU is running
-    -EINVAL: Invalid mpidr supplied
+    -EINVAL: Invalid mpidr or register value supplied
 
 
   KVM_DEV_ARM_VGIC_GRP_NR_IRQS

Documentation/virtual/kvm/devices/arm-vgic-v3.txt Groups: · line 199

Quoted 5 lines
Documentation/virtual/kvm/devices/arm-vgic-v3.txt +2 -0
@@ -193,6 +195,11 @@ Groups:
 
 	Bit[n] indicates the status for interrupt vINTID + n.
 
+	Getting or setting the level info for an edge-triggered interrupt is
+	not guaranteed to work.

I don't get this statement. is the API applicable to edge triggered IRQs?

Thanks

Eric
To restore the complete state of the VGIC, the

Quoted 7 lines
Documentation/virtual/kvm/devices/arm-vgic-v3.txt +3 -0
+	configuration (edge/level) of interrupts must be restored before
+	restoring the level.
+
     SGIs and any interrupt with a higher ID than the number of interrupts
     supported, will be RAZ/WI.  LPIs are always edge-triggered and are
     therefore not supported by this interface.
#25 lore raw

Re: [PATCH v10 8/8] arm/arm64: Documentation: Update arm-vgic-v3.txt

Quoted 30 lines · Auger Eric

Hi Vijaya,

Quoted 27 lines · On 01/12/2016 08:09, [email protected] wrote:

From: Vijaya Kumar K <[email protected]>

Update error code returned for Invalid CPU interface register value and access in AArch32 mode.

---
 Documentation/virtual/kvm/devices/arm-vgic-v3.txt | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
Documentation/virtual/kvm/devices/arm-vgic-v3.txt +3 -1
index 9348b3c..0f29850 100644
@@ -142,10 +142,12 @@ Groups:
     KVM_DEV_ARM_VGIC_CPU_SYSREGS accesses the CPU interface registers for the
     CPU specified by the mpidr field.
 
+    CPU interface registers access is not implemented for AArch32 mode.
+    Error -ENXIO is returned when accessed in AArch32 mode.
   Errors:
     -ENXIO: Getting or setting this register is not yet supported
     -EBUSY: VCPU is running
-    -EINVAL: Invalid mpidr supplied
+    -EINVAL: Invalid mpidr or register value supplied
 
 
Quoted 9 lines

Documentation/virtual/kvm/devices/arm-vgic-v3.txt Groups: · line 199

Quoted 7 lines
Documentation/virtual/kvm/devices/arm-vgic-v3.txt +2 -0
   KVM_DEV_ARM_VGIC_GRP_NR_IRQS
@@ -193,6 +195,11 @@ Groups:
 
 	Bit[n] indicates the status for interrupt vINTID + n.
 
+	Getting or setting the level info for an edge-triggered interrupt is
+	not guaranteed to work.

I don't get this statement. is the API applicable to edge triggered IRQs?

I guess this could be clarified to "is ignored", but with the earlier implementation you might be able to get away with doing "line_level=x; config=LEVEL;", but you could then at the same time break something with edge-triggered IRQs, so...

Thanks for looking at this in detail.

-Christoffer

#26 lore raw

Re: [PATCH v10 8/8] arm/arm64: Documentation: Update arm-vgic-v3.txt

Hi Christoffer,

In the document,

The mpidr field is used to specify which
redistributor is accessed. The mpidr is ignored for the distributor.

We still rely on MPIDR for KVM_DEV_ARM_VGIC_GRP_DIST_REGS to fetch vcpu info. So don't we need to remove this restriction?. Or force to use vcpu[0]?

Regards
Vijay

Quoted 50 lines · Christoffer Dall
Quoted 30 lines · Auger Eric

Hi Vijaya,

Quoted 27 lines · On 01/12/2016 08:09, [email protected] wrote:

From: Vijaya Kumar K <[email protected]>

Update error code returned for Invalid CPU interface register value and access in AArch32 mode.

---
 Documentation/virtual/kvm/devices/arm-vgic-v3.txt | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
Documentation/virtual/kvm/devices/arm-vgic-v3.txt +3 -1
index 9348b3c..0f29850 100644
@@ -142,10 +142,12 @@ Groups:
     KVM_DEV_ARM_VGIC_CPU_SYSREGS accesses the CPU interface registers for the
     CPU specified by the mpidr field.
 
+    CPU interface registers access is not implemented for AArch32 mode.
+    Error -ENXIO is returned when accessed in AArch32 mode.
   Errors:
     -ENXIO: Getting or setting this register is not yet supported
     -EBUSY: VCPU is running
-    -EINVAL: Invalid mpidr supplied
+    -EINVAL: Invalid mpidr or register value supplied
 
 
Quoted 9 lines

Documentation/virtual/kvm/devices/arm-vgic-v3.txt Groups: · line 199

Quoted 7 lines
Documentation/virtual/kvm/devices/arm-vgic-v3.txt +2 -0
   KVM_DEV_ARM_VGIC_GRP_NR_IRQS
@@ -193,6 +195,11 @@ Groups:
 
    Bit[n] indicates the status for interrupt vINTID + n.
 
+   Getting or setting the level info for an edge-triggered interrupt is
+   not guaranteed to work.

I don't get this statement. is the API applicable to edge triggered IRQs?

I guess this could be clarified to "is ignored", but with the earlier implementation you might be able to get away with doing "line_level=x; config=LEVEL;", but you could then at the same time break something with edge-triggered IRQs, so...

Thanks for looking at this in detail.

-Christoffer

#27 lore raw

Re: [PATCH v10 8/8] arm/arm64: Documentation: Update arm-vgic-v3.txt

Quoted 10 lines · Vijay Kilari

Hi Christoffer,

In the document,

The mpidr field is used to specify which
redistributor is accessed. The mpidr is ignored for the distributor.

We still rely on MPIDR for KVM_DEV_ARM_VGIC_GRP_DIST_REGS to fetch vcpu info. So don't we need to remove this restriction?. Or force to use vcpu[0]?

Does the data we return ever change if you supply a diferent MPIDR and access a distributor register? If not (and I don't think it should), then the API is properly defined but the internal implementation should not rely on the value provided by user space.

Thanks,
-Christoffer

Quoted 53 lines
Quoted 50 lines · Christoffer Dall
Quoted 30 lines · Auger Eric

Hi Vijaya,

Quoted 27 lines · vijay.kilari at gmail.com

From: Vijaya Kumar K <[email protected]>

Update error code returned for Invalid CPU interface register value and access in AArch32 mode.

---
 Documentation/virtual/kvm/devices/arm-vgic-v3.txt | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
Documentation/virtual/kvm/devices/arm-vgic-v3.txt +3 -1
index 9348b3c..0f29850 100644
@@ -142,10 +142,12 @@ Groups:
     KVM_DEV_ARM_VGIC_CPU_SYSREGS accesses the CPU interface registers for the
     CPU specified by the mpidr field.
 
+    CPU interface registers access is not implemented for AArch32 mode.
+    Error -ENXIO is returned when accessed in AArch32 mode.
   Errors:
     -ENXIO: Getting or setting this register is not yet supported
     -EBUSY: VCPU is running
-    -EINVAL: Invalid mpidr supplied
+    -EINVAL: Invalid mpidr or register value supplied
 
 
Quoted 9 lines

Documentation/virtual/kvm/devices/arm-vgic-v3.txt Groups: · line 199

Quoted 7 lines
Documentation/virtual/kvm/devices/arm-vgic-v3.txt +2 -0
   KVM_DEV_ARM_VGIC_GRP_NR_IRQS
@@ -193,6 +195,11 @@ Groups:
 
    Bit[n] indicates the status for interrupt vINTID + n.
 
+   Getting or setting the level info for an edge-triggered interrupt is
+   not guaranteed to work.

I don't get this statement. is the API applicable to edge triggered IRQs?

I guess this could be clarified to "is ignored", but with the earlier implementation you might be able to get away with doing "line_level=x; config=LEVEL;", but you could then at the same time break something with edge-triggered IRQs, so...

Thanks for looking at this in detail.

-Christoffer

#28 lore raw

Re: [PATCH v10 8/8] arm/arm64: Documentation: Update arm-vgic-v3.txt

Quoted 8 lines · Christoffer Dall
Quoted 7 lines · Vijay Kilari

Hi Christoffer,

In the document,

The mpidr field is used to specify which
redistributor is accessed. The mpidr is ignored for the distributor.

Quoted 8 lines
Quoted 3 lines

We still rely on MPIDR for KVM_DEV_ARM_VGIC_GRP_DIST_REGS to fetch vcpu info. So don't we need to remove this restriction?. Or force to use vcpu[0]?

Does the data we return ever change if you supply a diferent MPIDR and access a distributor register? If not (and I don't think it should), then the API is properly defined but the internal implementation should not rely on the value provided by user space.

Data does not change. However the common uaccess code expects vcpu.

Quoted 57 lines

Thanks,
-Christoffer

Quoted 53 lines
Quoted 50 lines · Christoffer Dall
Quoted 30 lines · Auger Eric

Hi Vijaya,

Quoted 27 lines · On 01/12/2016 08:09, [email protected] wrote:

From: Vijaya Kumar K <[email protected]>

Update error code returned for Invalid CPU interface register value and access in AArch32 mode.

---
 Documentation/virtual/kvm/devices/arm-vgic-v3.txt | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
Documentation/virtual/kvm/devices/arm-vgic-v3.txt +3 -1
index 9348b3c..0f29850 100644
@@ -142,10 +142,12 @@ Groups:
     KVM_DEV_ARM_VGIC_CPU_SYSREGS accesses the CPU interface registers for the
     CPU specified by the mpidr field.
 
+    CPU interface registers access is not implemented for AArch32 mode.
+    Error -ENXIO is returned when accessed in AArch32 mode.
   Errors:
     -ENXIO: Getting or setting this register is not yet supported
     -EBUSY: VCPU is running
-    -EINVAL: Invalid mpidr supplied
+    -EINVAL: Invalid mpidr or register value supplied
 
 
Quoted 9 lines

Documentation/virtual/kvm/devices/arm-vgic-v3.txt Groups: · line 199

Quoted 7 lines
Documentation/virtual/kvm/devices/arm-vgic-v3.txt +2 -0
   KVM_DEV_ARM_VGIC_GRP_NR_IRQS
@@ -193,6 +195,11 @@ Groups:
 
    Bit[n] indicates the status for interrupt vINTID + n.
 
+   Getting or setting the level info for an edge-triggered interrupt is
+   not guaranteed to work.

I don't get this statement. is the API applicable to edge triggered IRQs?

I guess this could be clarified to "is ignored", but with the earlier implementation you might be able to get away with doing "line_level=x; config=LEVEL;", but you could then at the same time break something with edge-triggered IRQs, so...

Thanks for looking at this in detail.

-Christoffer

#29 lore raw

Re: [PATCH v10 8/8] arm/arm64: Documentation: Update arm-vgic-v3.txt

Quoted 14 lines · Vijay Kilari
Quoted 12 lines · Christoffer Dall
Quoted 10 lines · Vijay Kilari

Hi Christoffer,

In the document,

The mpidr field is used to specify which
redistributor is accessed. The mpidr is ignored for the distributor.

We still rely on MPIDR for KVM_DEV_ARM_VGIC_GRP_DIST_REGS to fetch vcpu info. So don't we need to remove this restriction?. Or force to use vcpu[0]?

Quoted 7 lines
Quoted 4 lines

Does the data we return ever change if you supply a diferent MPIDR and access a distributor register? If not (and I don't think it should), then the API is properly defined but the internal implementation should not rely on the value provided by user space.

Data does not change. However the common uaccess code expects vcpu.

Just use vcpu[0] if it's not worth refactoring then.

-Christoffer

#30 lore raw
In reply to [email protected]

Hi Vijaya,

Quoted 7 lines · On 2016/12/1 15:09, [email protected] wrote:

From: Vijaya Kumar K <[email protected]>

This patchset adds API for saving and restoring
of VGICv3 registers to support live migration with new vgic feature. This API definition is as per version of VGICv3 specification Documentation/virtual/kvm/devices/arm-vgic-v3.txt

Quoted 5 lines

The patch 3 & 4 are picked from the Pavel's previous implementation.
http://www.spinics.net/lists/kvm/msg122040.html

NOTE: Only compilation tested for AArch32. No hardware to test.

Where can I fetch the latest corresponding QEMU patches? I didn't find them in qemu-devel/qemu-arm mail list.

Thanks,

Signature · 1 line
Shannon
#31 lore raw

Hi Shannon,

Quoted 10 lines · Shannon Zhao

Hi Vijaya,

Quoted 7 lines · On 2016/12/1 15:09, [email protected] wrote:

From: Vijaya Kumar K <[email protected]>

This patchset adds API for saving and restoring
of VGICv3 registers to support live migration with new vgic feature. This API definition is as per version of VGICv3 specification Documentation/virtual/kvm/devices/arm-vgic-v3.txt

Quoted 8 lines
Quoted 5 lines

The patch 3 & 4 are picked from the Pavel's previous implementation.
http://www.spinics.net/lists/kvm/msg122040.html

NOTE: Only compilation tested for AArch32. No hardware to test.

Where can I fetch the latest corresponding QEMU patches? I didn't find them in qemu-devel/qemu-arm mail list.

Vijaya must have gotten something wrong in sending to the lists, because I couldn't find them in the archive either, and Eric also couldn't find them. But I was cc'ed, so I've bounced them your way.

-Christoffer

#32 lore raw
In reply to [email protected]

Hi Vijaya,

Quoted 7 lines · vijay.kilari at gmail.com

From: Vijaya Kumar K <[email protected]>

This patchset adds API for saving and restoring
of VGICv3 registers to support live migration with new vgic feature. This API definition is as per version of VGICv3 specification Documentation/virtual/kvm/devices/arm-vgic-v3.txt

Quoted 5 lines

The patch 3 & 4 are picked from the Pavel's previous implementation.
http://www.spinics.net/lists/kvm/msg122040.html

NOTE: Only compilation tested for AArch32. No hardware to test.

I'm mostly happy with this series at this point, and everything beyond the ABI discussion can be fixed later if we should spot anything.

Assuming we can agree on the API for the level_info shortly, can you address the other comments in the series and reabse on v4.10-rc4 (or rc5 if that's out when you get there) and send out a new version that we can queue?

Thanks,
-Christoffer

Quoted 106 lines

v9 => v10:
- Dropped support for AArch32 mode.
- Fixed line level update
- Updated documentation
- Moved vgic-sys-reg-v3.c to arch/arm64/kvm/ and added vgic-v3-coproc.c to arch/arm/kvm for AArch32
- Fixed nits

v8 => v9:
- Rebased to kvmarm/next branch
- Introduce support for save and restore of CPU interface registers for AArch32 mode (9,10 and 11 patches). Only compilation tested.
- Fixed vmcr.ctlr format
- Updated error code for invalid CPU REG value in Documentation
- Updated commit messages and added comments required
- Queued IRQ when irq_line is set.
- Compatibility check on ICC_CTLR_EL1.SEIS and A3V

v7 => v8:
- Rebased to 4.9-rc3
- Fixed wrong parameter to VGIC_TO_MPIDR
v6 => v7:
- Rename all patches heading from vgic-new to vgic
- Moved caching of priority and ID bits from vgic global struct to vgic_cpu struct.

v5 => v6:
- Collated all register definitions to single patch (4)
- Introduce macro to convert userspace MPIDR format to MPIDR reg format
- Check on ICC_CTLR_EL1.CBPR value is made while accessing ICC_BPR1_EL1
- Cached ich_vtr_el2 and guests priority and ID bits
- Check on number of priority and ID bits when ICC_CTRL_EL1 write is made
- Check is made on SRE bit for ICC_SRE_EL1 write

v4 => v5:
- ICC_CTLR_EL1 access is updated to reflect HW values
- Updated ICC reg access mask and shift macros
- Introduced patch 4 for VMCR changes
- Other minor fixes.
v3 => v4:
- Rebased to latest code base
- Moved vgic_uaccess() from vgic-mmio-v2.c to vgic-mmio.c
- Dropped macro REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED_UACCESS
- Dropped LE conversion for userspace access
- Introduced vgic_uaccess_write_pending() for ISPENDR write
- Change macro KVM_DEV_ARM_VGIC_V3_CPUID_MASK to KVM_DEV_ARM_VGIC_V3_MIDR_MASK
- Refactored some code as common code.
- Changed handing of ICC_* registers
- Allowed ICC_SRE_EL1 read by userspace
- Fixed KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_* macros

v2 => v3:
- Implemented separate API for ISPENDR and ICPENDR to
read soft_pending instead of pending for level triggerred interrupts
- Implemented ioctl KVM_DEV_ARM_VGIC_GRP_LEVEL_INFO to access line level
- Rebased on top of Christoffer's patch set
http://www.spinics.net/lists/kvm/msg136840.html

NOTE: GICD_STATUSR and GICR_STATUSR are implemented as RAZ/WI.

v1 => v2:
- The init sequence change patch is no more required.
Fixed in patch 2 by using static vgic_io_dev regions structure instead of using dynamic allocation pointer.
- Updated commit message of patch 4.
- Dropped usage of union to manage 32-bit and 64-bit access in patch 1. Used local variable for 32-bit access.
- Updated macro __ARM64_SYS_REG and ARM64_SYS_REG in arch/arm64/include/uapi/asm/kvm.h as per qemu requirements.

Vijaya Kumar K (8):
arm/arm64: vgic: Implement support for userspace access arm/arm64: vgic: Add distributor and redistributor access arm/arm64: vgic: Introduce find_reg_by_id() irqchip/gic-v3: Add missing system register definitions arm/arm64: vgic: Introduce VENG0 and VENG1 fields to vmcr struct arm/arm64: vgic: Implement VGICv3 CPU interface access arm/arm64: vgic: Implement KVM_DEV_ARM_VGIC_GRP_LEVEL_INFO ioctl arm/arm64: Documentation: Update arm-vgic-v3.txt

 Documentation/virtual/kvm/devices/arm-vgic-v3.txt |   9 +-
 arch/arm/include/uapi/asm/kvm.h                   |  13 +
 arch/arm/kvm/Makefile                             |   4 +-
 arch/arm/kvm/vgic-v3-coproc.c                     |  35 +++
 arch/arm64/include/uapi/asm/kvm.h                 |  13 +
 arch/arm64/kvm/Makefile                           |   3 +-
 arch/arm64/kvm/sys_regs.c                         |  22 +-
 arch/arm64/kvm/sys_regs.h                         |   4 +
 arch/arm64/kvm/vgic-sys-reg-v3.c                  | 338 ++++++++++++++++++++++
 include/kvm/arm_vgic.h                            |   9 +
 include/linux/irqchip/arm-gic-v3.h                |  45 ++-
 virt/kvm/arm/vgic/vgic-kvm-device.c               | 215 +++++++++++++-
 virt/kvm/arm/vgic/vgic-mmio-v2.c                  |  57 +---
 virt/kvm/arm/vgic/vgic-mmio-v3.c                  | 204 +++++++++++--
 virt/kvm/arm/vgic/vgic-mmio.c                     | 162 ++++++++++-
 virt/kvm/arm/vgic/vgic-mmio.h                     |  28 ++
 virt/kvm/arm/vgic/vgic-v3.c                       |  28 +-
 virt/kvm/arm/vgic/vgic.h                          |  60 +++-
 18 files changed, 1144 insertions(+), 105 deletions(-)
 create mode 100644 arch/arm/kvm/vgic-v3-coproc.c
 create mode 100644 arch/arm64/kvm/vgic-sys-reg-v3.c
Signature · 1 line
1.9.1
#33 lore raw
Quoted 16 lines · Christoffer Dall

Hi Vijaya,

Quoted 7 lines · On Thu, Dec 01, 2016 at 12:39:39PM +0530, [email protected] wrote:

From: Vijaya Kumar K <[email protected]>

This patchset adds API for saving and restoring
of VGICv3 registers to support live migration with new vgic feature. This API definition is as per version of VGICv3 specification Documentation/virtual/kvm/devices/arm-vgic-v3.txt

Quoted 5 lines

The patch 3 & 4 are picked from the Pavel's previous implementation.
http://www.spinics.net/lists/kvm/msg122040.html

NOTE: Only compilation tested for AArch32. No hardware to test.

Quoted 7 lines

I'm mostly happy with this series at this point, and everything beyond the ABI discussion can be fixed later if we should spot anything.

Assuming we can agree on the API for the level_info shortly, can you address the other comments in the series and reabse on v4.10-rc4 (or rc5 if that's out when you get there) and send out a new version that we can queue?

OK.

Regards
Vijay