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).
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>
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>
36 #include <asm/uaccess.h>
39 * Architectures can override it:
41 void asmlinkage __attribute__((weak)) early_printk(const char *fmt, ...)
45 #define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT)
47 #ifdef CONFIG_DEBUG_LL
48 extern void printascii(char *);
51 /* printk's without a loglevel use this.. */
52 #define DEFAULT_MESSAGE_LOGLEVEL 4 /* KERN_WARNING */
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 */
58 DECLARE_WAIT_QUEUE_HEAD(log_wait);
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 */
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.
72 EXPORT_SYMBOL(oops_in_progress);
75 * console_sem protects the console_drivers list, and also
76 * provides serialisation for access to the entire console
79 static DECLARE_MUTEX(console_sem);
80 struct console *console_drivers;
81 EXPORT_SYMBOL_GPL(console_drivers);
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
91 static int console_locked, console_suspended;
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().
98 static DEFINE_SPINLOCK(logbuf_lock);
100 #define LOG_BUF_MASK (log_buf_len-1)
101 #define LOG_BUF(idx) (log_buf[(idx) & LOG_BUF_MASK])
104 * The indices into log_buf are not constrained to log_buf_len - they
105 * must be masked before subscripting
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 */
112 * Array of consoles built from command line options (console=)
114 struct console_cmdline
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 */
124 #define MAX_CMDLINECONSOLES 8
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);
132 /* Flag: console code may call schedule() */
133 static int console_may_schedule;
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 */
142 static int __init log_buf_len_setup(char *str)
144 unsigned size = memparse(str, &str);
148 size = roundup_pow_of_two(size);
149 if (size > log_buf_len) {
150 unsigned start, dest_idx, offset;
153 new_log_buf = alloc_bootmem(size);
155 printk(KERN_WARNING "log_buf_len: allocation failed\n");
159 spin_lock_irqsave(&logbuf_lock, flags);
161 log_buf = new_log_buf;
163 offset = start = min(con_start, log_start);
165 while (start != log_end) {
166 log_buf[dest_idx] = __log_buf[start & (__LOG_BUF_LEN - 1)];
173 spin_unlock_irqrestore(&logbuf_lock, flags);
175 printk(KERN_NOTICE "log_buf_len: %d\n", log_buf_len);
181 __setup("log_buf_len=", log_buf_len_setup);
183 #ifdef CONFIG_BOOT_PRINTK_DELAY
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 */
188 static int __init boot_delay_setup(char *str)
191 unsigned long long loops_per_msec;
193 lpj = preset_lpj ? preset_lpj : 1000000; /* some guess */
194 loops_per_msec = (unsigned long long)lpj / 1000 * HZ;
196 get_option(&str, &boot_delay);
197 if (boot_delay > 10 * 1000)
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);
206 __setup("boot_delay=", boot_delay_setup);
208 static void boot_delay_msec(void)
210 unsigned long long k;
211 unsigned long timeout;
213 if (boot_delay == 0 || system_state != SYSTEM_BOOTING)
216 k = (unsigned long long)printk_delay_msec * boot_delay;
218 timeout = jiffies + msecs_to_jiffies(boot_delay);
223 * use (volatile) jiffies to prevent
224 * compiler reduction; loop termination via jiffies
225 * is secondary and may or may not happen.
227 if (time_after(jiffies, timeout))
229 touch_nmi_watchdog();
233 static inline void boot_delay_msec(void)
239 * Commands to do_syslog:
241 * 0 -- Close the log. Currently a NOP.
242 * 1 -- Open the log. Currently a NOP.
243 * 2 -- Read from the log.
244 * 3 -- Read all messages remaining in the ring buffer.
245 * 4 -- Read and clear all messages remaining in the ring buffer
246 * 5 -- Clear ring buffer.
247 * 6 -- Disable printk's to console
248 * 7 -- Enable printk's to console
249 * 8 -- Set level of messages printed to console
250 * 9 -- Return number of unread characters in the log buffer
251 * 10 -- Return size of the log buffer
253 int do_syslog(int type, char __user *buf, int len)
255 unsigned i, j, limit, count;
260 error = security_syslog(type);
265 case 0: /* Close log */
267 case 1: /* Open log */
269 case 2: /* Read from log */
276 if (!access_ok(VERIFY_WRITE, buf, len)) {
280 error = wait_event_interruptible(log_wait,
281 (log_start - log_end));
285 spin_lock_irq(&logbuf_lock);
286 while (!error && (log_start != log_end) && i < len) {
287 c = LOG_BUF(log_start);
289 spin_unlock_irq(&logbuf_lock);
290 error = __put_user(c,buf);
294 spin_lock_irq(&logbuf_lock);
296 spin_unlock_irq(&logbuf_lock);
300 case 4: /* Read/clear last kernel messages */
303 case 3: /* Read last kernel messages */
310 if (!access_ok(VERIFY_WRITE, buf, len)) {
315 if (count > log_buf_len)
317 spin_lock_irq(&logbuf_lock);
318 if (count > logged_chars)
319 count = logged_chars;
324 * __put_user() could sleep, and while we sleep
325 * printk() could overwrite the messages
326 * we try to copy to user space. Therefore
327 * the messages are copied in reverse. <manfreds>
329 for (i = 0; i < count && !error; i++) {
331 if (j + log_buf_len < log_end)
334 spin_unlock_irq(&logbuf_lock);
335 error = __put_user(c,&buf[count-1-i]);
337 spin_lock_irq(&logbuf_lock);
339 spin_unlock_irq(&logbuf_lock);
344 int offset = count-error;
345 /* buffer overflow during copy, correct user buffer. */
346 for (i = 0; i < error; i++) {
347 if (__get_user(c,&buf[i+offset]) ||
348 __put_user(c,&buf[i])) {
356 case 5: /* Clear ring buffer */
359 case 6: /* Disable logging to console */
360 console_loglevel = minimum_console_loglevel;
362 case 7: /* Enable logging to console */
363 console_loglevel = default_console_loglevel;
365 case 8: /* Set level of messages printed to console */
367 if (len < 1 || len > 8)
369 if (len < minimum_console_loglevel)
370 len = minimum_console_loglevel;
371 console_loglevel = len;
374 case 9: /* Number of chars in the log buffer */
375 error = log_end - log_start;
377 case 10: /* Size of the log buffer */
388 SYSCALL_DEFINE3(syslog, int, type, char __user *, buf, int, len)
390 return do_syslog(type, buf, len);
394 * Call the console drivers on a range of log_buf
396 static void __call_console_drivers(unsigned start, unsigned end)
400 for (con = console_drivers; con; con = con->next) {
401 if ((con->flags & CON_ENABLED) && con->write &&
402 (cpu_online(smp_processor_id()) ||
403 (con->flags & CON_ANYTIME)))
404 con->write(con, &LOG_BUF(start), end - start);
408 static int __read_mostly ignore_loglevel;
410 static int __init ignore_loglevel_setup(char *str)
413 printk(KERN_INFO "debug: ignoring loglevel setting.\n");
418 early_param("ignore_loglevel", ignore_loglevel_setup);
421 * Write out chars from start to end - 1 inclusive
423 static void _call_console_drivers(unsigned start,
424 unsigned end, int msg_log_level)
426 if ((msg_log_level < console_loglevel || ignore_loglevel) &&
427 console_drivers && start != end) {
428 if ((start & LOG_BUF_MASK) > (end & LOG_BUF_MASK)) {
430 __call_console_drivers(start & LOG_BUF_MASK,
432 __call_console_drivers(0, end & LOG_BUF_MASK);
434 __call_console_drivers(start, end);
440 * Call the console drivers, asking them to write out
441 * log_buf[start] to log_buf[end - 1].
442 * The console_sem must be held.
444 static void call_console_drivers(unsigned start, unsigned end)
446 unsigned cur_index, start_print;
447 static int msg_level = -1;
449 BUG_ON(((int)(start - end)) > 0);
453 while (cur_index != end) {
454 if (msg_level < 0 && ((end - cur_index) > 2) &&
455 LOG_BUF(cur_index + 0) == '<' &&
456 LOG_BUF(cur_index + 1) >= '0' &&
457 LOG_BUF(cur_index + 1) <= '7' &&
458 LOG_BUF(cur_index + 2) == '>') {
459 msg_level = LOG_BUF(cur_index + 1) - '0';
461 start_print = cur_index;
463 while (cur_index != end) {
464 char c = LOG_BUF(cur_index);
470 * printk() has already given us loglevel tags in
471 * the buffer. This code is here in case the
472 * log buffer has wrapped right round and scribbled
475 msg_level = default_message_loglevel;
477 _call_console_drivers(start_print, cur_index, msg_level);
479 start_print = cur_index;
484 _call_console_drivers(start_print, end, msg_level);
487 static void emit_log_char(char c)
489 LOG_BUF(log_end) = c;
491 if (log_end - log_start > log_buf_len)
492 log_start = log_end - log_buf_len;
493 if (log_end - con_start > log_buf_len)
494 con_start = log_end - log_buf_len;
495 if (logged_chars < log_buf_len)
500 * Zap console related locks when oopsing. Only zap at most once
501 * every 10 seconds, to leave time for slow consoles to print a
504 static void zap_locks(void)
506 static unsigned long oops_timestamp;
508 if (time_after_eq(jiffies, oops_timestamp) &&
509 !time_after(jiffies, oops_timestamp + 30 * HZ))
512 oops_timestamp = jiffies;
514 /* If a crash is occurring, make sure we can't deadlock */
515 spin_lock_init(&logbuf_lock);
516 /* And make sure that we print immediately */
517 init_MUTEX(&console_sem);
520 #if defined(CONFIG_PRINTK_TIME)
521 static int printk_time = 1;
523 static int printk_time = 0;
525 module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR);
527 /* Check if we have any console registered that can be called early in boot. */
528 static int have_callable_console(void)
532 for (con = console_drivers; con; con = con->next)
533 if (con->flags & CON_ANYTIME)
540 * printk - print a kernel message
541 * @fmt: format string
543 * This is printk(). It can be called from any context. We want it to work.
545 * We try to grab the console_sem. If we succeed, it's easy - we log the output and
546 * call the console drivers. If we fail to get the semaphore we place the output
547 * into the log buffer and return. The current holder of the console_sem will
548 * notice the new output in release_console_sem() and will send it to the
549 * consoles before releasing the semaphore.
551 * One effect of this deferred printing is that code which calls printk() and
552 * then changes console_loglevel may break. This is because console_loglevel
553 * is inspected when the actual printing occurs.
558 * See the vsnprintf() documentation for format string extensions over C99.
561 asmlinkage int printk(const char *fmt, ...)
567 r = vprintk(fmt, args);
573 /* cpu currently holding logbuf_lock */
574 static volatile unsigned int printk_cpu = UINT_MAX;
577 * Can we actually use the console at this time on this cpu?
579 * Console drivers may assume that per-cpu resources have
580 * been allocated. So unless they're explicitly marked as
581 * being able to cope (CON_ANYTIME) don't call them until
582 * this CPU is officially up.
584 static inline int can_use_console(unsigned int cpu)
586 return cpu_online(cpu) || have_callable_console();
590 * Try to get console ownership to actually show the kernel
591 * messages from a 'printk'. Return true (and with the
592 * console_semaphore held, and 'console_locked' set) if it
593 * is successful, false otherwise.
595 * This gets called with the 'logbuf_lock' spinlock held and
596 * interrupts disabled. It should return with 'lockbuf_lock'
597 * released but interrupts still disabled.
599 static int acquire_console_semaphore_for_printk(unsigned int cpu)
603 if (!try_acquire_console_sem()) {
607 * If we can't use the console, we need to release
608 * the console semaphore by hand to avoid flushing
609 * the buffer. We need to hold the console semaphore
610 * in order to do this test safely.
612 if (!can_use_console(cpu)) {
618 printk_cpu = UINT_MAX;
619 spin_unlock(&logbuf_lock);
622 static const char recursion_bug_msg [] =
623 KERN_CRIT "BUG: recent printk recursion!\n";
624 static int recursion_bug;
625 static int new_text_line = 1;
626 static char printk_buf[1024];
628 asmlinkage int vprintk(const char *fmt, va_list args)
631 int current_log_level = default_message_loglevel;
639 /* This stops the holder of console_sem just where we want him */
640 raw_local_irq_save(flags);
641 this_cpu = smp_processor_id();
644 * Ouch, printk recursed into itself!
646 if (unlikely(printk_cpu == this_cpu)) {
648 * If a crash is occurring during printk() on this CPU,
649 * then try to get the crash message out but make sure
650 * we can't deadlock. Otherwise just return to avoid the
651 * recursion and return - but flag the recursion so that
652 * it can be printed at the next appropriate moment:
654 if (!oops_in_progress) {
656 goto out_restore_irqs;
662 spin_lock(&logbuf_lock);
663 printk_cpu = this_cpu;
667 strcpy(printk_buf, recursion_bug_msg);
668 printed_len = strlen(recursion_bug_msg);
670 /* Emit the output into the temporary buffer */
671 printed_len += vscnprintf(printk_buf + printed_len,
672 sizeof(printk_buf) - printed_len, fmt, args);
674 #ifdef CONFIG_DEBUG_LL
675 printascii(printk_buf);
679 * Copy the output into log_buf. If the caller didn't provide
680 * appropriate log level tags, we insert them here
682 for (p = printk_buf; *p; p++) {
684 /* If a token, set current_log_level and skip over */
685 if (p[0] == '<' && p[1] >= '0' && p[1] <= '7' &&
687 current_log_level = p[1] - '0';
692 /* Always output the token */
694 emit_log_char(current_log_level + '0');
700 /* Follow the token with the time */
703 unsigned long long t;
704 unsigned long nanosec_rem;
706 t = cpu_clock(printk_cpu);
707 nanosec_rem = do_div(t, 1000000000);
708 tlen = sprintf(tbuf, "[%5lu.%06lu] ",
712 for (tp = tbuf; tp < tbuf + tlen; tp++)
727 * Try to acquire and then immediately release the
728 * console semaphore. The release will do all the
729 * actual magic (print out buffers, wake up klogd,
732 * The acquire_console_semaphore_for_printk() function
733 * will release 'logbuf_lock' regardless of whether it
734 * actually gets the semaphore or not.
736 if (acquire_console_semaphore_for_printk(this_cpu))
737 release_console_sem();
741 raw_local_irq_restore(flags);
746 EXPORT_SYMBOL(printk);
747 EXPORT_SYMBOL(vprintk);
751 static void call_console_drivers(unsigned start, unsigned end)
757 static int __add_preferred_console(char *name, int idx, char *options,
760 struct console_cmdline *c;
764 * See if this tty is not yet registered, and
765 * if we have a slot free.
767 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
768 if (strcmp(console_cmdline[i].name, name) == 0 &&
769 console_cmdline[i].index == idx) {
771 selected_console = i;
774 if (i == MAX_CMDLINECONSOLES)
777 selected_console = i;
778 c = &console_cmdline[i];
779 strlcpy(c->name, name, sizeof(c->name));
780 c->options = options;
781 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
782 c->brl_options = brl_options;
788 * Set up a list of consoles. Called from init/main.c
790 static int __init console_setup(char *str)
792 char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for index */
793 char *s, *options, *brl_options = NULL;
796 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
797 if (!memcmp(str, "brl,", 4)) {
800 } else if (!memcmp(str, "brl=", 4)) {
801 brl_options = str + 4;
802 str = strchr(brl_options, ',');
804 printk(KERN_ERR "need port name after brl=\n");
812 * Decode str into name, index, options.
814 if (str[0] >= '0' && str[0] <= '9') {
816 strncpy(buf + 4, str, sizeof(buf) - 5);
818 strncpy(buf, str, sizeof(buf) - 1);
820 buf[sizeof(buf) - 1] = 0;
821 if ((options = strchr(str, ',')) != NULL)
824 if (!strcmp(str, "ttya"))
825 strcpy(buf, "ttyS0");
826 if (!strcmp(str, "ttyb"))
827 strcpy(buf, "ttyS1");
829 for (s = buf; *s; s++)
830 if ((*s >= '0' && *s <= '9') || *s == ',')
832 idx = simple_strtoul(s, NULL, 10);
835 __add_preferred_console(buf, idx, options, brl_options);
836 console_set_on_cmdline = 1;
839 __setup("console=", console_setup);
842 * add_preferred_console - add a device to the list of preferred consoles.
845 * @options: options for this console
847 * The last preferred console added will be used for kernel messages
848 * and stdin/out/err for init. Normally this is used by console_setup
849 * above to handle user-supplied console arguments; however it can also
850 * be used by arch-specific code either to override the user or more
851 * commonly to provide a default console (ie from PROM variables) when
852 * the user has not supplied one.
854 int add_preferred_console(char *name, int idx, char *options)
856 return __add_preferred_console(name, idx, options, NULL);
859 int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options)
861 struct console_cmdline *c;
864 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
865 if (strcmp(console_cmdline[i].name, name) == 0 &&
866 console_cmdline[i].index == idx) {
867 c = &console_cmdline[i];
868 strlcpy(c->name, name_new, sizeof(c->name));
869 c->name[sizeof(c->name) - 1] = 0;
870 c->options = options;
878 int console_suspend_enabled = 1;
879 EXPORT_SYMBOL(console_suspend_enabled);
881 static int __init console_suspend_disable(char *str)
883 console_suspend_enabled = 0;
886 __setup("no_console_suspend", console_suspend_disable);
889 * suspend_console - suspend the console subsystem
891 * This disables printk() while we go into suspend states
893 void suspend_console(void)
895 if (!console_suspend_enabled)
897 printk("Suspending console(s) (use no_console_suspend to debug)\n");
898 acquire_console_sem();
899 console_suspended = 1;
903 void resume_console(void)
905 if (!console_suspend_enabled)
908 console_suspended = 0;
909 release_console_sem();
913 * acquire_console_sem - lock the console system for exclusive use.
915 * Acquires a semaphore which guarantees that the caller has
916 * exclusive access to the console system and the console_drivers list.
918 * Can sleep, returns nothing.
920 void acquire_console_sem(void)
922 BUG_ON(in_interrupt());
924 if (console_suspended)
927 console_may_schedule = 1;
929 EXPORT_SYMBOL(acquire_console_sem);
931 int try_acquire_console_sem(void)
933 if (down_trylock(&console_sem))
935 if (console_suspended) {
940 console_may_schedule = 0;
943 EXPORT_SYMBOL(try_acquire_console_sem);
945 int is_console_locked(void)
947 return console_locked;
950 static DEFINE_PER_CPU(int, printk_pending);
952 void printk_tick(void)
954 if (__get_cpu_var(printk_pending)) {
955 __get_cpu_var(printk_pending) = 0;
956 wake_up_interruptible(&log_wait);
960 int printk_needs_cpu(int cpu)
962 return per_cpu(printk_pending, cpu);
965 void wake_up_klogd(void)
967 if (waitqueue_active(&log_wait))
968 __raw_get_cpu_var(printk_pending) = 1;
972 * release_console_sem - unlock the console system
974 * Releases the semaphore which the caller holds on the console system
975 * and the console driver list.
977 * While the semaphore was held, console output may have been buffered
978 * by printk(). If this is the case, release_console_sem() emits
979 * the output prior to releasing the semaphore.
981 * If there is output waiting for klogd, we wake it up.
983 * release_console_sem() may be called from any context.
985 void release_console_sem(void)
988 unsigned _con_start, _log_end;
989 unsigned wake_klogd = 0;
991 if (console_suspended) {
996 console_may_schedule = 0;
999 spin_lock_irqsave(&logbuf_lock, flags);
1000 wake_klogd |= log_start - log_end;
1001 if (con_start == log_end)
1002 break; /* Nothing to print */
1003 _con_start = con_start;
1005 con_start = log_end; /* Flush */
1006 spin_unlock(&logbuf_lock);
1007 stop_critical_timings(); /* don't trace print latency */
1008 call_console_drivers(_con_start, _log_end);
1009 start_critical_timings();
1010 local_irq_restore(flags);
1014 spin_unlock_irqrestore(&logbuf_lock, flags);
1018 EXPORT_SYMBOL(release_console_sem);
1021 * console_conditional_schedule - yield the CPU if required
1023 * If the console code is currently allowed to sleep, and
1024 * if this CPU should yield the CPU to another task, do
1027 * Must be called within acquire_console_sem().
1029 void __sched console_conditional_schedule(void)
1031 if (console_may_schedule)
1034 EXPORT_SYMBOL(console_conditional_schedule);
1036 void console_print(const char *s)
1038 printk(KERN_EMERG "%s", s);
1040 EXPORT_SYMBOL(console_print);
1042 void console_unblank(void)
1047 * console_unblank can no longer be called in interrupt context unless
1048 * oops_in_progress is set to 1..
1050 if (oops_in_progress) {
1051 if (down_trylock(&console_sem) != 0)
1054 acquire_console_sem();
1057 console_may_schedule = 0;
1058 for (c = console_drivers; c != NULL; c = c->next)
1059 if ((c->flags & CON_ENABLED) && c->unblank)
1061 release_console_sem();
1065 * Return the console tty driver structure and its associated index
1067 struct tty_driver *console_device(int *index)
1070 struct tty_driver *driver = NULL;
1072 acquire_console_sem();
1073 for (c = console_drivers; c != NULL; c = c->next) {
1076 driver = c->device(c, index);
1080 release_console_sem();
1085 * Prevent further output on the passed console device so that (for example)
1086 * serial drivers can disable console output before suspending a port, and can
1087 * re-enable output afterwards.
1089 void console_stop(struct console *console)
1091 acquire_console_sem();
1092 console->flags &= ~CON_ENABLED;
1093 release_console_sem();
1095 EXPORT_SYMBOL(console_stop);
1097 void console_start(struct console *console)
1099 acquire_console_sem();
1100 console->flags |= CON_ENABLED;
1101 release_console_sem();
1103 EXPORT_SYMBOL(console_start);
1106 * The console driver calls this routine during kernel initialization
1107 * to register the console printing procedure with printk() and to
1108 * print any messages that were printed by the kernel before the
1109 * console driver was initialized.
1111 void register_console(struct console *console)
1114 unsigned long flags;
1115 struct console *bootconsole = NULL;
1117 if (console_drivers) {
1118 if (console->flags & CON_BOOT)
1120 if (console_drivers->flags & CON_BOOT)
1121 bootconsole = console_drivers;
1124 if (preferred_console < 0 || bootconsole || !console_drivers)
1125 preferred_console = selected_console;
1127 if (console->early_setup)
1128 console->early_setup();
1131 * See if we want to use this console driver. If we
1132 * didn't select a console we take the first one
1133 * that registers here.
1135 if (preferred_console < 0) {
1136 if (console->index < 0)
1138 if (console->setup == NULL ||
1139 console->setup(console, NULL) == 0) {
1140 console->flags |= CON_ENABLED;
1141 if (console->device) {
1142 console->flags |= CON_CONSDEV;
1143 preferred_console = 0;
1149 * See if this console matches one we selected on
1152 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0];
1154 if (strcmp(console_cmdline[i].name, console->name) != 0)
1156 if (console->index >= 0 &&
1157 console->index != console_cmdline[i].index)
1159 if (console->index < 0)
1160 console->index = console_cmdline[i].index;
1161 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1162 if (console_cmdline[i].brl_options) {
1163 console->flags |= CON_BRL;
1164 braille_register_console(console,
1165 console_cmdline[i].index,
1166 console_cmdline[i].options,
1167 console_cmdline[i].brl_options);
1171 if (console->setup &&
1172 console->setup(console, console_cmdline[i].options) != 0)
1174 console->flags |= CON_ENABLED;
1175 console->index = console_cmdline[i].index;
1176 if (i == selected_console) {
1177 console->flags |= CON_CONSDEV;
1178 preferred_console = selected_console;
1183 if (!(console->flags & CON_ENABLED))
1186 if (bootconsole && (console->flags & CON_CONSDEV)) {
1187 printk(KERN_INFO "console handover: boot [%s%d] -> real [%s%d]\n",
1188 bootconsole->name, bootconsole->index,
1189 console->name, console->index);
1190 unregister_console(bootconsole);
1191 console->flags &= ~CON_PRINTBUFFER;
1193 printk(KERN_INFO "console [%s%d] enabled\n",
1194 console->name, console->index);
1198 * Put this console in the list - keep the
1199 * preferred driver at the head of the list.
1201 acquire_console_sem();
1202 if ((console->flags & CON_CONSDEV) || console_drivers == NULL) {
1203 console->next = console_drivers;
1204 console_drivers = console;
1206 console->next->flags &= ~CON_CONSDEV;
1208 console->next = console_drivers->next;
1209 console_drivers->next = console;
1211 if (console->flags & CON_PRINTBUFFER) {
1213 * release_console_sem() will print out the buffered messages
1216 spin_lock_irqsave(&logbuf_lock, flags);
1217 con_start = log_start;
1218 spin_unlock_irqrestore(&logbuf_lock, flags);
1220 release_console_sem();
1222 EXPORT_SYMBOL(register_console);
1224 int unregister_console(struct console *console)
1226 struct console *a, *b;
1229 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1230 if (console->flags & CON_BRL)
1231 return braille_unregister_console(console);
1234 acquire_console_sem();
1235 if (console_drivers == console) {
1236 console_drivers=console->next;
1238 } else if (console_drivers) {
1239 for (a=console_drivers->next, b=console_drivers ;
1240 a; b=a, a=b->next) {
1250 * If this isn't the last console and it has CON_CONSDEV set, we
1251 * need to set it on the next preferred console.
1253 if (console_drivers != NULL && console->flags & CON_CONSDEV)
1254 console_drivers->flags |= CON_CONSDEV;
1256 release_console_sem();
1259 EXPORT_SYMBOL(unregister_console);
1261 static int __init disable_boot_consoles(void)
1263 if (console_drivers != NULL) {
1264 if (console_drivers->flags & CON_BOOT) {
1265 printk(KERN_INFO "turn off boot console %s%d\n",
1266 console_drivers->name, console_drivers->index);
1267 return unregister_console(console_drivers);
1272 late_initcall(disable_boot_consoles);
1274 #if defined CONFIG_PRINTK
1277 * printk rate limiting, lifted from the networking subsystem.
1279 * This enforces a rate limit: not more than 10 kernel messages
1280 * every 5s to make a denial-of-service attack impossible.
1282 DEFINE_RATELIMIT_STATE(printk_ratelimit_state, 5 * HZ, 10);
1284 int printk_ratelimit(void)
1286 return __ratelimit(&printk_ratelimit_state);
1288 EXPORT_SYMBOL(printk_ratelimit);
1291 * printk_timed_ratelimit - caller-controlled printk ratelimiting
1292 * @caller_jiffies: pointer to caller's state
1293 * @interval_msecs: minimum interval between prints
1295 * printk_timed_ratelimit() returns true if more than @interval_msecs
1296 * milliseconds have elapsed since the last time printk_timed_ratelimit()
1299 bool printk_timed_ratelimit(unsigned long *caller_jiffies,
1300 unsigned int interval_msecs)
1302 if (*caller_jiffies == 0 || time_after(jiffies, *caller_jiffies)) {
1303 *caller_jiffies = jiffies + msecs_to_jiffies(interval_msecs);
1308 EXPORT_SYMBOL(printk_timed_ratelimit);