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 /* printk's without a loglevel use this.. */
48 #define DEFAULT_MESSAGE_LOGLEVEL 4 /* KERN_WARNING */
50 /* We show everything that is MORE important than this.. */
51 #define MINIMUM_CONSOLE_LOGLEVEL 1 /* Minimum loglevel we let people use */
52 #define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */
54 DECLARE_WAIT_QUEUE_HEAD(log_wait);
56 int console_printk[4] = {
57 DEFAULT_CONSOLE_LOGLEVEL, /* console_loglevel */
58 DEFAULT_MESSAGE_LOGLEVEL, /* default_message_loglevel */
59 MINIMUM_CONSOLE_LOGLEVEL, /* minimum_console_loglevel */
60 DEFAULT_CONSOLE_LOGLEVEL, /* default_console_loglevel */
64 * Low level drivers may need that to know if they can schedule in
65 * their unblank() callback or not. So let's export it.
68 EXPORT_SYMBOL(oops_in_progress);
71 * console_sem protects the console_drivers list, and also
72 * provides serialisation for access to the entire console
75 static DECLARE_MUTEX(console_sem);
76 struct console *console_drivers;
77 EXPORT_SYMBOL_GPL(console_drivers);
80 * This is used for debugging the mess that is the VT code by
81 * keeping track if we have the console semaphore held. It's
82 * definitely not the perfect debug tool (we don't know if _WE_
83 * hold it are racing, but it helps tracking those weird code
84 * path in the console code where we end up in places I want
85 * locked without the console sempahore held
87 static int console_locked, console_suspended;
90 * logbuf_lock protects log_buf, log_start, log_end, con_start and logged_chars
91 * It is also used in interesting ways to provide interlocking in
92 * release_console_sem().
94 static DEFINE_SPINLOCK(logbuf_lock);
96 #define LOG_BUF_MASK (log_buf_len-1)
97 #define LOG_BUF(idx) (log_buf[(idx) & LOG_BUF_MASK])
100 * The indices into log_buf are not constrained to log_buf_len - they
101 * must be masked before subscripting
103 static unsigned log_start; /* Index into log_buf: next char to be read by syslog() */
104 static unsigned con_start; /* Index into log_buf: next char to be sent to consoles */
105 static unsigned log_end; /* Index into log_buf: most-recently-written-char + 1 */
108 * Array of consoles built from command line options (console=)
110 struct console_cmdline
112 char name[8]; /* Name of the driver */
113 int index; /* Minor dev. to use */
114 char *options; /* Options for the driver */
115 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
116 char *brl_options; /* Options for braille driver */
120 #define MAX_CMDLINECONSOLES 8
122 static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
123 static int selected_console = -1;
124 static int preferred_console = -1;
125 int console_set_on_cmdline;
126 EXPORT_SYMBOL(console_set_on_cmdline);
128 /* Flag: console code may call schedule() */
129 static int console_may_schedule;
133 static char __log_buf[__LOG_BUF_LEN];
134 static char *log_buf = __log_buf;
135 static int log_buf_len = __LOG_BUF_LEN;
136 static unsigned logged_chars; /* Number of chars produced since last read+clear operation */
138 static int __init log_buf_len_setup(char *str)
140 unsigned size = memparse(str, &str);
144 size = roundup_pow_of_two(size);
145 if (size > log_buf_len) {
146 unsigned start, dest_idx, offset;
149 new_log_buf = alloc_bootmem(size);
151 printk(KERN_WARNING "log_buf_len: allocation failed\n");
155 spin_lock_irqsave(&logbuf_lock, flags);
157 log_buf = new_log_buf;
159 offset = start = min(con_start, log_start);
161 while (start != log_end) {
162 log_buf[dest_idx] = __log_buf[start & (__LOG_BUF_LEN - 1)];
169 spin_unlock_irqrestore(&logbuf_lock, flags);
171 printk(KERN_NOTICE "log_buf_len: %d\n", log_buf_len);
177 __setup("log_buf_len=", log_buf_len_setup);
179 #ifdef CONFIG_BOOT_PRINTK_DELAY
181 static unsigned int boot_delay; /* msecs delay after each printk during bootup */
182 static unsigned long long printk_delay_msec; /* per msec, based on boot_delay */
184 static int __init boot_delay_setup(char *str)
187 unsigned long long loops_per_msec;
189 lpj = preset_lpj ? preset_lpj : 1000000; /* some guess */
190 loops_per_msec = (unsigned long long)lpj / 1000 * HZ;
192 get_option(&str, &boot_delay);
193 if (boot_delay > 10 * 1000)
196 printk_delay_msec = loops_per_msec;
197 printk(KERN_DEBUG "boot_delay: %u, preset_lpj: %ld, lpj: %lu, "
198 "HZ: %d, printk_delay_msec: %llu\n",
199 boot_delay, preset_lpj, lpj, HZ, printk_delay_msec);
202 __setup("boot_delay=", boot_delay_setup);
204 static void boot_delay_msec(void)
206 unsigned long long k;
207 unsigned long timeout;
209 if (boot_delay == 0 || system_state != SYSTEM_BOOTING)
212 k = (unsigned long long)printk_delay_msec * boot_delay;
214 timeout = jiffies + msecs_to_jiffies(boot_delay);
219 * use (volatile) jiffies to prevent
220 * compiler reduction; loop termination via jiffies
221 * is secondary and may or may not happen.
223 if (time_after(jiffies, timeout))
225 touch_nmi_watchdog();
229 static inline void boot_delay_msec(void)
235 * Commands to do_syslog:
237 * 0 -- Close the log. Currently a NOP.
238 * 1 -- Open the log. Currently a NOP.
239 * 2 -- Read from the log.
240 * 3 -- Read all messages remaining in the ring buffer.
241 * 4 -- Read and clear all messages remaining in the ring buffer
242 * 5 -- Clear ring buffer.
243 * 6 -- Disable printk's to console
244 * 7 -- Enable printk's to console
245 * 8 -- Set level of messages printed to console
246 * 9 -- Return number of unread characters in the log buffer
247 * 10 -- Return size of the log buffer
249 int do_syslog(int type, char __user *buf, int len)
251 unsigned i, j, limit, count;
256 error = security_syslog(type);
261 case 0: /* Close log */
263 case 1: /* Open log */
265 case 2: /* Read from log */
272 if (!access_ok(VERIFY_WRITE, buf, len)) {
276 error = wait_event_interruptible(log_wait,
277 (log_start - log_end));
281 spin_lock_irq(&logbuf_lock);
282 while (!error && (log_start != log_end) && i < len) {
283 c = LOG_BUF(log_start);
285 spin_unlock_irq(&logbuf_lock);
286 error = __put_user(c,buf);
290 spin_lock_irq(&logbuf_lock);
292 spin_unlock_irq(&logbuf_lock);
296 case 4: /* Read/clear last kernel messages */
299 case 3: /* Read last kernel messages */
306 if (!access_ok(VERIFY_WRITE, buf, len)) {
311 if (count > log_buf_len)
313 spin_lock_irq(&logbuf_lock);
314 if (count > logged_chars)
315 count = logged_chars;
320 * __put_user() could sleep, and while we sleep
321 * printk() could overwrite the messages
322 * we try to copy to user space. Therefore
323 * the messages are copied in reverse. <manfreds>
325 for (i = 0; i < count && !error; i++) {
327 if (j + log_buf_len < log_end)
330 spin_unlock_irq(&logbuf_lock);
331 error = __put_user(c,&buf[count-1-i]);
333 spin_lock_irq(&logbuf_lock);
335 spin_unlock_irq(&logbuf_lock);
340 int offset = count-error;
341 /* buffer overflow during copy, correct user buffer. */
342 for (i = 0; i < error; i++) {
343 if (__get_user(c,&buf[i+offset]) ||
344 __put_user(c,&buf[i])) {
352 case 5: /* Clear ring buffer */
355 case 6: /* Disable logging to console */
356 console_loglevel = minimum_console_loglevel;
358 case 7: /* Enable logging to console */
359 console_loglevel = default_console_loglevel;
361 case 8: /* Set level of messages printed to console */
363 if (len < 1 || len > 8)
365 if (len < minimum_console_loglevel)
366 len = minimum_console_loglevel;
367 console_loglevel = len;
370 case 9: /* Number of chars in the log buffer */
371 error = log_end - log_start;
373 case 10: /* Size of the log buffer */
384 SYSCALL_DEFINE3(syslog, int, type, char __user *, buf, int, len)
386 return do_syslog(type, buf, len);
390 * Call the console drivers on a range of log_buf
392 static void __call_console_drivers(unsigned start, unsigned end)
396 for (con = console_drivers; con; con = con->next) {
397 if ((con->flags & CON_ENABLED) && con->write &&
398 (cpu_online(smp_processor_id()) ||
399 (con->flags & CON_ANYTIME)))
400 con->write(con, &LOG_BUF(start), end - start);
404 static int __read_mostly ignore_loglevel;
406 static int __init ignore_loglevel_setup(char *str)
409 printk(KERN_INFO "debug: ignoring loglevel setting.\n");
414 early_param("ignore_loglevel", ignore_loglevel_setup);
417 * Write out chars from start to end - 1 inclusive
419 static void _call_console_drivers(unsigned start,
420 unsigned end, int msg_log_level)
422 if ((msg_log_level < console_loglevel || ignore_loglevel) &&
423 console_drivers && start != end) {
424 if ((start & LOG_BUF_MASK) > (end & LOG_BUF_MASK)) {
426 __call_console_drivers(start & LOG_BUF_MASK,
428 __call_console_drivers(0, end & LOG_BUF_MASK);
430 __call_console_drivers(start, end);
436 * Call the console drivers, asking them to write out
437 * log_buf[start] to log_buf[end - 1].
438 * The console_sem must be held.
440 static void call_console_drivers(unsigned start, unsigned end)
442 unsigned cur_index, start_print;
443 static int msg_level = -1;
445 BUG_ON(((int)(start - end)) > 0);
449 while (cur_index != end) {
450 if (msg_level < 0 && ((end - cur_index) > 2) &&
451 LOG_BUF(cur_index + 0) == '<' &&
452 LOG_BUF(cur_index + 1) >= '0' &&
453 LOG_BUF(cur_index + 1) <= '7' &&
454 LOG_BUF(cur_index + 2) == '>') {
455 msg_level = LOG_BUF(cur_index + 1) - '0';
457 start_print = cur_index;
459 while (cur_index != end) {
460 char c = LOG_BUF(cur_index);
466 * printk() has already given us loglevel tags in
467 * the buffer. This code is here in case the
468 * log buffer has wrapped right round and scribbled
471 msg_level = default_message_loglevel;
473 _call_console_drivers(start_print, cur_index, msg_level);
475 start_print = cur_index;
480 _call_console_drivers(start_print, end, msg_level);
483 static void emit_log_char(char c)
485 LOG_BUF(log_end) = c;
487 if (log_end - log_start > log_buf_len)
488 log_start = log_end - log_buf_len;
489 if (log_end - con_start > log_buf_len)
490 con_start = log_end - log_buf_len;
491 if (logged_chars < log_buf_len)
496 * Zap console related locks when oopsing. Only zap at most once
497 * every 10 seconds, to leave time for slow consoles to print a
500 static void zap_locks(void)
502 static unsigned long oops_timestamp;
504 if (time_after_eq(jiffies, oops_timestamp) &&
505 !time_after(jiffies, oops_timestamp + 30 * HZ))
508 oops_timestamp = jiffies;
510 /* If a crash is occurring, make sure we can't deadlock */
511 spin_lock_init(&logbuf_lock);
512 /* And make sure that we print immediately */
513 init_MUTEX(&console_sem);
516 #if defined(CONFIG_PRINTK_TIME)
517 static int printk_time = 1;
519 static int printk_time = 0;
521 module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR);
523 /* Check if we have any console registered that can be called early in boot. */
524 static int have_callable_console(void)
528 for (con = console_drivers; con; con = con->next)
529 if (con->flags & CON_ANYTIME)
536 * printk - print a kernel message
537 * @fmt: format string
539 * This is printk(). It can be called from any context. We want it to work.
541 * We try to grab the console_sem. If we succeed, it's easy - we log the output and
542 * call the console drivers. If we fail to get the semaphore we place the output
543 * into the log buffer and return. The current holder of the console_sem will
544 * notice the new output in release_console_sem() and will send it to the
545 * consoles before releasing the semaphore.
547 * One effect of this deferred printing is that code which calls printk() and
548 * then changes console_loglevel may break. This is because console_loglevel
549 * is inspected when the actual printing occurs.
554 * See the vsnprintf() documentation for format string extensions over C99.
557 asmlinkage int printk(const char *fmt, ...)
563 r = vprintk(fmt, args);
569 /* cpu currently holding logbuf_lock */
570 static volatile unsigned int printk_cpu = UINT_MAX;
573 * Can we actually use the console at this time on this cpu?
575 * Console drivers may assume that per-cpu resources have
576 * been allocated. So unless they're explicitly marked as
577 * being able to cope (CON_ANYTIME) don't call them until
578 * this CPU is officially up.
580 static inline int can_use_console(unsigned int cpu)
582 return cpu_online(cpu) || have_callable_console();
586 * Try to get console ownership to actually show the kernel
587 * messages from a 'printk'. Return true (and with the
588 * console_semaphore held, and 'console_locked' set) if it
589 * is successful, false otherwise.
591 * This gets called with the 'logbuf_lock' spinlock held and
592 * interrupts disabled. It should return with 'lockbuf_lock'
593 * released but interrupts still disabled.
595 static int acquire_console_semaphore_for_printk(unsigned int cpu)
599 if (!try_acquire_console_sem()) {
603 * If we can't use the console, we need to release
604 * the console semaphore by hand to avoid flushing
605 * the buffer. We need to hold the console semaphore
606 * in order to do this test safely.
608 if (!can_use_console(cpu)) {
614 printk_cpu = UINT_MAX;
615 spin_unlock(&logbuf_lock);
618 static const char recursion_bug_msg [] =
619 KERN_CRIT "BUG: recent printk recursion!\n";
620 static int recursion_bug;
621 static int new_text_line = 1;
622 static char printk_buf[1024];
624 asmlinkage int vprintk(const char *fmt, va_list args)
627 int current_log_level = default_message_loglevel;
635 /* This stops the holder of console_sem just where we want him */
636 raw_local_irq_save(flags);
637 this_cpu = smp_processor_id();
640 * Ouch, printk recursed into itself!
642 if (unlikely(printk_cpu == this_cpu)) {
644 * If a crash is occurring during printk() on this CPU,
645 * then try to get the crash message out but make sure
646 * we can't deadlock. Otherwise just return to avoid the
647 * recursion and return - but flag the recursion so that
648 * it can be printed at the next appropriate moment:
650 if (!oops_in_progress) {
652 goto out_restore_irqs;
658 spin_lock(&logbuf_lock);
659 printk_cpu = this_cpu;
663 strcpy(printk_buf, recursion_bug_msg);
664 printed_len = strlen(recursion_bug_msg);
666 /* Emit the output into the temporary buffer */
667 printed_len += vscnprintf(printk_buf + printed_len,
668 sizeof(printk_buf) - printed_len, fmt, args);
672 * Copy the output into log_buf. If the caller didn't provide
673 * appropriate log level tags, we insert them here
675 for (p = printk_buf; *p; p++) {
677 /* If a token, set current_log_level and skip over */
678 if (p[0] == '<' && p[1] >= '0' && p[1] <= '7' &&
680 current_log_level = p[1] - '0';
685 /* Always output the token */
687 emit_log_char(current_log_level + '0');
693 /* Follow the token with the time */
696 unsigned long long t;
697 unsigned long nanosec_rem;
699 t = cpu_clock(printk_cpu);
700 nanosec_rem = do_div(t, 1000000000);
701 tlen = sprintf(tbuf, "[%5lu.%06lu] ",
705 for (tp = tbuf; tp < tbuf + tlen; tp++)
720 * Try to acquire and then immediately release the
721 * console semaphore. The release will do all the
722 * actual magic (print out buffers, wake up klogd,
725 * The acquire_console_semaphore_for_printk() function
726 * will release 'logbuf_lock' regardless of whether it
727 * actually gets the semaphore or not.
729 if (acquire_console_semaphore_for_printk(this_cpu))
730 release_console_sem();
734 raw_local_irq_restore(flags);
739 EXPORT_SYMBOL(printk);
740 EXPORT_SYMBOL(vprintk);
744 static void call_console_drivers(unsigned start, unsigned end)
750 static int __add_preferred_console(char *name, int idx, char *options,
753 struct console_cmdline *c;
757 * See if this tty is not yet registered, and
758 * if we have a slot free.
760 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
761 if (strcmp(console_cmdline[i].name, name) == 0 &&
762 console_cmdline[i].index == idx) {
764 selected_console = i;
767 if (i == MAX_CMDLINECONSOLES)
770 selected_console = i;
771 c = &console_cmdline[i];
772 strlcpy(c->name, name, sizeof(c->name));
773 c->options = options;
774 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
775 c->brl_options = brl_options;
781 * Set up a list of consoles. Called from init/main.c
783 static int __init console_setup(char *str)
785 char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for index */
786 char *s, *options, *brl_options = NULL;
789 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
790 if (!memcmp(str, "brl,", 4)) {
793 } else if (!memcmp(str, "brl=", 4)) {
794 brl_options = str + 4;
795 str = strchr(brl_options, ',');
797 printk(KERN_ERR "need port name after brl=\n");
805 * Decode str into name, index, options.
807 if (str[0] >= '0' && str[0] <= '9') {
809 strncpy(buf + 4, str, sizeof(buf) - 5);
811 strncpy(buf, str, sizeof(buf) - 1);
813 buf[sizeof(buf) - 1] = 0;
814 if ((options = strchr(str, ',')) != NULL)
817 if (!strcmp(str, "ttya"))
818 strcpy(buf, "ttyS0");
819 if (!strcmp(str, "ttyb"))
820 strcpy(buf, "ttyS1");
822 for (s = buf; *s; s++)
823 if ((*s >= '0' && *s <= '9') || *s == ',')
825 idx = simple_strtoul(s, NULL, 10);
828 __add_preferred_console(buf, idx, options, brl_options);
829 console_set_on_cmdline = 1;
832 __setup("console=", console_setup);
835 * add_preferred_console - add a device to the list of preferred consoles.
838 * @options: options for this console
840 * The last preferred console added will be used for kernel messages
841 * and stdin/out/err for init. Normally this is used by console_setup
842 * above to handle user-supplied console arguments; however it can also
843 * be used by arch-specific code either to override the user or more
844 * commonly to provide a default console (ie from PROM variables) when
845 * the user has not supplied one.
847 int add_preferred_console(char *name, int idx, char *options)
849 return __add_preferred_console(name, idx, options, NULL);
852 int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options)
854 struct console_cmdline *c;
857 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
858 if (strcmp(console_cmdline[i].name, name) == 0 &&
859 console_cmdline[i].index == idx) {
860 c = &console_cmdline[i];
861 strlcpy(c->name, name_new, sizeof(c->name));
862 c->name[sizeof(c->name) - 1] = 0;
863 c->options = options;
871 int console_suspend_enabled = 1;
872 EXPORT_SYMBOL(console_suspend_enabled);
874 static int __init console_suspend_disable(char *str)
876 console_suspend_enabled = 0;
879 __setup("no_console_suspend", console_suspend_disable);
882 * suspend_console - suspend the console subsystem
884 * This disables printk() while we go into suspend states
886 void suspend_console(void)
888 if (!console_suspend_enabled)
890 printk("Suspending console(s) (use no_console_suspend to debug)\n");
891 acquire_console_sem();
892 console_suspended = 1;
896 void resume_console(void)
898 if (!console_suspend_enabled)
901 console_suspended = 0;
902 release_console_sem();
906 * acquire_console_sem - lock the console system for exclusive use.
908 * Acquires a semaphore which guarantees that the caller has
909 * exclusive access to the console system and the console_drivers list.
911 * Can sleep, returns nothing.
913 void acquire_console_sem(void)
915 BUG_ON(in_interrupt());
917 if (console_suspended)
920 console_may_schedule = 1;
922 EXPORT_SYMBOL(acquire_console_sem);
924 int try_acquire_console_sem(void)
926 if (down_trylock(&console_sem))
928 if (console_suspended) {
933 console_may_schedule = 0;
936 EXPORT_SYMBOL(try_acquire_console_sem);
938 int is_console_locked(void)
940 return console_locked;
943 static DEFINE_PER_CPU(int, printk_pending);
945 void printk_tick(void)
947 if (__get_cpu_var(printk_pending)) {
948 __get_cpu_var(printk_pending) = 0;
949 wake_up_interruptible(&log_wait);
953 int printk_needs_cpu(int cpu)
955 return per_cpu(printk_pending, cpu);
958 void wake_up_klogd(void)
960 if (waitqueue_active(&log_wait))
961 __raw_get_cpu_var(printk_pending) = 1;
965 * release_console_sem - unlock the console system
967 * Releases the semaphore which the caller holds on the console system
968 * and the console driver list.
970 * While the semaphore was held, console output may have been buffered
971 * by printk(). If this is the case, release_console_sem() emits
972 * the output prior to releasing the semaphore.
974 * If there is output waiting for klogd, we wake it up.
976 * release_console_sem() may be called from any context.
978 void release_console_sem(void)
981 unsigned _con_start, _log_end;
982 unsigned wake_klogd = 0;
984 if (console_suspended) {
989 console_may_schedule = 0;
992 spin_lock_irqsave(&logbuf_lock, flags);
993 wake_klogd |= log_start - log_end;
994 if (con_start == log_end)
995 break; /* Nothing to print */
996 _con_start = con_start;
998 con_start = log_end; /* Flush */
999 spin_unlock(&logbuf_lock);
1000 stop_critical_timings(); /* don't trace print latency */
1001 call_console_drivers(_con_start, _log_end);
1002 start_critical_timings();
1003 local_irq_restore(flags);
1007 spin_unlock_irqrestore(&logbuf_lock, flags);
1011 EXPORT_SYMBOL(release_console_sem);
1014 * console_conditional_schedule - yield the CPU if required
1016 * If the console code is currently allowed to sleep, and
1017 * if this CPU should yield the CPU to another task, do
1020 * Must be called within acquire_console_sem().
1022 void __sched console_conditional_schedule(void)
1024 if (console_may_schedule)
1027 EXPORT_SYMBOL(console_conditional_schedule);
1029 void console_print(const char *s)
1031 printk(KERN_EMERG "%s", s);
1033 EXPORT_SYMBOL(console_print);
1035 void console_unblank(void)
1040 * console_unblank can no longer be called in interrupt context unless
1041 * oops_in_progress is set to 1..
1043 if (oops_in_progress) {
1044 if (down_trylock(&console_sem) != 0)
1047 acquire_console_sem();
1050 console_may_schedule = 0;
1051 for (c = console_drivers; c != NULL; c = c->next)
1052 if ((c->flags & CON_ENABLED) && c->unblank)
1054 release_console_sem();
1058 * Return the console tty driver structure and its associated index
1060 struct tty_driver *console_device(int *index)
1063 struct tty_driver *driver = NULL;
1065 acquire_console_sem();
1066 for (c = console_drivers; c != NULL; c = c->next) {
1069 driver = c->device(c, index);
1073 release_console_sem();
1078 * Prevent further output on the passed console device so that (for example)
1079 * serial drivers can disable console output before suspending a port, and can
1080 * re-enable output afterwards.
1082 void console_stop(struct console *console)
1084 acquire_console_sem();
1085 console->flags &= ~CON_ENABLED;
1086 release_console_sem();
1088 EXPORT_SYMBOL(console_stop);
1090 void console_start(struct console *console)
1092 acquire_console_sem();
1093 console->flags |= CON_ENABLED;
1094 release_console_sem();
1096 EXPORT_SYMBOL(console_start);
1099 * The console driver calls this routine during kernel initialization
1100 * to register the console printing procedure with printk() and to
1101 * print any messages that were printed by the kernel before the
1102 * console driver was initialized.
1104 void register_console(struct console *console)
1107 unsigned long flags;
1108 struct console *bootconsole = NULL;
1110 if (console_drivers) {
1111 if (console->flags & CON_BOOT)
1113 if (console_drivers->flags & CON_BOOT)
1114 bootconsole = console_drivers;
1117 if (preferred_console < 0 || bootconsole || !console_drivers)
1118 preferred_console = selected_console;
1120 if (console->early_setup)
1121 console->early_setup();
1124 * See if we want to use this console driver. If we
1125 * didn't select a console we take the first one
1126 * that registers here.
1128 if (preferred_console < 0) {
1129 if (console->index < 0)
1131 if (console->setup == NULL ||
1132 console->setup(console, NULL) == 0) {
1133 console->flags |= CON_ENABLED;
1134 if (console->device) {
1135 console->flags |= CON_CONSDEV;
1136 preferred_console = 0;
1142 * See if this console matches one we selected on
1145 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0];
1147 if (strcmp(console_cmdline[i].name, console->name) != 0)
1149 if (console->index >= 0 &&
1150 console->index != console_cmdline[i].index)
1152 if (console->index < 0)
1153 console->index = console_cmdline[i].index;
1154 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1155 if (console_cmdline[i].brl_options) {
1156 console->flags |= CON_BRL;
1157 braille_register_console(console,
1158 console_cmdline[i].index,
1159 console_cmdline[i].options,
1160 console_cmdline[i].brl_options);
1164 if (console->setup &&
1165 console->setup(console, console_cmdline[i].options) != 0)
1167 console->flags |= CON_ENABLED;
1168 console->index = console_cmdline[i].index;
1169 if (i == selected_console) {
1170 console->flags |= CON_CONSDEV;
1171 preferred_console = selected_console;
1176 if (!(console->flags & CON_ENABLED))
1179 if (bootconsole && (console->flags & CON_CONSDEV)) {
1180 printk(KERN_INFO "console handover: boot [%s%d] -> real [%s%d]\n",
1181 bootconsole->name, bootconsole->index,
1182 console->name, console->index);
1183 unregister_console(bootconsole);
1184 console->flags &= ~CON_PRINTBUFFER;
1186 printk(KERN_INFO "console [%s%d] enabled\n",
1187 console->name, console->index);
1191 * Put this console in the list - keep the
1192 * preferred driver at the head of the list.
1194 acquire_console_sem();
1195 if ((console->flags & CON_CONSDEV) || console_drivers == NULL) {
1196 console->next = console_drivers;
1197 console_drivers = console;
1199 console->next->flags &= ~CON_CONSDEV;
1201 console->next = console_drivers->next;
1202 console_drivers->next = console;
1204 if (console->flags & CON_PRINTBUFFER) {
1206 * release_console_sem() will print out the buffered messages
1209 spin_lock_irqsave(&logbuf_lock, flags);
1210 con_start = log_start;
1211 spin_unlock_irqrestore(&logbuf_lock, flags);
1213 release_console_sem();
1215 EXPORT_SYMBOL(register_console);
1217 int unregister_console(struct console *console)
1219 struct console *a, *b;
1222 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1223 if (console->flags & CON_BRL)
1224 return braille_unregister_console(console);
1227 acquire_console_sem();
1228 if (console_drivers == console) {
1229 console_drivers=console->next;
1231 } else if (console_drivers) {
1232 for (a=console_drivers->next, b=console_drivers ;
1233 a; b=a, a=b->next) {
1243 * If this isn't the last console and it has CON_CONSDEV set, we
1244 * need to set it on the next preferred console.
1246 if (console_drivers != NULL && console->flags & CON_CONSDEV)
1247 console_drivers->flags |= CON_CONSDEV;
1249 release_console_sem();
1252 EXPORT_SYMBOL(unregister_console);
1254 static int __init disable_boot_consoles(void)
1256 if (console_drivers != NULL) {
1257 if (console_drivers->flags & CON_BOOT) {
1258 printk(KERN_INFO "turn off boot console %s%d\n",
1259 console_drivers->name, console_drivers->index);
1260 return unregister_console(console_drivers);
1265 late_initcall(disable_boot_consoles);
1267 #if defined CONFIG_PRINTK
1270 * printk rate limiting, lifted from the networking subsystem.
1272 * This enforces a rate limit: not more than 10 kernel messages
1273 * every 5s to make a denial-of-service attack impossible.
1275 DEFINE_RATELIMIT_STATE(printk_ratelimit_state, 5 * HZ, 10);
1277 int printk_ratelimit(void)
1279 return __ratelimit(&printk_ratelimit_state);
1281 EXPORT_SYMBOL(printk_ratelimit);
1284 * printk_timed_ratelimit - caller-controlled printk ratelimiting
1285 * @caller_jiffies: pointer to caller's state
1286 * @interval_msecs: minimum interval between prints
1288 * printk_timed_ratelimit() returns true if more than @interval_msecs
1289 * milliseconds have elapsed since the last time printk_timed_ratelimit()
1292 bool printk_timed_ratelimit(unsigned long *caller_jiffies,
1293 unsigned int interval_msecs)
1295 if (*caller_jiffies == 0 || time_after(jiffies, *caller_jiffies)) {
1296 *caller_jiffies = jiffies + msecs_to_jiffies(interval_msecs);
1301 EXPORT_SYMBOL(printk_timed_ratelimit);