ia64/xen-unstable

view xen/drivers/char/console.c @ 19107:696351cde9a4

Allow memflags to be specified to alloc_xenheap_pages().

Signed-off-by: Keir Fraser <keir.fraser@citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Wed Jan 28 16:58:41 2009 +0000 (2009-01-28)
parents 4ccee1299bc7
children 3fd8f9b34941
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 *
8 * Added printf_ratelimit
9 * Taken from Linux - Author: Andi Kleen (net_ratelimit)
10 * Ported to Xen - Steven Rostedt - Red Hat
11 */
13 #include <xen/stdarg.h>
14 #include <xen/config.h>
15 #include <xen/version.h>
16 #include <xen/init.h>
17 #include <xen/lib.h>
18 #include <xen/errno.h>
19 #include <xen/event.h>
20 #include <xen/spinlock.h>
21 #include <xen/console.h>
22 #include <xen/serial.h>
23 #include <xen/softirq.h>
24 #include <xen/keyhandler.h>
25 #include <xen/mm.h>
26 #include <xen/delay.h>
27 #include <xen/guest_access.h>
28 #include <xen/shutdown.h>
29 #include <xen/vga.h>
30 #include <xen/kexec.h>
31 #include <asm/current.h>
32 #include <asm/debugger.h>
33 #include <asm/io.h>
34 #include <asm/div64.h>
35 #include <xsm/xsm.h>
36 #include <public/sysctl.h>
38 /* console: comma-separated list of console outputs. */
39 static char opt_console[30] = OPT_CONSOLE_STR;
40 string_param("console", opt_console);
42 /* conswitch: a character pair controlling console switching. */
43 /* Char 1: CTRL+<char1> is used to switch console input between Xen and DOM0 */
44 /* Char 2: If this character is 'x', then do not auto-switch to DOM0 when it */
45 /* boots. Any other value, or omitting the char, enables auto-switch */
46 static unsigned char opt_conswitch[3] = "a";
47 string_param("conswitch", opt_conswitch);
49 /* sync_console: force synchronous console output (useful for debugging). */
50 static int opt_sync_console;
51 boolean_param("sync_console", opt_sync_console);
53 /* console_to_ring: send guest (incl. dom 0) console data to console ring. */
54 static int opt_console_to_ring;
55 boolean_param("console_to_ring", opt_console_to_ring);
57 /* console_timestamps: include a timestamp prefix on every Xen console line. */
58 static int opt_console_timestamps;
59 boolean_param("console_timestamps", opt_console_timestamps);
61 #define CONRING_SIZE 16384
62 #define CONRING_IDX_MASK(i) ((i)&(CONRING_SIZE-1))
63 static char conring[CONRING_SIZE];
64 static uint32_t conringc, conringp;
66 static int sercon_handle = -1;
68 static DEFINE_SPINLOCK(console_lock);
70 /*
71 * To control the amount of printing, thresholds are added.
72 * These thresholds correspond to the XENLOG logging levels.
73 * There's an upper and lower threshold for non-guest messages and for
74 * guest-provoked messages. This works as follows, for a given log level L:
75 *
76 * L < lower_threshold : always logged
77 * lower_threshold <= L < upper_threshold : rate-limited logging
78 * upper_threshold <= L : never logged
79 *
80 * Note, in the above algorithm, to disable rate limiting simply make
81 * the lower threshold equal to the upper.
82 */
83 #ifdef NDEBUG
84 #define XENLOG_UPPER_THRESHOLD 2 /* Do not print INFO and DEBUG */
85 #define XENLOG_LOWER_THRESHOLD 2 /* Always print ERR and WARNING */
86 #define XENLOG_GUEST_UPPER_THRESHOLD 2 /* Do not print INFO and DEBUG */
87 #define XENLOG_GUEST_LOWER_THRESHOLD 0 /* Rate-limit ERR and WARNING */
88 #else
89 #define XENLOG_UPPER_THRESHOLD 4 /* Do not discard anything */
90 #define XENLOG_LOWER_THRESHOLD 4 /* Print everything */
91 #define XENLOG_GUEST_UPPER_THRESHOLD 4 /* Do not discard anything */
92 #define XENLOG_GUEST_LOWER_THRESHOLD 4 /* Print everything */
93 #endif
94 /*
95 * The XENLOG_DEFAULT is the default given to printks that
96 * do not have any print level associated with them.
97 */
98 #define XENLOG_DEFAULT 1 /* XENLOG_WARNING */
99 #define XENLOG_GUEST_DEFAULT 1 /* XENLOG_WARNING */
101 static int xenlog_upper_thresh = XENLOG_UPPER_THRESHOLD;
102 static int xenlog_lower_thresh = XENLOG_LOWER_THRESHOLD;
103 static int xenlog_guest_upper_thresh = XENLOG_GUEST_UPPER_THRESHOLD;
104 static int xenlog_guest_lower_thresh = XENLOG_GUEST_LOWER_THRESHOLD;
106 static void parse_loglvl(char *s);
107 static void parse_guest_loglvl(char *s);
109 /*
110 * <lvl> := none|error|warning|info|debug|all
111 * loglvl=<lvl_print_always>[/<lvl_print_ratelimit>]
112 * <lvl_print_always>: log level which is always printed
113 * <lvl_print_rlimit>: log level which is rate-limit printed
114 * Similar definitions for guest_loglvl, but applies to guest tracing.
115 * Defaults: loglvl=warning ; guest_loglvl=none/warning
116 */
117 custom_param("loglvl", parse_loglvl);
118 custom_param("guest_loglvl", parse_guest_loglvl);
120 static atomic_t print_everything = ATOMIC_INIT(0);
122 #define ___parse_loglvl(s, ps, lvlstr, lvlnum) \
123 if ( !strncmp((s), (lvlstr), strlen(lvlstr)) ) { \
124 *(ps) = (s) + strlen(lvlstr); \
125 return (lvlnum); \
126 }
128 static int __init __parse_loglvl(char *s, char **ps)
129 {
130 ___parse_loglvl(s, ps, "none", 0);
131 ___parse_loglvl(s, ps, "error", 1);
132 ___parse_loglvl(s, ps, "warning", 2);
133 ___parse_loglvl(s, ps, "info", 3);
134 ___parse_loglvl(s, ps, "debug", 4);
135 ___parse_loglvl(s, ps, "all", 4);
136 return 2; /* sane fallback */
137 }
139 static void __init _parse_loglvl(char *s, int *lower, int *upper)
140 {
141 *lower = *upper = __parse_loglvl(s, &s);
142 if ( *s == '/' )
143 *upper = __parse_loglvl(s+1, &s);
144 if ( *upper < *lower )
145 *upper = *lower;
146 }
148 static void __init parse_loglvl(char *s)
149 {
150 _parse_loglvl(s, &xenlog_lower_thresh, &xenlog_upper_thresh);
151 }
153 static void __init parse_guest_loglvl(char *s)
154 {
155 _parse_loglvl(s, &xenlog_guest_lower_thresh, &xenlog_guest_upper_thresh);
156 }
158 static char * __init loglvl_str(int lvl)
159 {
160 switch ( lvl )
161 {
162 case 0: return "Nothing";
163 case 1: return "Errors";
164 case 2: return "Errors and warnings";
165 case 3: return "Errors, warnings and info";
166 case 4: return "All";
167 }
168 return "???";
169 }
171 /*
172 * ********************************************************
173 * *************** ACCESS TO CONSOLE RING *****************
174 * ********************************************************
175 */
177 static void putchar_console_ring(int c)
178 {
179 ASSERT(spin_is_locked(&console_lock));
180 conring[CONRING_IDX_MASK(conringp++)] = c;
181 if ( (uint32_t)(conringp - conringc) > CONRING_SIZE )
182 conringc = conringp - CONRING_SIZE;
183 }
185 long read_console_ring(struct xen_sysctl_readconsole *op)
186 {
187 XEN_GUEST_HANDLE(char) str;
188 uint32_t idx, len, max, sofar, c;
190 str = guest_handle_cast(op->buffer, char),
191 max = op->count;
192 sofar = 0;
194 c = conringc;
195 if ( op->incremental && ((int32_t)(op->index - c) < 0) )
196 c = op->index;
198 while ( (c != conringp) && (sofar < max) )
199 {
200 idx = CONRING_IDX_MASK(c);
201 len = conringp - c;
202 if ( (idx + len) > CONRING_SIZE )
203 len = CONRING_SIZE - idx;
204 if ( (sofar + len) > max )
205 len = max - sofar;
206 if ( copy_to_guest_offset(str, sofar, &conring[idx], len) )
207 return -EFAULT;
208 sofar += len;
209 c += len;
210 }
212 if ( op->clear )
213 {
214 spin_lock_irq(&console_lock);
215 if ( (uint32_t)(conringp - c) > CONRING_SIZE )
216 conringc = conringp - CONRING_SIZE;
217 else
218 conringc = c;
219 spin_unlock_irq(&console_lock);
220 }
222 op->count = sofar;
223 op->index = c;
225 return 0;
226 }
229 /*
230 * *******************************************************
231 * *************** ACCESS TO SERIAL LINE *****************
232 * *******************************************************
233 */
235 /* Characters received over the serial line are buffered for domain 0. */
236 #define SERIAL_RX_SIZE 128
237 #define SERIAL_RX_MASK(_i) ((_i)&(SERIAL_RX_SIZE-1))
238 static char serial_rx_ring[SERIAL_RX_SIZE];
239 static unsigned int serial_rx_cons, serial_rx_prod;
241 static void (*serial_steal_fn)(const char *);
243 int console_steal(int handle, void (*fn)(const char *))
244 {
245 if ( (handle == -1) || (handle != sercon_handle) )
246 return 0;
248 if ( serial_steal_fn != NULL )
249 return -EBUSY;
251 serial_steal_fn = fn;
252 return 1;
253 }
255 void console_giveback(int id)
256 {
257 if ( id == 1 )
258 serial_steal_fn = NULL;
259 }
261 static void sercon_puts(const char *s)
262 {
263 if ( serial_steal_fn != NULL )
264 (*serial_steal_fn)(s);
265 else
266 serial_puts(sercon_handle, s);
267 }
269 /* CTRL-<switch_char> switches input direction between Xen and DOM0. */
270 #define switch_code (opt_conswitch[0]-'a'+1)
271 static int xen_rx = 1; /* FALSE => serial input passed to domain 0. */
273 static void switch_serial_input(void)
274 {
275 static char *input_str[2] = { "DOM0", "Xen" };
276 xen_rx = !xen_rx;
277 printk("*** Serial input -> %s", input_str[xen_rx]);
278 if ( switch_code )
279 printk(" (type 'CTRL-%c' three times to switch input to %s)",
280 opt_conswitch[0], input_str[!xen_rx]);
281 printk("\n");
282 }
284 static void __serial_rx(char c, struct cpu_user_regs *regs)
285 {
286 if ( xen_rx )
287 return handle_keypress(c, regs);
289 /* Deliver input to guest buffer, unless it is already full. */
290 if ( (serial_rx_prod-serial_rx_cons) != SERIAL_RX_SIZE )
291 serial_rx_ring[SERIAL_RX_MASK(serial_rx_prod++)] = c;
292 /* Always notify the guest: prevents receive path from getting stuck. */
293 send_guest_global_virq(dom0, VIRQ_CONSOLE);
294 }
296 static void serial_rx(char c, struct cpu_user_regs *regs)
297 {
298 static int switch_code_count = 0;
300 if ( switch_code && (c == switch_code) )
301 {
302 /* We eat CTRL-<switch_char> in groups of 3 to switch console input. */
303 if ( ++switch_code_count == 3 )
304 {
305 switch_serial_input();
306 switch_code_count = 0;
307 }
308 return;
309 }
311 for ( ; switch_code_count != 0; switch_code_count-- )
312 __serial_rx(switch_code, regs);
314 /* Finally process the just-received character. */
315 __serial_rx(c, regs);
316 }
318 static long guest_console_write(XEN_GUEST_HANDLE(char) buffer, int count)
319 {
320 char kbuf[128], *kptr;
321 int kcount;
323 while ( count > 0 )
324 {
325 while ( serial_tx_space(sercon_handle) < (serial_txbufsz / 2) )
326 {
327 if ( hypercall_preempt_check() )
328 break;
329 cpu_relax();
330 }
332 if ( hypercall_preempt_check() )
333 return hypercall_create_continuation(
334 __HYPERVISOR_console_io, "iih",
335 CONSOLEIO_write, count, buffer);
337 kcount = min_t(int, count, sizeof(kbuf)-1);
338 if ( copy_from_guest(kbuf, buffer, kcount) )
339 return -EFAULT;
340 kbuf[kcount] = '\0';
342 spin_lock_irq(&console_lock);
344 sercon_puts(kbuf);
345 vga_puts(kbuf);
347 if ( opt_console_to_ring )
348 {
349 for ( kptr = kbuf; *kptr != '\0'; kptr++ )
350 putchar_console_ring(*kptr);
351 send_guest_global_virq(dom0, VIRQ_CON_RING);
352 }
354 spin_unlock_irq(&console_lock);
356 guest_handle_add_offset(buffer, kcount);
357 count -= kcount;
358 }
360 return 0;
361 }
363 long do_console_io(int cmd, int count, XEN_GUEST_HANDLE(char) buffer)
364 {
365 long rc;
366 unsigned int idx, len;
368 #ifndef VERBOSE
369 /* Only domain 0 may access the emergency console. */
370 if ( current->domain->domain_id != 0 )
371 return -EPERM;
372 #endif
374 rc = xsm_console_io(current->domain, cmd);
375 if ( rc )
376 return rc;
378 switch ( cmd )
379 {
380 case CONSOLEIO_write:
381 rc = guest_console_write(buffer, count);
382 break;
383 case CONSOLEIO_read:
384 rc = 0;
385 while ( (serial_rx_cons != serial_rx_prod) && (rc < count) )
386 {
387 idx = SERIAL_RX_MASK(serial_rx_cons);
388 len = serial_rx_prod - serial_rx_cons;
389 if ( (idx + len) > SERIAL_RX_SIZE )
390 len = SERIAL_RX_SIZE - idx;
391 if ( (rc + len) > count )
392 len = count - rc;
393 if ( copy_to_guest_offset(buffer, rc, &serial_rx_ring[idx], len) )
394 {
395 rc = -EFAULT;
396 break;
397 }
398 rc += len;
399 serial_rx_cons += len;
400 }
401 break;
402 default:
403 rc = -ENOSYS;
404 break;
405 }
407 return rc;
408 }
411 /*
412 * *****************************************************
413 * *************** GENERIC CONSOLE I/O *****************
414 * *****************************************************
415 */
417 static void __putstr(const char *str)
418 {
419 int c;
421 ASSERT(spin_is_locked(&console_lock));
423 sercon_puts(str);
424 vga_puts(str);
426 while ( (c = *str++) != '\0' )
427 putchar_console_ring(c);
429 send_guest_global_virq(dom0, VIRQ_CON_RING);
430 }
432 static int printk_prefix_check(char *p, char **pp)
433 {
434 int loglvl = -1;
435 int upper_thresh = xenlog_upper_thresh;
436 int lower_thresh = xenlog_lower_thresh;
438 while ( (p[0] == '<') && (p[1] != '\0') && (p[2] == '>') )
439 {
440 switch ( p[1] )
441 {
442 case 'G':
443 upper_thresh = xenlog_guest_upper_thresh;
444 lower_thresh = xenlog_guest_lower_thresh;
445 if ( loglvl == -1 )
446 loglvl = XENLOG_GUEST_DEFAULT;
447 break;
448 case '0' ... '3':
449 loglvl = p[1] - '0';
450 break;
451 }
452 p += 3;
453 }
455 if ( loglvl == -1 )
456 loglvl = XENLOG_DEFAULT;
458 *pp = p;
460 return ((atomic_read(&print_everything) != 0) ||
461 (loglvl < lower_thresh) ||
462 ((loglvl < upper_thresh) && printk_ratelimit()));
463 }
465 static void printk_start_of_line(void)
466 {
467 struct tm tm;
468 char tstr[32];
470 __putstr("(XEN) ");
472 if ( !opt_console_timestamps )
473 return;
475 tm = wallclock_time();
476 if ( tm.tm_mday == 0 )
477 return;
479 snprintf(tstr, sizeof(tstr), "[%04u-%02u-%02u %02u:%02u:%02u] ",
480 1900 + tm.tm_year, tm.tm_mon + 1, tm.tm_mday,
481 tm.tm_hour, tm.tm_min, tm.tm_sec);
482 __putstr(tstr);
483 }
485 void printk(const char *fmt, ...)
486 {
487 static char buf[1024];
488 static int start_of_line = 1, do_print;
490 va_list args;
491 char *p, *q;
492 unsigned long flags;
494 /* console_lock can be acquired recursively from __printk_ratelimit(). */
495 local_irq_save(flags);
496 spin_lock_recursive(&console_lock);
498 va_start(args, fmt);
499 (void)vsnprintf(buf, sizeof(buf), fmt, args);
500 va_end(args);
502 p = buf;
504 while ( (q = strchr(p, '\n')) != NULL )
505 {
506 *q = '\0';
507 if ( start_of_line )
508 do_print = printk_prefix_check(p, &p);
509 if ( do_print )
510 {
511 if ( start_of_line )
512 printk_start_of_line();
513 __putstr(p);
514 __putstr("\n");
515 }
516 start_of_line = 1;
517 p = q + 1;
518 }
520 if ( *p != '\0' )
521 {
522 if ( start_of_line )
523 do_print = printk_prefix_check(p, &p);
524 if ( do_print )
525 {
526 if ( start_of_line )
527 printk_start_of_line();
528 __putstr(p);
529 }
530 start_of_line = 0;
531 }
533 spin_unlock_recursive(&console_lock);
534 local_irq_restore(flags);
535 }
537 void __init init_console(void)
538 {
539 char *p;
541 /* Where should console output go? */
542 for ( p = opt_console; p != NULL; p = strchr(p, ',') )
543 {
544 if ( *p == ',' )
545 p++;
546 if ( !strncmp(p, "vga", 3) )
547 vga_init();
548 else if ( strncmp(p, "com", 3) ||
549 (sercon_handle = serial_parse_handle(p)) == -1 )
550 {
551 char *q = strchr(p, ',');
552 if ( q != NULL )
553 *q = '\0';
554 printk("Bad console= option '%s'\n", p);
555 if ( q != NULL )
556 *q = ',';
557 }
558 }
560 serial_set_rx_handler(sercon_handle, serial_rx);
562 /* HELLO WORLD --- start-of-day banner text. */
563 spin_lock(&console_lock);
564 __putstr(xen_banner());
565 spin_unlock(&console_lock);
566 printk("Xen version %d.%d%s (%s@%s) (%s) %s\n",
567 xen_major_version(), xen_minor_version(), xen_extra_version(),
568 xen_compile_by(), xen_compile_domain(),
569 xen_compiler(), xen_compile_date());
570 printk("Latest ChangeSet: %s\n", xen_changeset());
572 if ( opt_sync_console )
573 {
574 serial_start_sync(sercon_handle);
575 add_taint(TAINT_SYNC_CONSOLE);
576 printk("Console output is synchronous.\n");
577 }
578 }
580 void __init console_endboot(void)
581 {
582 int i, j;
584 printk("Std. Loglevel: %s", loglvl_str(xenlog_lower_thresh));
585 if ( xenlog_upper_thresh != xenlog_lower_thresh )
586 printk(" (Rate-limited: %s)", loglvl_str(xenlog_upper_thresh));
587 printk("\nGuest Loglevel: %s", loglvl_str(xenlog_guest_lower_thresh));
588 if ( xenlog_guest_upper_thresh != xenlog_guest_lower_thresh )
589 printk(" (Rate-limited: %s)", loglvl_str(xenlog_guest_upper_thresh));
590 printk("\n");
592 if ( opt_sync_console )
593 {
594 printk("**********************************************\n");
595 printk("******* WARNING: CONSOLE OUTPUT IS SYNCHRONOUS\n");
596 printk("******* This option is intended to aid debugging "
597 "of Xen by ensuring\n");
598 printk("******* that all output is synchronously delivered "
599 "on the serial line.\n");
600 printk("******* However it can introduce SIGNIFICANT latencies "
601 "and affect\n");
602 printk("******* timekeeping. It is NOT recommended for "
603 "production use!\n");
604 printk("**********************************************\n");
605 for ( i = 0; i < 3; i++ )
606 {
607 printk("%d... ", 3-i);
608 for ( j = 0; j < 100; j++ )
609 {
610 process_pending_timers();
611 mdelay(10);
612 }
613 }
614 printk("\n");
615 }
617 vga_endboot();
619 /*
620 * If user specifies so, we fool the switch routine to redirect input
621 * straight back to Xen. I use this convoluted method so we still print
622 * a useful 'how to switch' message.
623 */
624 if ( opt_conswitch[1] == 'x' )
625 xen_rx = !xen_rx;
627 /* Serial input is directed to DOM0 by default. */
628 switch_serial_input();
629 }
631 int console_has(const char *device)
632 {
633 char *p;
635 for ( p = opt_console; p != NULL; p = strchr(p, ',') )
636 {
637 if ( *p == ',' )
638 p++;
639 if ( strncmp(p, device, strlen(device)) == 0 )
640 return 1;
641 }
643 return 0;
644 }
646 void console_start_log_everything(void)
647 {
648 serial_start_log_everything(sercon_handle);
649 atomic_inc(&print_everything);
650 }
652 void console_end_log_everything(void)
653 {
654 serial_end_log_everything(sercon_handle);
655 atomic_dec(&print_everything);
656 }
658 void console_force_unlock(void)
659 {
660 spin_lock_init(&console_lock);
661 serial_force_unlock(sercon_handle);
662 console_start_sync();
663 }
665 void console_force_lock(void)
666 {
667 spin_lock(&console_lock);
668 }
670 void console_start_sync(void)
671 {
672 atomic_inc(&print_everything);
673 serial_start_sync(sercon_handle);
674 }
676 void console_end_sync(void)
677 {
678 serial_end_sync(sercon_handle);
679 atomic_dec(&print_everything);
680 }
682 void console_putc(char c)
683 {
684 serial_putc(sercon_handle, c);
685 }
687 int console_getc(void)
688 {
689 return serial_getc(sercon_handle);
690 }
692 /*
693 * printk rate limiting, lifted from Linux.
694 *
695 * This enforces a rate limit: not more than one kernel message
696 * every printk_ratelimit_ms (millisecs).
697 */
698 int __printk_ratelimit(int ratelimit_ms, int ratelimit_burst)
699 {
700 static DEFINE_SPINLOCK(ratelimit_lock);
701 static unsigned long toks = 10 * 5 * 1000;
702 static unsigned long last_msg;
703 static int missed;
704 unsigned long flags;
705 unsigned long long now = NOW(); /* ns */
706 unsigned long ms;
708 do_div(now, 1000000);
709 ms = (unsigned long)now;
711 spin_lock_irqsave(&ratelimit_lock, flags);
712 toks += ms - last_msg;
713 last_msg = ms;
714 if ( toks > (ratelimit_burst * ratelimit_ms))
715 toks = ratelimit_burst * ratelimit_ms;
716 if ( toks >= ratelimit_ms )
717 {
718 int lost = missed;
719 missed = 0;
720 toks -= ratelimit_ms;
721 spin_unlock(&ratelimit_lock);
722 if ( lost )
723 {
724 char lost_str[8];
725 snprintf(lost_str, sizeof(lost_str), "%d", lost);
726 /* console_lock may already be acquired by printk(). */
727 spin_lock_recursive(&console_lock);
728 printk_start_of_line();
729 __putstr("printk: ");
730 __putstr(lost_str);
731 __putstr(" messages suppressed.\n");
732 spin_unlock_recursive(&console_lock);
733 }
734 local_irq_restore(flags);
735 return 1;
736 }
737 missed++;
738 spin_unlock_irqrestore(&ratelimit_lock, flags);
739 return 0;
740 }
742 /* minimum time in ms between messages */
743 int printk_ratelimit_ms = 5 * 1000;
745 /* number of messages we send before ratelimiting */
746 int printk_ratelimit_burst = 10;
748 int printk_ratelimit(void)
749 {
750 return __printk_ratelimit(printk_ratelimit_ms, printk_ratelimit_burst);
751 }
753 /*
754 * **************************************************************
755 * *************** Serial console ring buffer *******************
756 * **************************************************************
757 */
759 #ifdef DEBUG_TRACE_DUMP
761 /* Send output direct to console, or buffer it? */
762 static volatile int debugtrace_send_to_console;
764 static char *debugtrace_buf; /* Debug-trace buffer */
765 static unsigned int debugtrace_prd; /* Producer index */
766 static unsigned int debugtrace_kilobytes = 128, debugtrace_bytes;
767 static unsigned int debugtrace_used;
768 static DEFINE_SPINLOCK(debugtrace_lock);
769 integer_param("debugtrace", debugtrace_kilobytes);
771 static void debugtrace_dump_worker(void)
772 {
773 if ( (debugtrace_bytes == 0) || !debugtrace_used )
774 return;
776 printk("debugtrace_dump() starting\n");
778 /* Print oldest portion of the ring. */
779 ASSERT(debugtrace_buf[debugtrace_bytes - 1] == 0);
780 sercon_puts(&debugtrace_buf[debugtrace_prd]);
782 /* Print youngest portion of the ring. */
783 debugtrace_buf[debugtrace_prd] = '\0';
784 sercon_puts(&debugtrace_buf[0]);
786 memset(debugtrace_buf, '\0', debugtrace_bytes);
788 printk("debugtrace_dump() finished\n");
789 }
791 static void debugtrace_toggle(void)
792 {
793 unsigned long flags;
795 watchdog_disable();
796 spin_lock_irqsave(&debugtrace_lock, flags);
798 /*
799 * Dump the buffer *before* toggling, in case the act of dumping the
800 * buffer itself causes more printk() invocations.
801 */
802 printk("debugtrace_printk now writing to %s.\n",
803 !debugtrace_send_to_console ? "console": "buffer");
804 if ( !debugtrace_send_to_console )
805 debugtrace_dump_worker();
807 debugtrace_send_to_console = !debugtrace_send_to_console;
809 spin_unlock_irqrestore(&debugtrace_lock, flags);
810 watchdog_enable();
812 }
814 void debugtrace_dump(void)
815 {
816 unsigned long flags;
818 watchdog_disable();
819 spin_lock_irqsave(&debugtrace_lock, flags);
821 debugtrace_dump_worker();
823 spin_unlock_irqrestore(&debugtrace_lock, flags);
824 watchdog_enable();
825 }
827 void debugtrace_printk(const char *fmt, ...)
828 {
829 static char buf[1024];
830 static u32 count;
832 va_list args;
833 char *p;
834 unsigned long flags;
836 if ( debugtrace_bytes == 0 )
837 return;
839 debugtrace_used = 1;
841 spin_lock_irqsave(&debugtrace_lock, flags);
843 ASSERT(debugtrace_buf[debugtrace_bytes - 1] == 0);
845 snprintf(buf, sizeof(buf), "%u ", ++count);
847 va_start(args, fmt);
848 (void)vsnprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), fmt, args);
849 va_end(args);
851 if ( debugtrace_send_to_console )
852 {
853 serial_puts(sercon_handle, buf);
854 }
855 else
856 {
857 for ( p = buf; *p != '\0'; p++ )
858 {
859 debugtrace_buf[debugtrace_prd++] = *p;
860 /* Always leave a nul byte at the end of the buffer. */
861 if ( debugtrace_prd == (debugtrace_bytes - 1) )
862 debugtrace_prd = 0;
863 }
864 }
866 spin_unlock_irqrestore(&debugtrace_lock, flags);
867 }
869 static void debugtrace_key(unsigned char key)
870 {
871 debugtrace_toggle();
872 }
874 static int __init debugtrace_init(void)
875 {
876 int order;
877 unsigned int kbytes, bytes;
879 /* Round size down to next power of two. */
880 while ( (kbytes = (debugtrace_kilobytes & (debugtrace_kilobytes-1))) != 0 )
881 debugtrace_kilobytes = kbytes;
883 bytes = debugtrace_kilobytes << 10;
884 if ( bytes == 0 )
885 return 0;
887 order = get_order_from_bytes(bytes);
888 debugtrace_buf = alloc_xenheap_pages(order, 0);
889 ASSERT(debugtrace_buf != NULL);
891 memset(debugtrace_buf, '\0', bytes);
893 debugtrace_bytes = bytes;
895 register_keyhandler(
896 'T', debugtrace_key, "toggle debugtrace to console/buffer");
898 return 0;
899 }
900 __initcall(debugtrace_init);
902 #endif /* !NDEBUG */
905 /*
906 * **************************************************************
907 * *************** Debugging/tracing/error-report ***************
908 * **************************************************************
909 */
911 void panic(const char *fmt, ...)
912 {
913 va_list args;
914 unsigned long flags;
915 static DEFINE_SPINLOCK(lock);
916 static char buf[128];
918 debugtrace_dump();
920 /* Protects buf[] and ensure multi-line message prints atomically. */
921 spin_lock_irqsave(&lock, flags);
923 va_start(args, fmt);
924 (void)vsnprintf(buf, sizeof(buf), fmt, args);
925 va_end(args);
927 console_start_sync();
928 printk("\n****************************************\n");
929 printk("Panic on CPU %d:\n", smp_processor_id());
930 printk("%s", buf);
931 printk("****************************************\n\n");
932 if ( opt_noreboot )
933 printk("Manual reset required ('noreboot' specified)\n");
934 else
935 printk("Reboot in five seconds...\n");
937 spin_unlock_irqrestore(&lock, flags);
939 debugger_trap_immediate();
941 kexec_crash();
943 if ( opt_noreboot )
944 {
945 machine_halt();
946 }
947 else
948 {
949 watchdog_disable();
950 machine_restart(5000);
951 }
952 }
954 void __bug(char *file, int line)
955 {
956 console_start_sync();
957 printk("Xen BUG at %s:%d\n", file, line);
958 dump_execution_state();
959 panic("Xen BUG at %s:%d\n", file, line);
960 for ( ; ; ) ;
961 }
963 void __warn(char *file, int line)
964 {
965 printk("Xen WARN at %s:%d\n", file, line);
966 dump_execution_state();
967 }
970 /*
971 * **************************************************************
972 * ****************** Console suspend/resume ********************
973 * **************************************************************
974 */
976 static void suspend_steal_fn(const char *str) { }
977 static int suspend_steal_id;
979 int console_suspend(void)
980 {
981 suspend_steal_id = console_steal(sercon_handle, suspend_steal_fn);
982 serial_suspend();
983 return 0;
984 }
986 int console_resume(void)
987 {
988 serial_resume();
989 console_giveback(suspend_steal_id);
990 return 0;
991 }
993 /*
994 * Local variables:
995 * mode: C
996 * c-set-style: "BSD"
997 * c-basic-offset: 4
998 * tab-width: 4
999 * indent-tabs-mode: nil
1000 * End:
1001 */