ia64/xen-unstable

view xen/drivers/char/console.c @ 8546:50d46a775ad0

Send guest output on all xen-attached console devices
(i.e., on vga console as well as serial console, if dom0
didn't take over the vga console).

Signed-off-by: Jan Beulich <JBeulich@novell.com>
author kaf24@firebug.cl.cam.ac.uk
date Tue Jan 10 16:00:37 2006 +0100 (2006-01-10)
parents 06d84bf87159
children 1346a69694be
line source
1 /******************************************************************************
2 * console.c
3 *
4 * Emergency console I/O for Xen and the domain-0 guest OS.
5 *
6 * Copyright (c) 2002-2004, K A Fraser.
7 */
9 #include <stdarg.h>
10 #include <xen/config.h>
11 #include <xen/compile.h>
12 #include <xen/init.h>
13 #include <xen/lib.h>
14 #include <xen/errno.h>
15 #include <xen/event.h>
16 #include <xen/spinlock.h>
17 #include <xen/console.h>
18 #include <xen/serial.h>
19 #include <xen/softirq.h>
20 #include <xen/keyhandler.h>
21 #include <xen/mm.h>
22 #include <xen/delay.h>
23 #include <asm/current.h>
24 #include <asm/uaccess.h>
25 #include <asm/debugger.h>
26 #include <asm/io.h>
28 /* console: comma-separated list of console outputs. */
29 static char opt_console[30] = OPT_CONSOLE_STR;
30 string_param("console", opt_console);
32 /* conswitch: a character pair controlling console switching. */
33 /* Char 1: CTRL+<char1> is used to switch console input between Xen and DOM0 */
34 /* Char 2: If this character is 'x', then do not auto-switch to DOM0 when it */
35 /* boots. Any other value, or omitting the char, enables auto-switch */
36 static unsigned char opt_conswitch[5] = "a";
37 string_param("conswitch", opt_conswitch);
39 /* sync_console: force synchronous console output (useful for debugging). */
40 static int opt_sync_console;
41 boolean_param("sync_console", opt_sync_console);
43 static int xpos, ypos;
44 static unsigned char *video;
46 #define CONRING_SIZE 16384
47 #define CONRING_IDX_MASK(i) ((i)&(CONRING_SIZE-1))
48 static char conring[CONRING_SIZE];
49 static unsigned int conringc, conringp;
51 static char printk_prefix[16] = "";
53 static int sercon_handle = -1;
54 static int vgacon_enabled = 0;
56 spinlock_t console_lock = SPIN_LOCK_UNLOCKED;
58 /*
59 * *******************************************************
60 * *************** OUTPUT TO VGA CONSOLE *****************
61 * *******************************************************
62 */
64 /* VGA text (mode 3) definitions. */
65 #define COLUMNS 80
66 #define LINES 25
67 #define ATTRIBUTE 7
69 /* Clear the screen and initialize VIDEO, XPOS and YPOS. */
70 static void cls(void)
71 {
72 memset(video, 0, COLUMNS * LINES * 2);
73 xpos = ypos = 0;
74 outw(10+(1<<(5+8)), 0x3d4); /* cursor off */
75 }
77 static int detect_video(void *video_base)
78 {
79 volatile u16 *p = (volatile u16 *)video_base;
80 u16 saved1 = p[0], saved2 = p[1];
81 int video_found = 1;
83 p[0] = 0xAA55;
84 p[1] = 0x55AA;
85 if ( (p[0] != 0xAA55) || (p[1] != 0x55AA) )
86 video_found = 0;
88 p[0] = 0x55AA;
89 p[1] = 0xAA55;
90 if ( (p[0] != 0x55AA) || (p[1] != 0xAA55) )
91 video_found = 0;
93 p[0] = saved1;
94 p[1] = saved2;
96 return video_found;
97 }
99 static int detect_vga(void)
100 {
101 /*
102 * Look at a number of well-known locations. Even if video is not at
103 * 0xB8000 right now, it will appear there when we set up text mode 3.
104 *
105 * We assume if there is any sign of a video adaptor then it is at least
106 * VGA-compatible (surely noone runs CGA, EGA, .... these days?).
107 *
108 * These checks are basically to detect headless server boxes.
109 */
110 return (detect_video(__va(0xA0000)) ||
111 detect_video(__va(0xB0000)) ||
112 detect_video(__va(0xB8000)));
113 }
115 /* This is actually code from vgaHWRestore in an old version of XFree86 :-) */
116 static void init_vga(void)
117 {
118 /* The following VGA state was saved from a chip in text mode 3. */
119 static unsigned char regs[] = {
120 /* Sequencer registers */
121 0x03, 0x00, 0x03, 0x00, 0x02,
122 /* CRTC registers */
123 0x5f, 0x4f, 0x50, 0x82, 0x55, 0x81, 0xbf, 0x1f, 0x00, 0x4f, 0x20,
124 0x0e, 0x00, 0x00, 0x01, 0xe0, 0x9c, 0x8e, 0x8f, 0x28, 0x1f, 0x96,
125 0xb9, 0xa3, 0xff,
126 /* Graphic registers */
127 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x0e, 0x00, 0xff,
128 /* Attribute registers */
129 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x14, 0x07, 0x38, 0x39, 0x3a,
130 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x0c, 0x00, 0x0f, 0x08, 0x00
131 };
133 int i, j = 0;
134 volatile unsigned char tmp;
136 if ( !vgacon_enabled )
137 return;
139 if ( !detect_vga() )
140 {
141 printk("No VGA adaptor detected!\n");
142 vgacon_enabled = 0;
143 return;
144 }
146 video = __va(0xB8000);
148 tmp = inb(0x3da);
149 outb(0x00, 0x3c0);
151 for ( i = 0; i < 5; i++ )
152 outw((regs[j++] << 8) | i, 0x3c4);
154 /* Ensure CRTC registers 0-7 are unlocked by clearing bit 7 of CRTC[17]. */
155 outw(((regs[5+17] & 0x7F) << 8) | 17, 0x3d4);
157 for ( i = 0; i < 25; i++ )
158 outw((regs[j++] << 8) | i, 0x3d4);
160 for ( i = 0; i < 9; i++ )
161 outw((regs[j++] << 8) | i, 0x3ce);
163 for ( i = 0; i < 21; i++ )
164 {
165 tmp = inb(0x3da);
166 outb(i, 0x3c0);
167 outb(regs[j++], 0x3c0);
168 }
170 tmp = inb(0x3da);
171 outb(0x20, 0x3c0);
173 cls();
174 }
176 static void put_newline(void)
177 {
178 xpos = 0;
179 ypos++;
181 if (ypos >= LINES)
182 {
183 static char zeroarr[2*COLUMNS] = { 0 };
184 ypos = LINES-1;
185 memcpy((char*)video,
186 (char*)video + 2*COLUMNS, (LINES-1)*2*COLUMNS);
187 memcpy((char*)video + (LINES-1)*2*COLUMNS,
188 zeroarr, 2*COLUMNS);
189 }
190 }
192 static void putchar_console(int c)
193 {
194 if ( !vgacon_enabled )
195 return;
197 if ( c == '\n' )
198 {
199 put_newline();
200 }
201 else
202 {
203 video[(xpos + ypos * COLUMNS) * 2] = c & 0xFF;
204 video[(xpos + ypos * COLUMNS) * 2 + 1] = ATTRIBUTE;
205 if ( ++xpos >= COLUMNS )
206 put_newline();
207 }
208 }
211 /*
212 * ********************************************************
213 * *************** ACCESS TO CONSOLE RING *****************
214 * ********************************************************
215 */
217 static void putchar_console_ring(int c)
218 {
219 conring[CONRING_IDX_MASK(conringp++)] = c;
220 if ( (conringp - conringc) > CONRING_SIZE )
221 conringc = conringp - CONRING_SIZE;
222 }
224 long read_console_ring(char **pstr, u32 *pcount, int clear)
225 {
226 char *str = *pstr;
227 u32 count = *pcount;
228 unsigned int p, q;
229 unsigned long flags;
231 /* Start of buffer may get overwritten during copy. So copy backwards. */
232 for ( p = conringp, q = count; (p > conringc) && (q > 0); p--, q-- )
233 if ( put_user(conring[CONRING_IDX_MASK(p-1)], (char *)str+q-1) )
234 return -EFAULT;
236 if ( clear )
237 {
238 spin_lock_irqsave(&console_lock, flags);
239 conringc = conringp;
240 spin_unlock_irqrestore(&console_lock, flags);
241 }
243 *pstr = str + q;
244 *pcount = count - q;
245 return 0;
246 }
249 /*
250 * *******************************************************
251 * *************** ACCESS TO SERIAL LINE *****************
252 * *******************************************************
253 */
255 /* Characters received over the serial line are buffered for domain 0. */
256 #define SERIAL_RX_SIZE 128
257 #define SERIAL_RX_MASK(_i) ((_i)&(SERIAL_RX_SIZE-1))
258 static char serial_rx_ring[SERIAL_RX_SIZE];
259 static unsigned int serial_rx_cons, serial_rx_prod;
261 /* CTRL-<switch_char> switches input direction between Xen and DOM0. */
262 #define SWITCH_CODE (opt_conswitch[0]-'a'+1)
263 static int xen_rx = 1; /* FALSE => serial input passed to domain 0. */
265 static void switch_serial_input(void)
266 {
267 static char *input_str[2] = { "DOM0", "Xen" };
268 xen_rx = !xen_rx;
269 if ( SWITCH_CODE != 0 )
270 {
271 printk("*** Serial input -> %s "
272 "(type 'CTRL-%c' three times to switch input to %s).\n",
273 input_str[xen_rx], opt_conswitch[0], input_str[!xen_rx]);
274 }
275 }
277 static void __serial_rx(char c, struct cpu_user_regs *regs)
278 {
279 if ( xen_rx )
280 return handle_keypress(c, regs);
282 /* Deliver input to guest buffer, unless it is already full. */
283 if ( (serial_rx_prod-serial_rx_cons) != SERIAL_RX_SIZE )
284 serial_rx_ring[SERIAL_RX_MASK(serial_rx_prod++)] = c;
285 /* Always notify the guest: prevents receive path from getting stuck. */
286 send_guest_virq(dom0->vcpu[0], VIRQ_CONSOLE);
287 }
289 static void serial_rx(char c, struct cpu_user_regs *regs)
290 {
291 static int switch_code_count = 0;
293 if ( (SWITCH_CODE != 0) && (c == SWITCH_CODE) )
294 {
295 /* We eat CTRL-<switch_char> in groups of 3 to switch console input. */
296 if ( ++switch_code_count == 3 )
297 {
298 switch_serial_input();
299 switch_code_count = 0;
300 return;
301 }
302 }
303 else
304 {
305 switch_code_count = 0;
306 }
308 /* Finally process the just-received character. */
309 __serial_rx(c, regs);
310 }
312 long guest_console_write(char *buffer, int count)
313 {
314 char kbuf[128], *kptr;
315 int kcount;
317 while ( count > 0 )
318 {
319 while ( serial_tx_space(sercon_handle) < (SERIAL_TXBUFSZ / 2) )
320 {
321 if ( hypercall_preempt_check() )
322 break;
323 cpu_relax();
324 }
326 if ( hypercall_preempt_check() )
327 return hypercall3_create_continuation(
328 __HYPERVISOR_console_io, CONSOLEIO_write, count, buffer);
330 kcount = min_t(int, count, sizeof(kbuf)-1);
331 if ( copy_from_user(kbuf, buffer, kcount) )
332 return -EFAULT;
333 kbuf[kcount] = '\0';
335 serial_puts(sercon_handle, kbuf);
337 for ( kptr = kbuf; *kptr != '\0'; kptr++ )
338 putchar_console(*kptr);
340 buffer += kcount;
341 count -= kcount;
342 }
344 return 0;
345 }
347 long do_console_io(int cmd, int count, char *buffer)
348 {
349 long rc;
351 #ifndef VERBOSE
352 /* Only domain 0 may access the emergency console. */
353 if ( current->domain->domain_id != 0 )
354 return -EPERM;
355 #endif
357 switch ( cmd )
358 {
359 case CONSOLEIO_write:
360 rc = guest_console_write(buffer, count);
361 break;
362 case CONSOLEIO_read:
363 rc = 0;
364 while ( (serial_rx_cons != serial_rx_prod) && (rc < count) )
365 {
366 if ( put_user(serial_rx_ring[SERIAL_RX_MASK(serial_rx_cons)],
367 &buffer[rc]) )
368 {
369 rc = -EFAULT;
370 break;
371 }
372 rc++;
373 serial_rx_cons++;
374 }
375 break;
376 default:
377 rc = -ENOSYS;
378 break;
379 }
381 return rc;
382 }
385 /*
386 * *****************************************************
387 * *************** GENERIC CONSOLE I/O *****************
388 * *****************************************************
389 */
391 static inline void __putstr(const char *str)
392 {
393 int c;
395 serial_puts(sercon_handle, str);
397 while ( (c = *str++) != '\0' )
398 {
399 putchar_console(c);
400 putchar_console_ring(c);
401 }
402 }
404 void printf(const char *fmt, ...)
405 {
406 static char buf[1024];
407 static int start_of_line = 1;
409 va_list args;
410 char *p, *q;
411 unsigned long flags;
413 spin_lock_irqsave(&console_lock, flags);
415 va_start(args, fmt);
416 (void)vsnprintf(buf, sizeof(buf), fmt, args);
417 va_end(args);
419 p = buf;
420 while ( (q = strchr(p, '\n')) != NULL )
421 {
422 *q = '\0';
423 if ( start_of_line )
424 __putstr(printk_prefix);
425 __putstr(p);
426 __putstr("\n");
427 start_of_line = 1;
428 p = q + 1;
429 }
431 if ( *p != '\0' )
432 {
433 if ( start_of_line )
434 __putstr(printk_prefix);
435 __putstr(p);
436 start_of_line = 0;
437 }
439 spin_unlock_irqrestore(&console_lock, flags);
440 }
442 void set_printk_prefix(const char *prefix)
443 {
444 strcpy(printk_prefix, prefix);
445 }
447 void init_console(void)
448 {
449 char *p;
451 /* Where should console output go? */
452 for ( p = opt_console; p != NULL; p = strchr(p, ',') )
453 {
454 if ( *p == ',' )
455 p++;
456 if ( strncmp(p, "com", 3) == 0 )
457 sercon_handle = serial_parse_handle(p);
458 else if ( strncmp(p, "vga", 3) == 0 )
459 vgacon_enabled = 1;
460 }
462 init_vga();
464 serial_set_rx_handler(sercon_handle, serial_rx);
466 /* HELLO WORLD --- start-of-day banner text. */
467 printk(XEN_BANNER);
468 printk(" http://www.cl.cam.ac.uk/netos/xen\n");
469 printk(" University of Cambridge Computer Laboratory\n\n");
470 printk(" Xen version %d.%d%s (%s@%s) (%s) %s\n",
471 XEN_VERSION, XEN_SUBVERSION, XEN_EXTRAVERSION,
472 XEN_COMPILE_BY, XEN_COMPILE_DOMAIN,
473 XEN_COMPILER, XEN_COMPILE_DATE);
474 printk(" Latest ChangeSet: %s\n\n", XEN_CHANGESET);
475 set_printk_prefix("(XEN) ");
477 if ( opt_sync_console )
478 {
479 serial_start_sync(sercon_handle);
480 printk("Console output is synchronous.\n");
481 }
482 }
484 void console_endboot(int disable_vga)
485 {
486 if ( disable_vga )
487 vgacon_enabled = 0;
489 /*
490 * If user specifies so, we fool the switch routine to redirect input
491 * straight back to Xen. I use this convoluted method so we still print
492 * a useful 'how to switch' message.
493 */
494 if ( opt_conswitch[1] == 'x' )
495 xen_rx = !xen_rx;
497 /* Serial input is directed to DOM0 by default. */
498 switch_serial_input();
499 }
501 void console_force_unlock(void)
502 {
503 console_lock = SPIN_LOCK_UNLOCKED;
504 serial_force_unlock(sercon_handle);
505 }
507 void console_force_lock(void)
508 {
509 spin_lock(&console_lock);
510 }
512 void console_start_sync(void)
513 {
514 serial_start_sync(sercon_handle);
515 }
517 void console_end_sync(void)
518 {
519 serial_end_sync(sercon_handle);
520 }
522 void console_putc(char c)
523 {
524 serial_putc(sercon_handle, c);
525 }
527 int console_getc(void)
528 {
529 return serial_getc(sercon_handle);
530 }
533 /*
534 * **************************************************************
535 * *************** Serial console ring buffer *******************
536 * **************************************************************
537 */
539 #ifndef NDEBUG
541 /* Send output direct to console, or buffer it? */
542 int debugtrace_send_to_console;
544 static char *debugtrace_buf; /* Debug-trace buffer */
545 static unsigned int debugtrace_prd; /* Producer index */
546 static unsigned int debugtrace_kilobytes = 128, debugtrace_bytes;
547 static unsigned int debugtrace_used;
548 static spinlock_t debugtrace_lock = SPIN_LOCK_UNLOCKED;
549 integer_param("debugtrace", debugtrace_kilobytes);
551 void debugtrace_dump(void)
552 {
553 unsigned long flags;
555 if ( (debugtrace_bytes == 0) || !debugtrace_used )
556 return;
558 watchdog_disable();
560 spin_lock_irqsave(&debugtrace_lock, flags);
562 printk("debugtrace_dump() starting\n");
564 /* Print oldest portion of the ring. */
565 ASSERT(debugtrace_buf[debugtrace_bytes - 1] == 0);
566 serial_puts(sercon_handle, &debugtrace_buf[debugtrace_prd]);
568 /* Print youngest portion of the ring. */
569 debugtrace_buf[debugtrace_prd] = '\0';
570 serial_puts(sercon_handle, &debugtrace_buf[0]);
572 memset(debugtrace_buf, '\0', debugtrace_bytes);
574 printk("debugtrace_dump() finished\n");
576 spin_unlock_irqrestore(&debugtrace_lock, flags);
578 watchdog_enable();
579 }
581 void debugtrace_printk(const char *fmt, ...)
582 {
583 static char buf[1024];
585 va_list args;
586 char *p;
587 unsigned long flags;
589 if ( debugtrace_bytes == 0 )
590 return;
592 debugtrace_used = 1;
594 spin_lock_irqsave(&debugtrace_lock, flags);
596 ASSERT(debugtrace_buf[debugtrace_bytes - 1] == 0);
598 va_start(args, fmt);
599 (void)vsnprintf(buf, sizeof(buf), fmt, args);
600 va_end(args);
602 if ( debugtrace_send_to_console )
603 {
604 serial_puts(sercon_handle, buf);
605 }
606 else
607 {
608 for ( p = buf; *p != '\0'; p++ )
609 {
610 debugtrace_buf[debugtrace_prd++] = *p;
611 /* Always leave a nul byte at the end of the buffer. */
612 if ( debugtrace_prd == (debugtrace_bytes - 1) )
613 debugtrace_prd = 0;
614 }
615 }
617 spin_unlock_irqrestore(&debugtrace_lock, flags);
618 }
620 static int __init debugtrace_init(void)
621 {
622 int order;
623 unsigned int kbytes, bytes;
625 /* Round size down to next power of two. */
626 while ( (kbytes = (debugtrace_kilobytes & (debugtrace_kilobytes-1))) != 0 )
627 debugtrace_kilobytes = kbytes;
629 bytes = debugtrace_kilobytes << 10;
630 if ( bytes == 0 )
631 return 0;
633 order = get_order_from_bytes(bytes);
634 debugtrace_buf = alloc_xenheap_pages(order);
635 ASSERT(debugtrace_buf != NULL);
637 memset(debugtrace_buf, '\0', bytes);
639 debugtrace_bytes = bytes;
641 return 0;
642 }
643 __initcall(debugtrace_init);
645 #endif /* !NDEBUG */
649 /*
650 * **************************************************************
651 * *************** Debugging/tracing/error-report ***************
652 * **************************************************************
653 */
655 void panic(const char *fmt, ...)
656 {
657 va_list args;
658 char buf[128];
659 unsigned long flags;
660 static spinlock_t lock = SPIN_LOCK_UNLOCKED;
661 extern void machine_restart(char *);
663 debugtrace_dump();
665 va_start(args, fmt);
666 (void)vsnprintf(buf, sizeof(buf), fmt, args);
667 va_end(args);
669 debugger_trap_immediate();
671 /* Spit out multiline message in one go. */
672 spin_lock_irqsave(&lock, flags);
673 printk("\n****************************************\n");
674 printk("Panic on CPU %d:\n", smp_processor_id());
675 printk(buf);
676 printk("****************************************\n\n");
677 printk("Reboot in five seconds...\n");
678 spin_unlock_irqrestore(&lock, flags);
680 watchdog_disable();
681 mdelay(5000);
682 machine_restart(0);
683 }
685 /*
686 * Local variables:
687 * mode: C
688 * c-set-style: "BSD"
689 * c-basic-offset: 4
690 * tab-width: 4
691 * indent-tabs-mode: nil
692 * End:
693 */