1 /*
2 * linux/kernel/printk.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * Modified to make sys_syslog() more flexible: added commands to
7 * return the last 4k of kernel messages, regardless of whether
8 * they've been read or not. Added option to suppress kernel printk's
9 * to the console. Added hook for sending the console messages
10 * elsewhere, in preparation for a serial line console (someday).
11 * Ted Ts'o, 2/11/93.
12 * Modified for sysctl support, 1/8/97, Chris Horn.
13 * Fixed SMP synchronization, 08/08/99, Manfred Spraul
14 * manfred@colorfullife.com
15 * Rewrote bits to get rid of console_lock
16 * 01Mar01 Andrew Morton
17 */
18
19 #include <linux/kernel.h>
20 #include <linux/mm.h>
21 #include <linux/tty.h>
22 #include <linux/tty_driver.h>
23 #include <linux/console.h>
24 #include <linux/init.h>
25 #include <linux/jiffies.h>
26 #include <linux/nmi.h>
27 #include <linux/module.h>
28 #include <linux/moduleparam.h>
29 #include <linux/interrupt.h> /* For in_interrupt() */
30 #include <linux/delay.h>
31 #include <linux/smp.h>
32 #include <linux/security.h>
33 #include <linux/bootmem.h>
34 #include <linux/syscalls.h>
35
36 #include <asm/uaccess.h>
37
38 /*
39 * Architectures can override it:
40 */
early_printk(const char * fmt,...)41 void asmlinkage __attribute__((weak)) early_printk(const char *fmt, ...)
42 {
43 }
44
45 #define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT)
46
47 #ifdef CONFIG_DEBUG_LL
48 extern void printascii(char *);
49 #endif
50
51 /* printk's without a loglevel use this.. */
52 #define DEFAULT_MESSAGE_LOGLEVEL 4 /* KERN_WARNING */
53
54 /* We show everything that is MORE important than this.. */
55 #define MINIMUM_CONSOLE_LOGLEVEL 1 /* Minimum loglevel we let people use */
56 #define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */
57
58 DECLARE_WAIT_QUEUE_HEAD(log_wait);
59
60 int console_printk[4] = {
61 DEFAULT_CONSOLE_LOGLEVEL, /* console_loglevel */
62 DEFAULT_MESSAGE_LOGLEVEL, /* default_message_loglevel */
63 MINIMUM_CONSOLE_LOGLEVEL, /* minimum_console_loglevel */
64 DEFAULT_CONSOLE_LOGLEVEL, /* default_console_loglevel */
65 };
66
67 /*
68 * Low level drivers may need that to know if they can schedule in
69 * their unblank() callback or not. So let's export it.
70 */
71 int oops_in_progress;
72 EXPORT_SYMBOL(oops_in_progress);
73
74 /*
75 * console_sem protects the console_drivers list, and also
76 * provides serialisation for access to the entire console
77 * driver system.
78 */
79 static DECLARE_MUTEX(console_sem);
80 struct console *console_drivers;
81 EXPORT_SYMBOL_GPL(console_drivers);
82
83 /*
84 * This is used for debugging the mess that is the VT code by
85 * keeping track if we have the console semaphore held. It's
86 * definitely not the perfect debug tool (we don't know if _WE_
87 * hold it are racing, but it helps tracking those weird code
88 * path in the console code where we end up in places I want
89 * locked without the console sempahore held
90 */
91 static int console_locked, console_suspended;
92
93 /*
94 * logbuf_lock protects log_buf, log_start, log_end, con_start and logged_chars
95 * It is also used in interesting ways to provide interlocking in
96 * release_console_sem().
97 */
98 static DEFINE_SPINLOCK(logbuf_lock);
99
100 #define LOG_BUF_MASK (log_buf_len-1)
101 #define LOG_BUF(idx) (log_buf[(idx) & LOG_BUF_MASK])
102
103 /*
104 * The indices into log_buf are not constrained to log_buf_len - they
105 * must be masked before subscripting
106 */
107 static unsigned log_start; /* Index into log_buf: next char to be read by syslog() */
108 static unsigned con_start; /* Index into log_buf: next char to be sent to consoles */
109 static unsigned log_end; /* Index into log_buf: most-recently-written-char + 1 */
110
111 /*
112 * Array of consoles built from command line options (console=)
113 */
114 struct console_cmdline
115 {
116 char name[8]; /* Name of the driver */
117 int index; /* Minor dev. to use */
118 char *options; /* Options for the driver */
119 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
120 char *brl_options; /* Options for braille driver */
121 #endif
122 };
123
124 #define MAX_CMDLINECONSOLES 8
125
126 static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
127 static int selected_console = -1;
128 static int preferred_console = -1;
129 int console_set_on_cmdline;
130 EXPORT_SYMBOL(console_set_on_cmdline);
131
132 /* Flag: console code may call schedule() */
133 static int console_may_schedule;
134
135 #ifdef CONFIG_PRINTK
136
137 static char __log_buf[__LOG_BUF_LEN];
138 static char *log_buf = __log_buf;
139 static int log_buf_len = __LOG_BUF_LEN;
140 static unsigned logged_chars; /* Number of chars produced since last read+clear operation */
141
log_buf_len_setup(char * str)142 static int __init log_buf_len_setup(char *str)
143 {
144 unsigned size = memparse(str, &str);
145 unsigned long flags;
146
147 if (size)
148 size = roundup_pow_of_two(size);
149 if (size > log_buf_len) {
150 unsigned start, dest_idx, offset;
151 char *new_log_buf;
152
153 new_log_buf = alloc_bootmem(size);
154 if (!new_log_buf) {
155 printk(KERN_WARNING "log_buf_len: allocation failed\n");
156 goto out;
157 }
158
159 spin_lock_irqsave(&logbuf_lock, flags);
160 log_buf_len = size;
161 log_buf = new_log_buf;
162
163 offset = start = min(con_start, log_start);
164 dest_idx = 0;
165 while (start != log_end) {
166 log_buf[dest_idx] = __log_buf[start & (__LOG_BUF_LEN - 1)];
167 start++;
168 dest_idx++;
169 }
170 log_start -= offset;
171 con_start -= offset;
172 log_end -= offset;
173 spin_unlock_irqrestore(&logbuf_lock, flags);
174
175 printk(KERN_NOTICE "log_buf_len: %d\n", log_buf_len);
176 }
177 out:
178 return 1;
179 }
180
181 __setup("log_buf_len=", log_buf_len_setup);
182
183 #ifdef CONFIG_BOOT_PRINTK_DELAY
184
185 static unsigned int boot_delay; /* msecs delay after each printk during bootup */
186 static unsigned long long printk_delay_msec; /* per msec, based on boot_delay */
187
boot_delay_setup(char * str)188 static int __init boot_delay_setup(char *str)
189 {
190 unsigned long lpj;
191 unsigned long long loops_per_msec;
192
193 lpj = preset_lpj ? preset_lpj : 1000000; /* some guess */
194 loops_per_msec = (unsigned long long)lpj / 1000 * HZ;
195
196 get_option(&str, &boot_delay);
197 if (boot_delay > 10 * 1000)
198 boot_delay = 0;
199
200 printk_delay_msec = loops_per_msec;
201 printk(KERN_DEBUG "boot_delay: %u, preset_lpj: %ld, lpj: %lu, "
202 "HZ: %d, printk_delay_msec: %llu\n",
203 boot_delay, preset_lpj, lpj, HZ, printk_delay_msec);
204 return 1;
205 }
206 __setup("boot_delay=", boot_delay_setup);
207
boot_delay_msec(void)208 static void boot_delay_msec(void)
209 {
210 unsigned long long k;
211 unsigned long timeout;
212
213 if (boot_delay == 0 || system_state != SYSTEM_BOOTING)
214 return;
215
216 k = (unsigned long long)printk_delay_msec * boot_delay;
217
218 timeout = jiffies + msecs_to_jiffies(boot_delay);
219 while (k) {
220 k--;
221 cpu_relax();
222 /*
223 * use (volatile) jiffies to prevent
224 * compiler reduction; loop termination via jiffies
225 * is secondary and may or may not happen.
226 */
227 if (time_after(jiffies, timeout))
228 break;
229 touch_nmi_watchdog();
230 }
231 }
232 #else
boot_delay_msec(void)233 static inline void boot_delay_msec(void)
234 {
235 }
236 #endif
237
238 /*
239 * Return the number of unread characters in the log buffer.
240 */
log_buf_get_len(void)241 static int log_buf_get_len(void)
242 {
243 return logged_chars;
244 }
245
246 /*
247 * Clears the ring-buffer
248 */
log_buf_clear(void)249 void log_buf_clear(void)
250 {
251 logged_chars = 0;
252 }
253
254 /*
255 * Copy a range of characters from the log buffer.
256 */
log_buf_copy(char * dest,int idx,int len)257 int log_buf_copy(char *dest, int idx, int len)
258 {
259 int ret, max;
260 bool took_lock = false;
261
262 if (!oops_in_progress) {
263 spin_lock_irq(&logbuf_lock);
264 took_lock = true;
265 }
266
267 max = log_buf_get_len();
268 if (idx < 0 || idx >= max) {
269 ret = -1;
270 } else {
271 if (len > max - idx)
272 len = max - idx;
273 ret = len;
274 idx += (log_end - max);
275 while (len-- > 0)
276 dest[len] = LOG_BUF(idx + len);
277 }
278
279 if (took_lock)
280 spin_unlock_irq(&logbuf_lock);
281
282 return ret;
283 }
284
285 /*
286 * Commands to do_syslog:
287 *
288 * 0 -- Close the log. Currently a NOP.
289 * 1 -- Open the log. Currently a NOP.
290 * 2 -- Read from the log.
291 * 3 -- Read all messages remaining in the ring buffer.
292 * 4 -- Read and clear all messages remaining in the ring buffer
293 * 5 -- Clear ring buffer.
294 * 6 -- Disable printk's to console
295 * 7 -- Enable printk's to console
296 * 8 -- Set level of messages printed to console
297 * 9 -- Return number of unread characters in the log buffer
298 * 10 -- Return size of the log buffer
299 */
do_syslog(int type,char __user * buf,int len)300 int do_syslog(int type, char __user *buf, int len)
301 {
302 unsigned i, j, limit, count;
303 int do_clear = 0;
304 char c;
305 int error = 0;
306
307 error = security_syslog(type);
308 if (error)
309 return error;
310
311 switch (type) {
312 case 0: /* Close log */
313 break;
314 case 1: /* Open log */
315 break;
316 case 2: /* Read from log */
317 error = -EINVAL;
318 if (!buf || len < 0)
319 goto out;
320 error = 0;
321 if (!len)
322 goto out;
323 if (!access_ok(VERIFY_WRITE, buf, len)) {
324 error = -EFAULT;
325 goto out;
326 }
327 error = wait_event_interruptible(log_wait,
328 (log_start - log_end));
329 if (error)
330 goto out;
331 i = 0;
332 spin_lock_irq(&logbuf_lock);
333 while (!error && (log_start != log_end) && i < len) {
334 c = LOG_BUF(log_start);
335 log_start++;
336 spin_unlock_irq(&logbuf_lock);
337 error = __put_user(c,buf);
338 buf++;
339 i++;
340 cond_resched();
341 spin_lock_irq(&logbuf_lock);
342 }
343 spin_unlock_irq(&logbuf_lock);
344 if (!error)
345 error = i;
346 break;
347 case 4: /* Read/clear last kernel messages */
348 do_clear = 1;
349 /* FALL THRU */
350 case 3: /* Read last kernel messages */
351 error = -EINVAL;
352 if (!buf || len < 0)
353 goto out;
354 error = 0;
355 if (!len)
356 goto out;
357 if (!access_ok(VERIFY_WRITE, buf, len)) {
358 error = -EFAULT;
359 goto out;
360 }
361 count = len;
362 if (count > log_buf_len)
363 count = log_buf_len;
364 spin_lock_irq(&logbuf_lock);
365 if (count > logged_chars)
366 count = logged_chars;
367 if (do_clear)
368 logged_chars = 0;
369 limit = log_end;
370 /*
371 * __put_user() could sleep, and while we sleep
372 * printk() could overwrite the messages
373 * we try to copy to user space. Therefore
374 * the messages are copied in reverse. <manfreds>
375 */
376 for (i = 0; i < count && !error; i++) {
377 j = limit-1-i;
378 if (j + log_buf_len < log_end)
379 break;
380 c = LOG_BUF(j);
381 spin_unlock_irq(&logbuf_lock);
382 error = __put_user(c,&buf[count-1-i]);
383 cond_resched();
384 spin_lock_irq(&logbuf_lock);
385 }
386 spin_unlock_irq(&logbuf_lock);
387 if (error)
388 break;
389 error = i;
390 if (i != count) {
391 int offset = count-error;
392 /* buffer overflow during copy, correct user buffer. */
393 for (i = 0; i < error; i++) {
394 if (__get_user(c,&buf[i+offset]) ||
395 __put_user(c,&buf[i])) {
396 error = -EFAULT;
397 break;
398 }
399 cond_resched();
400 }
401 }
402 break;
403 case 5: /* Clear ring buffer */
404 logged_chars = 0;
405 break;
406 case 6: /* Disable logging to console */
407 console_loglevel = minimum_console_loglevel;
408 break;
409 case 7: /* Enable logging to console */
410 console_loglevel = default_console_loglevel;
411 break;
412 case 8: /* Set level of messages printed to console */
413 error = -EINVAL;
414 if (len < 1 || len > 8)
415 goto out;
416 if (len < minimum_console_loglevel)
417 len = minimum_console_loglevel;
418 console_loglevel = len;
419 error = 0;
420 break;
421 case 9: /* Number of chars in the log buffer */
422 error = log_end - log_start;
423 break;
424 case 10: /* Size of the log buffer */
425 error = log_buf_len;
426 break;
427 default:
428 error = -EINVAL;
429 break;
430 }
431 out:
432 return error;
433 }
434
SYSCALL_DEFINE3(syslog,int,type,char __user *,buf,int,len)435 SYSCALL_DEFINE3(syslog, int, type, char __user *, buf, int, len)
436 {
437 return do_syslog(type, buf, len);
438 }
439
440 /*
441 * Call the console drivers on a range of log_buf
442 */
__call_console_drivers(unsigned start,unsigned end)443 static void __call_console_drivers(unsigned start, unsigned end)
444 {
445 struct console *con;
446
447 for (con = console_drivers; con; con = con->next) {
448 if ((con->flags & CON_ENABLED) && con->write &&
449 (cpu_online(smp_processor_id()) ||
450 (con->flags & CON_ANYTIME)))
451 con->write(con, &LOG_BUF(start), end - start);
452 }
453 }
454
455 static int __read_mostly ignore_loglevel;
456
ignore_loglevel_setup(char * str)457 static int __init ignore_loglevel_setup(char *str)
458 {
459 ignore_loglevel = 1;
460 printk(KERN_INFO "debug: ignoring loglevel setting.\n");
461
462 return 0;
463 }
464
465 early_param("ignore_loglevel", ignore_loglevel_setup);
466
467 /*
468 * Write out chars from start to end - 1 inclusive
469 */
_call_console_drivers(unsigned start,unsigned end,int msg_log_level)470 static void _call_console_drivers(unsigned start,
471 unsigned end, int msg_log_level)
472 {
473 if ((msg_log_level < console_loglevel || ignore_loglevel) &&
474 console_drivers && start != end) {
475 if ((start & LOG_BUF_MASK) > (end & LOG_BUF_MASK)) {
476 /* wrapped write */
477 __call_console_drivers(start & LOG_BUF_MASK,
478 log_buf_len);
479 __call_console_drivers(0, end & LOG_BUF_MASK);
480 } else {
481 __call_console_drivers(start, end);
482 }
483 }
484 }
485
486 /*
487 * Call the console drivers, asking them to write out
488 * log_buf[start] to log_buf[end - 1].
489 * The console_sem must be held.
490 */
call_console_drivers(unsigned start,unsigned end)491 static void call_console_drivers(unsigned start, unsigned end)
492 {
493 unsigned cur_index, start_print;
494 static int msg_level = -1;
495
496 BUG_ON(((int)(start - end)) > 0);
497
498 cur_index = start;
499 start_print = start;
500 while (cur_index != end) {
501 if (msg_level < 0 && ((end - cur_index) > 2) &&
502 LOG_BUF(cur_index + 0) == '<' &&
503 LOG_BUF(cur_index + 1) >= '0' &&
504 LOG_BUF(cur_index + 1) <= '7' &&
505 LOG_BUF(cur_index + 2) == '>') {
506 msg_level = LOG_BUF(cur_index + 1) - '0';
507 cur_index += 3;
508 start_print = cur_index;
509 }
510 while (cur_index != end) {
511 char c = LOG_BUF(cur_index);
512
513 cur_index++;
514 if (c == '\n') {
515 if (msg_level < 0) {
516 /*
517 * printk() has already given us loglevel tags in
518 * the buffer. This code is here in case the
519 * log buffer has wrapped right round and scribbled
520 * on those tags
521 */
522 msg_level = default_message_loglevel;
523 }
524 _call_console_drivers(start_print, cur_index, msg_level);
525 msg_level = -1;
526 start_print = cur_index;
527 break;
528 }
529 }
530 }
531 _call_console_drivers(start_print, end, msg_level);
532 }
533
emit_log_char(char c)534 static void emit_log_char(char c)
535 {
536 LOG_BUF(log_end) = c;
537 log_end++;
538 if (log_end - log_start > log_buf_len)
539 log_start = log_end - log_buf_len;
540 if (log_end - con_start > log_buf_len)
541 con_start = log_end - log_buf_len;
542 if (logged_chars < log_buf_len)
543 logged_chars++;
544 }
545
546 /*
547 * Zap console related locks when oopsing. Only zap at most once
548 * every 10 seconds, to leave time for slow consoles to print a
549 * full oops.
550 */
zap_locks(void)551 static void zap_locks(void)
552 {
553 static unsigned long oops_timestamp;
554
555 if (time_after_eq(jiffies, oops_timestamp) &&
556 !time_after(jiffies, oops_timestamp + 30 * HZ))
557 return;
558
559 oops_timestamp = jiffies;
560
561 /* If a crash is occurring, make sure we can't deadlock */
562 spin_lock_init(&logbuf_lock);
563 /* And make sure that we print immediately */
564 init_MUTEX(&console_sem);
565 }
566
567 #if defined(CONFIG_PRINTK_TIME)
568 static int printk_time = 1;
569 #else
570 static int printk_time = 0;
571 #endif
572 module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR);
573
574 /* Check if we have any console registered that can be called early in boot. */
have_callable_console(void)575 static int have_callable_console(void)
576 {
577 struct console *con;
578
579 for (con = console_drivers; con; con = con->next)
580 if (con->flags & CON_ANYTIME)
581 return 1;
582
583 return 0;
584 }
585
586 /**
587 * printk - print a kernel message
588 * @fmt: format string
589 *
590 * This is printk(). It can be called from any context. We want it to work.
591 *
592 * We try to grab the console_sem. If we succeed, it's easy - we log the output and
593 * call the console drivers. If we fail to get the semaphore we place the output
594 * into the log buffer and return. The current holder of the console_sem will
595 * notice the new output in release_console_sem() and will send it to the
596 * consoles before releasing the semaphore.
597 *
598 * One effect of this deferred printing is that code which calls printk() and
599 * then changes console_loglevel may break. This is because console_loglevel
600 * is inspected when the actual printing occurs.
601 *
602 * See also:
603 * printf(3)
604 *
605 * See the vsnprintf() documentation for format string extensions over C99.
606 */
607
printk(const char * fmt,...)608 asmlinkage int printk(const char *fmt, ...)
609 {
610 va_list args;
611 int r;
612
613 va_start(args, fmt);
614 r = vprintk(fmt, args);
615 va_end(args);
616
617 return r;
618 }
619
620 /* cpu currently holding logbuf_lock */
621 static volatile unsigned int printk_cpu = UINT_MAX;
622
623 /*
624 * Can we actually use the console at this time on this cpu?
625 *
626 * Console drivers may assume that per-cpu resources have
627 * been allocated. So unless they're explicitly marked as
628 * being able to cope (CON_ANYTIME) don't call them until
629 * this CPU is officially up.
630 */
can_use_console(unsigned int cpu)631 static inline int can_use_console(unsigned int cpu)
632 {
633 return cpu_online(cpu) || have_callable_console();
634 }
635
636 /*
637 * Try to get console ownership to actually show the kernel
638 * messages from a 'printk'. Return true (and with the
639 * console_semaphore held, and 'console_locked' set) if it
640 * is successful, false otherwise.
641 *
642 * This gets called with the 'logbuf_lock' spinlock held and
643 * interrupts disabled. It should return with 'lockbuf_lock'
644 * released but interrupts still disabled.
645 */
acquire_console_semaphore_for_printk(unsigned int cpu)646 static int acquire_console_semaphore_for_printk(unsigned int cpu)
647 {
648 int retval = 0;
649
650 if (!try_acquire_console_sem()) {
651 retval = 1;
652
653 /*
654 * If we can't use the console, we need to release
655 * the console semaphore by hand to avoid flushing
656 * the buffer. We need to hold the console semaphore
657 * in order to do this test safely.
658 */
659 if (!can_use_console(cpu)) {
660 console_locked = 0;
661 up(&console_sem);
662 retval = 0;
663 }
664 }
665 printk_cpu = UINT_MAX;
666 spin_unlock(&logbuf_lock);
667 return retval;
668 }
669 static const char recursion_bug_msg [] =
670 KERN_CRIT "BUG: recent printk recursion!\n";
671 static int recursion_bug;
672 static int new_text_line = 1;
673 static char printk_buf[1024];
674
vprintk(const char * fmt,va_list args)675 asmlinkage int vprintk(const char *fmt, va_list args)
676 {
677 int printed_len = 0;
678 int current_log_level = default_message_loglevel;
679 unsigned long flags;
680 int this_cpu;
681 char *p;
682
683 boot_delay_msec();
684
685 preempt_disable();
686 /* This stops the holder of console_sem just where we want him */
687 raw_local_irq_save(flags);
688 this_cpu = smp_processor_id();
689
690 /*
691 * Ouch, printk recursed into itself!
692 */
693 if (unlikely(printk_cpu == this_cpu)) {
694 /*
695 * If a crash is occurring during printk() on this CPU,
696 * then try to get the crash message out but make sure
697 * we can't deadlock. Otherwise just return to avoid the
698 * recursion and return - but flag the recursion so that
699 * it can be printed at the next appropriate moment:
700 */
701 if (!oops_in_progress) {
702 recursion_bug = 1;
703 goto out_restore_irqs;
704 }
705 zap_locks();
706 }
707
708 lockdep_off();
709 spin_lock(&logbuf_lock);
710 printk_cpu = this_cpu;
711
712 if (recursion_bug) {
713 recursion_bug = 0;
714 strcpy(printk_buf, recursion_bug_msg);
715 printed_len = strlen(recursion_bug_msg);
716 }
717 /* Emit the output into the temporary buffer */
718 printed_len += vscnprintf(printk_buf + printed_len,
719 sizeof(printk_buf) - printed_len, fmt, args);
720
721
722 #ifdef CONFIG_DEBUG_LL
723 printascii(printk_buf);
724 #endif
725
726 /*
727 * Copy the output into log_buf. If the caller didn't provide
728 * appropriate log level tags, we insert them here
729 */
730 for (p = printk_buf; *p; p++) {
731 if (new_text_line) {
732 /* If a token, set current_log_level and skip over */
733 if (p[0] == '<' && p[1] >= '0' && p[1] <= '7' &&
734 p[2] == '>') {
735 current_log_level = p[1] - '0';
736 p += 3;
737 printed_len -= 3;
738 }
739
740 /* Always output the token */
741 emit_log_char('<');
742 emit_log_char(current_log_level + '0');
743 emit_log_char('>');
744 printed_len += 3;
745 new_text_line = 0;
746
747 if (printk_time) {
748 /* Follow the token with the time */
749 char tbuf[50], *tp;
750 unsigned tlen;
751 unsigned long long t;
752 unsigned long nanosec_rem;
753
754 t = cpu_clock(printk_cpu);
755 nanosec_rem = do_div(t, 1000000000);
756 tlen = sprintf(tbuf, "[%5lu.%06lu] ",
757 (unsigned long) t,
758 nanosec_rem / 1000);
759
760 for (tp = tbuf; tp < tbuf + tlen; tp++)
761 emit_log_char(*tp);
762 printed_len += tlen;
763 }
764
765 if (!*p)
766 break;
767 }
768
769 emit_log_char(*p);
770 if (*p == '\n')
771 new_text_line = 1;
772 }
773
774 /*
775 * Try to acquire and then immediately release the
776 * console semaphore. The release will do all the
777 * actual magic (print out buffers, wake up klogd,
778 * etc).
779 *
780 * The acquire_console_semaphore_for_printk() function
781 * will release 'logbuf_lock' regardless of whether it
782 * actually gets the semaphore or not.
783 */
784 if (acquire_console_semaphore_for_printk(this_cpu))
785 release_console_sem();
786
787 lockdep_on();
788 out_restore_irqs:
789 raw_local_irq_restore(flags);
790
791 preempt_enable();
792 return printed_len;
793 }
794 EXPORT_SYMBOL(printk);
795 EXPORT_SYMBOL(vprintk);
796
797 #else
798
call_console_drivers(unsigned start,unsigned end)799 static void call_console_drivers(unsigned start, unsigned end)
800 {
801 }
802
803 #endif
804
__add_preferred_console(char * name,int idx,char * options,char * brl_options)805 static int __add_preferred_console(char *name, int idx, char *options,
806 char *brl_options)
807 {
808 struct console_cmdline *c;
809 int i;
810
811 /*
812 * See if this tty is not yet registered, and
813 * if we have a slot free.
814 */
815 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
816 if (strcmp(console_cmdline[i].name, name) == 0 &&
817 console_cmdline[i].index == idx) {
818 if (!brl_options)
819 selected_console = i;
820 return 0;
821 }
822 if (i == MAX_CMDLINECONSOLES)
823 return -E2BIG;
824 if (!brl_options)
825 selected_console = i;
826 c = &console_cmdline[i];
827 strlcpy(c->name, name, sizeof(c->name));
828 c->options = options;
829 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
830 c->brl_options = brl_options;
831 #endif
832 c->index = idx;
833 return 0;
834 }
835 /*
836 * Set up a list of consoles. Called from init/main.c
837 */
console_setup(char * str)838 static int __init console_setup(char *str)
839 {
840 char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for index */
841 char *s, *options, *brl_options = NULL;
842 int idx;
843
844 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
845 if (!memcmp(str, "brl,", 4)) {
846 brl_options = "";
847 str += 4;
848 } else if (!memcmp(str, "brl=", 4)) {
849 brl_options = str + 4;
850 str = strchr(brl_options, ',');
851 if (!str) {
852 printk(KERN_ERR "need port name after brl=\n");
853 return 1;
854 }
855 *(str++) = 0;
856 }
857 #endif
858
859 /*
860 * Decode str into name, index, options.
861 */
862 if (str[0] >= '0' && str[0] <= '9') {
863 strcpy(buf, "ttyS");
864 strncpy(buf + 4, str, sizeof(buf) - 5);
865 } else {
866 strncpy(buf, str, sizeof(buf) - 1);
867 }
868 buf[sizeof(buf) - 1] = 0;
869 if ((options = strchr(str, ',')) != NULL)
870 *(options++) = 0;
871 #ifdef __sparc__
872 if (!strcmp(str, "ttya"))
873 strcpy(buf, "ttyS0");
874 if (!strcmp(str, "ttyb"))
875 strcpy(buf, "ttyS1");
876 #endif
877 for (s = buf; *s; s++)
878 if ((*s >= '0' && *s <= '9') || *s == ',')
879 break;
880 idx = simple_strtoul(s, NULL, 10);
881 *s = 0;
882
883 __add_preferred_console(buf, idx, options, brl_options);
884 console_set_on_cmdline = 1;
885 return 1;
886 }
887 __setup("console=", console_setup);
888
889 /**
890 * add_preferred_console - add a device to the list of preferred consoles.
891 * @name: device name
892 * @idx: device index
893 * @options: options for this console
894 *
895 * The last preferred console added will be used for kernel messages
896 * and stdin/out/err for init. Normally this is used by console_setup
897 * above to handle user-supplied console arguments; however it can also
898 * be used by arch-specific code either to override the user or more
899 * commonly to provide a default console (ie from PROM variables) when
900 * the user has not supplied one.
901 */
add_preferred_console(char * name,int idx,char * options)902 int add_preferred_console(char *name, int idx, char *options)
903 {
904 return __add_preferred_console(name, idx, options, NULL);
905 }
906
update_console_cmdline(char * name,int idx,char * name_new,int idx_new,char * options)907 int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options)
908 {
909 struct console_cmdline *c;
910 int i;
911
912 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
913 if (strcmp(console_cmdline[i].name, name) == 0 &&
914 console_cmdline[i].index == idx) {
915 c = &console_cmdline[i];
916 strlcpy(c->name, name_new, sizeof(c->name));
917 c->name[sizeof(c->name) - 1] = 0;
918 c->options = options;
919 c->index = idx_new;
920 return i;
921 }
922 /* not found */
923 return -1;
924 }
925
926 int console_suspend_enabled = 1;
927 EXPORT_SYMBOL(console_suspend_enabled);
928
console_suspend_disable(char * str)929 static int __init console_suspend_disable(char *str)
930 {
931 console_suspend_enabled = 0;
932 return 1;
933 }
934 __setup("no_console_suspend", console_suspend_disable);
935
936 /**
937 * suspend_console - suspend the console subsystem
938 *
939 * This disables printk() while we go into suspend states
940 */
suspend_console(void)941 void suspend_console(void)
942 {
943 if (!console_suspend_enabled)
944 return;
945 printk("Suspending console(s) (use no_console_suspend to debug)\n");
946 acquire_console_sem();
947 console_suspended = 1;
948 up(&console_sem);
949 }
950
resume_console(void)951 void resume_console(void)
952 {
953 if (!console_suspend_enabled)
954 return;
955 down(&console_sem);
956 console_suspended = 0;
957 release_console_sem();
958 }
959
960 /**
961 * acquire_console_sem - lock the console system for exclusive use.
962 *
963 * Acquires a semaphore which guarantees that the caller has
964 * exclusive access to the console system and the console_drivers list.
965 *
966 * Can sleep, returns nothing.
967 */
acquire_console_sem(void)968 void acquire_console_sem(void)
969 {
970 BUG_ON(in_interrupt());
971 down(&console_sem);
972 if (console_suspended)
973 return;
974 console_locked = 1;
975 console_may_schedule = 1;
976 }
977 EXPORT_SYMBOL(acquire_console_sem);
978
try_acquire_console_sem(void)979 int try_acquire_console_sem(void)
980 {
981 if (down_trylock(&console_sem))
982 return -1;
983 if (console_suspended) {
984 up(&console_sem);
985 return -1;
986 }
987 console_locked = 1;
988 console_may_schedule = 0;
989 return 0;
990 }
991 EXPORT_SYMBOL(try_acquire_console_sem);
992
is_console_locked(void)993 int is_console_locked(void)
994 {
995 return console_locked;
996 }
997
998 static DEFINE_PER_CPU(int, printk_pending);
999
printk_tick(void)1000 void printk_tick(void)
1001 {
1002 if (__get_cpu_var(printk_pending)) {
1003 __get_cpu_var(printk_pending) = 0;
1004 wake_up_interruptible(&log_wait);
1005 }
1006 }
1007
printk_needs_cpu(int cpu)1008 int printk_needs_cpu(int cpu)
1009 {
1010 return per_cpu(printk_pending, cpu);
1011 }
1012
wake_up_klogd(void)1013 void wake_up_klogd(void)
1014 {
1015 if (waitqueue_active(&log_wait))
1016 __raw_get_cpu_var(printk_pending) = 1;
1017 }
1018
1019 /**
1020 * release_console_sem - unlock the console system
1021 *
1022 * Releases the semaphore which the caller holds on the console system
1023 * and the console driver list.
1024 *
1025 * While the semaphore was held, console output may have been buffered
1026 * by printk(). If this is the case, release_console_sem() emits
1027 * the output prior to releasing the semaphore.
1028 *
1029 * If there is output waiting for klogd, we wake it up.
1030 *
1031 * release_console_sem() may be called from any context.
1032 */
release_console_sem(void)1033 void release_console_sem(void)
1034 {
1035 unsigned long flags;
1036 unsigned _con_start, _log_end;
1037 unsigned wake_klogd = 0;
1038
1039 if (console_suspended) {
1040 up(&console_sem);
1041 return;
1042 }
1043
1044 console_may_schedule = 0;
1045
1046 for ( ; ; ) {
1047 spin_lock_irqsave(&logbuf_lock, flags);
1048 wake_klogd |= log_start - log_end;
1049 if (con_start == log_end)
1050 break; /* Nothing to print */
1051 _con_start = con_start;
1052 _log_end = log_end;
1053 con_start = log_end; /* Flush */
1054 spin_unlock(&logbuf_lock);
1055 stop_critical_timings(); /* don't trace print latency */
1056 call_console_drivers(_con_start, _log_end);
1057 start_critical_timings();
1058 local_irq_restore(flags);
1059 }
1060 console_locked = 0;
1061 up(&console_sem);
1062 spin_unlock_irqrestore(&logbuf_lock, flags);
1063 if (wake_klogd)
1064 wake_up_klogd();
1065 }
1066 EXPORT_SYMBOL(release_console_sem);
1067
1068 /**
1069 * console_conditional_schedule - yield the CPU if required
1070 *
1071 * If the console code is currently allowed to sleep, and
1072 * if this CPU should yield the CPU to another task, do
1073 * so here.
1074 *
1075 * Must be called within acquire_console_sem().
1076 */
console_conditional_schedule(void)1077 void __sched console_conditional_schedule(void)
1078 {
1079 if (console_may_schedule)
1080 cond_resched();
1081 }
1082 EXPORT_SYMBOL(console_conditional_schedule);
1083
console_print(const char * s)1084 void console_print(const char *s)
1085 {
1086 printk(KERN_EMERG "%s", s);
1087 }
1088 EXPORT_SYMBOL(console_print);
1089
console_unblank(void)1090 void console_unblank(void)
1091 {
1092 struct console *c;
1093
1094 /*
1095 * console_unblank can no longer be called in interrupt context unless
1096 * oops_in_progress is set to 1..
1097 */
1098 if (oops_in_progress) {
1099 if (down_trylock(&console_sem) != 0)
1100 return;
1101 } else
1102 acquire_console_sem();
1103
1104 console_locked = 1;
1105 console_may_schedule = 0;
1106 for (c = console_drivers; c != NULL; c = c->next)
1107 if ((c->flags & CON_ENABLED) && c->unblank)
1108 c->unblank();
1109 release_console_sem();
1110 }
1111
1112 /*
1113 * Return the console tty driver structure and its associated index
1114 */
console_device(int * index)1115 struct tty_driver *console_device(int *index)
1116 {
1117 struct console *c;
1118 struct tty_driver *driver = NULL;
1119
1120 acquire_console_sem();
1121 for (c = console_drivers; c != NULL; c = c->next) {
1122 if (!c->device)
1123 continue;
1124 driver = c->device(c, index);
1125 if (driver)
1126 break;
1127 }
1128 release_console_sem();
1129 return driver;
1130 }
1131
1132 /*
1133 * Prevent further output on the passed console device so that (for example)
1134 * serial drivers can disable console output before suspending a port, and can
1135 * re-enable output afterwards.
1136 */
console_stop(struct console * console)1137 void console_stop(struct console *console)
1138 {
1139 acquire_console_sem();
1140 console->flags &= ~CON_ENABLED;
1141 release_console_sem();
1142 }
1143 EXPORT_SYMBOL(console_stop);
1144
console_start(struct console * console)1145 void console_start(struct console *console)
1146 {
1147 acquire_console_sem();
1148 console->flags |= CON_ENABLED;
1149 release_console_sem();
1150 }
1151 EXPORT_SYMBOL(console_start);
1152
1153 /*
1154 * The console driver calls this routine during kernel initialization
1155 * to register the console printing procedure with printk() and to
1156 * print any messages that were printed by the kernel before the
1157 * console driver was initialized.
1158 */
register_console(struct console * console)1159 void register_console(struct console *console)
1160 {
1161 int i;
1162 unsigned long flags;
1163 struct console *bootconsole = NULL;
1164
1165 if (console_drivers) {
1166 if (console->flags & CON_BOOT)
1167 return;
1168 if (console_drivers->flags & CON_BOOT)
1169 bootconsole = console_drivers;
1170 }
1171
1172 if (preferred_console < 0 || bootconsole || !console_drivers)
1173 preferred_console = selected_console;
1174
1175 if (console->early_setup)
1176 console->early_setup();
1177
1178 /*
1179 * See if we want to use this console driver. If we
1180 * didn't select a console we take the first one
1181 * that registers here.
1182 */
1183 if (preferred_console < 0) {
1184 if (console->index < 0)
1185 console->index = 0;
1186 if (console->setup == NULL ||
1187 console->setup(console, NULL) == 0) {
1188 console->flags |= CON_ENABLED;
1189 if (console->device) {
1190 console->flags |= CON_CONSDEV;
1191 preferred_console = 0;
1192 }
1193 }
1194 }
1195
1196 /*
1197 * See if this console matches one we selected on
1198 * the command line.
1199 */
1200 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0];
1201 i++) {
1202 if (strcmp(console_cmdline[i].name, console->name) != 0)
1203 continue;
1204 if (console->index >= 0 &&
1205 console->index != console_cmdline[i].index)
1206 continue;
1207 if (console->index < 0)
1208 console->index = console_cmdline[i].index;
1209 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1210 if (console_cmdline[i].brl_options) {
1211 console->flags |= CON_BRL;
1212 braille_register_console(console,
1213 console_cmdline[i].index,
1214 console_cmdline[i].options,
1215 console_cmdline[i].brl_options);
1216 return;
1217 }
1218 #endif
1219 if (console->setup &&
1220 console->setup(console, console_cmdline[i].options) != 0)
1221 break;
1222 console->flags |= CON_ENABLED;
1223 console->index = console_cmdline[i].index;
1224 if (i == selected_console) {
1225 console->flags |= CON_CONSDEV;
1226 preferred_console = selected_console;
1227 }
1228 break;
1229 }
1230
1231 if (!(console->flags & CON_ENABLED))
1232 return;
1233
1234 if (bootconsole && (console->flags & CON_CONSDEV)) {
1235 printk(KERN_INFO "console handover: boot [%s%d] -> real [%s%d]\n",
1236 bootconsole->name, bootconsole->index,
1237 console->name, console->index);
1238 unregister_console(bootconsole);
1239 console->flags &= ~CON_PRINTBUFFER;
1240 } else {
1241 printk(KERN_INFO "console [%s%d] enabled\n",
1242 console->name, console->index);
1243 }
1244
1245 /*
1246 * Put this console in the list - keep the
1247 * preferred driver at the head of the list.
1248 */
1249 acquire_console_sem();
1250 if ((console->flags & CON_CONSDEV) || console_drivers == NULL) {
1251 console->next = console_drivers;
1252 console_drivers = console;
1253 if (console->next)
1254 console->next->flags &= ~CON_CONSDEV;
1255 } else {
1256 console->next = console_drivers->next;
1257 console_drivers->next = console;
1258 }
1259 if (console->flags & CON_PRINTBUFFER) {
1260 /*
1261 * release_console_sem() will print out the buffered messages
1262 * for us.
1263 */
1264 spin_lock_irqsave(&logbuf_lock, flags);
1265 con_start = log_start;
1266 spin_unlock_irqrestore(&logbuf_lock, flags);
1267 }
1268 release_console_sem();
1269 }
1270 EXPORT_SYMBOL(register_console);
1271
unregister_console(struct console * console)1272 int unregister_console(struct console *console)
1273 {
1274 struct console *a, *b;
1275 int res = 1;
1276
1277 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1278 if (console->flags & CON_BRL)
1279 return braille_unregister_console(console);
1280 #endif
1281
1282 acquire_console_sem();
1283 if (console_drivers == console) {
1284 console_drivers=console->next;
1285 res = 0;
1286 } else if (console_drivers) {
1287 for (a=console_drivers->next, b=console_drivers ;
1288 a; b=a, a=b->next) {
1289 if (a == console) {
1290 b->next = a->next;
1291 res = 0;
1292 break;
1293 }
1294 }
1295 }
1296
1297 /*
1298 * If this isn't the last console and it has CON_CONSDEV set, we
1299 * need to set it on the next preferred console.
1300 */
1301 if (console_drivers != NULL && console->flags & CON_CONSDEV)
1302 console_drivers->flags |= CON_CONSDEV;
1303
1304 release_console_sem();
1305 return res;
1306 }
1307 EXPORT_SYMBOL(unregister_console);
1308
disable_boot_consoles(void)1309 static int __init disable_boot_consoles(void)
1310 {
1311 if (console_drivers != NULL) {
1312 if (console_drivers->flags & CON_BOOT) {
1313 printk(KERN_INFO "turn off boot console %s%d\n",
1314 console_drivers->name, console_drivers->index);
1315 return unregister_console(console_drivers);
1316 }
1317 }
1318 return 0;
1319 }
1320 late_initcall(disable_boot_consoles);
1321
1322 #if defined CONFIG_PRINTK
1323
1324 /*
1325 * printk rate limiting, lifted from the networking subsystem.
1326 *
1327 * This enforces a rate limit: not more than 10 kernel messages
1328 * every 5s to make a denial-of-service attack impossible.
1329 */
1330 DEFINE_RATELIMIT_STATE(printk_ratelimit_state, 5 * HZ, 10);
1331
printk_ratelimit(void)1332 int printk_ratelimit(void)
1333 {
1334 return __ratelimit(&printk_ratelimit_state);
1335 }
1336 EXPORT_SYMBOL(printk_ratelimit);
1337
1338 /**
1339 * printk_timed_ratelimit - caller-controlled printk ratelimiting
1340 * @caller_jiffies: pointer to caller's state
1341 * @interval_msecs: minimum interval between prints
1342 *
1343 * printk_timed_ratelimit() returns true if more than @interval_msecs
1344 * milliseconds have elapsed since the last time printk_timed_ratelimit()
1345 * returned true.
1346 */
printk_timed_ratelimit(unsigned long * caller_jiffies,unsigned int interval_msecs)1347 bool printk_timed_ratelimit(unsigned long *caller_jiffies,
1348 unsigned int interval_msecs)
1349 {
1350 if (*caller_jiffies == 0 || time_after(jiffies, *caller_jiffies)) {
1351 *caller_jiffies = jiffies + msecs_to_jiffies(interval_msecs);
1352 return true;
1353 }
1354 return false;
1355 }
1356 EXPORT_SYMBOL(printk_timed_ratelimit);
1357 #endif
1358