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 <andrewm@uow.edu.au>
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 __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 static DECLARE_MUTEX(secondary_console_sem);
81 struct console *console_drivers;
83 * This is used for debugging the mess that is the VT code by
84 * keeping track if we have the console semaphore held. It's
85 * definitely not the perfect debug tool (we don't know if _WE_
86 * hold it are racing, but it helps tracking those weird code
87 * path in the console code where we end up in places I want
88 * locked without the console sempahore held
90 static int console_locked, console_suspended;
93 * logbuf_lock protects log_buf, log_start, log_end, con_start and logged_chars
94 * It is also used in interesting ways to provide interlocking in
95 * release_console_sem().
97 static DEFINE_SPINLOCK(logbuf_lock);
99 #define LOG_BUF_MASK (log_buf_len-1)
100 #define LOG_BUF(idx) (log_buf[(idx) & LOG_BUF_MASK])
103 * The indices into log_buf are not constrained to log_buf_len - they
104 * must be masked before subscripting
106 static unsigned log_start; /* Index into log_buf: next char to be read by syslog() */
107 static unsigned con_start; /* Index into log_buf: next char to be sent to consoles */
108 static unsigned log_end; /* Index into log_buf: most-recently-written-char + 1 */
111 * Array of consoles built from command line options (console=)
113 struct console_cmdline
115 char name[8]; /* Name of the driver */
116 int index; /* Minor dev. to use */
117 char *options; /* Options for the 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;
126 /* Flag: console code may call schedule() */
127 static int console_may_schedule;
131 static char __log_buf[__LOG_BUF_LEN];
132 static char *log_buf = __log_buf;
133 static int log_buf_len = __LOG_BUF_LEN;
134 static unsigned logged_chars; /* Number of chars produced since last read+clear operation */
136 static int __init log_buf_len_setup(char *str)
138 unsigned size = memparse(str, &str);
142 size = roundup_pow_of_two(size);
143 if (size > log_buf_len) {
144 unsigned start, dest_idx, offset;
147 new_log_buf = alloc_bootmem(size);
149 printk(KERN_WARNING "log_buf_len: allocation failed\n");
153 spin_lock_irqsave(&logbuf_lock, flags);
155 log_buf = new_log_buf;
157 offset = start = min(con_start, log_start);
159 while (start != log_end) {
160 log_buf[dest_idx] = __log_buf[start & (__LOG_BUF_LEN - 1)];
167 spin_unlock_irqrestore(&logbuf_lock, flags);
169 printk(KERN_NOTICE "log_buf_len: %d\n", log_buf_len);
175 __setup("log_buf_len=", log_buf_len_setup);
177 #ifdef CONFIG_BOOT_PRINTK_DELAY
179 static unsigned int boot_delay; /* msecs delay after each printk during bootup */
180 static unsigned long long printk_delay_msec; /* per msec, based on boot_delay */
182 static int __init boot_delay_setup(char *str)
185 unsigned long long loops_per_msec;
187 lpj = preset_lpj ? preset_lpj : 1000000; /* some guess */
188 loops_per_msec = (unsigned long long)lpj / 1000 * HZ;
190 get_option(&str, &boot_delay);
191 if (boot_delay > 10 * 1000)
194 printk_delay_msec = loops_per_msec;
195 printk(KERN_DEBUG "boot_delay: %u, preset_lpj: %ld, lpj: %lu, "
196 "HZ: %d, printk_delay_msec: %llu\n",
197 boot_delay, preset_lpj, lpj, HZ, printk_delay_msec);
200 __setup("boot_delay=", boot_delay_setup);
202 static void boot_delay_msec(void)
204 unsigned long long k;
205 unsigned long timeout;
207 if (boot_delay == 0 || system_state != SYSTEM_BOOTING)
210 k = (unsigned long long)printk_delay_msec * boot_delay;
212 timeout = jiffies + msecs_to_jiffies(boot_delay);
217 * use (volatile) jiffies to prevent
218 * compiler reduction; loop termination via jiffies
219 * is secondary and may or may not happen.
221 if (time_after(jiffies, timeout))
223 touch_nmi_watchdog();
227 static inline void boot_delay_msec(void)
233 * Return the number of unread characters in the log buffer.
235 int log_buf_get_len(void)
241 * Copy a range of characters from the log buffer.
243 int log_buf_copy(char *dest, int idx, int len)
246 bool took_lock = false;
248 if (!oops_in_progress) {
249 spin_lock_irq(&logbuf_lock);
253 max = log_buf_get_len();
254 if (idx < 0 || idx >= max) {
260 idx += (log_end - max);
262 dest[len] = LOG_BUF(idx + len);
266 spin_unlock_irq(&logbuf_lock);
272 * Extract a single character from the log buffer.
274 int log_buf_read(int idx)
278 if (log_buf_copy(&ret, idx, 1) == 1)
285 * Commands to do_syslog:
287 * 0 -- Close the log. Currently a NOP.
288 * 1 -- Open the log. Currently a NOP.
289 * 2 -- Read from the log.
290 * 3 -- Read all messages remaining in the ring buffer.
291 * 4 -- Read and clear all messages remaining in the ring buffer
292 * 5 -- Clear ring buffer.
293 * 6 -- Disable printk's to console
294 * 7 -- Enable printk's to console
295 * 8 -- Set level of messages printed to console
296 * 9 -- Return number of unread characters in the log buffer
297 * 10 -- Return size of the log buffer
299 int do_syslog(int type, char __user *buf, int len)
301 unsigned i, j, limit, count;
306 error = security_syslog(type);
311 case 0: /* Close log */
313 case 1: /* Open log */
315 case 2: /* Read from log */
322 if (!access_ok(VERIFY_WRITE, buf, len)) {
326 error = wait_event_interruptible(log_wait,
327 (log_start - log_end));
331 spin_lock_irq(&logbuf_lock);
332 while (!error && (log_start != log_end) && i < len) {
333 c = LOG_BUF(log_start);
335 spin_unlock_irq(&logbuf_lock);
336 error = __put_user(c,buf);
340 spin_lock_irq(&logbuf_lock);
342 spin_unlock_irq(&logbuf_lock);
346 case 4: /* Read/clear last kernel messages */
349 case 3: /* Read last kernel messages */
356 if (!access_ok(VERIFY_WRITE, buf, len)) {
361 if (count > log_buf_len)
363 spin_lock_irq(&logbuf_lock);
364 if (count > logged_chars)
365 count = logged_chars;
370 * __put_user() could sleep, and while we sleep
371 * printk() could overwrite the messages
372 * we try to copy to user space. Therefore
373 * the messages are copied in reverse. <manfreds>
375 for (i = 0; i < count && !error; i++) {
377 if (j + log_buf_len < log_end)
380 spin_unlock_irq(&logbuf_lock);
381 error = __put_user(c,&buf[count-1-i]);
383 spin_lock_irq(&logbuf_lock);
385 spin_unlock_irq(&logbuf_lock);
390 int offset = count-error;
391 /* buffer overflow during copy, correct user buffer. */
392 for (i = 0; i < error; i++) {
393 if (__get_user(c,&buf[i+offset]) ||
394 __put_user(c,&buf[i])) {
402 case 5: /* Clear ring buffer */
405 case 6: /* Disable logging to console */
406 console_loglevel = minimum_console_loglevel;
408 case 7: /* Enable logging to console */
409 console_loglevel = default_console_loglevel;
411 case 8: /* Set level of messages printed to console */
413 if (len < 1 || len > 8)
415 if (len < minimum_console_loglevel)
416 len = minimum_console_loglevel;
417 console_loglevel = len;
420 case 9: /* Number of chars in the log buffer */
421 error = log_end - log_start;
423 case 10: /* Size of the log buffer */
434 asmlinkage long sys_syslog(int type, char __user *buf, int len)
436 return do_syslog(type, buf, len);
440 * Call the console drivers on a range of log_buf
442 static void __call_console_drivers(unsigned start, unsigned end)
446 for (con = console_drivers; con; con = con->next) {
447 if ((con->flags & CON_ENABLED) && con->write &&
448 (cpu_online(smp_processor_id()) ||
449 (con->flags & CON_ANYTIME)))
450 con->write(con, &LOG_BUF(start), end - start);
454 static int __read_mostly ignore_loglevel;
456 static int __init ignore_loglevel_setup(char *str)
459 printk(KERN_INFO "debug: ignoring loglevel setting.\n");
464 early_param("ignore_loglevel", ignore_loglevel_setup);
467 * Write out chars from start to end - 1 inclusive
469 static void _call_console_drivers(unsigned start,
470 unsigned end, int msg_log_level)
472 if ((msg_log_level < console_loglevel || ignore_loglevel) &&
473 console_drivers && start != end) {
474 if ((start & LOG_BUF_MASK) > (end & LOG_BUF_MASK)) {
476 __call_console_drivers(start & LOG_BUF_MASK,
478 __call_console_drivers(0, end & LOG_BUF_MASK);
480 __call_console_drivers(start, end);
486 * Call the console drivers, asking them to write out
487 * log_buf[start] to log_buf[end - 1].
488 * The console_sem must be held.
490 static void call_console_drivers(unsigned start, unsigned end)
492 unsigned cur_index, start_print;
493 static int msg_level = -1;
495 BUG_ON(((int)(start - end)) > 0);
499 while (cur_index != end) {
500 if (msg_level < 0 && ((end - cur_index) > 2) &&
501 LOG_BUF(cur_index + 0) == '<' &&
502 LOG_BUF(cur_index + 1) >= '0' &&
503 LOG_BUF(cur_index + 1) <= '7' &&
504 LOG_BUF(cur_index + 2) == '>') {
505 msg_level = LOG_BUF(cur_index + 1) - '0';
507 start_print = cur_index;
509 while (cur_index != end) {
510 char c = LOG_BUF(cur_index);
516 * printk() has already given us loglevel tags in
517 * the buffer. This code is here in case the
518 * log buffer has wrapped right round and scribbled
521 msg_level = default_message_loglevel;
523 _call_console_drivers(start_print, cur_index, msg_level);
525 start_print = cur_index;
530 _call_console_drivers(start_print, end, msg_level);
533 static void emit_log_char(char c)
535 LOG_BUF(log_end) = c;
537 if (log_end - log_start > log_buf_len)
538 log_start = log_end - log_buf_len;
539 if (log_end - con_start > log_buf_len)
540 con_start = log_end - log_buf_len;
541 if (logged_chars < log_buf_len)
546 * Zap console related locks when oopsing. Only zap at most once
547 * every 10 seconds, to leave time for slow consoles to print a
550 static void zap_locks(void)
552 static unsigned long oops_timestamp;
554 if (time_after_eq(jiffies, oops_timestamp) &&
555 !time_after(jiffies, oops_timestamp + 30 * HZ))
558 oops_timestamp = jiffies;
560 /* If a crash is occurring, make sure we can't deadlock */
561 spin_lock_init(&logbuf_lock);
562 /* And make sure that we print immediately */
563 init_MUTEX(&console_sem);
566 #if defined(CONFIG_PRINTK_TIME)
567 static int printk_time = 1;
569 static int printk_time = 0;
571 module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR);
573 /* Check if we have any console registered that can be called early in boot. */
574 static int have_callable_console(void)
578 for (con = console_drivers; con; con = con->next)
579 if (con->flags & CON_ANYTIME)
586 * printk - print a kernel message
587 * @fmt: format string
589 * This is printk(). It can be called from any context. We want it to work.
590 * Be aware of the fact that if oops_in_progress is not set, we might try to
591 * wake klogd up which could deadlock on runqueue lock if printk() is called
592 * from scheduler code.
594 * We try to grab the console_sem. If we succeed, it's easy - we log the output and
595 * call the console drivers. If we fail to get the semaphore we place the output
596 * into the log buffer and return. The current holder of the console_sem will
597 * notice the new output in release_console_sem() and will send it to the
598 * consoles before releasing the semaphore.
600 * One effect of this deferred printing is that code which calls printk() and
601 * then changes console_loglevel may break. This is because console_loglevel
602 * is inspected when the actual printing occurs.
608 asmlinkage int printk(const char *fmt, ...)
614 r = vprintk(fmt, args);
620 /* cpu currently holding logbuf_lock */
621 static volatile unsigned int printk_cpu = UINT_MAX;
624 * Can we actually use the console at this time on this cpu?
626 * Console drivers may assume that per-cpu resources have
627 * been allocated. So unless they're explicitly marked as
628 * being able to cope (CON_ANYTIME) don't call them until
629 * this CPU is officially up.
631 static inline int can_use_console(unsigned int cpu)
633 return cpu_online(cpu) || have_callable_console();
637 * Try to get console ownership to actually show the kernel
638 * messages from a 'printk'. Return true (and with the
639 * console_semaphore held, and 'console_locked' set) if it
640 * is successful, false otherwise.
642 * This gets called with the 'logbuf_lock' spinlock held and
643 * interrupts disabled. It should return with 'lockbuf_lock'
644 * released but interrupts still disabled.
646 static int acquire_console_semaphore_for_printk(unsigned int cpu)
650 if (!try_acquire_console_sem()) {
654 * If we can't use the console, we need to release
655 * the console semaphore by hand to avoid flushing
656 * the buffer. We need to hold the console semaphore
657 * in order to do this test safely.
659 if (!can_use_console(cpu)) {
665 printk_cpu = UINT_MAX;
666 spin_unlock(&logbuf_lock);
670 const char printk_recursion_bug_msg [] =
671 KERN_CRIT "BUG: recent printk recursion!\n";
672 static int printk_recursion_bug;
674 asmlinkage int vprintk(const char *fmt, va_list args)
676 static int log_level_unknown = 1;
677 static char printk_buf[1024];
687 /* This stops the holder of console_sem just where we want him */
688 raw_local_irq_save(flags);
689 this_cpu = smp_processor_id();
692 * Ouch, printk recursed into itself!
694 if (unlikely(printk_cpu == this_cpu)) {
696 * If a crash is occurring during printk() on this CPU,
697 * then try to get the crash message out but make sure
698 * we can't deadlock. Otherwise just return to avoid the
699 * recursion and return - but flag the recursion so that
700 * it can be printed at the next appropriate moment:
702 if (!oops_in_progress) {
703 printk_recursion_bug = 1;
704 goto out_restore_irqs;
710 spin_lock(&logbuf_lock);
711 printk_cpu = this_cpu;
713 if (printk_recursion_bug) {
714 printk_recursion_bug = 0;
715 strcpy(printk_buf, printk_recursion_bug_msg);
716 printed_len = sizeof(printk_recursion_bug_msg);
718 /* Emit the output into the temporary buffer */
719 printed_len += vscnprintf(printk_buf + printed_len,
720 sizeof(printk_buf) - printed_len, fmt, args);
722 #ifdef CONFIG_DEBUG_LL
723 printascii(printk_buf);
727 * Copy the output into log_buf. If the caller didn't provide
728 * appropriate log level tags, we insert them here
730 for (p = printk_buf; *p; p++) {
731 if (log_level_unknown) {
732 /* log_level_unknown signals the start of a new line */
737 unsigned long long t;
738 unsigned long nanosec_rem;
741 * force the log level token to be
742 * before the time output.
744 if (p[0] == '<' && p[1] >='0' &&
745 p[1] <= '7' && p[2] == '>') {
750 loglev_char = default_message_loglevel
753 t = cpu_clock(printk_cpu);
754 nanosec_rem = do_div(t, 1000000000);
761 for (tp = tbuf; tp < tbuf + tlen; tp++)
765 if (p[0] != '<' || p[1] < '0' ||
766 p[1] > '7' || p[2] != '>') {
768 emit_log_char(default_message_loglevel
774 log_level_unknown = 0;
780 log_level_unknown = 1;
784 * Try to acquire and then immediately release the
785 * console semaphore. The release will do all the
786 * actual magic (print out buffers, wake up klogd,
789 * The acquire_console_semaphore_for_printk() function
790 * will release 'logbuf_lock' regardless of whether it
791 * actually gets the semaphore or not.
793 if (acquire_console_semaphore_for_printk(this_cpu))
794 release_console_sem();
798 raw_local_irq_restore(flags);
803 EXPORT_SYMBOL(printk);
804 EXPORT_SYMBOL(vprintk);
808 asmlinkage long sys_syslog(int type, char __user *buf, int len)
813 static void call_console_drivers(unsigned start, unsigned end)
820 * Set up a list of consoles. Called from init/main.c
822 static int __init console_setup(char *str)
824 char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for index */
829 * Decode str into name, index, options.
831 if (str[0] >= '0' && str[0] <= '9') {
833 strncpy(buf + 4, str, sizeof(buf) - 5);
835 strncpy(buf, str, sizeof(buf) - 1);
837 buf[sizeof(buf) - 1] = 0;
838 if ((options = strchr(str, ',')) != NULL)
841 if (!strcmp(str, "ttya"))
842 strcpy(buf, "ttyS0");
843 if (!strcmp(str, "ttyb"))
844 strcpy(buf, "ttyS1");
846 for (s = buf; *s; s++)
847 if ((*s >= '0' && *s <= '9') || *s == ',')
849 idx = simple_strtoul(s, NULL, 10);
852 add_preferred_console(buf, idx, options);
855 __setup("console=", console_setup);
858 * add_preferred_console - add a device to the list of preferred consoles.
861 * @options: options for this console
863 * The last preferred console added will be used for kernel messages
864 * and stdin/out/err for init. Normally this is used by console_setup
865 * above to handle user-supplied console arguments; however it can also
866 * be used by arch-specific code either to override the user or more
867 * commonly to provide a default console (ie from PROM variables) when
868 * the user has not supplied one.
870 int add_preferred_console(char *name, int idx, char *options)
872 struct console_cmdline *c;
876 * See if this tty is not yet registered, and
877 * if we have a slot free.
879 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
880 if (strcmp(console_cmdline[i].name, name) == 0 &&
881 console_cmdline[i].index == idx) {
882 selected_console = i;
885 if (i == MAX_CMDLINECONSOLES)
887 selected_console = i;
888 c = &console_cmdline[i];
889 memcpy(c->name, name, sizeof(c->name));
890 c->name[sizeof(c->name) - 1] = 0;
891 c->options = options;
896 int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options)
898 struct console_cmdline *c;
901 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
902 if (strcmp(console_cmdline[i].name, name) == 0 &&
903 console_cmdline[i].index == idx) {
904 c = &console_cmdline[i];
905 memcpy(c->name, name_new, sizeof(c->name));
906 c->name[sizeof(c->name) - 1] = 0;
907 c->options = options;
915 int console_suspend_enabled = 1;
916 EXPORT_SYMBOL(console_suspend_enabled);
918 static int __init console_suspend_disable(char *str)
920 console_suspend_enabled = 0;
923 __setup("no_console_suspend", console_suspend_disable);
926 * suspend_console - suspend the console subsystem
928 * This disables printk() while we go into suspend states
930 void suspend_console(void)
932 if (!console_suspend_enabled)
934 printk("Suspending console(s)\n");
935 acquire_console_sem();
936 console_suspended = 1;
939 void resume_console(void)
941 if (!console_suspend_enabled)
943 console_suspended = 0;
944 release_console_sem();
948 * acquire_console_sem - lock the console system for exclusive use.
950 * Acquires a semaphore which guarantees that the caller has
951 * exclusive access to the console system and the console_drivers list.
953 * Can sleep, returns nothing.
955 void acquire_console_sem(void)
957 BUG_ON(in_interrupt());
958 if (console_suspended) {
959 down(&secondary_console_sem);
964 console_may_schedule = 1;
966 EXPORT_SYMBOL(acquire_console_sem);
968 int try_acquire_console_sem(void)
970 if (down_trylock(&console_sem))
973 console_may_schedule = 0;
976 EXPORT_SYMBOL(try_acquire_console_sem);
978 int is_console_locked(void)
980 return console_locked;
983 void wake_up_klogd(void)
985 if (!oops_in_progress && waitqueue_active(&log_wait))
986 wake_up_interruptible(&log_wait);
990 * release_console_sem - unlock the console system
992 * Releases the semaphore which the caller holds on the console system
993 * and the console driver list.
995 * While the semaphore was held, console output may have been buffered
996 * by printk(). If this is the case, release_console_sem() emits
997 * the output prior to releasing the semaphore.
999 * If there is output waiting for klogd, we wake it up.
1001 * release_console_sem() may be called from any context.
1003 void release_console_sem(void)
1005 unsigned long flags;
1006 unsigned _con_start, _log_end;
1007 unsigned wake_klogd = 0;
1009 if (console_suspended) {
1010 up(&secondary_console_sem);
1014 console_may_schedule = 0;
1017 spin_lock_irqsave(&logbuf_lock, flags);
1018 wake_klogd |= log_start - log_end;
1019 if (con_start == log_end)
1020 break; /* Nothing to print */
1021 _con_start = con_start;
1023 con_start = log_end; /* Flush */
1024 spin_unlock(&logbuf_lock);
1025 call_console_drivers(_con_start, _log_end);
1026 local_irq_restore(flags);
1030 spin_unlock_irqrestore(&logbuf_lock, flags);
1034 EXPORT_SYMBOL(release_console_sem);
1037 * console_conditional_schedule - yield the CPU if required
1039 * If the console code is currently allowed to sleep, and
1040 * if this CPU should yield the CPU to another task, do
1043 * Must be called within acquire_console_sem().
1045 void __sched console_conditional_schedule(void)
1047 if (console_may_schedule)
1050 EXPORT_SYMBOL(console_conditional_schedule);
1052 void console_print(const char *s)
1054 printk(KERN_EMERG "%s", s);
1056 EXPORT_SYMBOL(console_print);
1058 void console_unblank(void)
1063 * console_unblank can no longer be called in interrupt context unless
1064 * oops_in_progress is set to 1..
1066 if (oops_in_progress) {
1067 if (down_trylock(&console_sem) != 0)
1070 acquire_console_sem();
1073 console_may_schedule = 0;
1074 for (c = console_drivers; c != NULL; c = c->next)
1075 if ((c->flags & CON_ENABLED) && c->unblank)
1077 release_console_sem();
1081 * Return the console tty driver structure and its associated index
1083 struct tty_driver *console_device(int *index)
1086 struct tty_driver *driver = NULL;
1088 acquire_console_sem();
1089 for (c = console_drivers; c != NULL; c = c->next) {
1092 driver = c->device(c, index);
1096 release_console_sem();
1101 * Prevent further output on the passed console device so that (for example)
1102 * serial drivers can disable console output before suspending a port, and can
1103 * re-enable output afterwards.
1105 void console_stop(struct console *console)
1107 acquire_console_sem();
1108 console->flags &= ~CON_ENABLED;
1109 release_console_sem();
1111 EXPORT_SYMBOL(console_stop);
1113 void console_start(struct console *console)
1115 acquire_console_sem();
1116 console->flags |= CON_ENABLED;
1117 release_console_sem();
1119 EXPORT_SYMBOL(console_start);
1122 * The console driver calls this routine during kernel initialization
1123 * to register the console printing procedure with printk() and to
1124 * print any messages that were printed by the kernel before the
1125 * console driver was initialized.
1127 void register_console(struct console *console)
1130 unsigned long flags;
1131 struct console *bootconsole = NULL;
1133 if (console_drivers) {
1134 if (console->flags & CON_BOOT)
1136 if (console_drivers->flags & CON_BOOT)
1137 bootconsole = console_drivers;
1140 if (preferred_console < 0 || bootconsole || !console_drivers)
1141 preferred_console = selected_console;
1143 if (console->early_setup)
1144 console->early_setup();
1147 * See if we want to use this console driver. If we
1148 * didn't select a console we take the first one
1149 * that registers here.
1151 if (preferred_console < 0) {
1152 if (console->index < 0)
1154 if (console->setup == NULL ||
1155 console->setup(console, NULL) == 0) {
1156 console->flags |= CON_ENABLED | CON_CONSDEV;
1157 preferred_console = 0;
1162 * See if this console matches one we selected on
1165 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0];
1167 if (strcmp(console_cmdline[i].name, console->name) != 0)
1169 if (console->index >= 0 &&
1170 console->index != console_cmdline[i].index)
1172 if (console->index < 0)
1173 console->index = console_cmdline[i].index;
1174 if (console->setup &&
1175 console->setup(console, console_cmdline[i].options) != 0)
1177 console->flags |= CON_ENABLED;
1178 console->index = console_cmdline[i].index;
1179 if (i == selected_console) {
1180 console->flags |= CON_CONSDEV;
1181 preferred_console = selected_console;
1186 if (!(console->flags & CON_ENABLED))
1189 if (bootconsole && (console->flags & CON_CONSDEV)) {
1190 printk(KERN_INFO "console handover: boot [%s%d] -> real [%s%d]\n",
1191 bootconsole->name, bootconsole->index,
1192 console->name, console->index);
1193 unregister_console(bootconsole);
1194 console->flags &= ~CON_PRINTBUFFER;
1196 printk(KERN_INFO "console [%s%d] enabled\n",
1197 console->name, console->index);
1201 * Put this console in the list - keep the
1202 * preferred driver at the head of the list.
1204 acquire_console_sem();
1205 if ((console->flags & CON_CONSDEV) || console_drivers == NULL) {
1206 console->next = console_drivers;
1207 console_drivers = console;
1209 console->next->flags &= ~CON_CONSDEV;
1211 console->next = console_drivers->next;
1212 console_drivers->next = console;
1214 if (console->flags & CON_PRINTBUFFER) {
1216 * release_console_sem() will print out the buffered messages
1219 spin_lock_irqsave(&logbuf_lock, flags);
1220 con_start = log_start;
1221 spin_unlock_irqrestore(&logbuf_lock, flags);
1223 release_console_sem();
1225 EXPORT_SYMBOL(register_console);
1227 int unregister_console(struct console *console)
1229 struct console *a, *b;
1232 acquire_console_sem();
1233 if (console_drivers == console) {
1234 console_drivers=console->next;
1236 } else if (console_drivers) {
1237 for (a=console_drivers->next, b=console_drivers ;
1238 a; b=a, a=b->next) {
1248 * If this isn't the last console and it has CON_CONSDEV set, we
1249 * need to set it on the next preferred console.
1251 if (console_drivers != NULL && console->flags & CON_CONSDEV)
1252 console_drivers->flags |= CON_CONSDEV;
1254 release_console_sem();
1257 EXPORT_SYMBOL(unregister_console);
1259 static int __init disable_boot_consoles(void)
1261 if (console_drivers != NULL) {
1262 if (console_drivers->flags & CON_BOOT) {
1263 printk(KERN_INFO "turn off boot console %s%d\n",
1264 console_drivers->name, console_drivers->index);
1265 return unregister_console(console_drivers);
1270 late_initcall(disable_boot_consoles);
1273 * tty_write_message - write a message to a certain tty, not just the console.
1274 * @tty: the destination tty_struct
1275 * @msg: the message to write
1277 * This is used for messages that need to be redirected to a specific tty.
1278 * We don't put it into the syslog queue right now maybe in the future if
1281 void tty_write_message(struct tty_struct *tty, char *msg)
1283 if (tty && tty->driver->write)
1284 tty->driver->write(tty, msg, strlen(msg));
1288 #if defined CONFIG_PRINTK
1290 * printk rate limiting, lifted from the networking subsystem.
1292 * This enforces a rate limit: not more than one kernel message
1293 * every printk_ratelimit_jiffies to make a denial-of-service
1294 * attack impossible.
1296 int __printk_ratelimit(int ratelimit_jiffies, int ratelimit_burst)
1298 static DEFINE_SPINLOCK(ratelimit_lock);
1299 static unsigned toks = 10 * 5 * HZ;
1300 static unsigned long last_msg;
1302 unsigned long flags;
1303 unsigned long now = jiffies;
1305 spin_lock_irqsave(&ratelimit_lock, flags);
1306 toks += now - last_msg;
1308 if (toks > (ratelimit_burst * ratelimit_jiffies))
1309 toks = ratelimit_burst * ratelimit_jiffies;
1310 if (toks >= ratelimit_jiffies) {
1314 toks -= ratelimit_jiffies;
1315 spin_unlock_irqrestore(&ratelimit_lock, flags);
1317 printk(KERN_WARNING "printk: %d messages suppressed.\n", lost);
1321 spin_unlock_irqrestore(&ratelimit_lock, flags);
1324 EXPORT_SYMBOL(__printk_ratelimit);
1326 /* minimum time in jiffies between messages */
1327 int printk_ratelimit_jiffies = 5 * HZ;
1329 /* number of messages we send before ratelimiting */
1330 int printk_ratelimit_burst = 10;
1332 int printk_ratelimit(void)
1334 return __printk_ratelimit(printk_ratelimit_jiffies,
1335 printk_ratelimit_burst);
1337 EXPORT_SYMBOL(printk_ratelimit);
1340 * printk_timed_ratelimit - caller-controlled printk ratelimiting
1341 * @caller_jiffies: pointer to caller's state
1342 * @interval_msecs: minimum interval between prints
1344 * printk_timed_ratelimit() returns true if more than @interval_msecs
1345 * milliseconds have elapsed since the last time printk_timed_ratelimit()
1348 bool printk_timed_ratelimit(unsigned long *caller_jiffies,
1349 unsigned int interval_msecs)
1351 if (*caller_jiffies == 0 || time_after(jiffies, *caller_jiffies)) {
1352 *caller_jiffies = jiffies + msecs_to_jiffies(interval_msecs);
1357 EXPORT_SYMBOL(printk_timed_ratelimit);