ia64/xen-unstable

view xen/drivers/char/console.c @ 9721:2ae003c45ffe

Use ioremap instead of __va to map VGA memory area (to be uncached).

Signed-off-by: Tristan Gingold <tristan.gingold@bull.net>
author kaf24@firebug.cl.cam.ac.uk
date Fri Apr 14 14:37:19 2006 +0100 (2006-04-14)
parents 6692dd873e21
children 4e1b8be54311
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 <xen/guest_access.h>
24 #include <asm/current.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
68 #define VIDEO_SIZE (COLUMNS * LINES * 2)
70 /* Clear the screen and initialize VIDEO, XPOS and YPOS. */
71 static void cls(void)
72 {
73 memset(video, 0, VIDEO_SIZE);
74 xpos = ypos = 0;
75 outw(10+(1<<(5+8)), 0x3d4); /* cursor off */
76 }
78 static int detect_video(void *video_base)
79 {
80 volatile u16 *p = (volatile u16 *)video_base;
81 u16 saved1 = p[0], saved2 = p[1];
82 int video_found = 1;
84 p[0] = 0xAA55;
85 p[1] = 0x55AA;
86 if ( (p[0] != 0xAA55) || (p[1] != 0x55AA) )
87 video_found = 0;
89 p[0] = 0x55AA;
90 p[1] = 0xAA55;
91 if ( (p[0] != 0x55AA) || (p[1] != 0xAA55) )
92 video_found = 0;
94 p[0] = saved1;
95 p[1] = saved2;
97 return video_found;
98 }
100 static int detect_vga(void)
101 {
102 /*
103 * Look at a number of well-known locations. Even if video is not at
104 * 0xB8000 right now, it will appear there when we set up text mode 3.
105 *
106 * We assume if there is any sign of a video adaptor then it is at least
107 * VGA-compatible (surely noone runs CGA, EGA, .... these days?).
108 *
109 * These checks are basically to detect headless server boxes.
110 */
111 return (detect_video(ioremap(0xA0000, VIDEO_SIZE)) ||
112 detect_video(ioremap(0xB0000, VIDEO_SIZE)) ||
113 detect_video(ioremap(0xB8000, VIDEO_SIZE)));
114 }
116 /* This is actually code from vgaHWRestore in an old version of XFree86 :-) */
117 static void init_vga(void)
118 {
119 /* The following VGA state was saved from a chip in text mode 3. */
120 static unsigned char regs[] = {
121 /* Sequencer registers */
122 0x03, 0x00, 0x03, 0x00, 0x02,
123 /* CRTC registers */
124 0x5f, 0x4f, 0x50, 0x82, 0x55, 0x81, 0xbf, 0x1f, 0x00, 0x4f, 0x20,
125 0x0e, 0x00, 0x00, 0x01, 0xe0, 0x9c, 0x8e, 0x8f, 0x28, 0x1f, 0x96,
126 0xb9, 0xa3, 0xff,
127 /* Graphic registers */
128 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x0e, 0x00, 0xff,
129 /* Attribute registers */
130 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x14, 0x07, 0x38, 0x39, 0x3a,
131 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x0c, 0x00, 0x0f, 0x08, 0x00
132 };
134 int i, j = 0;
135 volatile unsigned char tmp;
137 if ( !vgacon_enabled )
138 return;
140 if ( !detect_vga() )
141 {
142 printk("No VGA adaptor detected!\n");
143 vgacon_enabled = 0;
144 return;
145 }
147 video = ioremap(0xB8000, VIDEO_SIZE);
149 tmp = inb(0x3da);
150 outb(0x00, 0x3c0);
152 for ( i = 0; i < 5; i++ )
153 outw((regs[j++] << 8) | i, 0x3c4);
155 /* Ensure CRTC registers 0-7 are unlocked by clearing bit 7 of CRTC[17]. */
156 outw(((regs[5+17] & 0x7F) << 8) | 17, 0x3d4);
158 for ( i = 0; i < 25; i++ )
159 outw((regs[j++] << 8) | i, 0x3d4);
161 for ( i = 0; i < 9; i++ )
162 outw((regs[j++] << 8) | i, 0x3ce);
164 for ( i = 0; i < 21; i++ )
165 {
166 tmp = inb(0x3da);
167 outb(i, 0x3c0);
168 outb(regs[j++], 0x3c0);
169 }
171 tmp = inb(0x3da);
172 outb(0x20, 0x3c0);
174 cls();
175 }
177 static void put_newline(void)
178 {
179 xpos = 0;
180 ypos++;
182 if (ypos >= LINES)
183 {
184 ypos = LINES-1;
185 memmove((char*)video,
186 (char*)video + 2*COLUMNS, (LINES-1)*2*COLUMNS);
187 memset((char*)video + (LINES-1)*2*COLUMNS, 0, 2*COLUMNS);
188 }
189 }
191 static void putchar_console(int c)
192 {
193 if ( !vgacon_enabled )
194 return;
196 if ( c == '\n' )
197 {
198 put_newline();
199 }
200 else
201 {
202 if ( xpos >= COLUMNS )
203 put_newline();
204 video[(xpos + ypos * COLUMNS) * 2] = c & 0xFF;
205 video[(xpos + ypos * COLUMNS) * 2 + 1] = ATTRIBUTE;
206 ++xpos;
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(GUEST_HANDLE(char) str, u32 *pcount, int clear)
225 {
226 unsigned int idx, len, max, sofar, c;
227 unsigned long flags;
229 max = *pcount;
230 sofar = 0;
232 c = conringc;
233 while ( c != conringp )
234 {
235 idx = CONRING_IDX_MASK(c);
236 len = conringp - c;
237 if ( (idx + len) > CONRING_SIZE )
238 len = CONRING_SIZE - idx;
239 if ( (sofar + len) > max )
240 len = max - sofar;
241 if ( copy_to_guest_offset(str, sofar, &conring[idx], len) )
242 return -EFAULT;
243 sofar += len;
244 c += len;
245 }
247 if ( clear )
248 {
249 spin_lock_irqsave(&console_lock, flags);
250 conringc = conringp;
251 spin_unlock_irqrestore(&console_lock, flags);
252 }
254 *pcount = sofar;
255 return 0;
256 }
259 /*
260 * *******************************************************
261 * *************** ACCESS TO SERIAL LINE *****************
262 * *******************************************************
263 */
265 /* Characters received over the serial line are buffered for domain 0. */
266 #define SERIAL_RX_SIZE 128
267 #define SERIAL_RX_MASK(_i) ((_i)&(SERIAL_RX_SIZE-1))
268 static char serial_rx_ring[SERIAL_RX_SIZE];
269 static unsigned int serial_rx_cons, serial_rx_prod;
271 /* CTRL-<switch_char> switches input direction between Xen and DOM0. */
272 #define SWITCH_CODE (opt_conswitch[0]-'a'+1)
273 static int xen_rx = 1; /* FALSE => serial input passed to domain 0. */
275 static void switch_serial_input(void)
276 {
277 static char *input_str[2] = { "DOM0", "Xen" };
278 xen_rx = !xen_rx;
279 if ( SWITCH_CODE != 0 )
280 {
281 printk("*** Serial input -> %s "
282 "(type 'CTRL-%c' three times to switch input to %s).\n",
283 input_str[xen_rx], opt_conswitch[0], input_str[!xen_rx]);
284 }
285 }
287 static void __serial_rx(char c, struct cpu_user_regs *regs)
288 {
289 if ( xen_rx )
290 return handle_keypress(c, regs);
292 /* Deliver input to guest buffer, unless it is already full. */
293 if ( (serial_rx_prod-serial_rx_cons) != SERIAL_RX_SIZE )
294 serial_rx_ring[SERIAL_RX_MASK(serial_rx_prod++)] = c;
295 /* Always notify the guest: prevents receive path from getting stuck. */
296 send_guest_global_virq(dom0, VIRQ_CONSOLE);
297 }
299 static void serial_rx(char c, struct cpu_user_regs *regs)
300 {
301 static int switch_code_count = 0;
303 if ( (SWITCH_CODE != 0) && (c == SWITCH_CODE) )
304 {
305 /* We eat CTRL-<switch_char> in groups of 3 to switch console input. */
306 if ( ++switch_code_count == 3 )
307 {
308 switch_serial_input();
309 switch_code_count = 0;
310 return;
311 }
312 }
313 else
314 {
315 switch_code_count = 0;
316 }
318 /* Finally process the just-received character. */
319 __serial_rx(c, regs);
320 }
322 static long guest_console_write(GUEST_HANDLE(char) buffer, int count)
323 {
324 char kbuf[128], *kptr;
325 int kcount;
327 while ( count > 0 )
328 {
329 while ( serial_tx_space(sercon_handle) < (SERIAL_TXBUFSZ / 2) )
330 {
331 if ( hypercall_preempt_check() )
332 break;
333 cpu_relax();
334 }
336 if ( hypercall_preempt_check() )
337 return hypercall_create_continuation(
338 __HYPERVISOR_console_io, "iih",
339 CONSOLEIO_write, count, buffer);
341 kcount = min_t(int, count, sizeof(kbuf)-1);
342 if ( copy_from_guest((char *)kbuf, buffer, kcount) )
343 return -EFAULT;
344 kbuf[kcount] = '\0';
346 serial_puts(sercon_handle, kbuf);
348 for ( kptr = kbuf; *kptr != '\0'; kptr++ )
349 putchar_console(*kptr);
351 guest_handle_add_offset(buffer, kcount);
352 count -= kcount;
353 }
355 return 0;
356 }
358 long do_console_io(int cmd, int count, GUEST_HANDLE(char) buffer)
359 {
360 long rc;
361 unsigned int idx, len;
363 #ifndef VERBOSE
364 /* Only domain 0 may access the emergency console. */
365 if ( current->domain->domain_id != 0 )
366 return -EPERM;
367 #endif
369 switch ( cmd )
370 {
371 case CONSOLEIO_write:
372 rc = guest_console_write(buffer, count);
373 break;
374 case CONSOLEIO_read:
375 rc = 0;
376 while ( (serial_rx_cons != serial_rx_prod) && (rc < count) )
377 {
378 idx = SERIAL_RX_MASK(serial_rx_cons);
379 len = serial_rx_prod - serial_rx_cons;
380 if ( (idx + len) > SERIAL_RX_SIZE )
381 len = SERIAL_RX_SIZE - idx;
382 if ( (rc + len) > count )
383 len = count - rc;
384 if ( copy_to_guest_offset(buffer, rc, &serial_rx_ring[idx], len) )
385 {
386 rc = -EFAULT;
387 break;
388 }
389 rc += len;
390 serial_rx_cons += len;
391 }
392 break;
393 default:
394 rc = -ENOSYS;
395 break;
396 }
398 return rc;
399 }
402 /*
403 * *****************************************************
404 * *************** GENERIC CONSOLE I/O *****************
405 * *****************************************************
406 */
408 static inline void __putstr(const char *str)
409 {
410 int c;
412 serial_puts(sercon_handle, str);
414 while ( (c = *str++) != '\0' )
415 {
416 putchar_console(c);
417 putchar_console_ring(c);
418 }
419 }
421 void printf(const char *fmt, ...)
422 {
423 static char buf[1024];
424 static int start_of_line = 1;
426 va_list args;
427 char *p, *q;
428 unsigned long flags;
430 spin_lock_irqsave(&console_lock, flags);
432 va_start(args, fmt);
433 (void)vsnprintf(buf, sizeof(buf), fmt, args);
434 va_end(args);
436 p = buf;
437 while ( (q = strchr(p, '\n')) != NULL )
438 {
439 *q = '\0';
440 if ( start_of_line )
441 __putstr(printk_prefix);
442 __putstr(p);
443 __putstr("\n");
444 start_of_line = 1;
445 p = q + 1;
446 }
448 if ( *p != '\0' )
449 {
450 if ( start_of_line )
451 __putstr(printk_prefix);
452 __putstr(p);
453 start_of_line = 0;
454 }
456 spin_unlock_irqrestore(&console_lock, flags);
457 }
459 void set_printk_prefix(const char *prefix)
460 {
461 strcpy(printk_prefix, prefix);
462 }
464 void init_console(void)
465 {
466 char *p;
468 /* Where should console output go? */
469 for ( p = opt_console; p != NULL; p = strchr(p, ',') )
470 {
471 if ( *p == ',' )
472 p++;
473 if ( strncmp(p, "com", 3) == 0 )
474 sercon_handle = serial_parse_handle(p);
475 else if ( strncmp(p, "vga", 3) == 0 )
476 vgacon_enabled = 1;
477 }
479 init_vga();
481 serial_set_rx_handler(sercon_handle, serial_rx);
483 /* HELLO WORLD --- start-of-day banner text. */
484 printk(XEN_BANNER);
485 printk(" http://www.cl.cam.ac.uk/netos/xen\n");
486 printk(" University of Cambridge Computer Laboratory\n\n");
487 printk(" Xen version %d.%d%s (%s@%s) (%s) %s\n",
488 XEN_VERSION, XEN_SUBVERSION, XEN_EXTRAVERSION,
489 XEN_COMPILE_BY, XEN_COMPILE_DOMAIN,
490 XEN_COMPILER, XEN_COMPILE_DATE);
491 printk(" Latest ChangeSet: %s\n\n", XEN_CHANGESET);
492 set_printk_prefix("(XEN) ");
494 if ( opt_sync_console )
495 {
496 serial_start_sync(sercon_handle);
497 printk("Console output is synchronous.\n");
498 }
499 }
501 void console_endboot(int disable_vga)
502 {
503 if ( disable_vga )
504 vgacon_enabled = 0;
506 /*
507 * If user specifies so, we fool the switch routine to redirect input
508 * straight back to Xen. I use this convoluted method so we still print
509 * a useful 'how to switch' message.
510 */
511 if ( opt_conswitch[1] == 'x' )
512 xen_rx = !xen_rx;
514 /* Serial input is directed to DOM0 by default. */
515 switch_serial_input();
516 }
518 void console_force_unlock(void)
519 {
520 console_lock = SPIN_LOCK_UNLOCKED;
521 serial_force_unlock(sercon_handle);
522 console_start_sync();
523 }
525 void console_force_lock(void)
526 {
527 spin_lock(&console_lock);
528 }
530 void console_start_sync(void)
531 {
532 serial_start_sync(sercon_handle);
533 }
535 void console_end_sync(void)
536 {
537 serial_end_sync(sercon_handle);
538 }
540 void console_putc(char c)
541 {
542 serial_putc(sercon_handle, c);
543 }
545 int console_getc(void)
546 {
547 return serial_getc(sercon_handle);
548 }
551 /*
552 * **************************************************************
553 * *************** Serial console ring buffer *******************
554 * **************************************************************
555 */
557 #ifndef NDEBUG
559 /* Send output direct to console, or buffer it? */
560 int debugtrace_send_to_console;
562 static char *debugtrace_buf; /* Debug-trace buffer */
563 static unsigned int debugtrace_prd; /* Producer index */
564 static unsigned int debugtrace_kilobytes = 128, debugtrace_bytes;
565 static unsigned int debugtrace_used;
566 static spinlock_t debugtrace_lock = SPIN_LOCK_UNLOCKED;
567 integer_param("debugtrace", debugtrace_kilobytes);
569 void debugtrace_dump(void)
570 {
571 unsigned long flags;
573 if ( (debugtrace_bytes == 0) || !debugtrace_used )
574 return;
576 watchdog_disable();
578 spin_lock_irqsave(&debugtrace_lock, flags);
580 printk("debugtrace_dump() starting\n");
582 /* Print oldest portion of the ring. */
583 ASSERT(debugtrace_buf[debugtrace_bytes - 1] == 0);
584 serial_puts(sercon_handle, &debugtrace_buf[debugtrace_prd]);
586 /* Print youngest portion of the ring. */
587 debugtrace_buf[debugtrace_prd] = '\0';
588 serial_puts(sercon_handle, &debugtrace_buf[0]);
590 memset(debugtrace_buf, '\0', debugtrace_bytes);
592 printk("debugtrace_dump() finished\n");
594 spin_unlock_irqrestore(&debugtrace_lock, flags);
596 watchdog_enable();
597 }
599 void debugtrace_printk(const char *fmt, ...)
600 {
601 static char buf[1024];
603 va_list args;
604 char *p;
605 unsigned long flags;
607 if ( debugtrace_bytes == 0 )
608 return;
610 debugtrace_used = 1;
612 spin_lock_irqsave(&debugtrace_lock, flags);
614 ASSERT(debugtrace_buf[debugtrace_bytes - 1] == 0);
616 va_start(args, fmt);
617 (void)vsnprintf(buf, sizeof(buf), fmt, args);
618 va_end(args);
620 if ( debugtrace_send_to_console )
621 {
622 serial_puts(sercon_handle, buf);
623 }
624 else
625 {
626 for ( p = buf; *p != '\0'; p++ )
627 {
628 debugtrace_buf[debugtrace_prd++] = *p;
629 /* Always leave a nul byte at the end of the buffer. */
630 if ( debugtrace_prd == (debugtrace_bytes - 1) )
631 debugtrace_prd = 0;
632 }
633 }
635 spin_unlock_irqrestore(&debugtrace_lock, flags);
636 }
638 static int __init debugtrace_init(void)
639 {
640 int order;
641 unsigned int kbytes, bytes;
643 /* Round size down to next power of two. */
644 while ( (kbytes = (debugtrace_kilobytes & (debugtrace_kilobytes-1))) != 0 )
645 debugtrace_kilobytes = kbytes;
647 bytes = debugtrace_kilobytes << 10;
648 if ( bytes == 0 )
649 return 0;
651 order = get_order_from_bytes(bytes);
652 debugtrace_buf = alloc_xenheap_pages(order);
653 ASSERT(debugtrace_buf != NULL);
655 memset(debugtrace_buf, '\0', bytes);
657 debugtrace_bytes = bytes;
659 return 0;
660 }
661 __initcall(debugtrace_init);
663 #endif /* !NDEBUG */
667 /*
668 * **************************************************************
669 * *************** Debugging/tracing/error-report ***************
670 * **************************************************************
671 */
673 void panic(const char *fmt, ...)
674 {
675 va_list args;
676 char buf[128];
677 unsigned long flags;
678 static spinlock_t lock = SPIN_LOCK_UNLOCKED;
679 extern void machine_restart(char *);
681 debugtrace_dump();
683 va_start(args, fmt);
684 (void)vsnprintf(buf, sizeof(buf), fmt, args);
685 va_end(args);
687 /* Spit out multiline message in one go. */
688 console_start_sync();
689 spin_lock_irqsave(&lock, flags);
690 printk("\n****************************************\n");
691 printk("Panic on CPU %d:\n", smp_processor_id());
692 printk(buf);
693 printk("****************************************\n\n");
694 printk("Reboot in five seconds...\n");
695 spin_unlock_irqrestore(&lock, flags);
697 debugger_trap_immediate();
699 watchdog_disable();
700 mdelay(5000);
701 machine_restart(0);
702 }
704 /*
705 * Local variables:
706 * mode: C
707 * c-set-style: "BSD"
708 * c-basic-offset: 4
709 * tab-width: 4
710 * indent-tabs-mode: nil
711 * End:
712 */