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

[Xen-devel] [PATCH RFC v1 34/74] x86/guest: add PV console code



From: Sergey Dyasli <sergey.dyasli@xxxxxxxxxx>

Signed-off-by: Sergey Dyasli <sergey.dyasli@xxxxxxxxxx>
Signed-off-by: Andrew Cooper <andrew.cooper3@xxxxxxxxxx>
---
 xen/drivers/char/Makefile             |   1 +
 xen/drivers/char/xen_pv_console.c     | 198 ++++++++++++++++++++++++++++++++++
 xen/include/asm-x86/fixmap.h          |   1 +
 xen/include/asm-x86/guest/hypercall.h |  33 ++++++
 xen/include/xen/pv_console.h          |  32 ++++++
 5 files changed, 265 insertions(+)
 create mode 100644 xen/drivers/char/xen_pv_console.c
 create mode 100644 xen/include/xen/pv_console.h

diff --git a/xen/drivers/char/Makefile b/xen/drivers/char/Makefile
index aa169d7961..9d48d0f2dc 100644
--- a/xen/drivers/char/Makefile
+++ b/xen/drivers/char/Makefile
@@ -8,3 +8,4 @@ obj-$(CONFIG_HAS_SCIF) += scif-uart.o
 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
diff --git a/xen/drivers/char/xen_pv_console.c 
b/xen/drivers/char/xen_pv_console.c
new file mode 100644
index 0000000000..5e494bc72a
--- /dev/null
+++ b/xen/drivers/char/xen_pv_console.c
@@ -0,0 +1,198 @@
+/******************************************************************************
+ * drivers/char/xen_pv_console.c
+ *
+ * A frontend driver for Xen's PV console.
+ * Can be used when Xen is running on top of Xen in pv-in-pvh mode.
+ * (Linux's name for this is hvc 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/hypercall.h>
+#include <xen/pv_console.h>
+
+#include <asm/fixmap.h>
+#include <asm/guest.h>
+
+#include <public/io/console.h>
+
+static struct xencons_interface *cons_ring;
+static evtchn_port_t cons_evtchn;
+static serial_rx_fn cons_rx_handler;
+static DEFINE_SPINLOCK(tx_lock);
+
+void __init pv_console_init(void)
+{
+    long r;
+    uint64_t raw_pfn = 0, raw_evtchn = 0;
+
+    if ( !xen_guest )
+    {
+        printk("PV console init failed: xen_guest mode is not active!\n");
+        return;
+    }
+
+    r = xen_hypercall_hvm_get_param(HVM_PARAM_CONSOLE_PFN, &raw_pfn);
+    if ( r < 0 )
+        goto error;
+
+    r = xen_hypercall_hvm_get_param(HVM_PARAM_CONSOLE_EVTCHN, &raw_evtchn);
+    if ( r < 0 )
+        goto error;
+
+    set_fixmap(FIX_PV_CONSOLE, raw_pfn << PAGE_SHIFT);
+    cons_ring = (struct xencons_interface *)fix_to_virt(FIX_PV_CONSOLE);
+    cons_evtchn = raw_evtchn;
+
+    printk("Initialised PV console at 0x%p with pfn %#lx and evtchn %#x\n",
+            cons_ring, raw_pfn, cons_evtchn);
+    return;
+
+ error:
+    printk("Couldn't initialise PV console\n");
+}
+
+void __init pv_console_set_rx_handler(serial_rx_fn fn)
+{
+    cons_rx_handler = fn;
+}
+
+void __init pv_console_init_postirq(void)
+{
+    if ( !cons_ring )
+        return;
+
+    xen_hypercall_evtchn_unmask(cons_evtchn);
+}
+
+static void notify_daemon(void)
+{
+    xen_hypercall_evtchn_send(cons_evtchn);
+}
+
+size_t pv_console_rx(struct cpu_user_regs *regs)
+{
+    char c;
+    XENCONS_RING_IDX cons, prod;
+    size_t recv = 0;
+
+    if ( !cons_ring )
+        return 0;
+
+    /* TODO: move this somewhere */
+    if ( !test_bit(cons_evtchn, XEN_shared_info->evtchn_pending) )
+        return 0;
+
+    prod = ACCESS_ONCE(cons_ring->in_prod);
+    cons = cons_ring->in_cons;
+    /* Get pointers before reading the ring */
+    smp_rmb();
+
+    ASSERT((prod - cons) <= sizeof(cons_ring->in));
+
+    while ( cons != prod )
+    {
+        c = cons_ring->in[MASK_XENCONS_IDX(cons++, cons_ring->in)];
+        if ( cons_rx_handler )
+            cons_rx_handler(c, regs);
+        recv++;
+    }
+
+    /* No need for a mem barrier because every character was already consumed 
*/
+    barrier();
+    ACCESS_ONCE(cons_ring->in_cons) = cons;
+    notify_daemon();
+
+    clear_bit(cons_evtchn, XEN_shared_info->evtchn_pending);
+
+    return recv;
+}
+
+static size_t pv_ring_puts(const char *buf)
+{
+    XENCONS_RING_IDX cons, prod;
+    size_t sent = 0, avail;
+    bool put_r = false;
+
+    while ( buf[sent] != '\0' || put_r )
+    {
+        cons = ACCESS_ONCE(cons_ring->out_cons);
+        prod = cons_ring->out_prod;
+
+        ASSERT((prod - cons) <= sizeof(cons_ring->out));
+        avail = sizeof(cons_ring->out) - (prod - cons);
+
+        if ( avail == 0 )
+        {
+            /* Wait for xenconsoled to consume our output */
+            xen_hypercall_sched_op(SCHEDOP_yield, NULL);
+            continue;
+        }
+
+        /* Update pointers before accessing the ring */
+        smp_rmb();
+
+        while ( avail && (buf[sent] != '\0' || put_r) )
+        {
+            if ( put_r )
+            {
+                cons_ring->out[MASK_XENCONS_IDX(prod++, cons_ring->out)] = 
'\r';
+                put_r = false;
+            }
+            else
+            {
+                cons_ring->out[MASK_XENCONS_IDX(prod++, cons_ring->out)] =
+                    buf[sent];
+
+                /* Send '\r' for every '\n' */
+                if ( buf[sent] == '\n' )
+                    put_r = true;
+                sent++;
+            }
+            avail--;
+        }
+
+        /* Write to the ring before updating the pointer */
+        smp_wmb();
+        ACCESS_ONCE(cons_ring->out_prod) = prod;
+        notify_daemon();
+    }
+
+    return sent;
+}
+
+void pv_console_puts(const char *buf)
+{
+    unsigned long flags;
+
+    if ( !cons_ring )
+        return;
+
+    spin_lock_irqsave(&tx_lock, flags);
+    pv_ring_puts(buf);
+    spin_unlock_irqrestore(&tx_lock, flags);
+}
+
+/*
+ * 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/asm-x86/fixmap.h b/xen/include/asm-x86/fixmap.h
index ded4ddf21b..16ccaa2c77 100644
--- a/xen/include/asm-x86/fixmap.h
+++ b/xen/include/asm-x86/fixmap.h
@@ -46,6 +46,7 @@ enum fixed_addresses {
     FIX_COM_END,
     FIX_EHCI_DBGP,
 #ifdef CONFIG_XEN_GUEST
+    FIX_PV_CONSOLE,
     FIX_XEN_SHARED_INFO,
 #endif /* CONFIG_XEN_GUEST */
     /* Everything else should go further down. */
diff --git a/xen/include/asm-x86/guest/hypercall.h 
b/xen/include/asm-x86/guest/hypercall.h
index d6d4d1946b..90b4755467 100644
--- a/xen/include/asm-x86/guest/hypercall.h
+++ b/xen/include/asm-x86/guest/hypercall.h
@@ -97,6 +97,11 @@ static inline long xen_hypercall_memory_op(unsigned int cmd, 
void *arg)
     return _hypercall64_2(long, __HYPERVISOR_memory_op, cmd, arg);
 }
 
+static inline long xen_hypercall_event_channel_op(unsigned int cmd, void *arg)
+{
+    return _hypercall64_2(long, __HYPERVISOR_event_channel_op, cmd, arg);
+}
+
 static inline long xen_hypercall_hvm_op(unsigned int op, void *arg)
 {
     return _hypercall64_2(long, __HYPERVISOR_hvm_op, op, arg);
@@ -117,6 +122,34 @@ static inline long xen_hypercall_shutdown(unsigned int 
reason)
     return xen_hypercall_sched_op(SCHEDOP_shutdown, &reason);
 }
 
+static inline long xen_hypercall_evtchn_send(evtchn_port_t port)
+{
+    struct evtchn_send send = { .port = port };
+
+    return xen_hypercall_event_channel_op(EVTCHNOP_send, &send);
+}
+
+static inline long xen_hypercall_evtchn_unmask(evtchn_port_t port)
+{
+    struct evtchn_unmask unmask = { .port = port };
+
+    return xen_hypercall_event_channel_op(EVTCHNOP_unmask, &unmask);
+}
+
+static inline long xen_hypercall_hvm_get_param(uint32_t index, uint64_t *value)
+{
+    struct xen_hvm_param xhv = {
+        .domid = DOMID_SELF,
+        .index = index,
+    };
+    long ret = xen_hypercall_hvm_op(HVMOP_get_param, &xhv);
+
+    if ( ret == 0 )
+        *value = xhv.value;
+
+    return ret;
+}
+
 static inline long xen_hypercall_set_evtchn_upcall_vector(
     unsigned int cpu, unsigned int vector)
 {
diff --git a/xen/include/xen/pv_console.h b/xen/include/xen/pv_console.h
new file mode 100644
index 0000000000..e578b56620
--- /dev/null
+++ b/xen/include/xen/pv_console.h
@@ -0,0 +1,32 @@
+#ifndef __XEN_PV_CONSOLE_H__
+#define __XEN_PV_CONSOLE_H__
+
+#include <xen/serial.h>
+
+#ifdef CONFIG_XEN_GUEST
+
+void pv_console_init(void);
+void pv_console_set_rx_handler(serial_rx_fn fn);
+void pv_console_init_postirq(void);
+void pv_console_puts(const char *buf);
+size_t pv_console_rx(struct cpu_user_regs *regs);
+
+#else
+
+static inline void pv_console_init(void) {}
+static inline void pv_console_set_rx_handler(serial_rx_fn fn) { }
+static inline void pv_console_init_postirq(void) { }
+static inline void pv_console_puts(const char *buf) { }
+static inline size_t pv_console_rx(struct cpu_user_regs *regs) { return 0; }
+
+#endif /* !CONFIG_XEN_GUEST */
+#endif /* __XEN_PV_CONSOLE_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®.