2 * Copyright (c) 2015 Julien Grall <julien.grall@citrix.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/module.h>
32 #include <sys/systm.h>
33 #include <sys/eventhandler.h>
34 #include <sys/consio.h>
39 #include <sys/systm.h>
40 #include <sys/taskqueue.h>
42 #include <sys/kernel.h>
47 #include <sys/reboot.h>
49 #include <machine/stdarg.h>
54 #include <xen/xen-os.h>
55 #include <xen/hypervisor.h>
56 #include <xen/xen_intr.h>
57 #include <xen/interface/io/console.h>
60 #include "opt_printf.h"
66 static char driver_name[] = "xc";
70 typedef void xencons_early_init_t(struct xencons_priv *cons);
71 typedef int xencons_init_t(device_t dev, struct tty *tp,
72 driver_intr_t intr_handler);
73 typedef int xencons_read_t(struct xencons_priv *cons, char *buffer,
75 typedef int xencons_write_t(struct xencons_priv *cons, const char *buffer,
80 * Called by the low-level driver during early boot.
81 * Only the minimal set up to get a console should be done here.
83 xencons_early_init_t *early_init;
84 /* Prepare the console to be fully use */
86 /* Read/write helpers */
88 xencons_write_t *write;
92 /* Mutex to protect the shared ring and the internal buffers */
94 /* Interrupt handler used for notify the backend */
95 xen_intr_handle_t intr_handle;
96 /* KDB internal state */
100 /* Status of the tty */
102 /* Callout used when the write buffer is full */
103 struct callout callout;
105 /* Internal buffers must be used with mtx locked */
106 #define WBUF_SIZE 4096
107 #define WBUF_MASK(_i) ((_i)&(WBUF_SIZE-1))
108 char wbuf[WBUF_SIZE];
109 unsigned int wc, wp; /* Consumer/producer wbuf */
111 #define RBUF_SIZE 1024
112 #define RBUF_MASK(_i) ((_i)&(RBUF_SIZE-1))
113 char rbuf[RBUF_SIZE];
114 unsigned int rc, rp; /* Consumer/producer rbuf */
116 /* Pointer to the console operations */
117 const struct xencons_ops *ops;
120 * Ring specific fields
121 * XXX: make an union?
123 /* Event channel number for early notification (PV only) */
125 /* Console shared page */
126 struct xencons_interface *intf;
130 * Data for the main console
131 * Necessary to support low-level console driver
133 static struct xencons_priv main_cons;
135 #define XC_POLLTIME (hz/10)
137 /*----------------------------- Debug function ------------------------------*/
145 putchar(int c, void *arg)
147 struct putchar_arg *pca;
149 pca = (struct putchar_arg *)arg;
151 if (pca->buf == NULL) {
153 * We have no buffer, output directly to the
154 * console char by char.
156 HYPERVISOR_console_write((char *)&c, 1);
158 pca->buf[pca->n_next++] = c;
159 if ((pca->size == pca->n_next) || (c = '\0')) {
160 /* Flush the buffer */
161 HYPERVISOR_console_write(pca->buf, pca->n_next);
168 xc_printf(const char *fmt, ...)
171 struct putchar_arg pca;
172 #ifdef PRINTF_BUFR_SIZE
173 char buf[PRINTF_BUFR_SIZE];
176 pca.size = sizeof(buf);
183 KASSERT((xen_domain()), ("call to xc_printf from non Xen guest"));
186 kvprintf(fmt, putchar, &pca, 10, ap);
189 #ifdef PRINTF_BUFR_SIZE
191 HYPERVISOR_console_write(buf, pca.n_next);
195 /*---------------------- Helpers for the console lock -----------------------*/
197 * The lock is not used when the kernel is panicing as it will never recover
198 * and we want to output no matter what it costs.
200 static inline void xencons_lock(struct xencons_priv *cons)
203 if (!KERNEL_PANICKED())
204 mtx_lock_spin(&cons->mtx);
208 static inline void xencons_unlock(struct xencons_priv *cons)
211 if (!KERNEL_PANICKED())
212 mtx_unlock_spin(&cons->mtx);
215 #define xencons_lock_assert(cons) mtx_assert(&(cons)->mtx, MA_OWNED)
217 /*------------------ Helpers for the hypervisor console ---------------------*/
219 xencons_early_init_hypervisor(struct xencons_priv *cons)
222 * Nothing to setup for the low-level console when using
223 * the hypervisor console.
228 xencons_init_hypervisor(device_t dev, struct tty *tp,
229 driver_intr_t intr_handler)
231 struct xencons_priv *cons;
234 cons = tty_softc(tp);
236 err = xen_intr_bind_virq(dev, VIRQ_CONSOLE, 0, NULL,
237 intr_handler, tp, INTR_TYPE_TTY | INTR_MPSAFE, &cons->intr_handle);
239 device_printf(dev, "Can't register console interrupt\n");
245 xencons_write_hypervisor(struct xencons_priv *cons, const char *buffer,
249 HYPERVISOR_console_io(CONSOLEIO_write, size, buffer);
255 xencons_read_hypervisor(struct xencons_priv *cons, char *buffer,
259 xencons_lock_assert(cons);
261 return (HYPERVISOR_console_io(CONSOLEIO_read, size, buffer));
264 static const struct xencons_ops xencons_hypervisor_ops = {
265 .early_init = xencons_early_init_hypervisor,
266 .init = xencons_init_hypervisor,
267 .read = xencons_read_hypervisor,
268 .write = xencons_write_hypervisor,
271 /*------------------ Helpers for the ring console ---------------------------*/
273 xencons_early_init_ring(struct xencons_priv *cons)
275 cons->intf = pmap_mapdev_attr(ptoa(xen_get_console_mfn()), PAGE_SIZE,
277 cons->evtchn = xen_get_console_evtchn();
281 xencons_init_ring(device_t dev, struct tty *tp, driver_intr_t intr_handler)
283 struct xencons_priv *cons;
286 cons = tty_softc(tp);
288 if (cons->evtchn == 0)
291 err = xen_intr_bind_local_port(dev, cons->evtchn, NULL,
292 intr_handler, tp, INTR_TYPE_TTY | INTR_MPSAFE, &cons->intr_handle);
300 xencons_notify_ring(struct xencons_priv *cons)
303 * The console may be used before the ring interrupt is properly
305 * If so, fallback to directly use the event channel hypercall.
307 if (__predict_true(cons->intr_handle != NULL))
308 xen_intr_signal(cons->intr_handle);
310 struct evtchn_send send = {
314 HYPERVISOR_event_channel_op(EVTCHNOP_send, &send);
319 xencons_write_ring(struct xencons_priv *cons, const char *buffer,
322 struct xencons_interface *intf;
323 XENCONS_RING_IDX wcons, wprod;
328 xencons_lock_assert(cons);
330 wcons = intf->out_cons;
331 wprod = intf->out_prod;
334 KASSERT((wprod - wcons) <= sizeof(intf->out),
335 ("console send ring inconsistent"));
337 for (sent = 0; sent < size; sent++, wprod++) {
338 if ((wprod - wcons) >= sizeof(intf->out))
340 intf->out[MASK_XENCONS_IDX(wprod, intf->out)] = buffer[sent];
344 intf->out_prod = wprod;
346 xencons_notify_ring(cons);
352 xencons_read_ring(struct xencons_priv *cons, char *buffer, unsigned int size)
354 struct xencons_interface *intf;
355 XENCONS_RING_IDX rcons, rprod;
360 xencons_lock_assert(cons);
362 rcons = intf->in_cons;
363 rprod = intf->in_prod;
366 for (rsz = 0; rsz < size; rsz++, rcons++) {
369 buffer[rsz] = intf->in[MASK_XENCONS_IDX(rcons, intf->in)];
373 intf->in_cons = rcons;
375 /* No need to notify the backend if nothing has been read */
377 xencons_notify_ring(cons);
382 static const struct xencons_ops xencons_ring_ops = {
383 .early_init = xencons_early_init_ring,
384 .init = xencons_init_ring,
385 .read = xencons_read_ring,
386 .write = xencons_write_ring,
389 /*------------------ Common implementation of the console -------------------*/
392 * Called by the low-level driver during early boot to initialize the
393 * main console driver.
394 * Only the minimal set up to get a console should be done here.
397 xencons_early_init(void)
400 mtx_init(&main_cons.mtx, "XCONS LOCK", NULL, MTX_SPIN);
402 if (xen_get_console_evtchn() == 0)
403 main_cons.ops = &xencons_hypervisor_ops;
405 main_cons.ops = &xencons_ring_ops;
407 main_cons.ops->early_init(&main_cons);
411 * Receive character from the console and put them in the internal buffer
412 * XXX: Handle overflow of the internal buffer
415 xencons_rx(struct xencons_priv *cons)
421 while ((sz = cons->ops->read(cons, buf, sizeof(buf))) > 0) {
424 for (i = 0; i < sz; i++)
425 cons->rbuf[RBUF_MASK(cons->rp++)] = buf[i];
427 xencons_unlock(cons);
430 /* Return true if the write buffer is full */
432 xencons_tx_full(struct xencons_priv *cons)
437 used = cons->wp - cons->wc;
438 xencons_unlock(cons);
440 return (used >= WBUF_SIZE);
444 xencons_tx_flush(struct xencons_priv *cons, int force)
449 while (cons->wc != cons->wp) {
451 sz = cons->wp - cons->wc;
452 if (sz > (WBUF_SIZE - WBUF_MASK(cons->wc)))
453 sz = WBUF_SIZE - WBUF_MASK(cons->wc);
454 sent = cons->ops->write(cons, &cons->wbuf[WBUF_MASK(cons->wc)],
458 * The other end may not have been initialized. Ignore
461 if (__predict_false(sent < 0))
465 * If force is set, spin until the console data is
466 * flushed through the domain controller.
468 if (sent == 0 && __predict_true(!force))
473 xencons_unlock(cons);
477 xencons_putc(struct xencons_priv *cons, int c, bool force_flush)
481 if ((cons->wp - cons->wc) < WBUF_SIZE)
482 cons->wbuf[WBUF_MASK(cons->wp++)] = c;
483 xencons_unlock(cons);
485 xencons_tx_flush(cons, force_flush);
487 return (xencons_tx_full(cons));
491 xencons_getc(struct xencons_priv *cons)
496 if (cons->rp != cons->rc) {
497 /* We need to return only one char */
498 ret = (int)cons->rbuf[RBUF_MASK(cons->rc)];
504 xencons_unlock(cons);
510 xencons_tx(struct tty *tp)
514 struct xencons_priv *cons;
516 cons = tty_softc(tp);
518 tty_lock_assert(tp, MA_OWNED);
521 * Don't transmit any character if the buffer is full. Otherwise,
522 * characters may be lost
524 if (xencons_tx_full(cons))
528 while (!cons_full && ttydisc_getc(tp, &c, 1) == 1)
529 cons_full = xencons_putc(cons, c, false);
535 xencons_intr(void *arg)
538 struct xencons_priv *cons;
542 cons = tty_softc(tp);
545 * The input will be used by the low-level console when KDB is active
551 * It's not necessary to retrieve input when the tty is not opened
559 while ((ret = xencons_getc(cons)) != -1) {
561 kdb_alt_break(ret, &cons->altbrk);
563 ttydisc_rint(tp, ret, 0);
565 ttydisc_rint_done(tp);
568 /* Try to flush remaining characters if necessary */
569 xencons_tx_flush(cons, 0);
573 * Helpers to call while shutting down:
574 * - Force flush all output
577 xencons_shutdown(void *arg, int howto)
583 xencons_tx_flush(tty_softc(tp), 1);
586 /*---------------------- Low-level console driver ---------------------------*/
588 xencons_cnprobe(struct consdev *cp)
594 cp->cn_pri = (boothowto & RB_SERIAL) ? CN_REMOTE : CN_NORMAL;
595 sprintf(cp->cn_name, "%s0", driver_name);
599 xencons_cninit(struct consdev *cp)
602 xencons_early_init();
606 xencons_cnterm(struct consdev *cp)
611 xencons_cngrab(struct consdev *cp)
616 xencons_cnungrab(struct consdev *cp)
621 xencons_cngetc(struct consdev *dev)
624 xencons_rx(&main_cons);
626 return (xencons_getc(&main_cons));
630 xencons_cnputc(struct consdev *dev, int c)
633 * The low-level console is used by KDB and panic. We have to ensure
634 * that any character sent will be seen by the backend.
636 xencons_putc(&main_cons, c, true);
639 CONSOLE_DRIVER(xencons);
641 /*----------------------------- TTY driver ---------------------------------*/
644 xencons_tty_open(struct tty *tp)
646 struct xencons_priv *cons;
648 cons = tty_softc(tp);
656 xencons_tty_close(struct tty *tp)
658 struct xencons_priv *cons;
660 cons = tty_softc(tp);
662 cons->opened = false;
666 xencons_timeout(void *v)
669 struct xencons_priv *cons;
672 cons = tty_softc(tp);
675 callout_reset(&cons->callout, XC_POLLTIME,
676 xencons_timeout, tp);
680 xencons_tty_outwakeup(struct tty *tp)
682 struct xencons_priv *cons;
684 cons = tty_softc(tp);
686 callout_stop(&cons->callout);
689 callout_reset(&cons->callout, XC_POLLTIME,
690 xencons_timeout, tp);
693 static struct ttydevsw xencons_ttydevsw = {
694 .tsw_flags = TF_NOPREFIX,
695 .tsw_open = xencons_tty_open,
696 .tsw_close = xencons_tty_close,
697 .tsw_outwakeup = xencons_tty_outwakeup,
700 /*------------------------ Main console driver ------------------------------*/
702 xencons_identify(driver_t *driver, device_t parent)
706 if (main_cons.ops == NULL)
709 child = BUS_ADD_CHILD(parent, 0, driver_name, 0);
713 xencons_probe(device_t dev)
716 device_set_desc(dev, "Xen Console");
717 return (BUS_PROBE_NOWILDCARD);
721 xencons_attach(device_t dev)
725 * The main console is already allocated statically in order to
726 * support low-level console
728 struct xencons_priv *cons;
733 tp = tty_alloc(&xencons_ttydevsw, cons);
734 tty_makedev(tp, NULL, "%s%r", driver_name, 0);
735 device_set_softc(dev, tp);
737 callout_init_mtx(&cons->callout, tty_getlock(tp), 0);
739 err = cons->ops->init(dev, tp, xencons_intr);
741 device_printf(dev, "Unable to initialize the console (%d)\n",
746 /* register handler to flush console on shutdown */
747 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, xencons_shutdown,
748 tp, SHUTDOWN_PRI_DEFAULT)) == NULL)
749 device_printf(dev, "shutdown event registration failed!\n");
755 xencons_resume(device_t dev)
757 struct xencons_priv *cons;
761 tp = device_get_softc(dev);
762 cons = tty_softc(tp);
763 xen_intr_unbind(&cons->intr_handle);
765 err = cons->ops->init(dev, tp, xencons_intr);
767 device_printf(dev, "Unable to resume the console (%d)\n", err);
774 static devclass_t xencons_devclass;
776 static device_method_t xencons_methods[] = {
777 DEVMETHOD(device_identify, xencons_identify),
778 DEVMETHOD(device_probe, xencons_probe),
779 DEVMETHOD(device_attach, xencons_attach),
780 DEVMETHOD(device_resume, xencons_resume),
785 static driver_t xencons_driver = {
791 DRIVER_MODULE(xc, xenpv, xencons_driver, xencons_devclass, 0, 0);