From: Sergey Dyasli <sergey.dya...@citrix.com> Signed-off-by: Sergey Dyasli <sergey.dya...@citrix.com> Signed-off-by: Wei Liu <wei.l...@citrix.com> [remove notify_guest helper and directly use pv_shim_inject_evtchn] Signed-off-by: Roger Pau Monné <roger....@citrix.com> Signed-off-by: Wei Liu <wei.l...@citrix.com> --- Changes since v1: - Use pv_shim_inject_evtchn. --- xen/arch/x86/pv/shim.c | 31 ++++++++-- xen/drivers/char/Makefile | 1 + xen/drivers/char/console.c | 6 ++ xen/drivers/char/consoled.c | 148 ++++++++++++++++++++++++++++++++++++++++++++ xen/include/xen/consoled.h | 27 ++++++++ 5 files changed, 209 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 eb8b146785..986f9da58a 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> @@ -127,13 +129,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_mapping(d, l4start, console_va, console_mfn); + dom0_update_physmap(d, (console_va - va_start) >> PAGE_SHIFT, + console_mfn, vphysmap); + consoled_set_ring_addr(page); + } pv_hypercall_table_replace(__HYPERVISOR_event_channel_op, (hypercall_fn_t *)pv_shim_event_channel_op, (hypercall_fn_t *)pv_shim_event_channel_op); @@ -341,7 +358,13 @@ static long pv_shim_event_channel_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg) 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 8acd358395..18f5b7f7aa 100644 --- a/xen/drivers/char/console.c +++ b/xen/drivers/char/console.c @@ -32,6 +32,7 @@ #include <xen/warning.h> #ifdef CONFIG_X86 +#include <xen/consoled.h> #include <xen/pv_console.h> #include <asm/guest.h> #endif @@ -415,6 +416,11 @@ 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); + +#ifdef CONFIG_X86 + if ( pv_shim && pv_console ) + consoled_guest_tx(c); +#endif } 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..552abf5766 --- /dev/null +++ b/xen/drivers/char/consoled.c @@ -0,0 +1,148 @@ +/****************************************************************************** + * 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> + +#include <asm/guest.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; +} + +#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); + + /* + * Latch pointers before accessing the ring. Included compiler barrier also + * ensures that pointers are really read only once into local variables. + */ + smp_rmb(); + + ASSERT((prod - cons) <= sizeof(cons_ring->out)); + + /* Is the ring empty? */ + if ( cons == prod ) + goto out; + + 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; + pv_shim_inject_evtchn(pv_console_evtchn()); + + 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; + + /* + * Latch pointers before accessing the ring. Included compiler barrier also + * ensures that pointers are really read only once into local variables. + */ + smp_rmb(); + + ASSERT((prod - cons) <= sizeof(cons_ring->in)); + + /* Is the ring out of space? */ + if ( sizeof(cons_ring->in) - (prod - cons) == 0 ) + goto notify; + + 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. */ + pv_shim_inject_evtchn(pv_console_evtchn()); + + 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@lists.xenproject.org https://lists.xenproject.org/mailman/listinfo/xen-devel