Merge to Fedora kernel-2.6.17-1.2187_FC5 patched with stable patch-2.6.17.13-vs2...
[linux-2.6.git] / drivers / xen / console / console.c
1 /******************************************************************************
2  * console.c
3  * 
4  * Virtual console driver.
5  * 
6  * Copyright (c) 2002-2004, K A Fraser.
7  * 
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:
13  * 
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:
20  * 
21  * The above copyright notice and this permission notice shall be included in
22  * all copies or substantial portions of the Software.
23  * 
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
30  * IN THE SOFTWARE.
31  */
32
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>
46 #include <linux/mm.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 <asm/io.h>
53 #include <asm/irq.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>
60
61 /*
62  * Modes:
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.
67  * 
68  * NB. In mode XC_TTY, we create dummy consoles for tty2-63. This suppresses
69  * warnings from standard distro startup scripts.
70  */
71 static enum { XC_OFF, XC_DEFAULT, XC_TTY, XC_SERIAL } xc_mode = XC_DEFAULT;
72 static int xc_num = -1;
73
74 #ifdef CONFIG_MAGIC_SYSRQ
75 static unsigned long sysrq_requested;
76 extern int sysrq_enabled;
77 #endif
78
79 static int __init xencons_setup(char *str)
80 {
81         char *q;
82         int n;
83
84         if (!strncmp(str, "ttyS", 4))
85                 xc_mode = XC_SERIAL;
86         else if (!strncmp(str, "tty", 3))
87                 xc_mode = XC_TTY;
88         else if (!strncmp(str, "off", 3))
89                 xc_mode = XC_OFF;
90
91         switch (xc_mode) {
92         case XC_SERIAL:
93                 n = simple_strtol(str+4, &q, 10);
94                 if (q > (str + 4))
95                         xc_num = n;
96                 break;
97         case XC_TTY:
98                 n = simple_strtol(str+3, &q, 10);
99                 if (q > (str + 3))
100                         xc_num = n;
101                 break;
102         default:
103                 break;
104         }
105
106         return 1;
107 }
108 __setup("xencons=", xencons_setup);
109
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))
113 static char *wbuf;
114 static unsigned int wc, wp; /* write_cons, write_prod */
115
116 static int __init xencons_bufsz_setup(char *str)
117 {
118         unsigned int goal;
119         goal = simple_strtoul(str, NULL, 0);
120         if (goal) {
121                 goal = roundup_pow_of_two(goal);
122                 if (wbuf_size < goal)
123                         wbuf_size = goal;
124         }
125         return 1;
126 }
127 __setup("xencons_bufsz=", xencons_bufsz_setup);
128
129 /* This lock protects accesses to the common transmit buffer. */
130 static DEFINE_SPINLOCK(xencons_lock);
131
132 /* Common transmit-kick routine. */
133 static void __xencons_tx_flush(void);
134
135 static struct tty_driver *xencons_driver;
136
137 /******************** Kernel console driver ********************************/
138
139 static void kcons_write(struct console *c, const char *s, unsigned int count)
140 {
141         int           i = 0;
142         unsigned long flags;
143
144         spin_lock_irqsave(&xencons_lock, flags);
145
146         while (i < count) {
147                 for (; i < count; i++) {
148                         if ((wp - wc) >= (wbuf_size - 1))
149                                 break;
150                         if ((wbuf[WBUF_MASK(wp++)] = s[i]) == '\n')
151                                 wbuf[WBUF_MASK(wp++)] = '\r';
152                 }
153
154                 __xencons_tx_flush();
155         }
156
157         spin_unlock_irqrestore(&xencons_lock, flags);
158 }
159
160 static void kcons_write_dom0(struct console *c, const char *s, unsigned int count)
161 {
162
163         while (count > 0) {
164                 int rc;
165                 rc = HYPERVISOR_console_io( CONSOLEIO_write, count, (char *)s);
166                 if (rc <= 0)
167                         break;
168                 count -= rc;
169                 s += rc;
170         }
171 }
172
173 static struct tty_driver *kcons_device(struct console *c, int *index)
174 {
175         *index = 0;
176         return xencons_driver;
177 }
178
179 static struct console kcons_info = {
180         .device = kcons_device,
181         .flags  = CON_PRINTBUFFER | CON_ENABLED,
182         .index  = -1,
183 };
184
185 #define __RETCODE 0
186 static int __init xen_console_init(void)
187 {
188         if (!is_running_on_xen())
189                 return __RETCODE;
190
191         if (xen_start_info->flags & SIF_INITDOMAIN) {
192                 if (xc_mode == XC_DEFAULT)
193                         xc_mode = XC_SERIAL;
194                 kcons_info.write = kcons_write_dom0;
195         } else {
196                 if (xc_mode == XC_DEFAULT)
197                         xc_mode = XC_TTY;
198                 kcons_info.write = kcons_write;
199         }
200
201         switch (xc_mode) {
202         case XC_SERIAL:
203                 strcpy(kcons_info.name, "ttyS");
204                 if (xc_num == -1)
205                         xc_num = 0;
206                 break;
207
208         case XC_TTY:
209                 strcpy(kcons_info.name, "tty");
210                 if (xc_num == -1)
211                         xc_num = 1;
212                 break;
213
214         default:
215                 return __RETCODE;
216         }
217
218         wbuf = alloc_bootmem(wbuf_size);
219
220         register_console(&kcons_info);
221
222         return __RETCODE;
223 }
224 console_initcall(xen_console_init);
225
226 /*** Useful function for console debugging -- goes straight to Xen. ***/
227 asmlinkage int xprintk(const char *fmt, ...)
228 {
229         va_list args;
230         int printk_len;
231         static char printk_buf[1024];
232
233         /* Emit the output into the temporary buffer */
234         va_start(args, fmt);
235         printk_len = vsnprintf(printk_buf, sizeof(printk_buf), fmt, args);
236         va_end(args);
237
238         /* Send the processed output directly to Xen. */
239         kcons_write_dom0(NULL, printk_buf, printk_len);
240
241         return 0;
242 }
243
244 /*** Forcibly flush console data before dying. ***/
245 void xencons_force_flush(void)
246 {
247         int sz;
248
249         /* Emergency console is synchronous, so there's nothing to flush. */
250         if (xen_start_info->flags & SIF_INITDOMAIN)
251                 return;
252
253         /* Spin until console data is flushed through to the daemon. */
254         while (wc != wp) {
255                 int sent = 0;
256                 if ((sz = wp - wc) == 0)
257                         continue;
258                 sent = xencons_ring_send(&wbuf[WBUF_MASK(wc)], sz);
259                 if (sent > 0)
260                         wc += sent;
261         }
262 }
263
264
265 /******************** User-space console driver (/dev/console) ************/
266
267 #define DRV(_d)         (_d)
268 #define DUMMY_TTY(_tty) ((xc_mode != XC_SERIAL) &&              \
269                          ((_tty)->index != (xc_num - 1)))
270
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;
275 static char x_char;
276
277 void xencons_rx(char *buf, unsigned len, struct pt_regs *regs)
278 {
279         int           i;
280         unsigned long flags;
281
282         spin_lock_irqsave(&xencons_lock, flags);
283         if (xencons_tty == NULL)
284                 goto out;
285
286         for (i = 0; i < len; i++) {
287 #ifdef CONFIG_MAGIC_SYSRQ
288                 if (sysrq_enabled) {
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;
295                                 sysrq_requested = 0;
296                                 if (time_before(jiffies, sysrq_timeout)) {
297                                         spin_unlock_irqrestore(
298                                                 &xencons_lock, flags);
299                                         handle_sysrq(
300                                                 buf[i], regs, xencons_tty);
301                                         spin_lock_irqsave(
302                                                 &xencons_lock, flags);
303                                         continue;
304                                 }
305                         }
306                 }
307 #endif
308                 tty_insert_flip_char(xencons_tty, buf[i], 0);
309         }
310         tty_flip_buffer_push(xencons_tty);
311
312  out:
313         spin_unlock_irqrestore(&xencons_lock, flags);
314 }
315
316 static void __xencons_tx_flush(void)
317 {
318         int sent, sz, work_done = 0;
319
320         if (x_char) {
321                 if (xen_start_info->flags & SIF_INITDOMAIN)
322                         kcons_write_dom0(NULL, &x_char, 1);
323                 else
324                         while (x_char)
325                                 if (xencons_ring_send(&x_char, 1) == 1)
326                                         break;
327                 x_char = 0;
328                 work_done = 1;
329         }
330
331         while (wc != wp) {
332                 sz = wp - wc;
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);
337                         wc += sz;
338                 } else {
339                         sent = xencons_ring_send(&wbuf[WBUF_MASK(wc)], sz);
340                         if (sent == 0)
341                                 break;
342                         wc += sent;
343                 }
344                 work_done = 1;
345         }
346
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);
352         }
353 }
354
355 void xencons_tx(void)
356 {
357         unsigned long flags;
358
359         spin_lock_irqsave(&xencons_lock, flags);
360         __xencons_tx_flush();
361         spin_unlock_irqrestore(&xencons_lock, flags);
362 }
363
364 /* Privileged receive callback and transmit kicker. */
365 static irqreturn_t xencons_priv_interrupt(int irq, void *dev_id,
366                                           struct pt_regs *regs)
367 {
368         static char rbuf[16];
369         int         l;
370
371         while ((l = HYPERVISOR_console_io(CONSOLEIO_read, 16, rbuf)) > 0)
372                 xencons_rx(rbuf, l, regs);
373
374         xencons_tx();
375
376         return IRQ_HANDLED;
377 }
378
379 static int xencons_write_room(struct tty_struct *tty)
380 {
381         return wbuf_size - (wp - wc);
382 }
383
384 static int xencons_chars_in_buffer(struct tty_struct *tty)
385 {
386         return wp - wc;
387 }
388
389 static void xencons_send_xchar(struct tty_struct *tty, char ch)
390 {
391         unsigned long flags;
392
393         if (DUMMY_TTY(tty))
394                 return;
395
396         spin_lock_irqsave(&xencons_lock, flags);
397         x_char = ch;
398         __xencons_tx_flush();
399         spin_unlock_irqrestore(&xencons_lock, flags);
400 }
401
402 static void xencons_throttle(struct tty_struct *tty)
403 {
404         if (DUMMY_TTY(tty))
405                 return;
406
407         if (I_IXOFF(tty))
408                 xencons_send_xchar(tty, STOP_CHAR(tty));
409 }
410
411 static void xencons_unthrottle(struct tty_struct *tty)
412 {
413         if (DUMMY_TTY(tty))
414                 return;
415
416         if (I_IXOFF(tty)) {
417                 if (x_char != 0)
418                         x_char = 0;
419                 else
420                         xencons_send_xchar(tty, START_CHAR(tty));
421         }
422 }
423
424 static void xencons_flush_buffer(struct tty_struct *tty)
425 {
426         unsigned long flags;
427
428         if (DUMMY_TTY(tty))
429                 return;
430
431         spin_lock_irqsave(&xencons_lock, flags);
432         wc = wp = 0;
433         spin_unlock_irqrestore(&xencons_lock, flags);
434 }
435
436 static inline int __xencons_put_char(int ch)
437 {
438         char _ch = (char)ch;
439         if ((wp - wc) == wbuf_size)
440                 return 0;
441         wbuf[WBUF_MASK(wp++)] = _ch;
442         return 1;
443 }
444
445 static int xencons_write(
446         struct tty_struct *tty,
447         const unsigned char *buf,
448         int count)
449 {
450         int i;
451         unsigned long flags;
452
453         if (DUMMY_TTY(tty))
454                 return count;
455
456         spin_lock_irqsave(&xencons_lock, flags);
457
458         for (i = 0; i < count; i++)
459                 if (!__xencons_put_char(buf[i]))
460                         break;
461
462         if (i != 0)
463                 __xencons_tx_flush();
464
465         spin_unlock_irqrestore(&xencons_lock, flags);
466
467         return i;
468 }
469
470 static void xencons_put_char(struct tty_struct *tty, u_char ch)
471 {
472         unsigned long flags;
473
474         if (DUMMY_TTY(tty))
475                 return;
476
477         spin_lock_irqsave(&xencons_lock, flags);
478         (void)__xencons_put_char(ch);
479         spin_unlock_irqrestore(&xencons_lock, flags);
480 }
481
482 static void xencons_flush_chars(struct tty_struct *tty)
483 {
484         unsigned long flags;
485
486         if (DUMMY_TTY(tty))
487                 return;
488
489         spin_lock_irqsave(&xencons_lock, flags);
490         __xencons_tx_flush();
491         spin_unlock_irqrestore(&xencons_lock, flags);
492 }
493
494 static void xencons_wait_until_sent(struct tty_struct *tty, int timeout)
495 {
496         unsigned long orig_jiffies = jiffies;
497
498         if (DUMMY_TTY(tty))
499                 return;
500
501         while (DRV(tty->driver)->chars_in_buffer(tty)) {
502                 set_current_state(TASK_INTERRUPTIBLE);
503                 schedule_timeout(1);
504                 if (signal_pending(current))
505                         break;
506                 if (timeout && time_after(jiffies, orig_jiffies + timeout))
507                         break;
508         }
509
510         set_current_state(TASK_RUNNING);
511 }
512
513 static int xencons_open(struct tty_struct *tty, struct file *filp)
514 {
515         unsigned long flags;
516
517         if (DUMMY_TTY(tty))
518                 return 0;
519
520         spin_lock_irqsave(&xencons_lock, flags);
521         tty->driver_data = NULL;
522         if (xencons_tty == NULL)
523                 xencons_tty = tty;
524         __xencons_tx_flush();
525         spin_unlock_irqrestore(&xencons_lock, flags);
526
527         return 0;
528 }
529
530 static void xencons_close(struct tty_struct *tty, struct file *filp)
531 {
532         unsigned long flags;
533
534         if (DUMMY_TTY(tty))
535                 return;
536
537         mutex_lock(&tty_mutex);
538
539         if (tty->count != 1) {
540                 mutex_unlock(&tty_mutex);
541                 return;
542         }
543
544         /* Prevent other threads from re-opening this tty. */
545         set_bit(TTY_CLOSING, &tty->flags);
546         mutex_unlock(&tty_mutex);
547
548         tty->closing = 1;
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);
554         tty->closing = 0;
555         spin_lock_irqsave(&xencons_lock, flags);
556         xencons_tty = NULL;
557         spin_unlock_irqrestore(&xencons_lock, flags);
558 }
559
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,
573 };
574
575 static int __init xencons_init(void)
576 {
577         int rc;
578
579         if (!is_running_on_xen())
580                 return -ENODEV;
581
582         if (xc_mode == XC_OFF)
583                 return 0;
584
585         xencons_ring_init();
586
587         xencons_driver = alloc_tty_driver((xc_mode == XC_SERIAL) ?
588                                           1 : MAX_NR_CONSOLES);
589         if (xencons_driver == NULL)
590                 return -ENOMEM;
591
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;
602
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;
607         } else {
608                 DRV(xencons_driver)->name        = "tty";
609                 DRV(xencons_driver)->minor_start = 1;
610                 DRV(xencons_driver)->name_base   = 1;
611         }
612
613         tty_set_operations(xencons_driver, &xencons_ops);
614
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;
622                 return rc;
623         }
624
625         if (xen_start_info->flags & SIF_INITDOMAIN) {
626                 xencons_priv_irq = bind_virq_to_irqhandler(
627                         VIRQ_CONSOLE,
628                         0,
629                         xencons_priv_interrupt,
630                         0,
631                         "console",
632                         NULL);
633                 BUG_ON(xencons_priv_irq < 0);
634         }
635
636         printk("Xen virtual console successfully installed as %s%d\n",
637                DRV(xencons_driver)->name, xc_num);
638
639         return 0;
640 }
641
642 module_init(xencons_init);
643
644 MODULE_LICENSE("Dual BSD/GPL");