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 */
12
13 #include <xen/version.h>
14 #include <xen/lib.h>
15 #include <xen/init.h>
16 #include <xen/event.h>
17 #include <xen/console.h>
18 #include <xen/serial.h>
19 #include <xen/softirq.h>
20 #include <xen/keyhandler.h>
21 #include <xen/guest_access.h>
22 #include <xen/watchdog.h>
23 #include <xen/shutdown.h>
24 #include <xen/video.h>
25 #include <xen/kexec.h>
26 #include <xen/ctype.h>
27 #include <xen/warning.h>
28 #include <asm/debugger.h>
29 #include <asm/div64.h>
30 #include <xen/hypercall.h> /* for do_console_io */
31 #include <xen/early_printk.h>
32 #include <xen/warning.h>
33
34 #ifdef CONFIG_X86
35 #include <xen/consoled.h>
36 #include <xen/pv_console.h>
37 #include <asm/guest.h>
38 #endif
39
40 /* console: comma-separated list of console outputs. */
41 static char __initdata opt_console[30] = OPT_CONSOLE_STR;
42 string_param("console", opt_console);
43
44 /* conswitch: a character pair controlling console switching. */
45 /* Char 1: CTRL+<char1> is used to switch console input between Xen and DOM0 */
46 /* Char 2: If this character is 'x', then do not auto-switch to DOM0 when it */
47 /* boots. Any other value, or omitting the char, enables auto-switch */
48 static unsigned char __read_mostly opt_conswitch[3] = "a";
49 string_runtime_param("conswitch", opt_conswitch);
50
51 /* sync_console: force synchronous console output (useful for debugging). */
52 static bool_t __initdata opt_sync_console;
53 boolean_param("sync_console", opt_sync_console);
54 static const char __initconst warning_sync_console[] =
55 "WARNING: CONSOLE OUTPUT IS SYNCHRONOUS\n"
56 "This option is intended to aid debugging of Xen by ensuring\n"
57 "that all output is synchronously delivered on the serial line.\n"
58 "However it can introduce SIGNIFICANT latencies and affect\n"
59 "timekeeping. It is NOT recommended for production use!\n";
60
61 /* console_to_ring: send guest (incl. dom 0) console data to console ring. */
62 static bool_t __read_mostly opt_console_to_ring;
63 boolean_param("console_to_ring", opt_console_to_ring);
64
65 /* console_timestamps: include a timestamp prefix on every Xen console line. */
66 enum con_timestamp_mode
67 {
68 TSM_NONE, /* No timestamps */
69 TSM_DATE, /* [YYYY-MM-DD HH:MM:SS] */
70 TSM_DATE_MS, /* [YYYY-MM-DD HH:MM:SS.mmm] */
71 TSM_BOOT /* [SSSSSS.uuuuuu] */
72 };
73
74 static enum con_timestamp_mode __read_mostly opt_con_timestamp_mode = TSM_NONE;
75
76 static int parse_console_timestamps(const char *s);
77 custom_runtime_param("console_timestamps", parse_console_timestamps);
78
79 /* conring_size: allows a large console ring than default (16kB). */
80 static uint32_t __initdata opt_conring_size;
81 size_param("conring_size", opt_conring_size);
82
83 #define _CONRING_SIZE 16384
84 #define CONRING_IDX_MASK(i) ((i)&(conring_size-1))
85 static char __initdata _conring[_CONRING_SIZE];
86 static char *__read_mostly conring = _conring;
87 static uint32_t __read_mostly conring_size = _CONRING_SIZE;
88 static uint32_t conringc, conringp;
89
90 static int __read_mostly sercon_handle = -1;
91
92 #ifdef CONFIG_X86
93 static bool __read_mostly opt_console_xen; /* console=xen */
94 #endif
95
96 static DEFINE_SPINLOCK(console_lock);
97
98 /*
99 * To control the amount of printing, thresholds are added.
100 * These thresholds correspond to the XENLOG logging levels.
101 * There's an upper and lower threshold for non-guest messages and for
102 * guest-provoked messages. This works as follows, for a given log level L:
103 *
104 * L < lower_threshold : always logged
105 * lower_threshold <= L < upper_threshold : rate-limited logging
106 * upper_threshold <= L : never logged
107 *
108 * Note, in the above algorithm, to disable rate limiting simply make
109 * the lower threshold equal to the upper.
110 */
111 #ifdef NDEBUG
112 #define XENLOG_UPPER_THRESHOLD 2 /* Do not print INFO and DEBUG */
113 #define XENLOG_LOWER_THRESHOLD 2 /* Always print ERR and WARNING */
114 #define XENLOG_GUEST_UPPER_THRESHOLD 2 /* Do not print INFO and DEBUG */
115 #define XENLOG_GUEST_LOWER_THRESHOLD 0 /* Rate-limit ERR and WARNING */
116 #else
117 #define XENLOG_UPPER_THRESHOLD 4 /* Do not discard anything */
118 #define XENLOG_LOWER_THRESHOLD 4 /* Print everything */
119 #define XENLOG_GUEST_UPPER_THRESHOLD 4 /* Do not discard anything */
120 #define XENLOG_GUEST_LOWER_THRESHOLD 4 /* Print everything */
121 #endif
122 /*
123 * The XENLOG_DEFAULT is the default given to printks that
124 * do not have any print level associated with them.
125 */
126 #define XENLOG_DEFAULT 1 /* XENLOG_WARNING */
127 #define XENLOG_GUEST_DEFAULT 1 /* XENLOG_WARNING */
128
129 static int __read_mostly xenlog_upper_thresh = XENLOG_UPPER_THRESHOLD;
130 static int __read_mostly xenlog_lower_thresh = XENLOG_LOWER_THRESHOLD;
131 static int __read_mostly xenlog_guest_upper_thresh =
132 XENLOG_GUEST_UPPER_THRESHOLD;
133 static int __read_mostly xenlog_guest_lower_thresh =
134 XENLOG_GUEST_LOWER_THRESHOLD;
135
136 static int parse_loglvl(const char *s);
137 static int parse_guest_loglvl(const char *s);
138
139 /*
140 * <lvl> := none|error|warning|info|debug|all
141 * loglvl=<lvl_print_always>[/<lvl_print_ratelimit>]
142 * <lvl_print_always>: log level which is always printed
143 * <lvl_print_rlimit>: log level which is rate-limit printed
144 * Similar definitions for guest_loglvl, but applies to guest tracing.
145 * Defaults: loglvl=warning ; guest_loglvl=none/warning
146 */
147 custom_runtime_param("loglvl", parse_loglvl);
148 custom_runtime_param("guest_loglvl", parse_guest_loglvl);
149
150 static atomic_t print_everything = ATOMIC_INIT(0);
151
152 #define ___parse_loglvl(s, ps, lvlstr, lvlnum) \
153 if ( !strncmp((s), (lvlstr), strlen(lvlstr)) ) { \
154 *(ps) = (s) + strlen(lvlstr); \
155 return (lvlnum); \
156 }
157
__parse_loglvl(const char * s,const char ** ps)158 static int __parse_loglvl(const char *s, const char **ps)
159 {
160 ___parse_loglvl(s, ps, "none", 0);
161 ___parse_loglvl(s, ps, "error", 1);
162 ___parse_loglvl(s, ps, "warning", 2);
163 ___parse_loglvl(s, ps, "info", 3);
164 ___parse_loglvl(s, ps, "debug", 4);
165 ___parse_loglvl(s, ps, "all", 4);
166 return 2; /* sane fallback */
167 }
168
_parse_loglvl(const char * s,int * lower,int * upper)169 static int _parse_loglvl(const char *s, int *lower, int *upper)
170 {
171 *lower = *upper = __parse_loglvl(s, &s);
172 if ( *s == '/' )
173 *upper = __parse_loglvl(s+1, &s);
174 if ( *upper < *lower )
175 *upper = *lower;
176
177 return *s ? -EINVAL : 0;
178 }
179
parse_loglvl(const char * s)180 static int parse_loglvl(const char *s)
181 {
182 return _parse_loglvl(s, &xenlog_lower_thresh, &xenlog_upper_thresh);
183 }
184
parse_guest_loglvl(const char * s)185 static int parse_guest_loglvl(const char *s)
186 {
187 return _parse_loglvl(s, &xenlog_guest_lower_thresh,
188 &xenlog_guest_upper_thresh);
189 }
190
loglvl_str(int lvl)191 static char *loglvl_str(int lvl)
192 {
193 switch ( lvl )
194 {
195 case 0: return "Nothing";
196 case 1: return "Errors";
197 case 2: return "Errors and warnings";
198 case 3: return "Errors, warnings and info";
199 case 4: return "All";
200 }
201 return "???";
202 }
203
204 static int *__read_mostly upper_thresh_adj = &xenlog_upper_thresh;
205 static int *__read_mostly lower_thresh_adj = &xenlog_lower_thresh;
206 static const char *__read_mostly thresh_adj = "standard";
207
do_toggle_guest(unsigned char key,struct cpu_user_regs * regs)208 static void do_toggle_guest(unsigned char key, struct cpu_user_regs *regs)
209 {
210 if ( upper_thresh_adj == &xenlog_upper_thresh )
211 {
212 upper_thresh_adj = &xenlog_guest_upper_thresh;
213 lower_thresh_adj = &xenlog_guest_lower_thresh;
214 thresh_adj = "guest";
215 }
216 else
217 {
218 upper_thresh_adj = &xenlog_upper_thresh;
219 lower_thresh_adj = &xenlog_lower_thresh;
220 thresh_adj = "standard";
221 }
222 printk("'%c' pressed -> %s log level adjustments enabled\n",
223 key, thresh_adj);
224 }
225
do_adj_thresh(unsigned char key)226 static void do_adj_thresh(unsigned char key)
227 {
228 if ( *upper_thresh_adj < *lower_thresh_adj )
229 *upper_thresh_adj = *lower_thresh_adj;
230 printk("'%c' pressed -> %s log level: %s (rate limited %s)\n",
231 key, thresh_adj, loglvl_str(*lower_thresh_adj),
232 loglvl_str(*upper_thresh_adj));
233 }
234
do_inc_thresh(unsigned char key,struct cpu_user_regs * regs)235 static void do_inc_thresh(unsigned char key, struct cpu_user_regs *regs)
236 {
237 ++*lower_thresh_adj;
238 do_adj_thresh(key);
239 }
240
do_dec_thresh(unsigned char key,struct cpu_user_regs * regs)241 static void do_dec_thresh(unsigned char key, struct cpu_user_regs *regs)
242 {
243 if ( *lower_thresh_adj )
244 --*lower_thresh_adj;
245 do_adj_thresh(key);
246 }
247
248 /*
249 * ********************************************************
250 * *************** ACCESS TO CONSOLE RING *****************
251 * ********************************************************
252 */
253
conring_puts(const char * str)254 static void conring_puts(const char *str)
255 {
256 char c;
257
258 ASSERT(spin_is_locked(&console_lock));
259
260 while ( (c = *str++) != '\0' )
261 conring[CONRING_IDX_MASK(conringp++)] = c;
262
263 if ( (uint32_t)(conringp - conringc) > conring_size )
264 conringc = conringp - conring_size;
265 }
266
read_console_ring(struct xen_sysctl_readconsole * op)267 long read_console_ring(struct xen_sysctl_readconsole *op)
268 {
269 XEN_GUEST_HANDLE_PARAM(char) str;
270 uint32_t idx, len, max, sofar, c, p;
271
272 str = guest_handle_cast(op->buffer, char),
273 max = op->count;
274 sofar = 0;
275
276 c = read_atomic(&conringc);
277 p = read_atomic(&conringp);
278 if ( op->incremental &&
279 (c <= p ? c < op->index && op->index <= p
280 : c < op->index || op->index <= p) )
281 c = op->index;
282
283 while ( (c != p) && (sofar < max) )
284 {
285 idx = CONRING_IDX_MASK(c);
286 len = p - c;
287 if ( (idx + len) > conring_size )
288 len = conring_size - idx;
289 if ( (sofar + len) > max )
290 len = max - sofar;
291 if ( copy_to_guest_offset(str, sofar, &conring[idx], len) )
292 return -EFAULT;
293 sofar += len;
294 c += len;
295 }
296
297 if ( op->clear )
298 {
299 spin_lock_irq(&console_lock);
300 conringc = p - c > conring_size ? p - conring_size : c;
301 spin_unlock_irq(&console_lock);
302 }
303
304 op->count = sofar;
305 op->index = c;
306
307 return 0;
308 }
309
310
311 /*
312 * *******************************************************
313 * *************** ACCESS TO SERIAL LINE *****************
314 * *******************************************************
315 */
316
317 /* Characters received over the serial line are buffered for domain 0. */
318 #define SERIAL_RX_SIZE 128
319 #define SERIAL_RX_MASK(_i) ((_i)&(SERIAL_RX_SIZE-1))
320 static char serial_rx_ring[SERIAL_RX_SIZE];
321 static unsigned int serial_rx_cons, serial_rx_prod;
322
323 static void (*serial_steal_fn)(const char *) = early_puts;
324
console_steal(int handle,void (* fn)(const char *))325 int console_steal(int handle, void (*fn)(const char *))
326 {
327 if ( (handle == -1) || (handle != sercon_handle) )
328 return 0;
329
330 if ( serial_steal_fn != NULL )
331 return -EBUSY;
332
333 serial_steal_fn = fn;
334 return 1;
335 }
336
console_giveback(int id)337 void console_giveback(int id)
338 {
339 if ( id == 1 )
340 serial_steal_fn = NULL;
341 }
342
sercon_puts(const char * s)343 static void sercon_puts(const char *s)
344 {
345 if ( serial_steal_fn != NULL )
346 (*serial_steal_fn)(s);
347 else
348 serial_puts(sercon_handle, s);
349
350 #ifdef CONFIG_X86
351 /* Copy all serial output into PV console */
352 pv_console_puts(s);
353 #endif
354 }
355
dump_console_ring_key(unsigned char key)356 static void dump_console_ring_key(unsigned char key)
357 {
358 uint32_t idx, len, sofar, c;
359 unsigned int order;
360 char *buf;
361
362 printk("'%c' pressed -> dumping console ring buffer (dmesg)\n", key);
363
364 /* create a buffer in which we'll copy the ring in the correct
365 order and NUL terminate */
366 order = get_order_from_bytes(conring_size + 1);
367 buf = alloc_xenheap_pages(order, 0);
368 if ( buf == NULL )
369 {
370 printk("unable to allocate memory!\n");
371 return;
372 }
373
374 c = conringc;
375 sofar = 0;
376 while ( (c != conringp) )
377 {
378 idx = CONRING_IDX_MASK(c);
379 len = conringp - c;
380 if ( (idx + len) > conring_size )
381 len = conring_size - idx;
382 memcpy(buf + sofar, &conring[idx], len);
383 sofar += len;
384 c += len;
385 }
386 buf[sofar] = '\0';
387
388 sercon_puts(buf);
389 video_puts(buf);
390
391 free_xenheap_pages(buf, order);
392 }
393
394 /* CTRL-<switch_char> switches input direction between Xen and DOM0. */
395 #define switch_code (opt_conswitch[0]-'a'+1)
396 static int __read_mostly xen_rx = 1; /* FALSE => input passed to domain 0. */
397
switch_serial_input(void)398 static void switch_serial_input(void)
399 {
400 static char *input_str[2] = { "DOM0", "Xen" };
401 xen_rx = !xen_rx;
402 printk("*** Serial input -> %s", input_str[xen_rx]);
403 if ( switch_code )
404 printk(" (type 'CTRL-%c' three times to switch input to %s)",
405 opt_conswitch[0], input_str[!xen_rx]);
406 printk("\n");
407 }
408
__serial_rx(char c,struct cpu_user_regs * regs)409 static void __serial_rx(char c, struct cpu_user_regs *regs)
410 {
411 if ( xen_rx )
412 return handle_keypress(c, regs);
413
414 /* Deliver input to guest buffer, unless it is already full. */
415 if ( (serial_rx_prod-serial_rx_cons) != SERIAL_RX_SIZE )
416 serial_rx_ring[SERIAL_RX_MASK(serial_rx_prod++)] = c;
417 /* Always notify the guest: prevents receive path from getting stuck. */
418 send_global_virq(VIRQ_CONSOLE);
419
420 #ifdef CONFIG_X86
421 if ( pv_shim && pv_console )
422 consoled_guest_tx(c);
423 #endif
424 }
425
serial_rx(char c,struct cpu_user_regs * regs)426 static void serial_rx(char c, struct cpu_user_regs *regs)
427 {
428 static int switch_code_count = 0;
429
430 if ( switch_code && (c == switch_code) )
431 {
432 /* We eat CTRL-<switch_char> in groups of 3 to switch console input. */
433 if ( ++switch_code_count == 3 )
434 {
435 switch_serial_input();
436 switch_code_count = 0;
437 }
438 return;
439 }
440
441 for ( ; switch_code_count != 0; switch_code_count-- )
442 __serial_rx(switch_code, regs);
443
444 /* Finally process the just-received character. */
445 __serial_rx(c, regs);
446 }
447
notify_dom0_con_ring(unsigned long unused)448 static void notify_dom0_con_ring(unsigned long unused)
449 {
450 send_global_virq(VIRQ_CON_RING);
451 }
452 static DECLARE_SOFTIRQ_TASKLET(notify_dom0_con_ring_tasklet,
453 notify_dom0_con_ring, 0);
454
455 #ifdef CONFIG_X86
xen_console_write_debug_port(const char * buf,size_t len)456 static inline void xen_console_write_debug_port(const char *buf, size_t len)
457 {
458 unsigned long tmp;
459 asm volatile ( "rep outsb;"
460 : "=&S" (tmp), "=&c" (tmp)
461 : "0" (buf), "1" (len), "d" (0xe9) );
462 }
463 #endif
464
guest_console_write(XEN_GUEST_HANDLE_PARAM (char)buffer,int count)465 static long guest_console_write(XEN_GUEST_HANDLE_PARAM(char) buffer, int count)
466 {
467 char kbuf[128];
468 int kcount = 0;
469 struct domain *cd = current->domain;
470
471 while ( count > 0 )
472 {
473 if ( kcount && hypercall_preempt_check() )
474 return hypercall_create_continuation(
475 __HYPERVISOR_console_io, "iih",
476 CONSOLEIO_write, count, buffer);
477
478 kcount = min_t(int, count, sizeof(kbuf)-1);
479 if ( copy_from_guest(kbuf, buffer, kcount) )
480 return -EFAULT;
481 kbuf[kcount] = '\0';
482
483 if ( is_hardware_domain(cd) )
484 {
485 /* Use direct console output as it could be interactive */
486 spin_lock_irq(&console_lock);
487
488 sercon_puts(kbuf);
489 video_puts(kbuf);
490
491 #ifdef CONFIG_X86
492 if ( opt_console_xen )
493 {
494 size_t len = strlen(kbuf);
495
496 if ( xen_guest )
497 xen_hypercall_console_write(kbuf, len);
498 else
499 xen_console_write_debug_port(kbuf, len);
500 }
501 #endif
502
503 if ( opt_console_to_ring )
504 {
505 conring_puts(kbuf);
506 tasklet_schedule(¬ify_dom0_con_ring_tasklet);
507 }
508
509 spin_unlock_irq(&console_lock);
510 }
511 else
512 {
513 char *kin = kbuf, *kout = kbuf, c;
514
515 /* Strip non-printable characters */
516 for ( ; ; )
517 {
518 c = *kin++;
519 if ( c == '\0' || c == '\n' )
520 break;
521 if ( isprint(c) || c == '\t' )
522 *kout++ = c;
523 }
524 *kout = '\0';
525 spin_lock(&cd->pbuf_lock);
526 if ( c == '\n' )
527 {
528 kcount = kin - kbuf;
529 cd->pbuf[cd->pbuf_idx] = '\0';
530 guest_printk(cd, XENLOG_G_DEBUG "%s%s\n", cd->pbuf, kbuf);
531 cd->pbuf_idx = 0;
532 }
533 else if ( cd->pbuf_idx + kcount < (DOMAIN_PBUF_SIZE - 1) )
534 {
535 /* buffer the output until a newline */
536 memcpy(cd->pbuf + cd->pbuf_idx, kbuf, kcount);
537 cd->pbuf_idx += kcount;
538 }
539 else
540 {
541 cd->pbuf[cd->pbuf_idx] = '\0';
542 guest_printk(cd, XENLOG_G_DEBUG "%s%s\n", cd->pbuf, kbuf);
543 cd->pbuf_idx = 0;
544 }
545 spin_unlock(&cd->pbuf_lock);
546 }
547
548 guest_handle_add_offset(buffer, kcount);
549 count -= kcount;
550 }
551
552 return 0;
553 }
554
do_console_io(int cmd,int count,XEN_GUEST_HANDLE_PARAM (char)buffer)555 long do_console_io(int cmd, int count, XEN_GUEST_HANDLE_PARAM(char) buffer)
556 {
557 long rc;
558 unsigned int idx, len;
559
560 rc = xsm_console_io(XSM_OTHER, current->domain, cmd);
561 if ( rc )
562 return rc;
563
564 switch ( cmd )
565 {
566 case CONSOLEIO_write:
567 rc = guest_console_write(buffer, count);
568 break;
569 case CONSOLEIO_read:
570 rc = 0;
571 while ( (serial_rx_cons != serial_rx_prod) && (rc < count) )
572 {
573 idx = SERIAL_RX_MASK(serial_rx_cons);
574 len = serial_rx_prod - serial_rx_cons;
575 if ( (idx + len) > SERIAL_RX_SIZE )
576 len = SERIAL_RX_SIZE - idx;
577 if ( (rc + len) > count )
578 len = count - rc;
579 if ( copy_to_guest_offset(buffer, rc, &serial_rx_ring[idx], len) )
580 {
581 rc = -EFAULT;
582 break;
583 }
584 rc += len;
585 serial_rx_cons += len;
586 }
587 break;
588 default:
589 rc = -ENOSYS;
590 break;
591 }
592
593 return rc;
594 }
595
596
597 /*
598 * *****************************************************
599 * *************** GENERIC CONSOLE I/O *****************
600 * *****************************************************
601 */
602
603 static bool_t console_locks_busted;
604
__putstr(const char * str)605 static void __putstr(const char *str)
606 {
607 ASSERT(spin_is_locked(&console_lock));
608
609 sercon_puts(str);
610 video_puts(str);
611
612 #ifdef CONFIG_X86
613 if ( opt_console_xen )
614 {
615 size_t len = strlen(str);
616
617 if ( xen_guest )
618 xen_hypercall_console_write(str, len);
619 else
620 xen_console_write_debug_port(str, len);
621 }
622 #endif
623
624 conring_puts(str);
625
626 if ( !console_locks_busted )
627 tasklet_schedule(¬ify_dom0_con_ring_tasklet);
628 }
629
printk_prefix_check(char * p,char ** pp)630 static int printk_prefix_check(char *p, char **pp)
631 {
632 int loglvl = -1;
633 int upper_thresh = xenlog_upper_thresh;
634 int lower_thresh = xenlog_lower_thresh;
635
636 while ( (p[0] == '<') && (p[1] != '\0') && (p[2] == '>') )
637 {
638 switch ( p[1] )
639 {
640 case 'G':
641 upper_thresh = xenlog_guest_upper_thresh;
642 lower_thresh = xenlog_guest_lower_thresh;
643 if ( loglvl == -1 )
644 loglvl = XENLOG_GUEST_DEFAULT;
645 break;
646 case '0' ... '3':
647 loglvl = p[1] - '0';
648 break;
649 }
650 p += 3;
651 }
652
653 if ( loglvl == -1 )
654 loglvl = XENLOG_DEFAULT;
655
656 *pp = p;
657
658 return ((atomic_read(&print_everything) != 0) ||
659 (loglvl < lower_thresh) ||
660 ((loglvl < upper_thresh) && printk_ratelimit()));
661 }
662
parse_console_timestamps(const char * s)663 static int parse_console_timestamps(const char *s)
664 {
665 switch ( parse_bool(s, NULL) )
666 {
667 case 0:
668 opt_con_timestamp_mode = TSM_NONE;
669 return 0;
670 case 1:
671 opt_con_timestamp_mode = TSM_DATE;
672 return 0;
673 }
674 if ( *s == '\0' || /* Compat for old booleanparam() */
675 !strcmp(s, "date") )
676 opt_con_timestamp_mode = TSM_DATE;
677 else if ( !strcmp(s, "datems") )
678 opt_con_timestamp_mode = TSM_DATE_MS;
679 else if ( !strcmp(s, "boot") )
680 opt_con_timestamp_mode = TSM_BOOT;
681 else if ( !strcmp(s, "none") )
682 opt_con_timestamp_mode = TSM_NONE;
683 else
684 return -EINVAL;
685
686 return 0;
687 }
688
printk_start_of_line(const char * prefix)689 static void printk_start_of_line(const char *prefix)
690 {
691 struct tm tm;
692 char tstr[32];
693 uint64_t sec, nsec;
694
695 __putstr(prefix);
696
697 switch ( opt_con_timestamp_mode )
698 {
699 case TSM_DATE:
700 case TSM_DATE_MS:
701 tm = wallclock_time(&nsec);
702
703 if ( tm.tm_mday == 0 )
704 return;
705
706 if ( opt_con_timestamp_mode == TSM_DATE )
707 snprintf(tstr, sizeof(tstr), "[%04u-%02u-%02u %02u:%02u:%02u] ",
708 1900 + tm.tm_year, tm.tm_mon + 1, tm.tm_mday,
709 tm.tm_hour, tm.tm_min, tm.tm_sec);
710 else
711 snprintf(tstr, sizeof(tstr),
712 "[%04u-%02u-%02u %02u:%02u:%02u.%03"PRIu64"] ",
713 1900 + tm.tm_year, tm.tm_mon + 1, tm.tm_mday,
714 tm.tm_hour, tm.tm_min, tm.tm_sec, nsec / 1000000);
715 break;
716
717 case TSM_BOOT:
718 sec = NOW();
719 nsec = do_div(sec, 1000000000);
720
721 snprintf(tstr, sizeof(tstr), "[%5"PRIu64".%06"PRIu64"] ",
722 sec, nsec / 1000);
723 break;
724
725 case TSM_NONE:
726 default:
727 return;
728 }
729
730 __putstr(tstr);
731 }
732
vprintk_common(const char * prefix,const char * fmt,va_list args)733 static void vprintk_common(const char *prefix, const char *fmt, va_list args)
734 {
735 struct vps {
736 bool_t continued, do_print;
737 } *state;
738 static DEFINE_PER_CPU(struct vps, state);
739 static char buf[1024];
740 char *p, *q;
741 unsigned long flags;
742
743 /* console_lock can be acquired recursively from __printk_ratelimit(). */
744 local_irq_save(flags);
745 spin_lock_recursive(&console_lock);
746 state = &this_cpu(state);
747
748 (void)vsnprintf(buf, sizeof(buf), fmt, args);
749
750 p = buf;
751
752 while ( (q = strchr(p, '\n')) != NULL )
753 {
754 *q = '\0';
755 if ( !state->continued )
756 state->do_print = printk_prefix_check(p, &p);
757 if ( state->do_print )
758 {
759 if ( !state->continued )
760 printk_start_of_line(prefix);
761 __putstr(p);
762 __putstr("\n");
763 }
764 state->continued = 0;
765 p = q + 1;
766 }
767
768 if ( *p != '\0' )
769 {
770 if ( !state->continued )
771 state->do_print = printk_prefix_check(p, &p);
772 if ( state->do_print )
773 {
774 if ( !state->continued )
775 printk_start_of_line(prefix);
776 __putstr(p);
777 }
778 state->continued = 1;
779 }
780
781 spin_unlock_recursive(&console_lock);
782 local_irq_restore(flags);
783 }
784
printk(const char * fmt,...)785 void printk(const char *fmt, ...)
786 {
787 va_list args;
788 va_start(args, fmt);
789 vprintk_common("(XEN) ", fmt, args);
790 va_end(args);
791 }
792
guest_printk(const struct domain * d,const char * fmt,...)793 void guest_printk(const struct domain *d, const char *fmt, ...)
794 {
795 va_list args;
796 char prefix[16];
797
798 snprintf(prefix, sizeof(prefix), "(d%d) ", d->domain_id);
799
800 va_start(args, fmt);
801 vprintk_common(prefix, fmt, args);
802 va_end(args);
803 }
804
console_init_preirq(void)805 void __init console_init_preirq(void)
806 {
807 char *p;
808 int sh;
809
810 serial_init_preirq();
811
812 /* Where should console output go? */
813 for ( p = opt_console; p != NULL; p = strchr(p, ',') )
814 {
815 if ( *p == ',' )
816 p++;
817 if ( !strncmp(p, "vga", 3) )
818 video_init();
819 #ifdef CONFIG_X86
820 else if ( !strncmp(p, "pv", 2) )
821 pv_console_init();
822 else if ( !strncmp(p, "xen", 3) )
823 opt_console_xen = true;
824 #endif
825 else if ( !strncmp(p, "none", 4) )
826 continue;
827 else if ( (sh = serial_parse_handle(p)) >= 0 )
828 {
829 sercon_handle = sh;
830 serial_steal_fn = NULL;
831 }
832 else
833 {
834 char *q = strchr(p, ',');
835 if ( q != NULL )
836 *q = '\0';
837 printk("Bad console= option '%s'\n", p);
838 if ( q != NULL )
839 *q = ',';
840 }
841 }
842
843 serial_set_rx_handler(sercon_handle, serial_rx);
844
845 #ifdef CONFIG_X86
846 pv_console_set_rx_handler(serial_rx);
847 #endif
848
849 /* HELLO WORLD --- start-of-day banner text. */
850 spin_lock(&console_lock);
851 __putstr(xen_banner());
852 spin_unlock(&console_lock);
853 printk("Xen version %d.%d%s (%s@%s) (%s) debug=%c " gcov_string " %s\n",
854 xen_major_version(), xen_minor_version(), xen_extra_version(),
855 xen_compile_by(), xen_compile_domain(),
856 xen_compiler(), debug_build() ? 'y' : 'n', xen_compile_date());
857 printk("Latest ChangeSet: %s\n", xen_changeset());
858
859 if ( opt_sync_console )
860 {
861 serial_start_sync(sercon_handle);
862 add_taint(TAINT_SYNC_CONSOLE);
863 printk("Console output is synchronous.\n");
864 warning_add(warning_sync_console);
865 }
866 }
867
console_init_ring(void)868 void __init console_init_ring(void)
869 {
870 char *ring;
871 unsigned int i, order, memflags;
872 unsigned long flags;
873
874 if ( !opt_conring_size )
875 return;
876
877 order = get_order_from_bytes(max(opt_conring_size, conring_size));
878 memflags = MEMF_bits(crashinfo_maxaddr_bits);
879 while ( (ring = alloc_xenheap_pages(order, memflags)) == NULL )
880 {
881 BUG_ON(order == 0);
882 order--;
883 }
884 opt_conring_size = PAGE_SIZE << order;
885
886 spin_lock_irqsave(&console_lock, flags);
887 for ( i = conringc ; i != conringp; i++ )
888 ring[i & (opt_conring_size - 1)] = conring[i & (conring_size - 1)];
889 conring = ring;
890 smp_wmb(); /* Allow users of console_force_unlock() to see larger buffer. */
891 conring_size = opt_conring_size;
892 spin_unlock_irqrestore(&console_lock, flags);
893
894 printk("Allocated console ring of %u KiB.\n", opt_conring_size >> 10);
895 }
896
console_init_postirq(void)897 void __init console_init_postirq(void)
898 {
899 serial_init_postirq();
900
901 #ifdef CONFIG_X86
902 pv_console_init_postirq();
903 #endif
904
905 if ( conring != _conring )
906 return;
907
908 if ( !opt_conring_size )
909 opt_conring_size = num_present_cpus() << (9 + xenlog_lower_thresh);
910
911 console_init_ring();
912 }
913
console_endboot(void)914 void __init console_endboot(void)
915 {
916 printk("Std. Loglevel: %s", loglvl_str(xenlog_lower_thresh));
917 if ( xenlog_upper_thresh != xenlog_lower_thresh )
918 printk(" (Rate-limited: %s)", loglvl_str(xenlog_upper_thresh));
919 printk("\nGuest Loglevel: %s", loglvl_str(xenlog_guest_lower_thresh));
920 if ( xenlog_guest_upper_thresh != xenlog_guest_lower_thresh )
921 printk(" (Rate-limited: %s)", loglvl_str(xenlog_guest_upper_thresh));
922 printk("\n");
923
924 warning_print();
925
926 video_endboot();
927
928 /*
929 * If user specifies so, we fool the switch routine to redirect input
930 * straight back to Xen. I use this convoluted method so we still print
931 * a useful 'how to switch' message.
932 */
933 if ( opt_conswitch[1] == 'x' )
934 xen_rx = !xen_rx;
935
936 register_keyhandler('w', dump_console_ring_key,
937 "synchronously dump console ring buffer (dmesg)", 0);
938 register_irq_keyhandler('+', &do_inc_thresh,
939 "increase log level threshold", 0);
940 register_irq_keyhandler('-', &do_dec_thresh,
941 "decrease log level threshold", 0);
942 register_irq_keyhandler('G', &do_toggle_guest,
943 "toggle host/guest log level adjustment", 0);
944
945 /* Serial input is directed to DOM0 by default. */
946 switch_serial_input();
947 }
948
console_has(const char * device)949 int __init console_has(const char *device)
950 {
951 char *p;
952
953 for ( p = opt_console; p != NULL; p = strchr(p, ',') )
954 {
955 if ( *p == ',' )
956 p++;
957 if ( strncmp(p, device, strlen(device)) == 0 )
958 return 1;
959 }
960
961 return 0;
962 }
963
console_start_log_everything(void)964 void console_start_log_everything(void)
965 {
966 serial_start_log_everything(sercon_handle);
967 atomic_inc(&print_everything);
968 }
969
console_end_log_everything(void)970 void console_end_log_everything(void)
971 {
972 serial_end_log_everything(sercon_handle);
973 atomic_dec(&print_everything);
974 }
975
console_lock_recursive_irqsave(void)976 unsigned long console_lock_recursive_irqsave(void)
977 {
978 unsigned long flags;
979
980 local_irq_save(flags);
981 spin_lock_recursive(&console_lock);
982
983 return flags;
984 }
985
console_unlock_recursive_irqrestore(unsigned long flags)986 void console_unlock_recursive_irqrestore(unsigned long flags)
987 {
988 spin_unlock_recursive(&console_lock);
989 local_irq_restore(flags);
990 }
991
console_force_unlock(void)992 void console_force_unlock(void)
993 {
994 watchdog_disable();
995 spin_lock_init(&console_lock);
996 serial_force_unlock(sercon_handle);
997 console_locks_busted = 1;
998 console_start_sync();
999 }
1000
console_start_sync(void)1001 void console_start_sync(void)
1002 {
1003 atomic_inc(&print_everything);
1004 serial_start_sync(sercon_handle);
1005 }
1006
console_end_sync(void)1007 void console_end_sync(void)
1008 {
1009 serial_end_sync(sercon_handle);
1010 atomic_dec(&print_everything);
1011 }
1012
1013 /*
1014 * printk rate limiting, lifted from Linux.
1015 *
1016 * This enforces a rate limit: not more than one kernel message
1017 * every printk_ratelimit_ms (millisecs).
1018 */
__printk_ratelimit(int ratelimit_ms,int ratelimit_burst)1019 int __printk_ratelimit(int ratelimit_ms, int ratelimit_burst)
1020 {
1021 static DEFINE_SPINLOCK(ratelimit_lock);
1022 static unsigned long toks = 10 * 5 * 1000;
1023 static unsigned long last_msg;
1024 static int missed;
1025 unsigned long flags;
1026 unsigned long long now = NOW(); /* ns */
1027 unsigned long ms;
1028
1029 do_div(now, 1000000);
1030 ms = (unsigned long)now;
1031
1032 spin_lock_irqsave(&ratelimit_lock, flags);
1033 toks += ms - last_msg;
1034 last_msg = ms;
1035 if ( toks > (ratelimit_burst * ratelimit_ms))
1036 toks = ratelimit_burst * ratelimit_ms;
1037 if ( toks >= ratelimit_ms )
1038 {
1039 int lost = missed;
1040 missed = 0;
1041 toks -= ratelimit_ms;
1042 spin_unlock(&ratelimit_lock);
1043 if ( lost )
1044 {
1045 char lost_str[8];
1046 snprintf(lost_str, sizeof(lost_str), "%d", lost);
1047 /* console_lock may already be acquired by printk(). */
1048 spin_lock_recursive(&console_lock);
1049 printk_start_of_line("(XEN) ");
1050 __putstr("printk: ");
1051 __putstr(lost_str);
1052 __putstr(" messages suppressed.\n");
1053 spin_unlock_recursive(&console_lock);
1054 }
1055 local_irq_restore(flags);
1056 return 1;
1057 }
1058 missed++;
1059 spin_unlock_irqrestore(&ratelimit_lock, flags);
1060 return 0;
1061 }
1062
1063 /* minimum time in ms between messages */
1064 static int __read_mostly printk_ratelimit_ms = 5 * 1000;
1065
1066 /* number of messages we send before ratelimiting */
1067 static int __read_mostly printk_ratelimit_burst = 10;
1068
printk_ratelimit(void)1069 int printk_ratelimit(void)
1070 {
1071 return __printk_ratelimit(printk_ratelimit_ms, printk_ratelimit_burst);
1072 }
1073
1074 /*
1075 * **************************************************************
1076 * *************** Serial console ring buffer *******************
1077 * **************************************************************
1078 */
1079
1080 #ifdef DEBUG_TRACE_DUMP
1081
1082 /* Send output direct to console, or buffer it? */
1083 static volatile int debugtrace_send_to_console;
1084
1085 static char *debugtrace_buf; /* Debug-trace buffer */
1086 static unsigned int debugtrace_prd; /* Producer index */
1087 static unsigned int debugtrace_kilobytes = 128, debugtrace_bytes;
1088 static unsigned int debugtrace_used;
1089 static DEFINE_SPINLOCK(debugtrace_lock);
1090 integer_param("debugtrace", debugtrace_kilobytes);
1091
debugtrace_dump_worker(void)1092 static void debugtrace_dump_worker(void)
1093 {
1094 if ( (debugtrace_bytes == 0) || !debugtrace_used )
1095 return;
1096
1097 printk("debugtrace_dump() starting\n");
1098
1099 /* Print oldest portion of the ring. */
1100 ASSERT(debugtrace_buf[debugtrace_bytes - 1] == 0);
1101 sercon_puts(&debugtrace_buf[debugtrace_prd]);
1102
1103 /* Print youngest portion of the ring. */
1104 debugtrace_buf[debugtrace_prd] = '\0';
1105 sercon_puts(&debugtrace_buf[0]);
1106
1107 memset(debugtrace_buf, '\0', debugtrace_bytes);
1108
1109 printk("debugtrace_dump() finished\n");
1110 }
1111
debugtrace_toggle(void)1112 static void debugtrace_toggle(void)
1113 {
1114 unsigned long flags;
1115
1116 watchdog_disable();
1117 spin_lock_irqsave(&debugtrace_lock, flags);
1118
1119 /*
1120 * Dump the buffer *before* toggling, in case the act of dumping the
1121 * buffer itself causes more printk() invocations.
1122 */
1123 printk("debugtrace_printk now writing to %s.\n",
1124 !debugtrace_send_to_console ? "console": "buffer");
1125 if ( !debugtrace_send_to_console )
1126 debugtrace_dump_worker();
1127
1128 debugtrace_send_to_console = !debugtrace_send_to_console;
1129
1130 spin_unlock_irqrestore(&debugtrace_lock, flags);
1131 watchdog_enable();
1132
1133 }
1134
debugtrace_dump(void)1135 void debugtrace_dump(void)
1136 {
1137 unsigned long flags;
1138
1139 watchdog_disable();
1140 spin_lock_irqsave(&debugtrace_lock, flags);
1141
1142 debugtrace_dump_worker();
1143
1144 spin_unlock_irqrestore(&debugtrace_lock, flags);
1145 watchdog_enable();
1146 }
1147
debugtrace_printk(const char * fmt,...)1148 void debugtrace_printk(const char *fmt, ...)
1149 {
1150 static char buf[1024];
1151 static u32 count;
1152
1153 va_list args;
1154 char *p;
1155 unsigned long flags;
1156
1157 if ( debugtrace_bytes == 0 )
1158 return;
1159
1160 debugtrace_used = 1;
1161
1162 spin_lock_irqsave(&debugtrace_lock, flags);
1163
1164 ASSERT(debugtrace_buf[debugtrace_bytes - 1] == 0);
1165
1166 snprintf(buf, sizeof(buf), "%u ", ++count);
1167
1168 va_start(args, fmt);
1169 (void)vsnprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), fmt, args);
1170 va_end(args);
1171
1172 if ( debugtrace_send_to_console )
1173 {
1174 serial_puts(sercon_handle, buf);
1175 }
1176 else
1177 {
1178 for ( p = buf; *p != '\0'; p++ )
1179 {
1180 debugtrace_buf[debugtrace_prd++] = *p;
1181 /* Always leave a nul byte at the end of the buffer. */
1182 if ( debugtrace_prd == (debugtrace_bytes - 1) )
1183 debugtrace_prd = 0;
1184 }
1185 }
1186
1187 spin_unlock_irqrestore(&debugtrace_lock, flags);
1188 }
1189
debugtrace_key(unsigned char key)1190 static void debugtrace_key(unsigned char key)
1191 {
1192 debugtrace_toggle();
1193 }
1194
debugtrace_init(void)1195 static int __init debugtrace_init(void)
1196 {
1197 int order;
1198 unsigned int kbytes, bytes;
1199
1200 /* Round size down to next power of two. */
1201 while ( (kbytes = (debugtrace_kilobytes & (debugtrace_kilobytes-1))) != 0 )
1202 debugtrace_kilobytes = kbytes;
1203
1204 bytes = debugtrace_kilobytes << 10;
1205 if ( bytes == 0 )
1206 return 0;
1207
1208 order = get_order_from_bytes(bytes);
1209 debugtrace_buf = alloc_xenheap_pages(order, 0);
1210 ASSERT(debugtrace_buf != NULL);
1211
1212 memset(debugtrace_buf, '\0', bytes);
1213
1214 debugtrace_bytes = bytes;
1215
1216 register_keyhandler('T', debugtrace_key,
1217 "toggle debugtrace to console/buffer", 0);
1218
1219 return 0;
1220 }
1221 __initcall(debugtrace_init);
1222
1223 #endif /* !NDEBUG */
1224
1225
1226 /*
1227 * **************************************************************
1228 * *************** Debugging/tracing/error-report ***************
1229 * **************************************************************
1230 */
1231
panic(const char * fmt,...)1232 void panic(const char *fmt, ...)
1233 {
1234 va_list args;
1235 unsigned long flags;
1236 static DEFINE_SPINLOCK(lock);
1237 static char buf[128];
1238
1239 debugtrace_dump();
1240
1241 /* Protects buf[] and ensure multi-line message prints atomically. */
1242 spin_lock_irqsave(&lock, flags);
1243
1244 va_start(args, fmt);
1245 (void)vsnprintf(buf, sizeof(buf), fmt, args);
1246 va_end(args);
1247
1248 console_start_sync();
1249 printk("\n****************************************\n");
1250 printk("Panic on CPU %d:\n", smp_processor_id());
1251 printk("%s\n", buf);
1252 printk("****************************************\n\n");
1253 if ( opt_noreboot )
1254 printk("Manual reset required ('noreboot' specified)\n");
1255 else
1256 #ifdef CONFIG_X86
1257 printk("%s in five seconds...\n", pv_shim ? "Crash" : "Reboot");
1258 #else
1259 printk("Reboot in five seconds...\n");
1260 #endif
1261
1262 spin_unlock_irqrestore(&lock, flags);
1263
1264 debugger_trap_immediate();
1265
1266 #ifdef CONFIG_KEXEC
1267 kexec_crash();
1268 #endif
1269
1270 if ( opt_noreboot )
1271 machine_halt();
1272 else
1273 machine_restart(5000);
1274 }
1275
1276 /*
1277 * **************************************************************
1278 * ****************** Console suspend/resume ********************
1279 * **************************************************************
1280 */
1281
suspend_steal_fn(const char * str)1282 static void suspend_steal_fn(const char *str) { }
1283 static int suspend_steal_id;
1284
console_suspend(void)1285 int console_suspend(void)
1286 {
1287 suspend_steal_id = console_steal(sercon_handle, suspend_steal_fn);
1288 serial_suspend();
1289 return 0;
1290 }
1291
console_resume(void)1292 int console_resume(void)
1293 {
1294 serial_resume();
1295 console_giveback(suspend_steal_id);
1296 return 0;
1297 }
1298
1299 /*
1300 * Local variables:
1301 * mode: C
1302 * c-file-style: "BSD"
1303 * c-basic-offset: 4
1304 * tab-width: 4
1305 * indent-tabs-mode: nil
1306 * End:
1307 */
1308
1309