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/module.h>
34 #include <linux/errno.h>
35 #include <linux/signal.h>
36 #include <linux/sched.h>
37 #include <linux/interrupt.h>
38 #include <linux/tty.h>
39 #include <linux/tty_flip.h>
40 #include <linux/serial.h>
41 #include <linux/major.h>
42 #include <linux/ioport.h>
44 #include <linux/slab.h>
45 #include <linux/init.h>
46 #include <linux/console.h>
47 #include <linux/sysrq.h>
49 #include <xen/interface/xen.h>
50 #include <xen/interface/event_channel.h>
55 * 'xencons=off' [XC_OFF]: Console is disabled.
56 * 'xencons=tty' [XC_TTY]: Console attached to '/dev/tty[0-9]+'.
57 * 'xencons=ttyS' [XC_SERIAL]: Console attached to '/dev/ttyS[0-9]+'.
58 * 'xencons=xvc' [XC_XVC]: Console attached to '/dev/xvc0'.
59 * 'xencons=hvc' [XC_HVC]: Console attached to '/dev/hvc0'.
62 * NB. In mode XC_TTY, we create dummy consoles for tty2-63. This suppresses
63 * warnings from standard distro startup scripts.
66 XC_OFF, XC_TTY, XC_SERIAL, XC_XVC, XC_HVC
68 static int xc_num = -1;
70 /* /dev/xvc0 device number allocated by lanana.org. */
71 #define XEN_XVC_MAJOR 204
72 #define XEN_XVC_MINOR 191
74 /* /dev/hvc0 device number */
75 #define XEN_HVC_MAJOR 229
76 #define XEN_HVC_MINOR 0
78 static int __init xencons_setup(char *str)
84 if (!strncmp(str, "ttyS", 4)) {
87 } else if (!strncmp(str, "tty", 3)) {
91 } else if (!strncmp(str, "xvc", 3)) {
94 } else if (!strncmp(str, "hvc", 3)) {
97 } else if (!strncmp(str, "off", 3)) {
102 n = simple_strtol(str, &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,
185 static int __init xen_console_init(void)
187 if (!is_running_on_xen())
190 if (is_initial_xendomain()) {
191 kcons_info.write = kcons_write_dom0;
193 if (!xen_start_info->console.domU.evtchn)
195 kcons_info.write = kcons_write;
200 strcpy(kcons_info.name, "xvc");
206 strcpy(kcons_info.name, "hvc");
209 if (!is_initial_xendomain())
210 add_preferred_console(kcons_info.name, xc_num, NULL);
214 strcpy(kcons_info.name, "ttyS");
220 strcpy(kcons_info.name, "tty");
229 wbuf = kmalloc(wbuf_size, GFP_KERNEL);
231 register_console(&kcons_info);
236 console_initcall(xen_console_init);
238 #ifdef CONFIG_XEN_PRIVILEGED_GUEST
239 /*** Useful function for console debugging -- goes straight to Xen. ***/
240 int xprintk(const char *fmt, ...)
244 static char printk_buf[1024];
246 /* Emit the output into the temporary buffer */
248 printk_len = vsnprintf(printk_buf, sizeof(printk_buf), fmt, args);
251 /* Send the processed output directly to Xen. */
252 kcons_write_dom0(NULL, printk_buf, printk_len);
258 /*** Forcibly flush console data before dying. ***/
259 void xencons_force_flush(void)
263 /* Emergency console is synchronous, so there's nothing to flush. */
264 if (!is_running_on_xen() ||
265 is_initial_xendomain() ||
266 !xen_start_info->console.domU.evtchn)
269 /* Spin until console data is flushed through to the daemon. */
272 if ((sz = wp - wc) == 0)
274 sent = xencons_ring_send(&wbuf[WBUF_MASK(wc)], sz);
281 #ifdef CONFIG_XEN_PRIVILEGED_GUEST
282 #include <linux/screen_info.h>
284 void __init dom0_init_screen_info(const struct dom0_vga_console_info *info, size_t size)
286 /* This is drawn from a dump from vgacon:startup in
288 screen_info.orig_video_mode = 3;
289 screen_info.orig_video_isVGA = 1;
290 screen_info.orig_video_lines = 25;
291 screen_info.orig_video_cols = 80;
292 screen_info.orig_video_ega_bx = 3;
293 screen_info.orig_video_points = 16;
294 screen_info.orig_y = screen_info.orig_video_lines - 1;
296 switch (info->video_type) {
297 case XEN_VGATYPE_TEXT_MODE_3:
298 if (size < offsetof(struct dom0_vga_console_info, u.text_mode_3)
299 + sizeof(info->u.text_mode_3))
301 screen_info.orig_video_lines = info->u.text_mode_3.rows;
302 screen_info.orig_video_cols = info->u.text_mode_3.columns;
303 screen_info.orig_x = info->u.text_mode_3.cursor_x;
304 screen_info.orig_y = info->u.text_mode_3.cursor_y;
305 screen_info.orig_video_points =
306 info->u.text_mode_3.font_height;
309 case XEN_VGATYPE_VESA_LFB:
310 case XEN_VGATYPE_EFI_LFB:
311 if (size < offsetof(struct dom0_vga_console_info,
312 u.vesa_lfb.gbl_caps))
314 screen_info.orig_video_isVGA = VIDEO_TYPE_VLFB;
315 screen_info.lfb_width = info->u.vesa_lfb.width;
316 screen_info.lfb_height = info->u.vesa_lfb.height;
317 screen_info.lfb_depth = info->u.vesa_lfb.bits_per_pixel;
318 screen_info.lfb_base = info->u.vesa_lfb.lfb_base;
319 screen_info.lfb_size = info->u.vesa_lfb.lfb_size;
320 screen_info.lfb_linelength = info->u.vesa_lfb.bytes_per_line;
321 screen_info.red_size = info->u.vesa_lfb.red_size;
322 screen_info.red_pos = info->u.vesa_lfb.red_pos;
323 screen_info.green_size = info->u.vesa_lfb.green_size;
324 screen_info.green_pos = info->u.vesa_lfb.green_pos;
325 screen_info.blue_size = info->u.vesa_lfb.blue_size;
326 screen_info.blue_pos = info->u.vesa_lfb.blue_pos;
327 screen_info.rsvd_size = info->u.vesa_lfb.rsvd_size;
328 screen_info.rsvd_pos = info->u.vesa_lfb.rsvd_pos;
329 if (info->video_type == XEN_VGATYPE_EFI_LFB) {
330 screen_info.orig_video_isVGA = VIDEO_TYPE_EFI;
333 if (size >= offsetof(struct dom0_vga_console_info,
335 + sizeof(info->u.vesa_lfb.gbl_caps))
336 screen_info.capabilities = info->u.vesa_lfb.gbl_caps;
337 if (size >= offsetof(struct dom0_vga_console_info,
338 u.vesa_lfb.mode_attrs)
339 + sizeof(info->u.vesa_lfb.mode_attrs))
340 screen_info.vesa_attributes = info->u.vesa_lfb.mode_attrs;
347 /******************** User-space console driver (/dev/console) ************/
350 #define DUMMY_TTY(_tty) ((xc_mode == XC_TTY) && \
351 ((_tty)->index != (xc_num - 1)))
353 static struct ktermios *xencons_termios[MAX_NR_CONSOLES];
354 static struct tty_struct *xencons_tty;
355 static int xencons_priv_irq;
358 void xencons_rx(char *buf, unsigned len)
363 spin_lock_irqsave(&xencons_lock, flags);
364 if (xencons_tty == NULL)
367 for (i = 0; i < len; i++) {
368 #ifdef CONFIG_MAGIC_SYSRQ
369 static unsigned long sysrq_requested;
371 if (buf[i] == '\x0f') { /* ^O */
372 if (!sysrq_requested) {
373 sysrq_requested = jiffies;
374 continue; /* don't print sysrq key */
377 } else if (sysrq_requested) {
378 unsigned long sysrq_timeout = sysrq_requested + HZ*2;
381 if (time_before(jiffies, sysrq_timeout)) {
382 spin_unlock_irqrestore(&xencons_lock, flags);
383 handle_sysrq(buf[i]);
384 spin_lock_irqsave(&xencons_lock, flags);
389 tty_insert_flip_char(xencons_tty, buf[i], 0);
391 tty_flip_buffer_push(xencons_tty);
394 spin_unlock_irqrestore(&xencons_lock, flags);
397 static void __xencons_tx_flush(void)
399 int sent, sz, work_done = 0;
402 if (is_initial_xendomain())
403 kcons_write_dom0(NULL, &x_char, 1);
406 if (xencons_ring_send(&x_char, 1) == 1)
414 if (sz > (wbuf_size - WBUF_MASK(wc)))
415 sz = wbuf_size - WBUF_MASK(wc);
416 if (is_initial_xendomain()) {
417 kcons_write_dom0(NULL, &wbuf[WBUF_MASK(wc)], sz);
420 sent = xencons_ring_send(&wbuf[WBUF_MASK(wc)], sz);
428 if (work_done && (xencons_tty != NULL)) {
429 wake_up_interruptible(&xencons_tty->write_wait);
430 tty_wakeup(xencons_tty);
434 void xencons_tx(void)
438 spin_lock_irqsave(&xencons_lock, flags);
439 __xencons_tx_flush();
440 spin_unlock_irqrestore(&xencons_lock, flags);
443 /* Privileged receive callback and transmit kicker. */
444 static irqreturn_t xencons_priv_interrupt(int irq, void *dev_id)
446 static char rbuf[16];
449 while ((l = HYPERVISOR_console_io(CONSOLEIO_read, 16, rbuf)) > 0)
457 static int xencons_write_room(struct tty_struct *tty)
459 return wbuf_size - (wp - wc);
462 static int xencons_chars_in_buffer(struct tty_struct *tty)
467 static void xencons_send_xchar(struct tty_struct *tty, char ch)
474 spin_lock_irqsave(&xencons_lock, flags);
476 __xencons_tx_flush();
477 spin_unlock_irqrestore(&xencons_lock, flags);
480 static void xencons_throttle(struct tty_struct *tty)
486 xencons_send_xchar(tty, STOP_CHAR(tty));
489 static void xencons_unthrottle(struct tty_struct *tty)
498 xencons_send_xchar(tty, START_CHAR(tty));
502 static void xencons_flush_buffer(struct tty_struct *tty)
509 spin_lock_irqsave(&xencons_lock, flags);
511 spin_unlock_irqrestore(&xencons_lock, flags);
514 static inline int __xencons_put_char(int ch)
517 if ((wp - wc) == wbuf_size)
519 wbuf[WBUF_MASK(wp++)] = _ch;
523 static int xencons_write(
524 struct tty_struct *tty,
525 const unsigned char *buf,
534 spin_lock_irqsave(&xencons_lock, flags);
536 for (i = 0; i < count; i++)
537 if (!__xencons_put_char(buf[i]))
541 __xencons_tx_flush();
543 spin_unlock_irqrestore(&xencons_lock, flags);
548 static int xencons_put_char(struct tty_struct *tty, u_char ch)
556 spin_lock_irqsave(&xencons_lock, flags);
557 ret = __xencons_put_char(ch);
558 spin_unlock_irqrestore(&xencons_lock, flags);
562 static void xencons_flush_chars(struct tty_struct *tty)
569 spin_lock_irqsave(&xencons_lock, flags);
570 __xencons_tx_flush();
571 spin_unlock_irqrestore(&xencons_lock, flags);
574 static void xencons_wait_until_sent(struct tty_struct *tty, int timeout)
576 unsigned long orig_jiffies = jiffies;
581 while (tty_chars_in_buffer(tty)) {
582 set_current_state(TASK_INTERRUPTIBLE);
584 if (signal_pending(current))
586 if (timeout && time_after(jiffies, orig_jiffies + timeout))
590 set_current_state(TASK_RUNNING);
593 static int xencons_open(struct tty_struct *tty, struct file *filp)
600 spin_lock_irqsave(&xencons_lock, flags);
601 tty->driver_data = NULL;
602 if (xencons_tty == NULL)
604 __xencons_tx_flush();
605 spin_unlock_irqrestore(&xencons_lock, flags);
610 static void xencons_close(struct tty_struct *tty, struct file *filp)
618 * Must follow lock nesting; callers are prepared for this
619 * (__tty_hangup) or don't care as they drop the lock right after our
620 * return (tty_release) in order to then acquire both in proper order.
623 mutex_lock(&tty_mutex);
626 if (tty->count != 1) {
627 mutex_unlock(&tty_mutex);
631 /* Prevent other threads from re-opening this tty. */
632 set_bit(TTY_CLOSING, &tty->flags);
633 mutex_unlock(&tty_mutex);
636 tty_wait_until_sent(tty, 0);
637 tty_driver_flush_buffer(tty);
638 if (tty->ldisc->ops->flush_buffer)
639 tty->ldisc->ops->flush_buffer(tty);
641 spin_lock_irqsave(&xencons_lock, flags);
643 spin_unlock_irqrestore(&xencons_lock, flags);
646 static const struct tty_operations xencons_ops = {
647 .open = xencons_open,
648 .close = xencons_close,
649 .write = xencons_write,
650 .write_room = xencons_write_room,
651 .put_char = xencons_put_char,
652 .flush_chars = xencons_flush_chars,
653 .chars_in_buffer = xencons_chars_in_buffer,
654 .send_xchar = xencons_send_xchar,
655 .flush_buffer = xencons_flush_buffer,
656 .throttle = xencons_throttle,
657 .unthrottle = xencons_unthrottle,
658 .wait_until_sent = xencons_wait_until_sent,
661 static int __init xencons_init(void)
665 if (!is_running_on_xen())
668 if (xc_mode == XC_OFF)
671 if (!is_initial_xendomain()) {
672 rc = xencons_ring_init();
677 xencons_driver = alloc_tty_driver((xc_mode == XC_TTY) ?
678 MAX_NR_CONSOLES : 1);
679 if (xencons_driver == NULL)
682 DRV(xencons_driver)->name = "xencons";
683 DRV(xencons_driver)->major = TTY_MAJOR;
684 DRV(xencons_driver)->type = TTY_DRIVER_TYPE_SERIAL;
685 DRV(xencons_driver)->subtype = SERIAL_TYPE_NORMAL;
686 DRV(xencons_driver)->init_termios = tty_std_termios;
687 DRV(xencons_driver)->flags =
688 TTY_DRIVER_REAL_RAW |
689 TTY_DRIVER_RESET_TERMIOS;
690 DRV(xencons_driver)->termios = xencons_termios;
694 DRV(xencons_driver)->name = "xvc";
695 DRV(xencons_driver)->major = XEN_XVC_MAJOR;
696 DRV(xencons_driver)->minor_start = XEN_XVC_MINOR;
697 DRV(xencons_driver)->name_base = xc_num;
700 DRV(xencons_driver)->name = "hvc";
701 DRV(xencons_driver)->major = XEN_HVC_MAJOR;
702 DRV(xencons_driver)->minor_start = XEN_HVC_MINOR;
703 DRV(xencons_driver)->name_base = xc_num;
706 DRV(xencons_driver)->name = "ttyS";
707 DRV(xencons_driver)->minor_start = 64 + xc_num;
708 DRV(xencons_driver)->name_base = xc_num;
711 DRV(xencons_driver)->name = "tty";
712 DRV(xencons_driver)->minor_start = 1;
713 DRV(xencons_driver)->name_base = 1;
717 tty_set_operations(xencons_driver, &xencons_ops);
719 if ((rc = tty_register_driver(DRV(xencons_driver))) != 0) {
720 pr_warning("WARNING: Failed to register Xen virtual "
721 "console driver as '%s%d'\n",
722 DRV(xencons_driver)->name,
723 DRV(xencons_driver)->name_base);
724 put_tty_driver(xencons_driver);
725 xencons_driver = NULL;
729 if (is_initial_xendomain()) {
730 xencons_priv_irq = bind_virq_to_irqhandler(
733 xencons_priv_interrupt,
737 BUG_ON(xencons_priv_irq < 0);
740 pr_info("Xen virtual console successfully installed as %s%d\n",
741 DRV(xencons_driver)->name, xc_num);
746 module_init(xencons_init);
748 MODULE_LICENSE("Dual BSD/GPL");