#include <xen/init.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>
})
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)
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;
}
--- /dev/null
+/******************************************************************************
+ * 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;
+
+ 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;
+
+ 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:
+ */
--- /dev/null
+#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:
+ */