V4L/DVB (13615): ir-core: create ir_input_register
[linux-2.6/linux-2.6-openrd.git] / kernel / printk.c
blobb5ac4d99c667c96ba55542f69963baf7b151f838
1 /*
2 * linux/kernel/printk.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
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
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 #include <linux/kexec.h>
36 #include <linux/ratelimit.h>
38 #include <asm/uaccess.h>
41 * for_each_console() allows you to iterate on each console
43 #define for_each_console(con) \
44 for (con = console_drivers; con != NULL; con = con->next)
47 * Architectures can override it:
49 void asmlinkage __attribute__((weak)) early_printk(const char *fmt, ...)
53 #define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT)
55 /* printk's without a loglevel use this.. */
56 #define DEFAULT_MESSAGE_LOGLEVEL 4 /* KERN_WARNING */
58 /* We show everything that is MORE important than this.. */
59 #define MINIMUM_CONSOLE_LOGLEVEL 1 /* Minimum loglevel we let people use */
60 #define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */
62 DECLARE_WAIT_QUEUE_HEAD(log_wait);
64 int console_printk[4] = {
65 DEFAULT_CONSOLE_LOGLEVEL, /* console_loglevel */
66 DEFAULT_MESSAGE_LOGLEVEL, /* default_message_loglevel */
67 MINIMUM_CONSOLE_LOGLEVEL, /* minimum_console_loglevel */
68 DEFAULT_CONSOLE_LOGLEVEL, /* default_console_loglevel */
71 static int saved_console_loglevel = -1;
74 * Low level drivers may need that to know if they can schedule in
75 * their unblank() callback or not. So let's export it.
77 int oops_in_progress;
78 EXPORT_SYMBOL(oops_in_progress);
81 * console_sem protects the console_drivers list, and also
82 * provides serialisation for access to the entire console
83 * driver system.
85 static DECLARE_MUTEX(console_sem);
86 struct console *console_drivers;
87 EXPORT_SYMBOL_GPL(console_drivers);
90 * This is used for debugging the mess that is the VT code by
91 * keeping track if we have the console semaphore held. It's
92 * definitely not the perfect debug tool (we don't know if _WE_
93 * hold it are racing, but it helps tracking those weird code
94 * path in the console code where we end up in places I want
95 * locked without the console sempahore held
97 static int console_locked, console_suspended;
100 * logbuf_lock protects log_buf, log_start, log_end, con_start and logged_chars
101 * It is also used in interesting ways to provide interlocking in
102 * release_console_sem().
104 static DEFINE_SPINLOCK(logbuf_lock);
106 #define LOG_BUF_MASK (log_buf_len-1)
107 #define LOG_BUF(idx) (log_buf[(idx) & LOG_BUF_MASK])
110 * The indices into log_buf are not constrained to log_buf_len - they
111 * must be masked before subscripting
113 static unsigned log_start; /* Index into log_buf: next char to be read by syslog() */
114 static unsigned con_start; /* Index into log_buf: next char to be sent to consoles */
115 static unsigned log_end; /* Index into log_buf: most-recently-written-char + 1 */
118 * Array of consoles built from command line options (console=)
120 struct console_cmdline
122 char name[8]; /* Name of the driver */
123 int index; /* Minor dev. to use */
124 char *options; /* Options for the driver */
125 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
126 char *brl_options; /* Options for braille driver */
127 #endif
130 #define MAX_CMDLINECONSOLES 8
132 static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
133 static int selected_console = -1;
134 static int preferred_console = -1;
135 int console_set_on_cmdline;
136 EXPORT_SYMBOL(console_set_on_cmdline);
138 /* Flag: console code may call schedule() */
139 static int console_may_schedule;
141 #ifdef CONFIG_PRINTK
143 static char __log_buf[__LOG_BUF_LEN];
144 static char *log_buf = __log_buf;
145 static int log_buf_len = __LOG_BUF_LEN;
146 static unsigned logged_chars; /* Number of chars produced since last read+clear operation */
148 #ifdef CONFIG_KEXEC
150 * This appends the listed symbols to /proc/vmcoreinfo
152 * /proc/vmcoreinfo is used by various utiilties, like crash and makedumpfile to
153 * obtain access to symbols that are otherwise very difficult to locate. These
154 * symbols are specifically used so that utilities can access and extract the
155 * dmesg log from a vmcore file after a crash.
157 void log_buf_kexec_setup(void)
159 VMCOREINFO_SYMBOL(log_buf);
160 VMCOREINFO_SYMBOL(log_end);
161 VMCOREINFO_SYMBOL(log_buf_len);
162 VMCOREINFO_SYMBOL(logged_chars);
164 #endif
166 static int __init log_buf_len_setup(char *str)
168 unsigned size = memparse(str, &str);
169 unsigned long flags;
171 if (size)
172 size = roundup_pow_of_two(size);
173 if (size > log_buf_len) {
174 unsigned start, dest_idx, offset;
175 char *new_log_buf;
177 new_log_buf = alloc_bootmem(size);
178 if (!new_log_buf) {
179 printk(KERN_WARNING "log_buf_len: allocation failed\n");
180 goto out;
183 spin_lock_irqsave(&logbuf_lock, flags);
184 log_buf_len = size;
185 log_buf = new_log_buf;
187 offset = start = min(con_start, log_start);
188 dest_idx = 0;
189 while (start != log_end) {
190 log_buf[dest_idx] = __log_buf[start & (__LOG_BUF_LEN - 1)];
191 start++;
192 dest_idx++;
194 log_start -= offset;
195 con_start -= offset;
196 log_end -= offset;
197 spin_unlock_irqrestore(&logbuf_lock, flags);
199 printk(KERN_NOTICE "log_buf_len: %d\n", log_buf_len);
201 out:
202 return 1;
205 __setup("log_buf_len=", log_buf_len_setup);
207 #ifdef CONFIG_BOOT_PRINTK_DELAY
209 static unsigned int boot_delay; /* msecs delay after each printk during bootup */
210 static unsigned long long loops_per_msec; /* based on boot_delay */
212 static int __init boot_delay_setup(char *str)
214 unsigned long lpj;
216 lpj = preset_lpj ? preset_lpj : 1000000; /* some guess */
217 loops_per_msec = (unsigned long long)lpj / 1000 * HZ;
219 get_option(&str, &boot_delay);
220 if (boot_delay > 10 * 1000)
221 boot_delay = 0;
223 pr_debug("boot_delay: %u, preset_lpj: %ld, lpj: %lu, "
224 "HZ: %d, loops_per_msec: %llu\n",
225 boot_delay, preset_lpj, lpj, HZ, loops_per_msec);
226 return 1;
228 __setup("boot_delay=", boot_delay_setup);
230 static void boot_delay_msec(void)
232 unsigned long long k;
233 unsigned long timeout;
235 if (boot_delay == 0 || system_state != SYSTEM_BOOTING)
236 return;
238 k = (unsigned long long)loops_per_msec * boot_delay;
240 timeout = jiffies + msecs_to_jiffies(boot_delay);
241 while (k) {
242 k--;
243 cpu_relax();
245 * use (volatile) jiffies to prevent
246 * compiler reduction; loop termination via jiffies
247 * is secondary and may or may not happen.
249 if (time_after(jiffies, timeout))
250 break;
251 touch_nmi_watchdog();
254 #else
255 static inline void boot_delay_msec(void)
258 #endif
261 * Commands to do_syslog:
263 * 0 -- Close the log. Currently a NOP.
264 * 1 -- Open the log. Currently a NOP.
265 * 2 -- Read from the log.
266 * 3 -- Read all messages remaining in the ring buffer.
267 * 4 -- Read and clear all messages remaining in the ring buffer
268 * 5 -- Clear ring buffer.
269 * 6 -- Disable printk's to console
270 * 7 -- Enable printk's to console
271 * 8 -- Set level of messages printed to console
272 * 9 -- Return number of unread characters in the log buffer
273 * 10 -- Return size of the log buffer
275 int do_syslog(int type, char __user *buf, int len)
277 unsigned i, j, limit, count;
278 int do_clear = 0;
279 char c;
280 int error = 0;
282 error = security_syslog(type);
283 if (error)
284 return error;
286 switch (type) {
287 case 0: /* Close log */
288 break;
289 case 1: /* Open log */
290 break;
291 case 2: /* Read from log */
292 error = -EINVAL;
293 if (!buf || len < 0)
294 goto out;
295 error = 0;
296 if (!len)
297 goto out;
298 if (!access_ok(VERIFY_WRITE, buf, len)) {
299 error = -EFAULT;
300 goto out;
302 error = wait_event_interruptible(log_wait,
303 (log_start - log_end));
304 if (error)
305 goto out;
306 i = 0;
307 spin_lock_irq(&logbuf_lock);
308 while (!error && (log_start != log_end) && i < len) {
309 c = LOG_BUF(log_start);
310 log_start++;
311 spin_unlock_irq(&logbuf_lock);
312 error = __put_user(c,buf);
313 buf++;
314 i++;
315 cond_resched();
316 spin_lock_irq(&logbuf_lock);
318 spin_unlock_irq(&logbuf_lock);
319 if (!error)
320 error = i;
321 break;
322 case 4: /* Read/clear last kernel messages */
323 do_clear = 1;
324 /* FALL THRU */
325 case 3: /* Read last kernel messages */
326 error = -EINVAL;
327 if (!buf || len < 0)
328 goto out;
329 error = 0;
330 if (!len)
331 goto out;
332 if (!access_ok(VERIFY_WRITE, buf, len)) {
333 error = -EFAULT;
334 goto out;
336 count = len;
337 if (count > log_buf_len)
338 count = log_buf_len;
339 spin_lock_irq(&logbuf_lock);
340 if (count > logged_chars)
341 count = logged_chars;
342 if (do_clear)
343 logged_chars = 0;
344 limit = log_end;
346 * __put_user() could sleep, and while we sleep
347 * printk() could overwrite the messages
348 * we try to copy to user space. Therefore
349 * the messages are copied in reverse. <manfreds>
351 for (i = 0; i < count && !error; i++) {
352 j = limit-1-i;
353 if (j + log_buf_len < log_end)
354 break;
355 c = LOG_BUF(j);
356 spin_unlock_irq(&logbuf_lock);
357 error = __put_user(c,&buf[count-1-i]);
358 cond_resched();
359 spin_lock_irq(&logbuf_lock);
361 spin_unlock_irq(&logbuf_lock);
362 if (error)
363 break;
364 error = i;
365 if (i != count) {
366 int offset = count-error;
367 /* buffer overflow during copy, correct user buffer. */
368 for (i = 0; i < error; i++) {
369 if (__get_user(c,&buf[i+offset]) ||
370 __put_user(c,&buf[i])) {
371 error = -EFAULT;
372 break;
374 cond_resched();
377 break;
378 case 5: /* Clear ring buffer */
379 logged_chars = 0;
380 break;
381 case 6: /* Disable logging to console */
382 if (saved_console_loglevel == -1)
383 saved_console_loglevel = console_loglevel;
384 console_loglevel = minimum_console_loglevel;
385 break;
386 case 7: /* Enable logging to console */
387 if (saved_console_loglevel != -1) {
388 console_loglevel = saved_console_loglevel;
389 saved_console_loglevel = -1;
391 break;
392 case 8: /* Set level of messages printed to console */
393 error = -EINVAL;
394 if (len < 1 || len > 8)
395 goto out;
396 if (len < minimum_console_loglevel)
397 len = minimum_console_loglevel;
398 console_loglevel = len;
399 /* Implicitly re-enable logging to console */
400 saved_console_loglevel = -1;
401 error = 0;
402 break;
403 case 9: /* Number of chars in the log buffer */
404 error = log_end - log_start;
405 break;
406 case 10: /* Size of the log buffer */
407 error = log_buf_len;
408 break;
409 default:
410 error = -EINVAL;
411 break;
413 out:
414 return error;
417 SYSCALL_DEFINE3(syslog, int, type, char __user *, buf, int, len)
419 return do_syslog(type, buf, len);
423 * Call the console drivers on a range of log_buf
425 static void __call_console_drivers(unsigned start, unsigned end)
427 struct console *con;
429 for_each_console(con) {
430 if ((con->flags & CON_ENABLED) && con->write &&
431 (cpu_online(smp_processor_id()) ||
432 (con->flags & CON_ANYTIME)))
433 con->write(con, &LOG_BUF(start), end - start);
437 static int __read_mostly ignore_loglevel;
439 static int __init ignore_loglevel_setup(char *str)
441 ignore_loglevel = 1;
442 printk(KERN_INFO "debug: ignoring loglevel setting.\n");
444 return 0;
447 early_param("ignore_loglevel", ignore_loglevel_setup);
450 * Write out chars from start to end - 1 inclusive
452 static void _call_console_drivers(unsigned start,
453 unsigned end, int msg_log_level)
455 if ((msg_log_level < console_loglevel || ignore_loglevel) &&
456 console_drivers && start != end) {
457 if ((start & LOG_BUF_MASK) > (end & LOG_BUF_MASK)) {
458 /* wrapped write */
459 __call_console_drivers(start & LOG_BUF_MASK,
460 log_buf_len);
461 __call_console_drivers(0, end & LOG_BUF_MASK);
462 } else {
463 __call_console_drivers(start, end);
469 * Call the console drivers, asking them to write out
470 * log_buf[start] to log_buf[end - 1].
471 * The console_sem must be held.
473 static void call_console_drivers(unsigned start, unsigned end)
475 unsigned cur_index, start_print;
476 static int msg_level = -1;
478 BUG_ON(((int)(start - end)) > 0);
480 cur_index = start;
481 start_print = start;
482 while (cur_index != end) {
483 if (msg_level < 0 && ((end - cur_index) > 2) &&
484 LOG_BUF(cur_index + 0) == '<' &&
485 LOG_BUF(cur_index + 1) >= '0' &&
486 LOG_BUF(cur_index + 1) <= '7' &&
487 LOG_BUF(cur_index + 2) == '>') {
488 msg_level = LOG_BUF(cur_index + 1) - '0';
489 cur_index += 3;
490 start_print = cur_index;
492 while (cur_index != end) {
493 char c = LOG_BUF(cur_index);
495 cur_index++;
496 if (c == '\n') {
497 if (msg_level < 0) {
499 * printk() has already given us loglevel tags in
500 * the buffer. This code is here in case the
501 * log buffer has wrapped right round and scribbled
502 * on those tags
504 msg_level = default_message_loglevel;
506 _call_console_drivers(start_print, cur_index, msg_level);
507 msg_level = -1;
508 start_print = cur_index;
509 break;
513 _call_console_drivers(start_print, end, msg_level);
516 static void emit_log_char(char c)
518 LOG_BUF(log_end) = c;
519 log_end++;
520 if (log_end - log_start > log_buf_len)
521 log_start = log_end - log_buf_len;
522 if (log_end - con_start > log_buf_len)
523 con_start = log_end - log_buf_len;
524 if (logged_chars < log_buf_len)
525 logged_chars++;
529 * Zap console related locks when oopsing. Only zap at most once
530 * every 10 seconds, to leave time for slow consoles to print a
531 * full oops.
533 static void zap_locks(void)
535 static unsigned long oops_timestamp;
537 if (time_after_eq(jiffies, oops_timestamp) &&
538 !time_after(jiffies, oops_timestamp + 30 * HZ))
539 return;
541 oops_timestamp = jiffies;
543 /* If a crash is occurring, make sure we can't deadlock */
544 spin_lock_init(&logbuf_lock);
545 /* And make sure that we print immediately */
546 init_MUTEX(&console_sem);
549 #if defined(CONFIG_PRINTK_TIME)
550 static int printk_time = 1;
551 #else
552 static int printk_time = 0;
553 #endif
554 module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR);
556 /* Check if we have any console registered that can be called early in boot. */
557 static int have_callable_console(void)
559 struct console *con;
561 for_each_console(con)
562 if (con->flags & CON_ANYTIME)
563 return 1;
565 return 0;
569 * printk - print a kernel message
570 * @fmt: format string
572 * This is printk(). It can be called from any context. We want it to work.
574 * We try to grab the console_sem. If we succeed, it's easy - we log the output and
575 * call the console drivers. If we fail to get the semaphore we place the output
576 * into the log buffer and return. The current holder of the console_sem will
577 * notice the new output in release_console_sem() and will send it to the
578 * consoles before releasing the semaphore.
580 * One effect of this deferred printing is that code which calls printk() and
581 * then changes console_loglevel may break. This is because console_loglevel
582 * is inspected when the actual printing occurs.
584 * See also:
585 * printf(3)
587 * See the vsnprintf() documentation for format string extensions over C99.
590 asmlinkage int printk(const char *fmt, ...)
592 va_list args;
593 int r;
595 va_start(args, fmt);
596 r = vprintk(fmt, args);
597 va_end(args);
599 return r;
602 /* cpu currently holding logbuf_lock */
603 static volatile unsigned int printk_cpu = UINT_MAX;
606 * Can we actually use the console at this time on this cpu?
608 * Console drivers may assume that per-cpu resources have
609 * been allocated. So unless they're explicitly marked as
610 * being able to cope (CON_ANYTIME) don't call them until
611 * this CPU is officially up.
613 static inline int can_use_console(unsigned int cpu)
615 return cpu_online(cpu) || have_callable_console();
619 * Try to get console ownership to actually show the kernel
620 * messages from a 'printk'. Return true (and with the
621 * console_semaphore held, and 'console_locked' set) if it
622 * is successful, false otherwise.
624 * This gets called with the 'logbuf_lock' spinlock held and
625 * interrupts disabled. It should return with 'lockbuf_lock'
626 * released but interrupts still disabled.
628 static int acquire_console_semaphore_for_printk(unsigned int cpu)
630 int retval = 0;
632 if (!try_acquire_console_sem()) {
633 retval = 1;
636 * If we can't use the console, we need to release
637 * the console semaphore by hand to avoid flushing
638 * the buffer. We need to hold the console semaphore
639 * in order to do this test safely.
641 if (!can_use_console(cpu)) {
642 console_locked = 0;
643 up(&console_sem);
644 retval = 0;
647 printk_cpu = UINT_MAX;
648 spin_unlock(&logbuf_lock);
649 return retval;
651 static const char recursion_bug_msg [] =
652 KERN_CRIT "BUG: recent printk recursion!\n";
653 static int recursion_bug;
654 static int new_text_line = 1;
655 static char printk_buf[1024];
657 int printk_delay_msec __read_mostly;
659 static inline void printk_delay(void)
661 if (unlikely(printk_delay_msec)) {
662 int m = printk_delay_msec;
664 while (m--) {
665 mdelay(1);
666 touch_nmi_watchdog();
671 asmlinkage int vprintk(const char *fmt, va_list args)
673 int printed_len = 0;
674 int current_log_level = default_message_loglevel;
675 unsigned long flags;
676 int this_cpu;
677 char *p;
679 boot_delay_msec();
680 printk_delay();
682 preempt_disable();
683 /* This stops the holder of console_sem just where we want him */
684 raw_local_irq_save(flags);
685 this_cpu = smp_processor_id();
688 * Ouch, printk recursed into itself!
690 if (unlikely(printk_cpu == this_cpu)) {
692 * If a crash is occurring during printk() on this CPU,
693 * then try to get the crash message out but make sure
694 * we can't deadlock. Otherwise just return to avoid the
695 * recursion and return - but flag the recursion so that
696 * it can be printed at the next appropriate moment:
698 if (!oops_in_progress) {
699 recursion_bug = 1;
700 goto out_restore_irqs;
702 zap_locks();
705 lockdep_off();
706 spin_lock(&logbuf_lock);
707 printk_cpu = this_cpu;
709 if (recursion_bug) {
710 recursion_bug = 0;
711 strcpy(printk_buf, recursion_bug_msg);
712 printed_len = strlen(recursion_bug_msg);
714 /* Emit the output into the temporary buffer */
715 printed_len += vscnprintf(printk_buf + printed_len,
716 sizeof(printk_buf) - printed_len, fmt, args);
719 p = printk_buf;
721 /* Do we have a loglevel in the string? */
722 if (p[0] == '<') {
723 unsigned char c = p[1];
724 if (c && p[2] == '>') {
725 switch (c) {
726 case '0' ... '7': /* loglevel */
727 current_log_level = c - '0';
728 /* Fallthrough - make sure we're on a new line */
729 case 'd': /* KERN_DEFAULT */
730 if (!new_text_line) {
731 emit_log_char('\n');
732 new_text_line = 1;
734 /* Fallthrough - skip the loglevel */
735 case 'c': /* KERN_CONT */
736 p += 3;
737 break;
743 * Copy the output into log_buf. If the caller didn't provide
744 * appropriate log level tags, we insert them here
746 for ( ; *p; p++) {
747 if (new_text_line) {
748 /* Always output the token */
749 emit_log_char('<');
750 emit_log_char(current_log_level + '0');
751 emit_log_char('>');
752 printed_len += 3;
753 new_text_line = 0;
755 if (printk_time) {
756 /* Follow the token with the time */
757 char tbuf[50], *tp;
758 unsigned tlen;
759 unsigned long long t;
760 unsigned long nanosec_rem;
762 t = cpu_clock(printk_cpu);
763 nanosec_rem = do_div(t, 1000000000);
764 tlen = sprintf(tbuf, "[%5lu.%06lu] ",
765 (unsigned long) t,
766 nanosec_rem / 1000);
768 for (tp = tbuf; tp < tbuf + tlen; tp++)
769 emit_log_char(*tp);
770 printed_len += tlen;
773 if (!*p)
774 break;
777 emit_log_char(*p);
778 if (*p == '\n')
779 new_text_line = 1;
783 * Try to acquire and then immediately release the
784 * console semaphore. The release will do all the
785 * actual magic (print out buffers, wake up klogd,
786 * etc).
788 * The acquire_console_semaphore_for_printk() function
789 * will release 'logbuf_lock' regardless of whether it
790 * actually gets the semaphore or not.
792 if (acquire_console_semaphore_for_printk(this_cpu))
793 release_console_sem();
795 lockdep_on();
796 out_restore_irqs:
797 raw_local_irq_restore(flags);
799 preempt_enable();
800 return printed_len;
802 EXPORT_SYMBOL(printk);
803 EXPORT_SYMBOL(vprintk);
805 #else
807 static void call_console_drivers(unsigned start, unsigned end)
811 #endif
813 static int __add_preferred_console(char *name, int idx, char *options,
814 char *brl_options)
816 struct console_cmdline *c;
817 int i;
820 * See if this tty is not yet registered, and
821 * if we have a slot free.
823 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
824 if (strcmp(console_cmdline[i].name, name) == 0 &&
825 console_cmdline[i].index == idx) {
826 if (!brl_options)
827 selected_console = i;
828 return 0;
830 if (i == MAX_CMDLINECONSOLES)
831 return -E2BIG;
832 if (!brl_options)
833 selected_console = i;
834 c = &console_cmdline[i];
835 strlcpy(c->name, name, sizeof(c->name));
836 c->options = options;
837 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
838 c->brl_options = brl_options;
839 #endif
840 c->index = idx;
841 return 0;
844 * Set up a list of consoles. Called from init/main.c
846 static int __init console_setup(char *str)
848 char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for index */
849 char *s, *options, *brl_options = NULL;
850 int idx;
852 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
853 if (!memcmp(str, "brl,", 4)) {
854 brl_options = "";
855 str += 4;
856 } else if (!memcmp(str, "brl=", 4)) {
857 brl_options = str + 4;
858 str = strchr(brl_options, ',');
859 if (!str) {
860 printk(KERN_ERR "need port name after brl=\n");
861 return 1;
863 *(str++) = 0;
865 #endif
868 * Decode str into name, index, options.
870 if (str[0] >= '0' && str[0] <= '9') {
871 strcpy(buf, "ttyS");
872 strncpy(buf + 4, str, sizeof(buf) - 5);
873 } else {
874 strncpy(buf, str, sizeof(buf) - 1);
876 buf[sizeof(buf) - 1] = 0;
877 if ((options = strchr(str, ',')) != NULL)
878 *(options++) = 0;
879 #ifdef __sparc__
880 if (!strcmp(str, "ttya"))
881 strcpy(buf, "ttyS0");
882 if (!strcmp(str, "ttyb"))
883 strcpy(buf, "ttyS1");
884 #endif
885 for (s = buf; *s; s++)
886 if ((*s >= '0' && *s <= '9') || *s == ',')
887 break;
888 idx = simple_strtoul(s, NULL, 10);
889 *s = 0;
891 __add_preferred_console(buf, idx, options, brl_options);
892 console_set_on_cmdline = 1;
893 return 1;
895 __setup("console=", console_setup);
898 * add_preferred_console - add a device to the list of preferred consoles.
899 * @name: device name
900 * @idx: device index
901 * @options: options for this console
903 * The last preferred console added will be used for kernel messages
904 * and stdin/out/err for init. Normally this is used by console_setup
905 * above to handle user-supplied console arguments; however it can also
906 * be used by arch-specific code either to override the user or more
907 * commonly to provide a default console (ie from PROM variables) when
908 * the user has not supplied one.
910 int add_preferred_console(char *name, int idx, char *options)
912 return __add_preferred_console(name, idx, options, NULL);
915 int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options)
917 struct console_cmdline *c;
918 int i;
920 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
921 if (strcmp(console_cmdline[i].name, name) == 0 &&
922 console_cmdline[i].index == idx) {
923 c = &console_cmdline[i];
924 strlcpy(c->name, name_new, sizeof(c->name));
925 c->name[sizeof(c->name) - 1] = 0;
926 c->options = options;
927 c->index = idx_new;
928 return i;
930 /* not found */
931 return -1;
934 int console_suspend_enabled = 1;
935 EXPORT_SYMBOL(console_suspend_enabled);
937 static int __init console_suspend_disable(char *str)
939 console_suspend_enabled = 0;
940 return 1;
942 __setup("no_console_suspend", console_suspend_disable);
945 * suspend_console - suspend the console subsystem
947 * This disables printk() while we go into suspend states
949 void suspend_console(void)
951 if (!console_suspend_enabled)
952 return;
953 printk("Suspending console(s) (use no_console_suspend to debug)\n");
954 acquire_console_sem();
955 console_suspended = 1;
956 up(&console_sem);
959 void resume_console(void)
961 if (!console_suspend_enabled)
962 return;
963 down(&console_sem);
964 console_suspended = 0;
965 release_console_sem();
969 * acquire_console_sem - lock the console system for exclusive use.
971 * Acquires a semaphore which guarantees that the caller has
972 * exclusive access to the console system and the console_drivers list.
974 * Can sleep, returns nothing.
976 void acquire_console_sem(void)
978 BUG_ON(in_interrupt());
979 down(&console_sem);
980 if (console_suspended)
981 return;
982 console_locked = 1;
983 console_may_schedule = 1;
985 EXPORT_SYMBOL(acquire_console_sem);
987 int try_acquire_console_sem(void)
989 if (down_trylock(&console_sem))
990 return -1;
991 if (console_suspended) {
992 up(&console_sem);
993 return -1;
995 console_locked = 1;
996 console_may_schedule = 0;
997 return 0;
999 EXPORT_SYMBOL(try_acquire_console_sem);
1001 int is_console_locked(void)
1003 return console_locked;
1006 static DEFINE_PER_CPU(int, printk_pending);
1008 void printk_tick(void)
1010 if (__get_cpu_var(printk_pending)) {
1011 __get_cpu_var(printk_pending) = 0;
1012 wake_up_interruptible(&log_wait);
1016 int printk_needs_cpu(int cpu)
1018 return per_cpu(printk_pending, cpu);
1021 void wake_up_klogd(void)
1023 if (waitqueue_active(&log_wait))
1024 __raw_get_cpu_var(printk_pending) = 1;
1028 * release_console_sem - unlock the console system
1030 * Releases the semaphore which the caller holds on the console system
1031 * and the console driver list.
1033 * While the semaphore was held, console output may have been buffered
1034 * by printk(). If this is the case, release_console_sem() emits
1035 * the output prior to releasing the semaphore.
1037 * If there is output waiting for klogd, we wake it up.
1039 * release_console_sem() may be called from any context.
1041 void release_console_sem(void)
1043 unsigned long flags;
1044 unsigned _con_start, _log_end;
1045 unsigned wake_klogd = 0;
1047 if (console_suspended) {
1048 up(&console_sem);
1049 return;
1052 console_may_schedule = 0;
1054 for ( ; ; ) {
1055 spin_lock_irqsave(&logbuf_lock, flags);
1056 wake_klogd |= log_start - log_end;
1057 if (con_start == log_end)
1058 break; /* Nothing to print */
1059 _con_start = con_start;
1060 _log_end = log_end;
1061 con_start = log_end; /* Flush */
1062 spin_unlock(&logbuf_lock);
1063 stop_critical_timings(); /* don't trace print latency */
1064 call_console_drivers(_con_start, _log_end);
1065 start_critical_timings();
1066 local_irq_restore(flags);
1068 console_locked = 0;
1069 up(&console_sem);
1070 spin_unlock_irqrestore(&logbuf_lock, flags);
1071 if (wake_klogd)
1072 wake_up_klogd();
1074 EXPORT_SYMBOL(release_console_sem);
1077 * console_conditional_schedule - yield the CPU if required
1079 * If the console code is currently allowed to sleep, and
1080 * if this CPU should yield the CPU to another task, do
1081 * so here.
1083 * Must be called within acquire_console_sem().
1085 void __sched console_conditional_schedule(void)
1087 if (console_may_schedule)
1088 cond_resched();
1090 EXPORT_SYMBOL(console_conditional_schedule);
1092 void console_unblank(void)
1094 struct console *c;
1097 * console_unblank can no longer be called in interrupt context unless
1098 * oops_in_progress is set to 1..
1100 if (oops_in_progress) {
1101 if (down_trylock(&console_sem) != 0)
1102 return;
1103 } else
1104 acquire_console_sem();
1106 console_locked = 1;
1107 console_may_schedule = 0;
1108 for_each_console(c)
1109 if ((c->flags & CON_ENABLED) && c->unblank)
1110 c->unblank();
1111 release_console_sem();
1115 * Return the console tty driver structure and its associated index
1117 struct tty_driver *console_device(int *index)
1119 struct console *c;
1120 struct tty_driver *driver = NULL;
1122 acquire_console_sem();
1123 for_each_console(c) {
1124 if (!c->device)
1125 continue;
1126 driver = c->device(c, index);
1127 if (driver)
1128 break;
1130 release_console_sem();
1131 return driver;
1135 * Prevent further output on the passed console device so that (for example)
1136 * serial drivers can disable console output before suspending a port, and can
1137 * re-enable output afterwards.
1139 void console_stop(struct console *console)
1141 acquire_console_sem();
1142 console->flags &= ~CON_ENABLED;
1143 release_console_sem();
1145 EXPORT_SYMBOL(console_stop);
1147 void console_start(struct console *console)
1149 acquire_console_sem();
1150 console->flags |= CON_ENABLED;
1151 release_console_sem();
1153 EXPORT_SYMBOL(console_start);
1156 * The console driver calls this routine during kernel initialization
1157 * to register the console printing procedure with printk() and to
1158 * print any messages that were printed by the kernel before the
1159 * console driver was initialized.
1161 * This can happen pretty early during the boot process (because of
1162 * early_printk) - sometimes before setup_arch() completes - be careful
1163 * of what kernel features are used - they may not be initialised yet.
1165 * There are two types of consoles - bootconsoles (early_printk) and
1166 * "real" consoles (everything which is not a bootconsole) which are
1167 * handled differently.
1168 * - Any number of bootconsoles can be registered at any time.
1169 * - As soon as a "real" console is registered, all bootconsoles
1170 * will be unregistered automatically.
1171 * - Once a "real" console is registered, any attempt to register a
1172 * bootconsoles will be rejected
1174 void register_console(struct console *newcon)
1176 int i;
1177 unsigned long flags;
1178 struct console *bcon = NULL;
1181 * before we register a new CON_BOOT console, make sure we don't
1182 * already have a valid console
1184 if (console_drivers && newcon->flags & CON_BOOT) {
1185 /* find the last or real console */
1186 for_each_console(bcon) {
1187 if (!(bcon->flags & CON_BOOT)) {
1188 printk(KERN_INFO "Too late to register bootconsole %s%d\n",
1189 newcon->name, newcon->index);
1190 return;
1195 if (console_drivers && console_drivers->flags & CON_BOOT)
1196 bcon = console_drivers;
1198 if (preferred_console < 0 || bcon || !console_drivers)
1199 preferred_console = selected_console;
1201 if (newcon->early_setup)
1202 newcon->early_setup();
1205 * See if we want to use this console driver. If we
1206 * didn't select a console we take the first one
1207 * that registers here.
1209 if (preferred_console < 0) {
1210 if (newcon->index < 0)
1211 newcon->index = 0;
1212 if (newcon->setup == NULL ||
1213 newcon->setup(newcon, NULL) == 0) {
1214 newcon->flags |= CON_ENABLED;
1215 if (newcon->device) {
1216 newcon->flags |= CON_CONSDEV;
1217 preferred_console = 0;
1223 * See if this console matches one we selected on
1224 * the command line.
1226 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0];
1227 i++) {
1228 if (strcmp(console_cmdline[i].name, newcon->name) != 0)
1229 continue;
1230 if (newcon->index >= 0 &&
1231 newcon->index != console_cmdline[i].index)
1232 continue;
1233 if (newcon->index < 0)
1234 newcon->index = console_cmdline[i].index;
1235 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1236 if (console_cmdline[i].brl_options) {
1237 newcon->flags |= CON_BRL;
1238 braille_register_console(newcon,
1239 console_cmdline[i].index,
1240 console_cmdline[i].options,
1241 console_cmdline[i].brl_options);
1242 return;
1244 #endif
1245 if (newcon->setup &&
1246 newcon->setup(newcon, console_cmdline[i].options) != 0)
1247 break;
1248 newcon->flags |= CON_ENABLED;
1249 newcon->index = console_cmdline[i].index;
1250 if (i == selected_console) {
1251 newcon->flags |= CON_CONSDEV;
1252 preferred_console = selected_console;
1254 break;
1257 if (!(newcon->flags & CON_ENABLED))
1258 return;
1261 * If we have a bootconsole, and are switching to a real console,
1262 * don't print everything out again, since when the boot console, and
1263 * the real console are the same physical device, it's annoying to
1264 * see the beginning boot messages twice
1266 if (bcon && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV))
1267 newcon->flags &= ~CON_PRINTBUFFER;
1270 * Put this console in the list - keep the
1271 * preferred driver at the head of the list.
1273 acquire_console_sem();
1274 if ((newcon->flags & CON_CONSDEV) || console_drivers == NULL) {
1275 newcon->next = console_drivers;
1276 console_drivers = newcon;
1277 if (newcon->next)
1278 newcon->next->flags &= ~CON_CONSDEV;
1279 } else {
1280 newcon->next = console_drivers->next;
1281 console_drivers->next = newcon;
1283 if (newcon->flags & CON_PRINTBUFFER) {
1285 * release_console_sem() will print out the buffered messages
1286 * for us.
1288 spin_lock_irqsave(&logbuf_lock, flags);
1289 con_start = log_start;
1290 spin_unlock_irqrestore(&logbuf_lock, flags);
1292 release_console_sem();
1295 * By unregistering the bootconsoles after we enable the real console
1296 * we get the "console xxx enabled" message on all the consoles -
1297 * boot consoles, real consoles, etc - this is to ensure that end
1298 * users know there might be something in the kernel's log buffer that
1299 * went to the bootconsole (that they do not see on the real console)
1301 if (bcon && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV)) {
1302 /* we need to iterate through twice, to make sure we print
1303 * everything out, before we unregister the console(s)
1305 printk(KERN_INFO "console [%s%d] enabled, bootconsole disabled\n",
1306 newcon->name, newcon->index);
1307 for_each_console(bcon)
1308 if (bcon->flags & CON_BOOT)
1309 unregister_console(bcon);
1310 } else {
1311 printk(KERN_INFO "%sconsole [%s%d] enabled\n",
1312 (newcon->flags & CON_BOOT) ? "boot" : "" ,
1313 newcon->name, newcon->index);
1316 EXPORT_SYMBOL(register_console);
1318 int unregister_console(struct console *console)
1320 struct console *a, *b;
1321 int res = 1;
1323 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1324 if (console->flags & CON_BRL)
1325 return braille_unregister_console(console);
1326 #endif
1328 acquire_console_sem();
1329 if (console_drivers == console) {
1330 console_drivers=console->next;
1331 res = 0;
1332 } else if (console_drivers) {
1333 for (a=console_drivers->next, b=console_drivers ;
1334 a; b=a, a=b->next) {
1335 if (a == console) {
1336 b->next = a->next;
1337 res = 0;
1338 break;
1344 * If this isn't the last console and it has CON_CONSDEV set, we
1345 * need to set it on the next preferred console.
1347 if (console_drivers != NULL && console->flags & CON_CONSDEV)
1348 console_drivers->flags |= CON_CONSDEV;
1350 release_console_sem();
1351 return res;
1353 EXPORT_SYMBOL(unregister_console);
1355 static int __init disable_boot_consoles(void)
1357 struct console *con;
1359 for_each_console(con) {
1360 if (con->flags & CON_BOOT) {
1361 printk(KERN_INFO "turn off boot console %s%d\n",
1362 con->name, con->index);
1363 unregister_console(con);
1366 return 0;
1368 late_initcall(disable_boot_consoles);
1370 #if defined CONFIG_PRINTK
1373 * printk rate limiting, lifted from the networking subsystem.
1375 * This enforces a rate limit: not more than 10 kernel messages
1376 * every 5s to make a denial-of-service attack impossible.
1378 DEFINE_RATELIMIT_STATE(printk_ratelimit_state, 5 * HZ, 10);
1380 int __printk_ratelimit(const char *func)
1382 return ___ratelimit(&printk_ratelimit_state, func);
1384 EXPORT_SYMBOL(__printk_ratelimit);
1387 * printk_timed_ratelimit - caller-controlled printk ratelimiting
1388 * @caller_jiffies: pointer to caller's state
1389 * @interval_msecs: minimum interval between prints
1391 * printk_timed_ratelimit() returns true if more than @interval_msecs
1392 * milliseconds have elapsed since the last time printk_timed_ratelimit()
1393 * returned true.
1395 bool printk_timed_ratelimit(unsigned long *caller_jiffies,
1396 unsigned int interval_msecs)
1398 if (*caller_jiffies == 0
1399 || !time_in_range(jiffies, *caller_jiffies,
1400 *caller_jiffies
1401 + msecs_to_jiffies(interval_msecs))) {
1402 *caller_jiffies = jiffies;
1403 return true;
1405 return false;
1407 EXPORT_SYMBOL(printk_timed_ratelimit);
1408 #endif