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

[Xen-devel] [PATCH RFC v1 57/74] x86/pv-shim: shadow PV console's page for L2 DomU



From: Sergey Dyasli <sergey.dyasli@xxxxxxxxxx>

Signed-off-by: Sergey Dyasli <sergey.dyasli@xxxxxxxxxx>
---
 xen/arch/x86/pv/shim.c      |  31 ++++++++--
 xen/drivers/char/Makefile   |   1 +
 xen/drivers/char/console.c  |   4 ++
 xen/drivers/char/consoled.c | 145 ++++++++++++++++++++++++++++++++++++++++++++
 xen/include/xen/consoled.h  |  27 +++++++++
 5 files changed, 204 insertions(+), 4 deletions(-)
 create mode 100644 xen/drivers/char/consoled.c
 create mode 100644 xen/include/xen/consoled.h

diff --git a/xen/arch/x86/pv/shim.c b/xen/arch/x86/pv/shim.c
index 98c1e31e8f..74b24bf950 100644
--- a/xen/arch/x86/pv/shim.c
+++ b/xen/arch/x86/pv/shim.c
@@ -25,6 +25,8 @@
 #include <xen/iocap.h>
 #include <xen/shutdown.h>
 #include <xen/types.h>
+#include <xen/consoled.h>
+#include <xen/pv_console.h>
 
 #include <asm/apic.h>
 #include <asm/dom0_build.h>
@@ -125,13 +127,28 @@ void __init pv_shim_setup_dom(struct domain *d, 
l4_pgentry_t *l4start,
 })
     SET_AND_MAP_PARAM(HVM_PARAM_STORE_PFN, si->store_mfn, store_va);
     SET_AND_MAP_PARAM(HVM_PARAM_STORE_EVTCHN, si->store_evtchn, 0);
+    SET_AND_MAP_PARAM(HVM_PARAM_CONSOLE_EVTCHN, si->console.domU.evtchn, 0);
     if ( !pv_console )
-    {
         SET_AND_MAP_PARAM(HVM_PARAM_CONSOLE_PFN, si->console.domU.mfn,
                           console_va);
-        SET_AND_MAP_PARAM(HVM_PARAM_CONSOLE_EVTCHN, si->console.domU.evtchn, 
0);
-    }
 #undef SET_AND_MAP_PARAM
+    else
+    {
+        /* Allocate a new page for DomU's PV console */
+        void *page = alloc_xenheap_pages(0, MEMF_bits(32));
+        uint64_t console_mfn;
+
+        ASSERT(page);
+        clear_page(page);
+        console_mfn = virt_to_mfn(page);
+        si->console.domU.mfn = console_mfn;
+        share_xen_page_with_guest(mfn_to_page(console_mfn), d,
+                                  XENSHARE_writable);
+        replace_va(d, l4start, console_va, console_mfn);
+        dom0_update_physmap(d, (console_va - va_start) >> PAGE_SHIFT,
+                            console_mfn, vphysmap);
+        consoled_set_ring_addr(page);
+    }
 }
 
 void pv_shim_shutdown(uint8_t reason)
@@ -334,7 +351,13 @@ case EVTCHNOP_##cmd: {                                     
             \
         if ( copy_from_guest(&send, arg, 1) != 0 )
             return -EFAULT;
 
-        rc = xen_hypercall_event_channel_op(EVTCHNOP_send, &send);
+        if ( pv_console && send.port == pv_console_evtchn() )
+        {
+            consoled_guest_rx();
+            rc = 0;
+        }
+        else
+            rc = xen_hypercall_event_channel_op(EVTCHNOP_send, &send);
 
         break;
     }
diff --git a/xen/drivers/char/Makefile b/xen/drivers/char/Makefile
index 9d48d0f2dc..0d48b16e8d 100644
--- a/xen/drivers/char/Makefile
+++ b/xen/drivers/char/Makefile
@@ -9,3 +9,4 @@ obj-$(CONFIG_HAS_EHCI) += ehci-dbgp.o
 obj-$(CONFIG_ARM) += arm-uart.o
 obj-y += serial.o
 obj-$(CONFIG_XEN_GUEST) += xen_pv_console.o
+obj-$(CONFIG_PV_SHIM) += consoled.o
diff --git a/xen/drivers/char/console.c b/xen/drivers/char/console.c
index 354e020d19..3c615a255c 100644
--- a/xen/drivers/char/console.c
+++ b/xen/drivers/char/console.c
@@ -15,6 +15,7 @@
 #include <xen/init.h>
 #include <xen/event.h>
 #include <xen/console.h>
+#include <xen/consoled.h>
 #include <xen/serial.h>
 #include <xen/pv_console.h>
 #include <xen/softirq.h>
@@ -408,6 +409,9 @@ static void __serial_rx(char c, struct cpu_user_regs *regs)
         serial_rx_ring[SERIAL_RX_MASK(serial_rx_prod++)] = c;
     /* Always notify the guest: prevents receive path from getting stuck. */
     send_global_virq(VIRQ_CONSOLE);
+
+    if ( pv_shim && pv_console )
+        consoled_guest_tx(c);
 }
 
 static void serial_rx(char c, struct cpu_user_regs *regs)
diff --git a/xen/drivers/char/consoled.c b/xen/drivers/char/consoled.c
new file mode 100644
index 0000000000..9a4b504208
--- /dev/null
+++ b/xen/drivers/char/consoled.c
@@ -0,0 +1,145 @@
+/******************************************************************************
+ * drivers/char/consoled.c
+ *
+ * A backend driver for Xen's PV console.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * 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.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Copyright (c) 2017 Citrix Systems Ltd.
+ */
+
+#include <xen/lib.h>
+#include <xen/event.h>
+#include <xen/pv_console.h>
+#include <xen/consoled.h>
+
+static struct xencons_interface *cons_ring;
+static DEFINE_SPINLOCK(rx_lock);
+
+void consoled_set_ring_addr(struct xencons_interface *ring)
+{
+    cons_ring = ring;
+}
+
+struct xencons_interface *consoled_get_ring_addr(void)
+{
+    return cons_ring;
+}
+
+static void notify_guest(void)
+{
+    unsigned int cpu = 0, port = pv_console_evtchn();
+
+    evtchn_port_set_pending(pv_domain, cpu, evtchn_from_port(pv_domain, port));
+}
+
+#define BUF_SZ 255
+static char buf[BUF_SZ + 1];
+
+/* Receives characters from a domain's PV console */
+size_t consoled_guest_rx(void)
+{
+    size_t recv = 0, idx = 0;
+    XENCONS_RING_IDX cons, prod;
+
+    if ( !cons_ring )
+        return 0;
+
+    spin_lock(&rx_lock);
+
+    cons = cons_ring->out_cons;
+    prod = ACCESS_ONCE(cons_ring->out_prod);
+    ASSERT((prod - cons) <= sizeof(cons_ring->out));
+
+    /* Is the ring empty? */
+    if ( cons == prod )
+        goto out;
+
+    /* Update pointers before accessing the ring */
+    smp_rmb();
+
+    while ( cons != prod )
+    {
+        char c = cons_ring->out[MASK_XENCONS_IDX(cons++, cons_ring->out)];
+
+        buf[idx++] = c;
+        recv++;
+
+        if ( idx >= BUF_SZ )
+        {
+            pv_console_puts(buf);
+            idx = 0;
+        }
+    }
+
+    if ( idx )
+    {
+        buf[idx] = '\0';
+        pv_console_puts(buf);
+    }
+
+    /* No need for a mem barrier because every character was already consumed 
*/
+    barrier();
+    ACCESS_ONCE(cons_ring->out_cons) = cons;
+    notify_guest();
+
+ out:
+    spin_unlock(&rx_lock);
+
+    return recv;
+}
+
+/* Sends a character into a domain's PV console */
+size_t consoled_guest_tx(char c)
+{
+    size_t sent = 0;
+    XENCONS_RING_IDX cons, prod;
+
+    if ( !cons_ring )
+        return 0;
+
+    cons = ACCESS_ONCE(cons_ring->in_cons);
+    prod = cons_ring->in_prod;
+    ASSERT((prod - cons) <= sizeof(cons_ring->in));
+
+    /* Is the ring out of space? */
+    if ( sizeof(cons_ring->in) - (prod - cons) == 0 )
+        goto notify;
+
+    /* Update pointers before accessing the ring */
+    smp_rmb();
+
+    cons_ring->in[MASK_XENCONS_IDX(prod++, cons_ring->in)] = c;
+    sent++;
+
+    /* Write to the ring before updating the pointer */
+    smp_wmb();
+    ACCESS_ONCE(cons_ring->in_prod) = prod;
+
+ notify:
+    /* Always notify the guest: prevents receive path from getting stuck. */
+    notify_guest();
+
+    return sent;
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/xen/include/xen/consoled.h b/xen/include/xen/consoled.h
new file mode 100644
index 0000000000..fd5d220a8a
--- /dev/null
+++ b/xen/include/xen/consoled.h
@@ -0,0 +1,27 @@
+#ifndef __XEN_CONSOLED_H__
+#define __XEN_CONSOLED_H__
+
+#include <public/io/console.h>
+
+#ifdef CONFIG_PV_SHIM
+
+void consoled_set_ring_addr(struct xencons_interface *ring);
+struct xencons_interface *consoled_get_ring_addr(void);
+size_t consoled_guest_rx(void);
+size_t consoled_guest_tx(char c);
+
+#else
+
+size_t consoled_guest_tx(char c) { return 0; }
+
+#endif /* !CONFIG_PV_SHIM */
+#endif /* __XEN_CONSOLED_H__ */
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
-- 
2.11.0


_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxxxxxxxxx
https://lists.xenproject.org/mailman/listinfo/xen-devel

 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.