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/version.h>
34 #include <linux/module.h>
35 #include <linux/errno.h>
36 #include <linux/signal.h>
37 #include <linux/sched.h>
38 #include <linux/interrupt.h>
39 #include <linux/tty.h>
40 #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>
52 #include <linux/screen_info.h>
55 #include <asm/uaccess.h>
56 #include <xen/interface/xen.h>
57 #include <xen/interface/event_channel.h>
58 #include <asm/hypervisor.h>
59 #include <xen/evtchn.h>
60 #include <xen/xenbus.h>
61 #include <xen/xencons.h>
65 * 'xencons=off' [XC_OFF]: Console is disabled.
66 * 'xencons=tty' [XC_TTY]: Console attached to '/dev/tty[0-9]+'.
67 * 'xencons=ttyS' [XC_SERIAL]: Console attached to '/dev/ttyS[0-9]+'.
68 * 'xencons=xvc' [XC_XVC]: Console attached to '/dev/xvc0'.
69 * [XC_DEFAULT]: DOM0 -> XC_SERIAL ; all others -> XC_TTY.
71 * NB. In mode XC_TTY, we create dummy consoles for tty2-63. This suppresses
72 * warnings from standard distro startup scripts.
75 XC_OFF, XC_DEFAULT, XC_TTY, XC_SERIAL, XC_XVC
76 } xc_mode = XC_DEFAULT;
77 static int xc_num = -1;
79 /* /dev/xvc0 device number allocated by lanana.org. */
80 #define XEN_XVC_MAJOR 204
81 #define XEN_XVC_MINOR 191
83 #ifdef CONFIG_MAGIC_SYSRQ
84 static unsigned long sysrq_requested;
87 static int __init xencons_setup(char *str)
92 if (!strncmp(str, "ttyS", 4)) {
95 } else if (!strncmp(str, "tty", 3)) {
98 } else if (!strncmp(str, "xvc", 3)) {
101 } else if (!strncmp(str, "off", 3)) {
106 n = simple_strtol(str, &q, 10);
112 __setup("xencons=", xencons_setup);
114 /* The kernel and user-land drivers share a common transmit buffer. */
115 static unsigned int wbuf_size = 4096;
116 #define WBUF_MASK(_i) ((_i)&(wbuf_size-1))
118 static unsigned int wc, wp; /* write_cons, write_prod */
120 static int __init xencons_bufsz_setup(char *str)
123 goal = simple_strtoul(str, NULL, 0);
125 goal = roundup_pow_of_two(goal);
126 if (wbuf_size < goal)
131 __setup("xencons_bufsz=", xencons_bufsz_setup);
133 /* This lock protects accesses to the common transmit buffer. */
134 static DEFINE_SPINLOCK(xencons_lock);
136 /* Common transmit-kick routine. */
137 static void __xencons_tx_flush(void);
139 static struct tty_driver *xencons_driver;
141 /******************** Kernel console driver ********************************/
143 static void kcons_write(struct console *c, const char *s, unsigned int count)
148 spin_lock_irqsave(&xencons_lock, flags);
151 for (; i < count; i++) {
152 if ((wp - wc) >= (wbuf_size - 1))
154 if ((wbuf[WBUF_MASK(wp++)] = s[i]) == '\n')
155 wbuf[WBUF_MASK(wp++)] = '\r';
158 __xencons_tx_flush();
161 spin_unlock_irqrestore(&xencons_lock, flags);
164 static void kcons_write_dom0(struct console *c, const char *s, unsigned int count)
169 rc = HYPERVISOR_console_io( CONSOLEIO_write, count, (char *)s);
177 static struct tty_driver *kcons_device(struct console *c, int *index)
180 return xencons_driver;
183 static struct console kcons_info = {
184 .device = kcons_device,
185 .flags = CON_PRINTBUFFER | CON_ENABLED,
189 static int __init xen_console_init(void)
191 if (!is_running_on_xen())
194 if (is_initial_xendomain()) {
195 if (xc_mode == XC_DEFAULT)
197 kcons_info.write = kcons_write_dom0;
199 if (!xen_start_info->console.domU.evtchn)
201 if (xc_mode == XC_DEFAULT)
203 kcons_info.write = kcons_write;
208 strcpy(kcons_info.name, "xvc");
214 strcpy(kcons_info.name, "ttyS");
220 strcpy(kcons_info.name, "tty");
229 wbuf = alloc_bootmem(wbuf_size);
231 register_console(&kcons_info);
236 console_initcall(xen_console_init);
238 /*** Useful function for console debugging -- goes straight to Xen. ***/
239 asmlinkage int xprintk(const char *fmt, ...)
243 static char printk_buf[1024];
245 /* Emit the output into the temporary buffer */
247 printk_len = vsnprintf(printk_buf, sizeof(printk_buf), fmt, args);
250 /* Send the processed output directly to Xen. */
251 kcons_write_dom0(NULL, printk_buf, printk_len);
256 /*** Forcibly flush console data before dying. ***/
257 void xencons_force_flush(void)
261 /* Emergency console is synchronous, so there's nothing to flush. */
262 if (!is_running_on_xen() ||
263 is_initial_xendomain() ||
264 !xen_start_info->console.domU.evtchn)
267 /* Spin until console data is flushed through to the daemon. */
270 if ((sz = wp - wc) == 0)
272 sent = xencons_ring_send(&wbuf[WBUF_MASK(wc)], sz);
279 void dom0_init_screen_info(const struct dom0_vga_console_info *info)
281 switch (info->video_type) {
282 case XEN_VGATYPE_TEXT_MODE_3:
283 screen_info.orig_video_mode = 3;
284 screen_info.orig_video_ega_bx = 3;
285 screen_info.orig_video_isVGA = 1;
286 screen_info.orig_video_lines = info->u.text_mode_3.rows;
287 screen_info.orig_video_cols = info->u.text_mode_3.columns;
288 screen_info.orig_x = info->u.text_mode_3.cursor_x;
289 screen_info.orig_y = info->u.text_mode_3.cursor_y;
290 screen_info.orig_video_points =
291 info->u.text_mode_3.font_height;
293 case XEN_VGATYPE_VESA_LFB:
294 screen_info.orig_video_isVGA = VIDEO_TYPE_VLFB;
295 screen_info.lfb_width = info->u.vesa_lfb.width;
296 screen_info.lfb_height = info->u.vesa_lfb.height;
297 screen_info.lfb_depth = info->u.vesa_lfb.bits_per_pixel;
298 screen_info.lfb_base = info->u.vesa_lfb.lfb_base;
299 screen_info.lfb_size = info->u.vesa_lfb.lfb_size;
300 screen_info.lfb_linelength = info->u.vesa_lfb.bytes_per_line;
301 screen_info.red_size = info->u.vesa_lfb.red_size;
302 screen_info.red_pos = info->u.vesa_lfb.red_pos;
303 screen_info.green_size = info->u.vesa_lfb.green_size;
304 screen_info.green_pos = info->u.vesa_lfb.green_pos;
305 screen_info.blue_size = info->u.vesa_lfb.blue_size;
306 screen_info.blue_pos = info->u.vesa_lfb.blue_pos;
307 screen_info.rsvd_size = info->u.vesa_lfb.rsvd_size;
308 screen_info.rsvd_pos = info->u.vesa_lfb.rsvd_pos;
314 /******************** User-space console driver (/dev/console) ************/
317 #define DUMMY_TTY(_tty) ((xc_mode == XC_TTY) && \
318 ((_tty)->index != (xc_num - 1)))
320 static struct ktermios *xencons_termios[MAX_NR_CONSOLES];
321 static struct ktermios *xencons_termios_locked[MAX_NR_CONSOLES];
322 static struct tty_struct *xencons_tty;
323 static int xencons_priv_irq;
326 void xencons_rx(char *buf, unsigned len)
331 spin_lock_irqsave(&xencons_lock, flags);
332 if (xencons_tty == NULL)
335 for (i = 0; i < len; i++) {
336 #ifdef CONFIG_MAGIC_SYSRQ
338 if (buf[i] == '\x0f') { /* ^O */
339 sysrq_requested = jiffies;
340 continue; /* don't print the sysrq key */
341 } else if (sysrq_requested) {
342 unsigned long sysrq_timeout =
343 sysrq_requested + HZ*2;
345 if (time_before(jiffies, sysrq_timeout)) {
346 spin_unlock_irqrestore(
347 &xencons_lock, flags);
349 buf[i], xencons_tty);
351 &xencons_lock, flags);
357 tty_insert_flip_char(xencons_tty, buf[i], 0);
359 tty_flip_buffer_push(xencons_tty);
362 spin_unlock_irqrestore(&xencons_lock, flags);
365 static void __xencons_tx_flush(void)
367 int sent, sz, work_done = 0;
370 if (is_initial_xendomain())
371 kcons_write_dom0(NULL, &x_char, 1);
374 if (xencons_ring_send(&x_char, 1) == 1)
382 if (sz > (wbuf_size - WBUF_MASK(wc)))
383 sz = wbuf_size - WBUF_MASK(wc);
384 if (is_initial_xendomain()) {
385 kcons_write_dom0(NULL, &wbuf[WBUF_MASK(wc)], sz);
388 sent = xencons_ring_send(&wbuf[WBUF_MASK(wc)], sz);
396 if (work_done && (xencons_tty != NULL)) {
397 wake_up_interruptible(&xencons_tty->write_wait);
398 if ((xencons_tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
399 (xencons_tty->ldisc.write_wakeup != NULL))
400 (xencons_tty->ldisc.write_wakeup)(xencons_tty);
404 void xencons_tx(void)
408 spin_lock_irqsave(&xencons_lock, flags);
409 __xencons_tx_flush();
410 spin_unlock_irqrestore(&xencons_lock, flags);
413 /* Privileged receive callback and transmit kicker. */
414 static irqreturn_t xencons_priv_interrupt(int irq, void *dev_id)
416 static char rbuf[16];
419 while ((l = HYPERVISOR_console_io(CONSOLEIO_read, 16, rbuf)) > 0)
427 static int xencons_write_room(struct tty_struct *tty)
429 return wbuf_size - (wp - wc);
432 static int xencons_chars_in_buffer(struct tty_struct *tty)
437 static void xencons_send_xchar(struct tty_struct *tty, char ch)
444 spin_lock_irqsave(&xencons_lock, flags);
446 __xencons_tx_flush();
447 spin_unlock_irqrestore(&xencons_lock, flags);
450 static void xencons_throttle(struct tty_struct *tty)
456 xencons_send_xchar(tty, STOP_CHAR(tty));
459 static void xencons_unthrottle(struct tty_struct *tty)
468 xencons_send_xchar(tty, START_CHAR(tty));
472 static void xencons_flush_buffer(struct tty_struct *tty)
479 spin_lock_irqsave(&xencons_lock, flags);
481 spin_unlock_irqrestore(&xencons_lock, flags);
484 static inline int __xencons_put_char(int ch)
487 if ((wp - wc) == wbuf_size)
489 wbuf[WBUF_MASK(wp++)] = _ch;
493 static int xencons_write(
494 struct tty_struct *tty,
495 const unsigned char *buf,
504 spin_lock_irqsave(&xencons_lock, flags);
506 for (i = 0; i < count; i++)
507 if (!__xencons_put_char(buf[i]))
511 __xencons_tx_flush();
513 spin_unlock_irqrestore(&xencons_lock, flags);
518 static void xencons_put_char(struct tty_struct *tty, u_char ch)
525 spin_lock_irqsave(&xencons_lock, flags);
526 (void)__xencons_put_char(ch);
527 spin_unlock_irqrestore(&xencons_lock, flags);
530 static void xencons_flush_chars(struct tty_struct *tty)
537 spin_lock_irqsave(&xencons_lock, flags);
538 __xencons_tx_flush();
539 spin_unlock_irqrestore(&xencons_lock, flags);
542 static void xencons_wait_until_sent(struct tty_struct *tty, int timeout)
544 unsigned long orig_jiffies = jiffies;
549 while (DRV(tty->driver)->chars_in_buffer(tty)) {
550 set_current_state(TASK_INTERRUPTIBLE);
552 if (signal_pending(current))
554 if (timeout && time_after(jiffies, orig_jiffies + timeout))
558 set_current_state(TASK_RUNNING);
561 static int xencons_open(struct tty_struct *tty, struct file *filp)
568 spin_lock_irqsave(&xencons_lock, flags);
569 tty->driver_data = NULL;
570 if (xencons_tty == NULL)
572 __xencons_tx_flush();
573 spin_unlock_irqrestore(&xencons_lock, flags);
578 static void xencons_close(struct tty_struct *tty, struct file *filp)
585 mutex_lock(&tty_mutex);
587 if (tty->count != 1) {
588 mutex_unlock(&tty_mutex);
592 /* Prevent other threads from re-opening this tty. */
593 set_bit(TTY_CLOSING, &tty->flags);
594 mutex_unlock(&tty_mutex);
597 tty_wait_until_sent(tty, 0);
598 if (DRV(tty->driver)->flush_buffer != NULL)
599 DRV(tty->driver)->flush_buffer(tty);
600 if (tty->ldisc.flush_buffer != NULL)
601 tty->ldisc.flush_buffer(tty);
603 spin_lock_irqsave(&xencons_lock, flags);
605 spin_unlock_irqrestore(&xencons_lock, flags);
608 static struct tty_operations xencons_ops = {
609 .open = xencons_open,
610 .close = xencons_close,
611 .write = xencons_write,
612 .write_room = xencons_write_room,
613 .put_char = xencons_put_char,
614 .flush_chars = xencons_flush_chars,
615 .chars_in_buffer = xencons_chars_in_buffer,
616 .send_xchar = xencons_send_xchar,
617 .flush_buffer = xencons_flush_buffer,
618 .throttle = xencons_throttle,
619 .unthrottle = xencons_unthrottle,
620 .wait_until_sent = xencons_wait_until_sent,
623 static int __init xencons_init(void)
627 if (!is_running_on_xen())
630 if (xc_mode == XC_OFF)
633 if (!is_initial_xendomain()) {
634 rc = xencons_ring_init();
639 xencons_driver = alloc_tty_driver((xc_mode == XC_TTY) ?
640 MAX_NR_CONSOLES : 1);
641 if (xencons_driver == NULL)
644 DRV(xencons_driver)->name = "xencons";
645 DRV(xencons_driver)->major = TTY_MAJOR;
646 DRV(xencons_driver)->type = TTY_DRIVER_TYPE_SERIAL;
647 DRV(xencons_driver)->subtype = SERIAL_TYPE_NORMAL;
648 DRV(xencons_driver)->init_termios = tty_std_termios;
649 DRV(xencons_driver)->flags =
650 TTY_DRIVER_REAL_RAW |
651 TTY_DRIVER_RESET_TERMIOS;
652 DRV(xencons_driver)->termios = xencons_termios;
653 DRV(xencons_driver)->termios_locked = xencons_termios_locked;
657 DRV(xencons_driver)->name = "xvc";
658 DRV(xencons_driver)->major = XEN_XVC_MAJOR;
659 DRV(xencons_driver)->minor_start = XEN_XVC_MINOR;
660 DRV(xencons_driver)->name_base = xc_num;
663 DRV(xencons_driver)->name = "ttyS";
664 DRV(xencons_driver)->minor_start = 64 + xc_num;
665 DRV(xencons_driver)->name_base = xc_num;
668 DRV(xencons_driver)->name = "tty";
669 DRV(xencons_driver)->minor_start = 1;
670 DRV(xencons_driver)->name_base = 1;
674 tty_set_operations(xencons_driver, &xencons_ops);
676 if ((rc = tty_register_driver(DRV(xencons_driver))) != 0) {
677 printk("WARNING: Failed to register Xen virtual "
678 "console driver as '%s%d'\n",
679 DRV(xencons_driver)->name,
680 DRV(xencons_driver)->name_base);
681 put_tty_driver(xencons_driver);
682 xencons_driver = NULL;
686 if (is_initial_xendomain()) {
687 xencons_priv_irq = bind_virq_to_irqhandler(
690 xencons_priv_interrupt,
694 BUG_ON(xencons_priv_irq < 0);
697 printk("Xen virtual console successfully installed as %s%d\n",
698 DRV(xencons_driver)->name, xc_num);
700 /* Check about framebuffer messing up the console */
701 if (!is_initial_xendomain() &&
702 !xenbus_exists(XBT_NIL, "device", "vfb")) {
703 /* FIXME: this is ugly */
704 unregister_console(&kcons_info);
705 kcons_info.flags |= CON_CONSDEV;
706 register_console(&kcons_info);
712 module_init(xencons_init);
714 MODULE_LICENSE("Dual BSD/GPL");