1 /******************************************************************************
4 * Virtual console driver.
6 * Copyright (c) 2002-2004, K A Fraser.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version 2
10 * as published by the Free Software Foundation; or, when distributed
11 * separately from the Linux kernel or incorporated into other
12 * software packages, subject to the following license:
14 * Permission is hereby granted, free of charge, to any person obtaining a copy
15 * of this source file (the "Software"), to deal in the Software without
16 * restriction, including without limitation the rights to use, copy, modify,
17 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
18 * and to permit persons to whom the Software is furnished to do so, subject to
19 * the following conditions:
21 * The above copyright notice and this permission notice shall be included in
22 * all copies or substantial portions of the Software.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
33 #include <linux/config.h>
34 #include <linux/version.h>
35 #include <linux/module.h>
36 #include <linux/errno.h>
37 #include <linux/signal.h>
38 #include <linux/sched.h>
39 #include <linux/interrupt.h>
40 #include <linux/tty.h>
41 #include <linux/tty_flip.h>
42 #include <linux/serial.h>
43 #include <linux/major.h>
44 #include <linux/ptrace.h>
45 #include <linux/ioport.h>
47 #include <linux/slab.h>
48 #include <linux/init.h>
49 #include <linux/console.h>
50 #include <linux/bootmem.h>
51 #include <linux/sysrq.h>
54 #include <asm/uaccess.h>
55 #include <xen/interface/xen.h>
56 #include <xen/interface/event_channel.h>
57 #include <asm/hypervisor.h>
58 #include <xen/evtchn.h>
59 #include <xen/xencons.h>
63 * 'xencons=off' [XC_OFF]: Console is disabled.
64 * 'xencons=tty' [XC_TTY]: Console attached to '/dev/tty[0-9]+'.
65 * 'xencons=ttyS' [XC_SERIAL]: Console attached to '/dev/ttyS[0-9]+'.
66 * [XC_DEFAULT]: DOM0 -> XC_SERIAL ; all others -> XC_TTY.
68 * NB. In mode XC_TTY, we create dummy consoles for tty2-63. This suppresses
69 * warnings from standard distro startup scripts.
71 static enum { XC_OFF, XC_DEFAULT, XC_TTY, XC_SERIAL } xc_mode = XC_DEFAULT;
72 static int xc_num = -1;
74 #ifdef CONFIG_MAGIC_SYSRQ
75 static unsigned long sysrq_requested;
76 extern int sysrq_enabled;
79 static int __init xencons_setup(char *str)
84 if (!strncmp(str, "ttyS", 4))
86 else if (!strncmp(str, "tty", 3))
88 else if (!strncmp(str, "off", 3))
93 n = simple_strtol(str+4, &q, 10);
98 n = simple_strtol(str+3, &q, 10);
108 __setup("xencons=", xencons_setup);
110 /* The kernel and user-land drivers share a common transmit buffer. */
111 static unsigned int wbuf_size = 4096;
112 #define WBUF_MASK(_i) ((_i)&(wbuf_size-1))
114 static unsigned int wc, wp; /* write_cons, write_prod */
116 static int __init xencons_bufsz_setup(char *str)
119 goal = simple_strtoul(str, NULL, 0);
121 goal = roundup_pow_of_two(goal);
122 if (wbuf_size < goal)
127 __setup("xencons_bufsz=", xencons_bufsz_setup);
129 /* This lock protects accesses to the common transmit buffer. */
130 static DEFINE_SPINLOCK(xencons_lock);
132 /* Common transmit-kick routine. */
133 static void __xencons_tx_flush(void);
135 static struct tty_driver *xencons_driver;
137 /******************** Kernel console driver ********************************/
139 static void kcons_write(struct console *c, const char *s, unsigned int count)
144 spin_lock_irqsave(&xencons_lock, flags);
147 for (; i < count; i++) {
148 if ((wp - wc) >= (wbuf_size - 1))
150 if ((wbuf[WBUF_MASK(wp++)] = s[i]) == '\n')
151 wbuf[WBUF_MASK(wp++)] = '\r';
154 __xencons_tx_flush();
157 spin_unlock_irqrestore(&xencons_lock, flags);
160 static void kcons_write_dom0(struct console *c, const char *s, unsigned int count)
165 rc = HYPERVISOR_console_io( CONSOLEIO_write, count, (char *)s);
173 static struct tty_driver *kcons_device(struct console *c, int *index)
176 return xencons_driver;
179 static struct console kcons_info = {
180 .device = kcons_device,
181 .flags = CON_PRINTBUFFER | CON_ENABLED,
186 static int __init xen_console_init(void)
188 if (!is_running_on_xen())
191 if (xen_start_info->flags & SIF_INITDOMAIN) {
192 if (xc_mode == XC_DEFAULT)
194 kcons_info.write = kcons_write_dom0;
196 if (xc_mode == XC_DEFAULT)
198 kcons_info.write = kcons_write;
203 strcpy(kcons_info.name, "ttyS");
209 strcpy(kcons_info.name, "tty");
218 wbuf = alloc_bootmem(wbuf_size);
220 register_console(&kcons_info);
224 console_initcall(xen_console_init);
226 /*** Useful function for console debugging -- goes straight to Xen. ***/
227 asmlinkage int xprintk(const char *fmt, ...)
231 static char printk_buf[1024];
233 /* Emit the output into the temporary buffer */
235 printk_len = vsnprintf(printk_buf, sizeof(printk_buf), fmt, args);
238 /* Send the processed output directly to Xen. */
239 kcons_write_dom0(NULL, printk_buf, printk_len);
244 /*** Forcibly flush console data before dying. ***/
245 void xencons_force_flush(void)
249 /* Emergency console is synchronous, so there's nothing to flush. */
250 if (xen_start_info->flags & SIF_INITDOMAIN)
253 /* Spin until console data is flushed through to the daemon. */
256 if ((sz = wp - wc) == 0)
258 sent = xencons_ring_send(&wbuf[WBUF_MASK(wc)], sz);
265 /******************** User-space console driver (/dev/console) ************/
268 #define DUMMY_TTY(_tty) ((xc_mode != XC_SERIAL) && \
269 ((_tty)->index != (xc_num - 1)))
271 static struct termios *xencons_termios[MAX_NR_CONSOLES];
272 static struct termios *xencons_termios_locked[MAX_NR_CONSOLES];
273 static struct tty_struct *xencons_tty;
274 static int xencons_priv_irq;
277 void xencons_rx(char *buf, unsigned len, struct pt_regs *regs)
282 spin_lock_irqsave(&xencons_lock, flags);
283 if (xencons_tty == NULL)
286 for (i = 0; i < len; i++) {
287 #ifdef CONFIG_MAGIC_SYSRQ
289 if (buf[i] == '\x0f') { /* ^O */
290 sysrq_requested = jiffies;
291 continue; /* don't print the sysrq key */
292 } else if (sysrq_requested) {
293 unsigned long sysrq_timeout =
294 sysrq_requested + HZ*2;
296 if (time_before(jiffies, sysrq_timeout)) {
297 spin_unlock_irqrestore(
298 &xencons_lock, flags);
300 buf[i], regs, xencons_tty);
302 &xencons_lock, flags);
308 tty_insert_flip_char(xencons_tty, buf[i], 0);
310 tty_flip_buffer_push(xencons_tty);
313 spin_unlock_irqrestore(&xencons_lock, flags);
316 static void __xencons_tx_flush(void)
318 int sent, sz, work_done = 0;
321 if (xen_start_info->flags & SIF_INITDOMAIN)
322 kcons_write_dom0(NULL, &x_char, 1);
325 if (xencons_ring_send(&x_char, 1) == 1)
333 if (sz > (wbuf_size - WBUF_MASK(wc)))
334 sz = wbuf_size - WBUF_MASK(wc);
335 if (xen_start_info->flags & SIF_INITDOMAIN) {
336 kcons_write_dom0(NULL, &wbuf[WBUF_MASK(wc)], sz);
339 sent = xencons_ring_send(&wbuf[WBUF_MASK(wc)], sz);
347 if (work_done && (xencons_tty != NULL)) {
348 wake_up_interruptible(&xencons_tty->write_wait);
349 if ((xencons_tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
350 (xencons_tty->ldisc.write_wakeup != NULL))
351 (xencons_tty->ldisc.write_wakeup)(xencons_tty);
355 void xencons_tx(void)
359 spin_lock_irqsave(&xencons_lock, flags);
360 __xencons_tx_flush();
361 spin_unlock_irqrestore(&xencons_lock, flags);
364 /* Privileged receive callback and transmit kicker. */
365 static irqreturn_t xencons_priv_interrupt(int irq, void *dev_id,
366 struct pt_regs *regs)
368 static char rbuf[16];
371 while ((l = HYPERVISOR_console_io(CONSOLEIO_read, 16, rbuf)) > 0)
372 xencons_rx(rbuf, l, regs);
379 static int xencons_write_room(struct tty_struct *tty)
381 return wbuf_size - (wp - wc);
384 static int xencons_chars_in_buffer(struct tty_struct *tty)
389 static void xencons_send_xchar(struct tty_struct *tty, char ch)
396 spin_lock_irqsave(&xencons_lock, flags);
398 __xencons_tx_flush();
399 spin_unlock_irqrestore(&xencons_lock, flags);
402 static void xencons_throttle(struct tty_struct *tty)
408 xencons_send_xchar(tty, STOP_CHAR(tty));
411 static void xencons_unthrottle(struct tty_struct *tty)
420 xencons_send_xchar(tty, START_CHAR(tty));
424 static void xencons_flush_buffer(struct tty_struct *tty)
431 spin_lock_irqsave(&xencons_lock, flags);
433 spin_unlock_irqrestore(&xencons_lock, flags);
436 static inline int __xencons_put_char(int ch)
439 if ((wp - wc) == wbuf_size)
441 wbuf[WBUF_MASK(wp++)] = _ch;
445 static int xencons_write(
446 struct tty_struct *tty,
447 const unsigned char *buf,
456 spin_lock_irqsave(&xencons_lock, flags);
458 for (i = 0; i < count; i++)
459 if (!__xencons_put_char(buf[i]))
463 __xencons_tx_flush();
465 spin_unlock_irqrestore(&xencons_lock, flags);
470 static void xencons_put_char(struct tty_struct *tty, u_char ch)
477 spin_lock_irqsave(&xencons_lock, flags);
478 (void)__xencons_put_char(ch);
479 spin_unlock_irqrestore(&xencons_lock, flags);
482 static void xencons_flush_chars(struct tty_struct *tty)
489 spin_lock_irqsave(&xencons_lock, flags);
490 __xencons_tx_flush();
491 spin_unlock_irqrestore(&xencons_lock, flags);
494 static void xencons_wait_until_sent(struct tty_struct *tty, int timeout)
496 unsigned long orig_jiffies = jiffies;
501 while (DRV(tty->driver)->chars_in_buffer(tty)) {
502 set_current_state(TASK_INTERRUPTIBLE);
504 if (signal_pending(current))
506 if (timeout && time_after(jiffies, orig_jiffies + timeout))
510 set_current_state(TASK_RUNNING);
513 static int xencons_open(struct tty_struct *tty, struct file *filp)
520 spin_lock_irqsave(&xencons_lock, flags);
521 tty->driver_data = NULL;
522 if (xencons_tty == NULL)
524 __xencons_tx_flush();
525 spin_unlock_irqrestore(&xencons_lock, flags);
530 static void xencons_close(struct tty_struct *tty, struct file *filp)
537 mutex_lock(&tty_mutex);
539 if (tty->count != 1) {
540 mutex_unlock(&tty_mutex);
544 /* Prevent other threads from re-opening this tty. */
545 set_bit(TTY_CLOSING, &tty->flags);
546 mutex_unlock(&tty_mutex);
549 tty_wait_until_sent(tty, 0);
550 if (DRV(tty->driver)->flush_buffer != NULL)
551 DRV(tty->driver)->flush_buffer(tty);
552 if (tty->ldisc.flush_buffer != NULL)
553 tty->ldisc.flush_buffer(tty);
555 spin_lock_irqsave(&xencons_lock, flags);
557 spin_unlock_irqrestore(&xencons_lock, flags);
560 static struct tty_operations xencons_ops = {
561 .open = xencons_open,
562 .close = xencons_close,
563 .write = xencons_write,
564 .write_room = xencons_write_room,
565 .put_char = xencons_put_char,
566 .flush_chars = xencons_flush_chars,
567 .chars_in_buffer = xencons_chars_in_buffer,
568 .send_xchar = xencons_send_xchar,
569 .flush_buffer = xencons_flush_buffer,
570 .throttle = xencons_throttle,
571 .unthrottle = xencons_unthrottle,
572 .wait_until_sent = xencons_wait_until_sent,
575 static int __init xencons_init(void)
579 if (!is_running_on_xen())
582 if (xc_mode == XC_OFF)
587 xencons_driver = alloc_tty_driver((xc_mode == XC_SERIAL) ?
588 1 : MAX_NR_CONSOLES);
589 if (xencons_driver == NULL)
592 DRV(xencons_driver)->name = "xencons";
593 DRV(xencons_driver)->major = TTY_MAJOR;
594 DRV(xencons_driver)->type = TTY_DRIVER_TYPE_SERIAL;
595 DRV(xencons_driver)->subtype = SERIAL_TYPE_NORMAL;
596 DRV(xencons_driver)->init_termios = tty_std_termios;
597 DRV(xencons_driver)->flags =
598 TTY_DRIVER_REAL_RAW |
599 TTY_DRIVER_RESET_TERMIOS;
600 DRV(xencons_driver)->termios = xencons_termios;
601 DRV(xencons_driver)->termios_locked = xencons_termios_locked;
603 if (xc_mode == XC_SERIAL) {
604 DRV(xencons_driver)->name = "ttyS";
605 DRV(xencons_driver)->minor_start = 64 + xc_num;
606 DRV(xencons_driver)->name_base = 0 + xc_num;
608 DRV(xencons_driver)->name = "tty";
609 DRV(xencons_driver)->minor_start = 1;
610 DRV(xencons_driver)->name_base = 1;
613 tty_set_operations(xencons_driver, &xencons_ops);
615 if ((rc = tty_register_driver(DRV(xencons_driver))) != 0) {
616 printk("WARNING: Failed to register Xen virtual "
617 "console driver as '%s%d'\n",
618 DRV(xencons_driver)->name,
619 DRV(xencons_driver)->name_base);
620 put_tty_driver(xencons_driver);
621 xencons_driver = NULL;
625 if (xen_start_info->flags & SIF_INITDOMAIN) {
626 xencons_priv_irq = bind_virq_to_irqhandler(
629 xencons_priv_interrupt,
633 BUG_ON(xencons_priv_irq < 0);
636 printk("Xen virtual console successfully installed as %s%d\n",
637 DRV(xencons_driver)->name, xc_num);
642 module_init(xencons_init);
644 MODULE_LICENSE("Dual BSD/GPL");