Skip to contentSkip to conversation

New VGIC(-v2) implementation

PATCH In review Andre Przywara 6 participants 122 messages Mar 21, 2018 – Apr 3, 2018 Page 2 of 2

lore
Share & tools
lore
Acked-by on v3 37/39
#115 lore raw

Re: [PATCH v3 37/39] ARM: new VGIC: vgic-init: implement map_resources

In reply to Andre Przywara
Quoted 35 lines · Andre Przywara

map_resources is the last initialization step needed before the first VCPU is run. At that stage the code stores the MMIO base addresses used. Also it registers the respective register frames with the MMIO framework.

This is based on Linux commit cbae53e663ea, written by Eric Auger.

---
 xen/arch/arm/vgic/vgic-v2.c | 66 +++++++++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/vgic/vgic.h    |  1 +
 2 files changed, 67 insertions(+)
xen/arch/arm/vgic/vgic-v2.c +14 -0
index ce77e58857..5516a8534f 100644
@@ -235,6 +235,72 @@ void vgic_v2_enable(struct vcpu *vcpu)
     gic_hw_ops->update_hcr_status(GICH_HCR_EN, true);
 }
 
+int vgic_v2_map_resources(struct domain *d)
+{
+    struct vgic_dist *dist = &d->arch.vgic;
+    paddr_t cbase, csize;
+    paddr_t vbase;
+    int ret;
+
+    /*
+     * The hardware domain gets the hardware address.
+     * Guests get the virtual platform layout.
+     */
+    if ( is_hardware_domain(d) )
+    {
+        d->arch.vgic.vgic_dist_base = gic_v2_hw_data.dbase;

xen/arch/arm/vgic/vgic-v2.c void vgic_v2_enable(struct vcpu *vcpu) · line 261

Quoted 10 lines
xen/arch/arm/vgic/vgic-v2.c +10 -0
+        /*
+         * For the hardware domain, we always map the whole HW CPU
+         * interface region in order to match the device tree (the "reg"
+         * properties is copied as it is).
+         * Note that we assume the size of the CPU interface is always
+         * aligned to PAGE_SIZE.
+         */
+        cbase = gic_v2_hw_data.cbase; /* was: dist->vgic_cpu_base */
+        csize = gic_v2_hw_data.csize;
+        vbase = gic_v2_hw_data.vbase; /* was: kvm_vgic_global_state.vcpu_base */

NIT: do we really need "was: kvm_vgic_global_state.vcpu_base" here?

Quoted 30 lines
xen/arch/arm/vgic/vgic-v2.c +30 -0
+    }
+    else
+    {
+        d->arch.vgic.vgic_dist_base = GUEST_GICD_BASE;
+        /*
+         * The CPU interface exposed to the guest is always 8kB. We may
+         * need to add an offset to the virtual CPU interface base
+         * address when in the GIC is aliased to get a 8kB contiguous
+         * region.
+         */
+        BUILD_BUG_ON(GUEST_GICC_SIZE != SZ_8K);
+        cbase = GUEST_GICC_BASE;
+        csize = GUEST_GICC_SIZE;
+        vbase = gic_v2_hw_data.vbase + gic_v2_hw_data.aliased_offset;
+    }
+
+
+    ret = vgic_register_dist_iodev(d, gaddr_to_gfn(dist->vgic_dist_base),
+                                   VGIC_V2);
+    if ( ret )
+    {
+        gdprintk(XENLOG_ERR, "Unable to register VGIC MMIO regions\n");
+        return ret;
+    }
+
+    /*
+     * Map the gic virtual cpu interface in the gic cpu interface
+     * region of the guest.
+     */
+    ret = map_mmio_regions(d, gaddr_to_gfn(cbase), csize / PAGE_SIZE,

xen/arch/arm/vgic/vgic-v2.c void vgic_v2_enable(struct vcpu *vcpu) · line 301

Quoted 10 lines
xen/arch/arm/vgic/vgic-v2.c +10 -0
+                           maddr_to_mfn(vbase));
+    if ( ret )
+    {
+        gdprintk(XENLOG_ERR, "Unable to remap VGIC CPU to VCPU\n");
+        return ret;
+    }
+
+    dist->ready = true;
+
+        return 0;

Code style

With these fixed:

Quoted 20 lines
xen/arch/arm/vgic/vgic-v2.c +2 -0
+}
+
 /*
  * Local variables:
  * mode: C
xen/arch/arm/vgic/vgic.h +1 -0
index 112952fbf9..e8e407adbe 100644
@@ -67,6 +67,7 @@ void vgic_v2_fold_lr_state(struct vcpu *vcpu);
 void vgic_v2_populate_lr(struct vcpu *vcpu, struct vgic_irq *irq, int lr);
 void vgic_v2_set_underflow(struct vcpu *vcpu);
 void vgic_v2_enable(struct vcpu *vcpu);
+int vgic_v2_map_resources(struct domain *d);
 int vgic_register_dist_iodev(struct domain *d, gfn_t dist_base_fn,
                              enum vgic_type);
 
Signature · 1 line
2.14.1

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#116 lore raw

PATCH v3 38/39 ARM: new VGIC: Allocate two pages for struct vcpu

In reply to Andre Przywara

At the moment we allocate exactly one page for struct vcpu on ARM, also have a check in place to prevent it growing beyond 4KB.
As the struct includes the state of all 32 private (per-VCPU) interrupts, we are at 3840 bytes on arm64 at the moment already. Growing the per-IRQ VGIC structure even slightly makes the VCPU quickly exceed the 4K limit. The new VGIC will need more space per virtual IRQ. I spent a few hours trying to trim this down, but couldn't get it below 4KB, even with the nasty hacks piling up to save some bytes here and there.
It turns out that beyond efficiency, maybe, there is no real technical reason this struct has to fit in one page, so lifting the limit to two pages seems like the most pragmatic solution.
Restrict this to compiling with the new VGIC and for ARM64 only.

---
Changelog v2 ... v3:
- rework alloc_vcpu_struct() to avoid nasty #ifdef

Changelog v1 ... v2:
- confine change to new VGIC and ARM64 only

 xen/arch/arm/domain.c | 25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)
xen/arch/arm/domain.c +21 -4
index 9688e62f78..23bda3f7db 100644
@@ -505,19 +505,36 @@ void dump_pageframe_info(struct domain *d)
 
 }
 
+/*
+ * The new VGIC has a bigger per-IRQ structure, so we need more than one
+ * page on ARM64. Cowardly increase the limit in this case.
+ */
+#if defined(CONFIG_NEW_VGIC) && defined(CONFIG_ARM_64)
+#define PAGES_PER_VCPU  2
+#else
+#define PAGES_PER_VCPU  1
+#endif
+
 struct vcpu *alloc_vcpu_struct(void)
 {
     struct vcpu *v;
-    BUILD_BUG_ON(sizeof(*v) > PAGE_SIZE);
-    v = alloc_xenheap_pages(0, 0);
+
+    BUILD_BUG_ON(sizeof(*v) > PAGES_PER_VCPU * PAGE_SIZE);
+    v = alloc_xenheap_pages(get_order_from_pages(PAGES_PER_VCPU), 0);
     if ( v != NULL )
-        clear_page(v);
+    {
+        unsigned int i;
+
+        for ( i = 0; i < PAGES_PER_VCPU; i++ )
+            clear_page((void *)v + i * PAGE_SIZE);
+    }
+
     return v;
 }
 
 void free_vcpu_struct(struct vcpu *v)
 {
-    free_xenheap_page(v);
+    free_xenheap_pages(v, get_order_from_pages(PAGES_PER_VCPU));
 }
 
 int vcpu_initialise(struct vcpu *v)
Signature · 7 lines
2.14.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Acked-by on v3 38/39
#117 lore raw

Re: [PATCH v3 38/39] ARM: new VGIC: Allocate two pages for struct vcpu

Hi Andre,

Quoted 40 lines · Andre Przywara

At the moment we allocate exactly one page for struct vcpu on ARM, also have a check in place to prevent it growing beyond 4KB.
As the struct includes the state of all 32 private (per-VCPU) interrupts, we are at 3840 bytes on arm64 at the moment already. Growing the per-IRQ VGIC structure even slightly makes the VCPU quickly exceed the 4K limit. The new VGIC will need more space per virtual IRQ. I spent a few hours trying to trim this down, but couldn't get it below 4KB, even with the nasty hacks piling up to save some bytes here and there.
It turns out that beyond efficiency, maybe, there is no real technical reason this struct has to fit in one page, so lifting the limit to two pages seems like the most pragmatic solution.
Restrict this to compiling with the new VGIC and for ARM64 only.

---
Changelog v2 ... v3:
- rework alloc_vcpu_struct() to avoid nasty #ifdef

Changelog v1 ... v2:
- confine change to new VGIC and ARM64 only

  xen/arch/arm/domain.c | 25 +++++++++++++++++++++----
  1 file changed, 21 insertions(+), 4 deletions(-)
xen/arch/arm/domain.c +8 -0
index 9688e62f78..23bda3f7db 100644
@@ -505,19 +505,36 @@ void dump_pageframe_info(struct domain *d)
 
  }
 
+/*
+ * The new VGIC has a bigger per-IRQ structure, so we need more than one
+ * page on ARM64. Cowardly increase the limit in this case.
+ */
+#if defined(CONFIG_NEW_VGIC) && defined(CONFIG_ARM_64)
+#define PAGES_PER_VCPU  2
+#else
+#define PAGES_PER_VCPU  1

xen/arch/arm/domain.c void dump_pageframe_info(struct domain *d) · line 523

Quoted 10 lines
xen/arch/arm/domain.c +5 -2
+#endif
+
  struct vcpu *alloc_vcpu_struct(void)
  {
      struct vcpu *v;
-    BUILD_BUG_ON(sizeof(*v) > PAGE_SIZE);
-    v = alloc_xenheap_pages(0, 0);
+
+    BUILD_BUG_ON(sizeof(*v) > PAGES_PER_VCPU * PAGE_SIZE);
+    v = alloc_xenheap_pages(get_order_from_pages(PAGES_PER_VCPU), 0);

I was suggesting to use get_order_from_pages(sizeof (...)) so if we end up to be smaller, you don't lose a page for nothing. But I am ok with that too and can revisit later. So:

Quoted 10 lines
xen/arch/arm/domain.c +7 -1
      if ( v != NULL )
-        clear_page(v);
+    {
+        unsigned int i;
+
+        for ( i = 0; i < PAGES_PER_VCPU; i++ )
+            clear_page((void *)v + i * PAGE_SIZE);
+    }
+

xen/arch/arm/domain.c void dump_pageframe_info(struct domain *d) · line 540

Quoted 10 lines
xen/arch/arm/domain.c +1 -1
      return v;
  }
 
  void free_vcpu_struct(struct vcpu *v)
  {
-    free_xenheap_page(v);
+    free_xenheap_pages(v, get_order_from_pages(PAGES_PER_VCPU));
  }
 
  int vcpu_initialise(struct vcpu *v)

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#118 lore raw

Re: [PATCH v3 38/39] ARM: new VGIC: Allocate two pages for struct vcpu

Quoted 56 lines · Julien Grall

Hi Andre,

Quoted 38 lines · Andre Przywara

At the moment we allocate exactly one page for struct vcpu on ARM, also have a check in place to prevent it growing beyond 4KB.
As the struct includes the state of all 32 private (per-VCPU) interrupts, we are at 3840 bytes on arm64 at the moment already. Growing the per-IRQ VGIC structure even slightly makes the VCPU quickly exceed the 4K limit. The new VGIC will need more space per virtual IRQ. I spent a few hours trying to trim this down, but couldn't get it below 4KB, even with the nasty hacks piling up to save some bytes here and there.
It turns out that beyond efficiency, maybe, there is no real technical reason this struct has to fit in one page, so lifting the limit to two pages seems like the most pragmatic solution.
Restrict this to compiling with the new VGIC and for ARM64 only.

---
Changelog v2 ... v3:
- rework alloc_vcpu_struct() to avoid nasty #ifdef

Changelog v1 ... v2:
- confine change to new VGIC and ARM64 only

  xen/arch/arm/domain.c | 25 +++++++++++++++++++++----
  1 file changed, 21 insertions(+), 4 deletions(-)
xen/arch/arm/domain.c +7 -0
index 9688e62f78..23bda3f7db 100644
@@ -505,19 +505,36 @@ void dump_pageframe_info(struct domain *d)
    }
  +/*
+ * The new VGIC has a bigger per-IRQ structure, so we need more than one
+ * page on ARM64. Cowardly increase the limit in this case.
+ */
+#if defined(CONFIG_NEW_VGIC) && defined(CONFIG_ARM_64)
+#define PAGES_PER_VCPU  2
+#else
+#define PAGES_PER_VCPU  1

xen/arch/arm/domain.c void dump_pageframe_info(struct domain *d) · line 521

Quoted 10 lines
xen/arch/arm/domain.c +5 -2
+#endif
+
  struct vcpu *alloc_vcpu_struct(void)
  {
      struct vcpu *v;
-    BUILD_BUG_ON(sizeof(*v) > PAGE_SIZE);
-    v = alloc_xenheap_pages(0, 0);
+
+    BUILD_BUG_ON(sizeof(*v) > PAGES_PER_VCPU * PAGE_SIZE);
+    v = alloc_xenheap_pages(get_order_from_pages(PAGES_PER_VCPU), 0);

I was suggesting to use get_order_from_pages(sizeof (...)) so if we end up to be smaller, you don't lose a page for nothing. But I am ok with that too and can revisit later. So:

Quoted 1 line

That is actually a good suggestion, let's not lose track of it.

With that fix:

Quoted 10 lines
Quoted 10 lines
xen/arch/arm/domain.c +7 -1
      if ( v != NULL )
-        clear_page(v);
+    {
+        unsigned int i;
+
+        for ( i = 0; i < PAGES_PER_VCPU; i++ )
+            clear_page((void *)v + i * PAGE_SIZE);
+    }
+
      return v;
Quoted 8 lines

xen/arch/arm/domain.c void dump_pageframe_info(struct domain *d)

Quoted 8 lines
xen/arch/arm/domain.c +1 -1
  }
    void free_vcpu_struct(struct vcpu *v)
  {
-    free_xenheap_page(v);
+    free_xenheap_pages(v, get_order_from_pages(PAGES_PER_VCPU));
  }
    int vcpu_initialise(struct vcpu *v)
 

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#119 lore raw

PATCH v3 39/39 ARM: VGIC: wire new VGIC(-v2) files into Xen build system

In reply to Andre Przywara

Now that we have both the old VGIC prepared to cope with a sibling and the code for the new VGIC in place, lets add a Kconfig option to enable the new code and wire it into the Xen build system.
This will add a compile time option to use either the "old" or the "new" VGIC.
In the moment this is restricted to a vGIC-v2. To make the build system happy, we provide a temporary dummy implementation of vgic_v3_setup_hw() to allow building for now.

---
Changelog v2 ... v3:
- fix indentation of Kconfig entry
- select NEEDS_LIST_SORT
- drop unconditional list_sort.o inclusion

Changelog v1 ... v2:
- add Kconfig help text
- use separate Makefile in vgic/ directory
- protect compilation without GICV3 support
- always include list_sort() in build

 xen/arch/arm/Kconfig       | 18 +++++++++++++++++-
 xen/arch/arm/Makefile      |  5 ++++-
 xen/arch/arm/vgic/Makefile |  5 +++++
 xen/arch/arm/vgic/vgic.c   | 10 ++++++++++
 4 files changed, 36 insertions(+), 2 deletions(-)
 create mode 100644 xen/arch/arm/vgic/Makefile
xen/arch/arm/Kconfig +17 -1
index 2782ee6589..8174c0c635 100644
@@ -48,7 +48,23 @@ config HAS_GICV3
 config HAS_ITS
         bool
         prompt "GICv3 ITS MSI controller support" if EXPERT = "y"
-        depends on HAS_GICV3
+        depends on HAS_GICV3 && !NEW_VGIC
+
+config NEW_VGIC
+	bool
+	prompt "Use new VGIC implementation"
+	select NEEDS_LIST_SORT
+	---help---
+
+	This is an alternative implementation of the ARM GIC interrupt
+	controller emulation, based on the Linux/KVM VGIC. It has a better
+	design and fixes many shortcomings of the existing GIC emulation in
+	Xen. It will eventually replace the existing/old VGIC.
+	However at the moment it lacks support for Dom0 using the ITS for
+	using MSIs.
+	Say Y if you want to help testing this new code or if you experience
+	problems with the standard emulation.
+	At the moment this implementation is not security supported.
 
 config SBSA_VUART_CONSOLE
 	bool "Emulated SBSA UART console support"
xen/arch/arm/Makefile +4 -1
index 41d7366527..a9533b107e 100644
@@ -16,7 +16,6 @@ obj-y += domain_build.o
 obj-y += domctl.o
 obj-$(EARLY_PRINTK) += early_printk.o
 obj-y += gic.o
-obj-y += gic-vgic.o
 obj-y += gic-v2.o
 obj-$(CONFIG_HAS_GICV3) += gic-v3.o
 obj-$(CONFIG_HAS_ITS) += gic-v3-its.o
@@ -47,10 +46,14 @@ obj-y += sysctl.o
 obj-y += time.o
 obj-y += traps.o
 obj-y += vcpreg.o
+subdir-$(CONFIG_NEW_VGIC) += vgic
+ifneq ($(CONFIG_NEW_VGIC),y)
+obj-y += gic-vgic.o
 obj-y += vgic.o
 obj-y += vgic-v2.o
 obj-$(CONFIG_HAS_GICV3) += vgic-v3.o
 obj-$(CONFIG_HAS_ITS) += vgic-v3-its.o
+endif
 obj-y += vm_event.o
 obj-y += vtimer.o
 obj-$(CONFIG_SBSA_VUART_CONSOLE) += vpl011.o
xen/arch/arm/vgic/Makefile new file +5 -0
new file mode 100644
index 0000000000..806826948e
@@ -0,0 +1,5 @@
+obj-y += vgic.o
+obj-y += vgic-v2.o
+obj-y += vgic-mmio.o
+obj-y += vgic-mmio-v2.o
+obj-y += vgic-init.o
xen/arch/arm/vgic/vgic.c +10 -0
index 131358a5a1..22c70ff7cd 100644
@@ -981,6 +981,16 @@ unsigned int vgic_max_vcpus(const struct domain *d)
     return min_t(unsigned int, MAX_VIRT_CPUS, vgic_vcpu_limit);
 }
 
+#ifdef CONFIG_HAS_GICV3
+void vgic_v3_setup_hw(paddr_t dbase,
+                      unsigned int nr_rdist_regions,
+                      const struct rdist_region *regions,
+                      unsigned int intid_bits)
+{
+    /* Dummy implementation to allow building without actual vGICv3 support. */
+}
+#endif
+
 /*
  * Local variables:
  * mode: C
Signature · 7 lines
2.14.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#120 lore raw

Re: [PATCH v3 39/39] ARM: VGIC: wire new VGIC(-v2) files into Xen build system

Hi Andre,

Quoted 5 lines · Andre Przywara
xen/arch/arm/vgic/vgic.c +0 -0
index 131358a5a1..22c70ff7cd 100644

xen/arch/arm/vgic/vgic.c unsigned int vgic_max_vcpus(const struct domain *d) · line 990

Quoted 10 lines
xen/arch/arm/vgic/vgic.c +7 -0
@@ -981,6 +981,16 @@ unsigned int vgic_max_vcpus(const struct domain *d)
      return min_t(unsigned int, MAX_VIRT_CPUS, vgic_vcpu_limit);
  }
 
+#ifdef CONFIG_HAS_GICV3
+void vgic_v3_setup_hw(paddr_t dbase,
+                      unsigned int nr_rdist_regions,
+                      const struct rdist_region *regions,
+                      unsigned int intid_bits)
+{
+    /* Dummy implementation to allow building without actual vGICv3 support. */

One major inconvenience with that solution is GICv3 driver is going to be initialized but then you hit the BUG_ON() in domain_vgic_register. This is really not nice for the user but it is not obvious why the BUG_ON() is hit.

I am ok if you don't want to touch the Kconfig. But I would at least implement that helper with a panic("vGICv3 not yet supported with the new vGIC");

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#121 lore raw

Re: [PATCH v3 39/39] ARM: VGIC: wire new VGIC(-v2) files into Xen build system

Hi,

Quoted 24 lines · Julien Grall

Hi Andre,

Quoted 14 lines · Andre Przywara
xen/arch/arm/vgic/vgic.c +0 -0
index 131358a5a1..22c70ff7cd 100644
@@ -981,6 +981,16 @@ unsigned int vgic_max_vcpus(const struct domain *d)
       return min_t(unsigned int, MAX_VIRT_CPUS, vgic_vcpu_limit);
   }

  +#ifdef CONFIG_HAS_GICV3

xen/arch/arm/vgic/vgic.c +6 -0
+void vgic_v3_setup_hw(paddr_t dbase,
+                      unsigned int nr_rdist_regions,
+                      const struct rdist_region *regions,
+                      unsigned int intid_bits)
+{
+    /* Dummy implementation to allow building without actual vGICv3
Quoted 1 line

support. */

One major inconvenience with that solution is GICv3 driver is going to be initialized but then you hit the BUG_ON() in domain_vgic_register. This is really not nice for the user but it is not obvious why the BUG_ON() is hit.

Quoted 3 lines

I am ok if you don't want to touch the Kconfig. But I would at least implement that helper with a panic("vGICv3 not yet supported with the new vGIC");

Yes, that's a good point (and easy to implement!) ;-)

Also I think we should have something saying that we are using the new VGIC, so we have it in the logs. Just realised this when I was wondering if my machine is currently using the new or the old VGIC ;-)

I put something in the vgic_v2_setup_hw() implementation.

Cheers,
Andre.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel