Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/arch/xen update to __XEN_INTERFACE_VERSION__ 0x0003020a ...



details:   https://anonhg.NetBSD.org/src/rev/b739093981f8
branches:  trunk
changeset: 970984:b739093981f8
user:      jdolecek <jdolecek%NetBSD.org@localhost>
date:      Thu Apr 09 19:26:37 2020 +0000

description:
update to __XEN_INTERFACE_VERSION__ 0x0003020a aka Xen 3.2.10

this brings grant memory v2 support:
- status separated from flags - revoking access needs just memory barrier,
  no need for expensive cmpxchg16 any more
- sub-page hypervisor copy-only grants, to be used by xennet(4)
- 64-bit frame, i.e. support for DomU RAM >16TB

the grant table is now always allocated on boot to maximum size, it's now
never grown in runtime; switch back to regular kmem_alloc()/kmem_free()

code now requires v2 support, no compatibility for grant version 1 retained -
Xen v2 support predates all currently supported Xen versions

also interface for baloon changed slightly, code updated

diffstat:

 sys/arch/xen/conf/std.xenversion |    4 +-
 sys/arch/xen/include/xen.h       |   16 +----
 sys/arch/xen/x86/xen_bus_dma.c   |   14 +-
 sys/arch/xen/xen/balloon.c       |    8 +-
 sys/arch/xen/xen/xengnt.c        |  142 +++++++++++++++++++++++++++++++-------
 5 files changed, 127 insertions(+), 57 deletions(-)

diffs (truncated from 411 to 300 lines):

diff -r 0061280c97a8 -r b739093981f8 sys/arch/xen/conf/std.xenversion
--- a/sys/arch/xen/conf/std.xenversion  Thu Apr 09 18:20:40 2020 +0000
+++ b/sys/arch/xen/conf/std.xenversion  Thu Apr 09 19:26:37 2020 +0000
@@ -1,6 +1,6 @@
-# $NetBSD: std.xenversion,v 1.1 2020/04/09 14:39:10 jdolecek Exp $
+# $NetBSD: std.xenversion,v 1.2 2020/04/09 19:26:37 jdolecek Exp $
 #
 # Xen options shared for all archs (i386, amd64)
 
-options        __XEN_INTERFACE_VERSION__=0x00030208 # Xen 3.1 interface
+options        __XEN_INTERFACE_VERSION__=0x0003020a # Xen 3.2.10 interface
 
diff -r 0061280c97a8 -r b739093981f8 sys/arch/xen/include/xen.h
--- a/sys/arch/xen/include/xen.h        Thu Apr 09 18:20:40 2020 +0000
+++ b/sys/arch/xen/include/xen.h        Thu Apr 09 19:26:37 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: xen.h,v 1.44 2019/05/09 17:09:50 bouyer Exp $  */
+/*     $NetBSD: xen.h,v 1.45 2020/04/09 19:26:37 jdolecek Exp $        */
 
 /*
  *
@@ -170,20 +170,6 @@
        return result;
 }
 
-static inline uint16_t
-xen_atomic_cmpxchg16(volatile uint16_t *ptr, uint16_t  val, uint16_t newval)
-{
-       unsigned long result;
-
-        __asm volatile(__LOCK_PREFIX
-           "cmpxchgw %w1,%2"
-           :"=a" (result)
-           :"q"(newval), "m" (*ptr), "0" (val)
-           :"memory");
-
-       return result;
-}
-
 static __inline void
 xen_atomic_setbits_l (volatile XATOMIC_T *ptr, unsigned long bits) {  
 #ifdef __x86_64__
diff -r 0061280c97a8 -r b739093981f8 sys/arch/xen/x86/xen_bus_dma.c
--- a/sys/arch/xen/x86/xen_bus_dma.c    Thu Apr 09 18:20:40 2020 +0000
+++ b/sys/arch/xen/x86/xen_bus_dma.c    Thu Apr 09 19:26:37 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: xen_bus_dma.c,v 1.28 2018/09/03 16:29:29 riastradh Exp $       */
+/*     $NetBSD: xen_bus_dma.c,v 1.29 2020/04/09 19:26:37 jdolecek Exp $        */
 /*     NetBSD bus_dma.c,v 1.21 2005/04/16 07:53:35 yamt Exp */
 
 /*-
@@ -32,7 +32,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: xen_bus_dma.c,v 1.28 2018/09/03 16:29:29 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: xen_bus_dma.c,v 1.29 2020/04/09 19:26:37 jdolecek Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -94,7 +94,7 @@
                set_xen_guest_handle(res.extent_start, &mfn);
                res.nr_extents = 1;
                res.extent_order = 0;
-               res.address_bits = 0;
+               res.mem_flags = 0;
                res.domid = DOMID_SELF;
                error = HYPERVISOR_memory_op(XENMEM_decrease_reservation, &res);
                if (error != 1) {
@@ -113,14 +113,14 @@
        set_xen_guest_handle(res.extent_start, &mfn);
        res.nr_extents = 1;
        res.extent_order = order;
-       res.address_bits = get_order(high) + PAGE_SHIFT;
+       res.mem_flags = XENMEMF_address_bits(get_order(high) + PAGE_SHIFT);
        res.domid = DOMID_SELF;
        error = HYPERVISOR_memory_op(XENMEM_increase_reservation, &res);
        if (error != 1) {
 #ifdef DEBUG
                printf("xen_alloc_contig: XENMEM_increase_reservation "
-                   "failed: %d (order %d address_bits %d)\n",
-                   error, order, res.address_bits);
+                   "failed: %d (order %d mem_flags %d)\n",
+                   error, order, res.mem_flags);
 #endif
                error = ENOMEM;
                pg = NULL;
@@ -166,7 +166,7 @@
                set_xen_guest_handle(res.extent_start, &mfn);
                res.nr_extents = 1;
                res.extent_order = 0;
-               res.address_bits = 32;
+               res.mem_flags = XENMEMF_address_bits(32);
                res.domid = DOMID_SELF;
                if (HYPERVISOR_memory_op(XENMEM_increase_reservation, &res)
                    < 0) {
diff -r 0061280c97a8 -r b739093981f8 sys/arch/xen/xen/balloon.c
--- a/sys/arch/xen/xen/balloon.c        Thu Apr 09 18:20:40 2020 +0000
+++ b/sys/arch/xen/xen/balloon.c        Thu Apr 09 19:26:37 2020 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: balloon.c,v 1.19 2018/06/24 20:28:58 jdolecek Exp $ */
+/* $NetBSD: balloon.c,v 1.20 2020/04/09 19:26:38 jdolecek Exp $ */
 
 /*-
  * Copyright (c) 2010 The NetBSD Foundation, Inc.
@@ -75,7 +75,7 @@
 #endif
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: balloon.c,v 1.19 2018/06/24 20:28:58 jdolecek Exp $");
+__KERNEL_RCSID(0, "$NetBSD: balloon.c,v 1.20 2020/04/09 19:26:38 jdolecek Exp $");
 
 #include <sys/inttypes.h>
 #include <sys/device.h>
@@ -378,7 +378,7 @@
        xen_pfn_t *mfn_list = sc->sc_mfn_list;
 
        struct xen_memory_reservation reservation = {
-               .address_bits = 0,
+               .mem_flags = 0,
                .extent_order = 0,
                .domid        = DOMID_SELF
        };
@@ -449,7 +449,7 @@
        xen_pfn_t *mfn_list = sc->sc_mfn_list;
 
        struct xen_memory_reservation reservation = {
-               .address_bits = 0,
+               .mem_flags = 0,
                .extent_order = 0,
                .domid        = DOMID_SELF
        };
diff -r 0061280c97a8 -r b739093981f8 sys/arch/xen/xen/xengnt.c
--- a/sys/arch/xen/xen/xengnt.c Thu Apr 09 18:20:40 2020 +0000
+++ b/sys/arch/xen/xen/xengnt.c Thu Apr 09 19:26:37 2020 +0000
@@ -1,4 +1,4 @@
-/*      $NetBSD: xengnt.c,v 1.30 2020/04/09 15:54:41 bouyer Exp $      */
+/*      $NetBSD: xengnt.c,v 1.31 2020/04/09 19:26:38 jdolecek Exp $      */
 
 /*
  * Copyright (c) 2006 Manuel Bouyer.
@@ -26,7 +26,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: xengnt.c,v 1.30 2020/04/09 15:54:41 bouyer Exp $");
+__KERNEL_RCSID(0, "$NetBSD: xengnt.c,v 1.31 2020/04/09 19:26:38 jdolecek Exp $");
 
 #include <sys/types.h>
 #include <sys/param.h>
@@ -49,7 +49,8 @@
 #define DPRINTF(x)
 #endif
 
-#define NR_GRANT_ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(grant_entry_t))
+#define NR_GRANT_ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(grant_entry_v2_t))
+#define NR_GRANT_STATUS_PER_PAGE (PAGE_SIZE / sizeof(grant_status_t))
 
 /* External tools reserve first few grant table entries. */
 #define NR_RESERVED_ENTRIES 8
@@ -58,6 +59,8 @@
 int gnt_nr_grant_frames;
 /* Maximum number of frames that can make up the grant table */
 int gnt_max_grant_frames;
+/* Number of grant status frames */
+int gnt_status_frames;
 
 /* table of free grant entries */
 grant_ref_t *gnt_entries;
@@ -67,17 +70,20 @@
 #define XENGNT_NO_ENTRY 0xffffffff
 
 /* VM address of the grant table */
-grant_entry_t *grant_table;
+grant_entry_v2_t *grant_table;
+grant_status_t *grant_status;
 kmutex_t grant_lock;
 
 static grant_ref_t xengnt_get_entry(void);
 static void xengnt_free_entry(grant_ref_t);
 static int xengnt_more_entries(void);
+static int xengnt_map_status(void);
 
 void
 xengnt_init(void)
 {
        struct gnttab_query_size query;
+       struct gnttab_set_version gntversion;
        int rc;
        int nr_grant_entries;
        int i;
@@ -88,7 +94,16 @@
                gnt_max_grant_frames = 4; /* Legacy max number of frames */
        else
                gnt_max_grant_frames = query.max_nr_frames;
-       gnt_nr_grant_frames = 0;
+
+       /*
+        * Always allocate max number of grant frames, never expand in runtime
+        */
+       gnt_nr_grant_frames = gnt_max_grant_frames;
+
+       gntversion.version = 2;
+       rc = HYPERVISOR_grant_table_op(GNTTABOP_set_version, &gntversion, 1);
+       if (rc < 0 || gntversion.version != 2)
+               panic("GNTTABOP_set_version 2 failed %d", rc);
 
        nr_grant_entries =
            gnt_max_grant_frames * NR_GRANT_ENTRIES_PER_PAGE;
@@ -96,12 +111,20 @@
        grant_table = (void *)uvm_km_alloc(kernel_map,
            gnt_max_grant_frames * PAGE_SIZE, 0, UVM_KMF_VAONLY);
        if (grant_table == NULL)
-               panic("xengnt_init() no VM space");
+               panic("xengnt_init() table no VM space");
+
        gnt_entries = kmem_alloc((nr_grant_entries + 1) * sizeof(grant_ref_t),
            KM_SLEEP);
        for (i = 0; i <= nr_grant_entries; i++)
                gnt_entries[i] = XENGNT_NO_ENTRY;
 
+       gnt_status_frames =
+           round_page(nr_grant_entries * sizeof(grant_status_t)) / PAGE_SIZE;
+       grant_status = (void *)uvm_km_alloc(kernel_map,
+           gnt_status_frames * PAGE_SIZE, 0, UVM_KMF_VAONLY);
+       if (grant_status == NULL)
+               panic("xengnt_init() status no VM space");
+
        mutex_init(&grant_lock, MUTEX_DEFAULT, IPL_VM);
 
        xengnt_resume();
@@ -124,6 +147,7 @@
                if (xengnt_more_entries() != 0)
                        panic("xengnt_resume: can't restore grant frames");
        }
+       xengnt_map_status();
        mutex_exit(&grant_lock);
        return true;
 }
@@ -144,13 +168,67 @@
                gnt_entries[i] = XENGNT_NO_ENTRY;
        }
        
-       /* Remove virtual => machine mapping */
+       /* Remove virtual => machine mapping for grant table */
        pmap_kremove((vaddr_t)grant_table, gnt_nr_grant_frames * PAGE_SIZE);
+
+       /* Remove virtual => machine mapping for status table */
+       pmap_kremove((vaddr_t)grant_status, gnt_status_frames * PAGE_SIZE);
+
        pmap_update(pmap_kernel());
        mutex_exit(&grant_lock);
        return true;
 }
 
+/*
+ * Get status frames and enter them into the VA space.
+ */
+static int
+xengnt_map_status(void)
+{
+       gnttab_get_status_frames_t getstatus;
+       u_long *pages;
+       size_t sz;
+
+       KASSERT(mutex_owned(&grant_lock));
+
+       sz = gnt_status_frames * sizeof(u_long);
+       pages = kmem_alloc(sz, KM_NOSLEEP);
+       if (pages == NULL)
+               return ENOMEM;
+
+       getstatus.dom = DOMID_SELF;
+       getstatus.nr_frames = gnt_status_frames;
+       set_xen_guest_handle(getstatus.frame_list, pages);
+
+       /*
+        * get the status frames, and return the list of their virtual
+        * addresses in 'pages'
+        */
+       if (HYPERVISOR_grant_table_op(GNTTABOP_get_status_frames,
+           &getstatus, 1) != 0)
+               panic("%s: get_status_frames failed", __func__);
+       if (getstatus.status != GNTST_okay) {
+               aprint_error("%s: get_status_frames returned %d\n",
+                   __func__, getstatus.status);
+               kmem_free(pages, sz);
+               return ENOMEM;
+       }
+
+       /*
+        * map between status_table addresses and the machine addresses of
+        * the status table frames
+        */
+       for (int i = 0; i < gnt_status_frames; i++) {
+               pmap_kenter_ma(((vaddr_t)grant_status) + i * PAGE_SIZE,
+                   ((paddr_t)pages[i]) << PAGE_SHIFT,
+                   VM_PROT_WRITE, 0);
+       }
+       pmap_update(pmap_kernel());



Home | Main Index | Thread Index | Old Index