ia64/xen-unstable

view xen/drivers/char/console.c @ 6293:47d49e8b8042

Merge.

Signed-off-by: Steven Smith, sos22@cam.ac.uk
author sos22@douglas.cl.cam.ac.uk
date Fri Aug 19 15:47:19 2005 +0000 (2005-08-19)
parents 66348ff38ec1 87dec3b9c546
children f51fe43c5d1c 5f4724c13040 81576d3d1ca8 3a8f27c6d56c
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];
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 buffer += kcount;
338 count -= kcount;
339 }
341 return 0;
342 }
344 long do_console_io(int cmd, int count, char *buffer)
345 {
346 long rc;
348 #ifndef VERBOSE
349 /* Only domain 0 may access the emergency console. */
350 if ( current->domain->domain_id != 0 )
351 return -EPERM;
352 #endif
354 switch ( cmd )
355 {
356 case CONSOLEIO_write:
357 rc = guest_console_write(buffer, count);
358 break;
359 case CONSOLEIO_read:
360 rc = 0;
361 while ( (serial_rx_cons != serial_rx_prod) && (rc < count) )
362 {
363 if ( put_user(serial_rx_ring[SERIAL_RX_MASK(serial_rx_cons)],
364 &buffer[rc]) )
365 {
366 rc = -EFAULT;
367 break;
368 }
369 rc++;
370 serial_rx_cons++;
371 }
372 break;
373 default:
374 rc = -ENOSYS;
375 break;
376 }
378 return rc;
379 }
382 /*
383 * *****************************************************
384 * *************** GENERIC CONSOLE I/O *****************
385 * *****************************************************
386 */
388 static inline void __putstr(const char *str)
389 {
390 int c;
392 serial_puts(sercon_handle, str);
394 while ( (c = *str++) != '\0' )
395 {
396 putchar_console(c);
397 putchar_console_ring(c);
398 }
399 }
401 void printf(const char *fmt, ...)
402 {
403 static char buf[1024];
404 static int start_of_line = 1;
406 va_list args;
407 char *p, *q;
408 unsigned long flags;
410 spin_lock_irqsave(&console_lock, flags);
412 va_start(args, fmt);
413 (void)vsnprintf(buf, sizeof(buf), fmt, args);
414 va_end(args);
416 p = buf;
417 while ( (q = strchr(p, '\n')) != NULL )
418 {
419 *q = '\0';
420 if ( start_of_line )
421 __putstr(printk_prefix);
422 __putstr(p);
423 __putstr("\n");
424 start_of_line = 1;
425 p = q + 1;
426 }
428 if ( *p != '\0' )
429 {
430 if ( start_of_line )
431 __putstr(printk_prefix);
432 __putstr(p);
433 start_of_line = 0;
434 }
436 spin_unlock_irqrestore(&console_lock, flags);
437 }
439 void set_printk_prefix(const char *prefix)
440 {
441 strcpy(printk_prefix, prefix);
442 }
444 void init_console(void)
445 {
446 char *p;
448 /* Where should console output go? */
449 for ( p = opt_console; p != NULL; p = strchr(p, ',') )
450 {
451 if ( *p == ',' )
452 p++;
453 if ( strncmp(p, "com", 3) == 0 )
454 sercon_handle = serial_parse_handle(p);
455 else if ( strncmp(p, "vga", 3) == 0 )
456 vgacon_enabled = 1;
457 }
459 init_vga();
461 serial_set_rx_handler(sercon_handle, serial_rx);
463 /* HELLO WORLD --- start-of-day banner text. */
464 printk(XEN_BANNER);
465 printk(" http://www.cl.cam.ac.uk/netos/xen\n");
466 printk(" University of Cambridge Computer Laboratory\n\n");
467 printk(" Xen version %d.%d%s (%s@%s) (%s) %s\n",
468 XEN_VERSION, XEN_SUBVERSION, XEN_EXTRAVERSION,
469 XEN_COMPILE_BY, XEN_COMPILE_DOMAIN,
470 XEN_COMPILER, XEN_COMPILE_DATE);
471 printk(" Latest ChangeSet: %s\n\n", XEN_CHANGESET);
472 set_printk_prefix("(XEN) ");
474 if ( opt_sync_console )
475 {
476 serial_start_sync(sercon_handle);
477 printk("Console output is synchronous.\n");
478 }
479 }
481 void console_endboot(int disable_vga)
482 {
483 if ( disable_vga )
484 vgacon_enabled = 0;
486 /*
487 * If user specifies so, we fool the switch routine to redirect input
488 * straight back to Xen. I use this convoluted method so we still print
489 * a useful 'how to switch' message.
490 */
491 if ( opt_conswitch[1] == 'x' )
492 xen_rx = !xen_rx;
494 /* Serial input is directed to DOM0 by default. */
495 switch_serial_input();
496 }
498 void console_force_unlock(void)
499 {
500 console_lock = SPIN_LOCK_UNLOCKED;
501 serial_force_unlock(sercon_handle);
502 }
504 void console_force_lock(void)
505 {
506 spin_lock(&console_lock);
507 }
509 void console_start_sync(void)
510 {
511 serial_start_sync(sercon_handle);
512 }
514 void console_end_sync(void)
515 {
516 serial_end_sync(sercon_handle);
517 }
519 void console_putc(char c)
520 {
521 serial_putc(sercon_handle, c);
522 }
524 int console_getc(void)
525 {
526 return serial_getc(sercon_handle);
527 }
530 /*
531 * **************************************************************
532 * *************** Serial console ring buffer *******************
533 * **************************************************************
534 */
536 #ifndef NDEBUG
538 /* Send output direct to console, or buffer it? */
539 int debugtrace_send_to_console;
541 static char *debugtrace_buf; /* Debug-trace buffer */
542 static unsigned int debugtrace_prd; /* Producer index */
543 static unsigned int debugtrace_kilobytes = 128, debugtrace_bytes;
544 static unsigned int debugtrace_used;
545 static spinlock_t debugtrace_lock = SPIN_LOCK_UNLOCKED;
546 integer_param("debugtrace", debugtrace_kilobytes);
548 void debugtrace_dump(void)
549 {
550 unsigned long flags;
552 if ( (debugtrace_bytes == 0) || !debugtrace_used )
553 return;
555 watchdog_disable();
557 spin_lock_irqsave(&debugtrace_lock, flags);
559 printk("debugtrace_dump() starting\n");
561 /* Print oldest portion of the ring. */
562 ASSERT(debugtrace_buf[debugtrace_bytes - 1] == 0);
563 serial_puts(sercon_handle, &debugtrace_buf[debugtrace_prd]);
565 /* Print youngest portion of the ring. */
566 debugtrace_buf[debugtrace_prd] = '\0';
567 serial_puts(sercon_handle, &debugtrace_buf[0]);
569 memset(debugtrace_buf, '\0', debugtrace_bytes);
571 printk("debugtrace_dump() finished\n");
573 spin_unlock_irqrestore(&debugtrace_lock, flags);
575 watchdog_enable();
576 }
578 void debugtrace_printk(const char *fmt, ...)
579 {
580 static char buf[1024];
582 va_list args;
583 char *p;
584 unsigned long flags;
586 if ( debugtrace_bytes == 0 )
587 return;
589 debugtrace_used = 1;
591 spin_lock_irqsave(&debugtrace_lock, flags);
593 ASSERT(debugtrace_buf[debugtrace_bytes - 1] == 0);
595 va_start(args, fmt);
596 (void)vsnprintf(buf, sizeof(buf), fmt, args);
597 va_end(args);
599 if ( debugtrace_send_to_console )
600 {
601 serial_puts(sercon_handle, buf);
602 }
603 else
604 {
605 for ( p = buf; *p != '\0'; p++ )
606 {
607 debugtrace_buf[debugtrace_prd++] = *p;
608 /* Always leave a nul byte at the end of the buffer. */
609 if ( debugtrace_prd == (debugtrace_bytes - 1) )
610 debugtrace_prd = 0;
611 }
612 }
614 spin_unlock_irqrestore(&debugtrace_lock, flags);
615 }
617 static int __init debugtrace_init(void)
618 {
619 int order;
620 unsigned int kbytes, bytes;
622 /* Round size down to next power of two. */
623 while ( (kbytes = (debugtrace_kilobytes & (debugtrace_kilobytes-1))) != 0 )
624 debugtrace_kilobytes = kbytes;
626 bytes = debugtrace_kilobytes << 10;
627 if ( bytes == 0 )
628 return 0;
630 order = get_order(bytes);
631 debugtrace_buf = alloc_xenheap_pages(order);
632 ASSERT(debugtrace_buf != NULL);
634 memset(debugtrace_buf, '\0', bytes);
636 debugtrace_bytes = bytes;
638 return 0;
639 }
640 __initcall(debugtrace_init);
642 #endif /* !NDEBUG */
646 /*
647 * **************************************************************
648 * *************** Debugging/tracing/error-report ***************
649 * **************************************************************
650 */
652 void panic(const char *fmt, ...)
653 {
654 va_list args;
655 char buf[128];
656 unsigned long flags;
657 static spinlock_t lock = SPIN_LOCK_UNLOCKED;
658 extern void machine_restart(char *);
660 debugtrace_dump();
662 va_start(args, fmt);
663 (void)vsnprintf(buf, sizeof(buf), fmt, args);
664 va_end(args);
666 debugger_trap_immediate();
668 /* Spit out multiline message in one go. */
669 spin_lock_irqsave(&lock, flags);
670 printk("\n****************************************\n");
671 printk("Panic on CPU %d:\n", smp_processor_id());
672 printk(buf);
673 printk("****************************************\n\n");
674 printk("Reboot in five seconds...\n");
675 spin_unlock_irqrestore(&lock, flags);
677 watchdog_disable();
678 mdelay(5000);
679 machine_restart(0);
680 }
682 /*
683 * Local variables:
684 * mode: C
685 * c-set-style: "BSD"
686 * c-basic-offset: 4
687 * tab-width: 4
688 * indent-tabs-mode: nil
689 * End:
690 */