KVM: arm64: vgic-its: Implement vgic_its_has_attr_regs and attr_regs_access
authorEric Auger <eric.auger@redhat.com>
Tue, 20 Dec 2016 08:33:13 +0000 (09:33 +0100)
committerChristoffer Dall <cdall@linaro.org>
Mon, 8 May 2017 12:33:54 +0000 (14:33 +0200)
This patch implements vgic_its_has_attr_regs and vgic_its_attr_regs_access
upon the MMIO framework. VGIC ITS KVM device KVM_DEV_ARM_VGIC_GRP_ITS_REGS
group becomes functional.

At least GITS_CREADR and GITS_IIDR require to differentiate a guest write
action from a user access. As such let's introduce a new uaccess_its_write
vgic_register_region callback.

Signed-off-by: Eric Auger <eric.auger@redhat.com>
Reviewed-by: Christoffer Dall <cdall@linaro.org>
Reviewed-by: Marc Zyngier <marc.zyngier@arm.com>
virt/kvm/arm/vgic/vgic-its.c
virt/kvm/arm/vgic/vgic-mmio.h

index f687e91741f0c59467c674216a7700d37829470e..fd2660d0d6943aba7a074f82e7f498c48c879ed8 100644 (file)
@@ -1469,14 +1469,89 @@ static void vgic_its_destroy(struct kvm_device *kvm_dev)
 int vgic_its_has_attr_regs(struct kvm_device *dev,
                           struct kvm_device_attr *attr)
 {
-       return -ENXIO;
+       const struct vgic_register_region *region;
+       gpa_t offset = attr->attr;
+       int align;
+
+       align = (offset < GITS_TYPER) || (offset >= GITS_PIDR4) ? 0x3 : 0x7;
+
+       if (offset & align)
+               return -EINVAL;
+
+       region = vgic_find_mmio_region(its_registers,
+                                      ARRAY_SIZE(its_registers),
+                                      offset);
+       if (!region)
+               return -ENXIO;
+
+       return 0;
 }
 
 int vgic_its_attr_regs_access(struct kvm_device *dev,
                              struct kvm_device_attr *attr,
                              u64 *reg, bool is_write)
 {
-       return -ENXIO;
+       const struct vgic_register_region *region;
+       struct vgic_its *its;
+       gpa_t addr, offset;
+       unsigned int len;
+       int align, ret = 0;
+
+       its = dev->private;
+       offset = attr->attr;
+
+       /*
+        * Although the spec supports upper/lower 32-bit accesses to
+        * 64-bit ITS registers, the userspace ABI requires 64-bit
+        * accesses to all 64-bit wide registers. We therefore only
+        * support 32-bit accesses to GITS_CTLR, GITS_IIDR and GITS ID
+        * registers
+        */
+       if ((offset < GITS_TYPER) || (offset >= GITS_PIDR4))
+               align = 0x3;
+       else
+               align = 0x7;
+
+       if (offset & align)
+               return -EINVAL;
+
+       mutex_lock(&dev->kvm->lock);
+
+       if (IS_VGIC_ADDR_UNDEF(its->vgic_its_base)) {
+               ret = -ENXIO;
+               goto out;
+       }
+
+       region = vgic_find_mmio_region(its_registers,
+                                      ARRAY_SIZE(its_registers),
+                                      offset);
+       if (!region) {
+               ret = -ENXIO;
+               goto out;
+       }
+
+       if (!lock_all_vcpus(dev->kvm)) {
+               ret = -EBUSY;
+               goto out;
+       }
+
+       addr = its->vgic_its_base + offset;
+
+       len = region->access_flags & VGIC_ACCESS_64bit ? 8 : 4;
+
+       if (is_write) {
+               if (region->uaccess_its_write)
+                       ret = region->uaccess_its_write(dev->kvm, its, addr,
+                                                       len, *reg);
+               else
+                       region->its_write(dev->kvm, its, addr, len, *reg);
+       } else {
+               *reg = region->its_read(dev->kvm, its, addr, len);
+       }
+       unlock_all_vcpus(dev->kvm);
+out:
+       mutex_unlock(&dev->kvm->lock);
+       return ret;
 }
 
 static int vgic_its_has_attr(struct kvm_device *dev,
index 6eec91bb16576e71649fce21226f046e8d2672da..ea4171acdef3b2da35326fb2ded7dd1b37b8eaa1 100644 (file)
@@ -36,8 +36,13 @@ struct vgic_register_region {
        };
        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);
+       union {
+               void (*uaccess_write)(struct kvm_vcpu *vcpu, gpa_t addr,
+                                     unsigned int len, unsigned long val);
+               int (*uaccess_its_write)(struct kvm *kvm, struct vgic_its *its,
+                                        gpa_t addr, unsigned int len,
+                                        unsigned long val);
+       };
 };
 
 extern struct kvm_io_device_ops kvm_io_gic_ops;