ia64/xen-unstable

view xen/drivers/char/console.c @ 9597:8f7aad20b4a5

Backtrack on the new interface for reserved event-channel
ports, as binding them in user space via the evtchn driver
would be a pain. Instead extend VIRQs so they can be
classified as 'global' or 'per vcpu'. The former can only
be allocated once per guest, but can be re-bound to
an arbitrary VCPU.

Signed-off-by: Keir Fraser <keir@xensource.com>
author kaf24@firebug.cl.cam.ac.uk
date Wed Apr 05 19:30:02 2006 +0100 (2006-04-05)
parents 7086a4e96ce0
children be0a1f376223
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
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 if ( xpos >= COLUMNS )
204 put_newline();
205 video[(xpos + ypos * COLUMNS) * 2] = c & 0xFF;
206 video[(xpos + ypos * COLUMNS) * 2 + 1] = ATTRIBUTE;
207 ++xpos;
208 }
209 }
212 /*
213 * ********************************************************
214 * *************** ACCESS TO CONSOLE RING *****************
215 * ********************************************************
216 */
218 static void putchar_console_ring(int c)
219 {
220 conring[CONRING_IDX_MASK(conringp++)] = c;
221 if ( (conringp - conringc) > CONRING_SIZE )
222 conringc = conringp - CONRING_SIZE;
223 }
225 long read_console_ring(GUEST_HANDLE(char) str, u32 *pcount, int clear)
226 {
227 unsigned int idx, len, max, sofar, c;
228 unsigned long flags;
230 max = *pcount;
231 sofar = 0;
233 c = conringc;
234 while ( c != conringp )
235 {
236 idx = CONRING_IDX_MASK(c);
237 len = conringp - c;
238 if ( (idx + len) > CONRING_SIZE )
239 len = CONRING_SIZE - idx;
240 if ( (sofar + len) > max )
241 len = max - sofar;
242 if ( copy_to_guest_offset(str, sofar, &conring[idx], len) )
243 return -EFAULT;
244 sofar += len;
245 c += len;
246 }
248 if ( clear )
249 {
250 spin_lock_irqsave(&console_lock, flags);
251 conringc = conringp;
252 spin_unlock_irqrestore(&console_lock, flags);
253 }
255 *pcount = sofar;
256 return 0;
257 }
260 /*
261 * *******************************************************
262 * *************** ACCESS TO SERIAL LINE *****************
263 * *******************************************************
264 */
266 /* Characters received over the serial line are buffered for domain 0. */
267 #define SERIAL_RX_SIZE 128
268 #define SERIAL_RX_MASK(_i) ((_i)&(SERIAL_RX_SIZE-1))
269 static char serial_rx_ring[SERIAL_RX_SIZE];
270 static unsigned int serial_rx_cons, serial_rx_prod;
272 /* CTRL-<switch_char> switches input direction between Xen and DOM0. */
273 #define SWITCH_CODE (opt_conswitch[0]-'a'+1)
274 static int xen_rx = 1; /* FALSE => serial input passed to domain 0. */
276 static void switch_serial_input(void)
277 {
278 static char *input_str[2] = { "DOM0", "Xen" };
279 xen_rx = !xen_rx;
280 if ( SWITCH_CODE != 0 )
281 {
282 printk("*** Serial input -> %s "
283 "(type 'CTRL-%c' three times to switch input to %s).\n",
284 input_str[xen_rx], opt_conswitch[0], input_str[!xen_rx]);
285 }
286 }
288 static void __serial_rx(char c, struct cpu_user_regs *regs)
289 {
290 if ( xen_rx )
291 return handle_keypress(c, regs);
293 /* Deliver input to guest buffer, unless it is already full. */
294 if ( (serial_rx_prod-serial_rx_cons) != SERIAL_RX_SIZE )
295 serial_rx_ring[SERIAL_RX_MASK(serial_rx_prod++)] = c;
296 /* Always notify the guest: prevents receive path from getting stuck. */
297 send_guest_global_virq(dom0, VIRQ_CONSOLE);
298 }
300 static void serial_rx(char c, struct cpu_user_regs *regs)
301 {
302 static int switch_code_count = 0;
304 if ( (SWITCH_CODE != 0) && (c == SWITCH_CODE) )
305 {
306 /* We eat CTRL-<switch_char> in groups of 3 to switch console input. */
307 if ( ++switch_code_count == 3 )
308 {
309 switch_serial_input();
310 switch_code_count = 0;
311 return;
312 }
313 }
314 else
315 {
316 switch_code_count = 0;
317 }
319 /* Finally process the just-received character. */
320 __serial_rx(c, regs);
321 }
323 static long guest_console_write(GUEST_HANDLE(char) buffer, int count)
324 {
325 char kbuf[128], *kptr;
326 int kcount;
328 while ( count > 0 )
329 {
330 while ( serial_tx_space(sercon_handle) < (SERIAL_TXBUFSZ / 2) )
331 {
332 if ( hypercall_preempt_check() )
333 break;
334 cpu_relax();
335 }
337 if ( hypercall_preempt_check() )
338 return hypercall_create_continuation(
339 __HYPERVISOR_console_io, "iih",
340 CONSOLEIO_write, count, buffer);
342 kcount = min_t(int, count, sizeof(kbuf)-1);
343 if ( copy_from_guest((char *)kbuf, buffer, kcount) )
344 return -EFAULT;
345 kbuf[kcount] = '\0';
347 serial_puts(sercon_handle, kbuf);
349 for ( kptr = kbuf; *kptr != '\0'; kptr++ )
350 putchar_console(*kptr);
352 guest_handle_add_offset(buffer, kcount);
353 count -= kcount;
354 }
356 return 0;
357 }
359 long do_console_io(int cmd, int count, GUEST_HANDLE(char) buffer)
360 {
361 long rc;
362 unsigned int idx, len;
364 #ifndef VERBOSE
365 /* Only domain 0 may access the emergency console. */
366 if ( current->domain->domain_id != 0 )
367 return -EPERM;
368 #endif
370 switch ( cmd )
371 {
372 case CONSOLEIO_write:
373 rc = guest_console_write(buffer, count);
374 break;
375 case CONSOLEIO_read:
376 rc = 0;
377 while ( (serial_rx_cons != serial_rx_prod) && (rc < count) )
378 {
379 idx = SERIAL_RX_MASK(serial_rx_cons);
380 len = serial_rx_prod - serial_rx_cons;
381 if ( (idx + len) > SERIAL_RX_SIZE )
382 len = SERIAL_RX_SIZE - idx;
383 if ( (rc + len) > count )
384 len = count - rc;
385 if ( copy_to_guest_offset(buffer, rc, &serial_rx_ring[idx], len) )
386 {
387 rc = -EFAULT;
388 break;
389 }
390 rc += len;
391 serial_rx_cons += len;
392 }
393 break;
394 default:
395 rc = -ENOSYS;
396 break;
397 }
399 return rc;
400 }
403 /*
404 * *****************************************************
405 * *************** GENERIC CONSOLE I/O *****************
406 * *****************************************************
407 */
409 static inline void __putstr(const char *str)
410 {
411 int c;
413 serial_puts(sercon_handle, str);
415 while ( (c = *str++) != '\0' )
416 {
417 putchar_console(c);
418 putchar_console_ring(c);
419 }
420 }
422 void printf(const char *fmt, ...)
423 {
424 static char buf[1024];
425 static int start_of_line = 1;
427 va_list args;
428 char *p, *q;
429 unsigned long flags;
431 spin_lock_irqsave(&console_lock, flags);
433 va_start(args, fmt);
434 (void)vsnprintf(buf, sizeof(buf), fmt, args);
435 va_end(args);
437 p = buf;
438 while ( (q = strchr(p, '\n')) != NULL )
439 {
440 *q = '\0';
441 if ( start_of_line )
442 __putstr(printk_prefix);
443 __putstr(p);
444 __putstr("\n");
445 start_of_line = 1;
446 p = q + 1;
447 }
449 if ( *p != '\0' )
450 {
451 if ( start_of_line )
452 __putstr(printk_prefix);
453 __putstr(p);
454 start_of_line = 0;
455 }
457 spin_unlock_irqrestore(&console_lock, flags);
458 }
460 void set_printk_prefix(const char *prefix)
461 {
462 strcpy(printk_prefix, prefix);
463 }
465 void init_console(void)
466 {
467 char *p;
469 /* Where should console output go? */
470 for ( p = opt_console; p != NULL; p = strchr(p, ',') )
471 {
472 if ( *p == ',' )
473 p++;
474 if ( strncmp(p, "com", 3) == 0 )
475 sercon_handle = serial_parse_handle(p);
476 else if ( strncmp(p, "vga", 3) == 0 )
477 vgacon_enabled = 1;
478 }
480 init_vga();
482 serial_set_rx_handler(sercon_handle, serial_rx);
484 /* HELLO WORLD --- start-of-day banner text. */
485 printk(XEN_BANNER);
486 printk(" http://www.cl.cam.ac.uk/netos/xen\n");
487 printk(" University of Cambridge Computer Laboratory\n\n");
488 printk(" Xen version %d.%d%s (%s@%s) (%s) %s\n",
489 XEN_VERSION, XEN_SUBVERSION, XEN_EXTRAVERSION,
490 XEN_COMPILE_BY, XEN_COMPILE_DOMAIN,
491 XEN_COMPILER, XEN_COMPILE_DATE);
492 printk(" Latest ChangeSet: %s\n\n", XEN_CHANGESET);
493 set_printk_prefix("(XEN) ");
495 if ( opt_sync_console )
496 {
497 serial_start_sync(sercon_handle);
498 printk("Console output is synchronous.\n");
499 }
500 }
502 void console_endboot(int disable_vga)
503 {
504 if ( disable_vga )
505 vgacon_enabled = 0;
507 /*
508 * If user specifies so, we fool the switch routine to redirect input
509 * straight back to Xen. I use this convoluted method so we still print
510 * a useful 'how to switch' message.
511 */
512 if ( opt_conswitch[1] == 'x' )
513 xen_rx = !xen_rx;
515 /* Serial input is directed to DOM0 by default. */
516 switch_serial_input();
517 }
519 void console_force_unlock(void)
520 {
521 console_lock = SPIN_LOCK_UNLOCKED;
522 serial_force_unlock(sercon_handle);
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 */