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 /* 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 static DECLARE_MUTEX(secondary_console_sem);
77 struct console *console_drivers;
79 * This is used for debugging the mess that is the VT code by
80 * keeping track if we have the console semaphore held. It's
81 * definitely not the perfect debug tool (we don't know if _WE_
82 * hold it are racing, but it helps tracking those weird code
83 * path in the console code where we end up in places I want
84 * locked without the console sempahore held
86 static int console_locked, console_suspended;
89 * logbuf_lock protects log_buf, log_start, log_end, con_start and logged_chars
90 * It is also used in interesting ways to provide interlocking in
91 * release_console_sem().
93 static DEFINE_SPINLOCK(logbuf_lock);
95 #define LOG_BUF_MASK (log_buf_len-1)
96 #define LOG_BUF(idx) (log_buf[(idx) & LOG_BUF_MASK])
99 * The indices into log_buf are not constrained to log_buf_len - they
100 * must be masked before subscripting
102 static unsigned log_start; /* Index into log_buf: next char to be read by syslog() */
103 static unsigned con_start; /* Index into log_buf: next char to be sent to consoles */
104 static unsigned log_end; /* Index into log_buf: most-recently-written-char + 1 */
107 * Array of consoles built from command line options (console=)
109 struct console_cmdline
111 char name[8]; /* Name of the driver */
112 int index; /* Minor dev. to use */
113 char *options; /* Options for the driver */
114 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
115 char *brl_options; /* Options for braille driver */
119 #define MAX_CMDLINECONSOLES 8
121 static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
122 static int selected_console = -1;
123 static int preferred_console = -1;
124 int console_set_on_cmdline;
125 EXPORT_SYMBOL(console_set_on_cmdline);
127 /* Flag: console code may call schedule() */
128 static int console_may_schedule;
132 static char __log_buf[__LOG_BUF_LEN];
133 static char *log_buf = __log_buf;
134 static int log_buf_len = __LOG_BUF_LEN;
135 static unsigned logged_chars; /* Number of chars produced since last read+clear operation */
137 static int __init log_buf_len_setup(char *str)
139 unsigned size = memparse(str, &str);
143 size = roundup_pow_of_two(size);
144 if (size > log_buf_len) {
145 unsigned start, dest_idx, offset;
148 new_log_buf = alloc_bootmem(size);
150 printk(KERN_WARNING "log_buf_len: allocation failed\n");
154 spin_lock_irqsave(&logbuf_lock, flags);
156 log_buf = new_log_buf;
158 offset = start = min(con_start, log_start);
160 while (start != log_end) {
161 log_buf[dest_idx] = __log_buf[start & (__LOG_BUF_LEN - 1)];
168 spin_unlock_irqrestore(&logbuf_lock, flags);
170 printk(KERN_NOTICE "log_buf_len: %d\n", log_buf_len);
176 __setup("log_buf_len=", log_buf_len_setup);
178 #ifdef CONFIG_BOOT_PRINTK_DELAY
180 static unsigned int boot_delay; /* msecs delay after each printk during bootup */
181 static unsigned long long printk_delay_msec; /* per msec, based on boot_delay */
183 static int __init boot_delay_setup(char *str)
186 unsigned long long loops_per_msec;
188 lpj = preset_lpj ? preset_lpj : 1000000; /* some guess */
189 loops_per_msec = (unsigned long long)lpj / 1000 * HZ;
191 get_option(&str, &boot_delay);
192 if (boot_delay > 10 * 1000)
195 printk_delay_msec = loops_per_msec;
196 printk(KERN_DEBUG "boot_delay: %u, preset_lpj: %ld, lpj: %lu, "
197 "HZ: %d, printk_delay_msec: %llu\n",
198 boot_delay, preset_lpj, lpj, HZ, printk_delay_msec);
201 __setup("boot_delay=", boot_delay_setup);
203 static void boot_delay_msec(void)
205 unsigned long long k;
206 unsigned long timeout;
208 if (boot_delay == 0 || system_state != SYSTEM_BOOTING)
211 k = (unsigned long long)printk_delay_msec * boot_delay;
213 timeout = jiffies + msecs_to_jiffies(boot_delay);
218 * use (volatile) jiffies to prevent
219 * compiler reduction; loop termination via jiffies
220 * is secondary and may or may not happen.
222 if (time_after(jiffies, timeout))
224 touch_nmi_watchdog();
228 static inline void boot_delay_msec(void)
234 * Return the number of unread characters in the log buffer.
236 int log_buf_get_len(void)
242 * Copy a range of characters from the log buffer.
244 int log_buf_copy(char *dest, int idx, int len)
247 bool took_lock = false;
249 if (!oops_in_progress) {
250 spin_lock_irq(&logbuf_lock);
254 max = log_buf_get_len();
255 if (idx < 0 || idx >= max) {
261 idx += (log_end - max);
263 dest[len] = LOG_BUF(idx + len);
267 spin_unlock_irq(&logbuf_lock);
273 * Extract a single character from the log buffer.
275 int log_buf_read(int idx)
279 if (log_buf_copy(&ret, idx, 1) == 1)
286 * Commands to do_syslog:
288 * 0 -- Close the log. Currently a NOP.
289 * 1 -- Open the log. Currently a NOP.
290 * 2 -- Read from the log.
291 * 3 -- Read all messages remaining in the ring buffer.
292 * 4 -- Read and clear all messages remaining in the ring buffer
293 * 5 -- Clear ring buffer.
294 * 6 -- Disable printk's to console
295 * 7 -- Enable printk's to console
296 * 8 -- Set level of messages printed to console
297 * 9 -- Return number of unread characters in the log buffer
298 * 10 -- Return size of the log buffer
300 int do_syslog(int type, char __user *buf, int len)
302 unsigned i, j, limit, count;
307 error = security_syslog(type);
312 case 0: /* Close log */
314 case 1: /* Open log */
316 case 2: /* Read from log */
323 if (!access_ok(VERIFY_WRITE, buf, len)) {
327 error = wait_event_interruptible(log_wait,
328 (log_start - log_end));
332 spin_lock_irq(&logbuf_lock);
333 while (!error && (log_start != log_end) && i < len) {
334 c = LOG_BUF(log_start);
336 spin_unlock_irq(&logbuf_lock);
337 error = __put_user(c,buf);
341 spin_lock_irq(&logbuf_lock);
343 spin_unlock_irq(&logbuf_lock);
347 case 4: /* Read/clear last kernel messages */
350 case 3: /* Read last kernel messages */
357 if (!access_ok(VERIFY_WRITE, buf, len)) {
362 if (count > log_buf_len)
364 spin_lock_irq(&logbuf_lock);
365 if (count > logged_chars)
366 count = logged_chars;
371 * __put_user() could sleep, and while we sleep
372 * printk() could overwrite the messages
373 * we try to copy to user space. Therefore
374 * the messages are copied in reverse. <manfreds>
376 for (i = 0; i < count && !error; i++) {
378 if (j + log_buf_len < log_end)
381 spin_unlock_irq(&logbuf_lock);
382 error = __put_user(c,&buf[count-1-i]);
384 spin_lock_irq(&logbuf_lock);
386 spin_unlock_irq(&logbuf_lock);
391 int offset = count-error;
392 /* buffer overflow during copy, correct user buffer. */
393 for (i = 0; i < error; i++) {
394 if (__get_user(c,&buf[i+offset]) ||
395 __put_user(c,&buf[i])) {
403 case 5: /* Clear ring buffer */
406 case 6: /* Disable logging to console */
407 console_loglevel = minimum_console_loglevel;
409 case 7: /* Enable logging to console */
410 console_loglevel = default_console_loglevel;
412 case 8: /* Set level of messages printed to console */
414 if (len < 1 || len > 8)
416 if (len < minimum_console_loglevel)
417 len = minimum_console_loglevel;
418 console_loglevel = len;
421 case 9: /* Number of chars in the log buffer */
422 error = log_end - log_start;
424 case 10: /* Size of the log buffer */
435 asmlinkage long sys_syslog(int type, char __user *buf, int len)
437 return do_syslog(type, buf, len);
441 * Call the console drivers on a range of log_buf
443 static void __call_console_drivers(unsigned start, unsigned end)
447 for (con = console_drivers; con; con = con->next) {
448 if ((con->flags & CON_ENABLED) && con->write &&
449 (cpu_online(smp_processor_id()) ||
450 (con->flags & CON_ANYTIME)))
451 con->write(con, &LOG_BUF(start), end - start);
455 static int __read_mostly ignore_loglevel;
457 static int __init ignore_loglevel_setup(char *str)
460 printk(KERN_INFO "debug: ignoring loglevel setting.\n");
465 early_param("ignore_loglevel", ignore_loglevel_setup);
468 * Write out chars from start to end - 1 inclusive
470 static void _call_console_drivers(unsigned start,
471 unsigned end, int msg_log_level)
473 if ((msg_log_level < console_loglevel || ignore_loglevel) &&
474 console_drivers && start != end) {
475 if ((start & LOG_BUF_MASK) > (end & LOG_BUF_MASK)) {
477 __call_console_drivers(start & LOG_BUF_MASK,
479 __call_console_drivers(0, end & LOG_BUF_MASK);
481 __call_console_drivers(start, end);
487 * Call the console drivers, asking them to write out
488 * log_buf[start] to log_buf[end - 1].
489 * The console_sem must be held.
491 static void call_console_drivers(unsigned start, unsigned end)
493 unsigned cur_index, start_print;
494 static int msg_level = -1;
496 BUG_ON(((int)(start - end)) > 0);
500 while (cur_index != end) {
501 if (msg_level < 0 && ((end - cur_index) > 2) &&
502 LOG_BUF(cur_index + 0) == '<' &&
503 LOG_BUF(cur_index + 1) >= '0' &&
504 LOG_BUF(cur_index + 1) <= '7' &&
505 LOG_BUF(cur_index + 2) == '>') {
506 msg_level = LOG_BUF(cur_index + 1) - '0';
508 start_print = cur_index;
510 while (cur_index != end) {
511 char c = LOG_BUF(cur_index);
517 * printk() has already given us loglevel tags in
518 * the buffer. This code is here in case the
519 * log buffer has wrapped right round and scribbled
522 msg_level = default_message_loglevel;
524 _call_console_drivers(start_print, cur_index, msg_level);
526 start_print = cur_index;
531 _call_console_drivers(start_print, end, msg_level);
534 static void emit_log_char(char c)
536 LOG_BUF(log_end) = c;
538 if (log_end - log_start > log_buf_len)
539 log_start = log_end - log_buf_len;
540 if (log_end - con_start > log_buf_len)
541 con_start = log_end - log_buf_len;
542 if (logged_chars < log_buf_len)
547 * Zap console related locks when oopsing. Only zap at most once
548 * every 10 seconds, to leave time for slow consoles to print a
551 static void zap_locks(void)
553 static unsigned long oops_timestamp;
555 if (time_after_eq(jiffies, oops_timestamp) &&
556 !time_after(jiffies, oops_timestamp + 30 * HZ))
559 oops_timestamp = jiffies;
561 /* If a crash is occurring, make sure we can't deadlock */
562 spin_lock_init(&logbuf_lock);
563 /* And make sure that we print immediately */
564 init_MUTEX(&console_sem);
567 #if defined(CONFIG_PRINTK_TIME)
568 static int printk_time = 1;
570 static int printk_time = 0;
572 module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR);
574 /* Check if we have any console registered that can be called early in boot. */
575 static int have_callable_console(void)
579 for (con = console_drivers; con; con = con->next)
580 if (con->flags & CON_ANYTIME)
587 * printk - print a kernel message
588 * @fmt: format string
590 * This is printk(). It can be called from any context. We want it to work.
591 * Be aware of the fact that if oops_in_progress is not set, we might try to
592 * wake klogd up which could deadlock on runqueue lock if printk() is called
593 * from scheduler code.
595 * We try to grab the console_sem. If we succeed, it's easy - we log the output and
596 * call the console drivers. If we fail to get the semaphore we place the output
597 * into the log buffer and return. The current holder of the console_sem will
598 * notice the new output in release_console_sem() and will send it to the
599 * consoles before releasing the semaphore.
601 * One effect of this deferred printing is that code which calls printk() and
602 * then changes console_loglevel may break. This is because console_loglevel
603 * is inspected when the actual printing occurs.
609 asmlinkage int printk(const char *fmt, ...)
615 r = vprintk(fmt, args);
621 /* cpu currently holding logbuf_lock */
622 static volatile unsigned int printk_cpu = UINT_MAX;
625 * Can we actually use the console at this time on this cpu?
627 * Console drivers may assume that per-cpu resources have
628 * been allocated. So unless they're explicitly marked as
629 * being able to cope (CON_ANYTIME) don't call them until
630 * this CPU is officially up.
632 static inline int can_use_console(unsigned int cpu)
634 return cpu_online(cpu) || have_callable_console();
638 * Try to get console ownership to actually show the kernel
639 * messages from a 'printk'. Return true (and with the
640 * console_semaphore held, and 'console_locked' set) if it
641 * is successful, false otherwise.
643 * This gets called with the 'logbuf_lock' spinlock held and
644 * interrupts disabled. It should return with 'lockbuf_lock'
645 * released but interrupts still disabled.
647 static int acquire_console_semaphore_for_printk(unsigned int cpu)
651 if (!try_acquire_console_sem()) {
655 * If we can't use the console, we need to release
656 * the console semaphore by hand to avoid flushing
657 * the buffer. We need to hold the console semaphore
658 * in order to do this test safely.
660 if (!can_use_console(cpu)) {
666 printk_cpu = UINT_MAX;
667 spin_unlock(&logbuf_lock);
671 const char printk_recursion_bug_msg [] =
672 KERN_CRIT "BUG: recent printk recursion!\n";
673 static int printk_recursion_bug;
675 asmlinkage int vprintk(const char *fmt, va_list args)
677 static int log_level_unknown = 1;
678 static char printk_buf[1024];
688 /* This stops the holder of console_sem just where we want him */
689 raw_local_irq_save(flags);
690 this_cpu = smp_processor_id();
693 * Ouch, printk recursed into itself!
695 if (unlikely(printk_cpu == this_cpu)) {
697 * If a crash is occurring during printk() on this CPU,
698 * then try to get the crash message out but make sure
699 * we can't deadlock. Otherwise just return to avoid the
700 * recursion and return - but flag the recursion so that
701 * it can be printed at the next appropriate moment:
703 if (!oops_in_progress) {
704 printk_recursion_bug = 1;
705 goto out_restore_irqs;
711 spin_lock(&logbuf_lock);
712 printk_cpu = this_cpu;
714 if (printk_recursion_bug) {
715 printk_recursion_bug = 0;
716 strcpy(printk_buf, printk_recursion_bug_msg);
717 printed_len = sizeof(printk_recursion_bug_msg);
719 /* Emit the output into the temporary buffer */
720 printed_len += vscnprintf(printk_buf + printed_len,
721 sizeof(printk_buf) - printed_len, fmt, args);
724 * Copy the output into log_buf. If the caller didn't provide
725 * appropriate log level tags, we insert them here
727 for (p = printk_buf; *p; p++) {
728 if (log_level_unknown) {
729 /* log_level_unknown signals the start of a new line */
734 unsigned long long t;
735 unsigned long nanosec_rem;
738 * force the log level token to be
739 * before the time output.
741 if (p[0] == '<' && p[1] >='0' &&
742 p[1] <= '7' && p[2] == '>') {
747 loglev_char = default_message_loglevel
750 t = cpu_clock(printk_cpu);
751 nanosec_rem = do_div(t, 1000000000);
758 for (tp = tbuf; tp < tbuf + tlen; tp++)
762 if (p[0] != '<' || p[1] < '0' ||
763 p[1] > '7' || p[2] != '>') {
765 emit_log_char(default_message_loglevel
771 log_level_unknown = 0;
777 log_level_unknown = 1;
781 * Try to acquire and then immediately release the
782 * console semaphore. The release will do all the
783 * actual magic (print out buffers, wake up klogd,
786 * The acquire_console_semaphore_for_printk() function
787 * will release 'logbuf_lock' regardless of whether it
788 * actually gets the semaphore or not.
790 if (acquire_console_semaphore_for_printk(this_cpu))
791 release_console_sem();
795 raw_local_irq_restore(flags);
800 EXPORT_SYMBOL(printk);
801 EXPORT_SYMBOL(vprintk);
805 asmlinkage long sys_syslog(int type, char __user *buf, int len)
810 static void call_console_drivers(unsigned start, unsigned end)
816 static int __add_preferred_console(char *name, int idx, char *options,
819 struct console_cmdline *c;
823 * See if this tty is not yet registered, and
824 * if we have a slot free.
826 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
827 if (strcmp(console_cmdline[i].name, name) == 0 &&
828 console_cmdline[i].index == idx) {
830 selected_console = i;
833 if (i == MAX_CMDLINECONSOLES)
836 selected_console = i;
837 c = &console_cmdline[i];
838 strlcpy(c->name, name, sizeof(c->name));
839 c->options = options;
840 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
841 c->brl_options = brl_options;
847 * Set up a list of consoles. Called from init/main.c
849 static int __init console_setup(char *str)
851 char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for index */
852 char *s, *options, *brl_options = NULL;
855 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
856 if (!memcmp(str, "brl,", 4)) {
859 } else if (!memcmp(str, "brl=", 4)) {
860 brl_options = str + 4;
861 str = strchr(brl_options, ',');
863 printk(KERN_ERR "need port name after brl=\n");
871 * Decode str into name, index, options.
873 if (str[0] >= '0' && str[0] <= '9') {
875 strncpy(buf + 4, str, sizeof(buf) - 5);
877 strncpy(buf, str, sizeof(buf) - 1);
879 buf[sizeof(buf) - 1] = 0;
880 if ((options = strchr(str, ',')) != NULL)
883 if (!strcmp(str, "ttya"))
884 strcpy(buf, "ttyS0");
885 if (!strcmp(str, "ttyb"))
886 strcpy(buf, "ttyS1");
888 for (s = buf; *s; s++)
889 if ((*s >= '0' && *s <= '9') || *s == ',')
891 idx = simple_strtoul(s, NULL, 10);
894 __add_preferred_console(buf, idx, options, brl_options);
895 console_set_on_cmdline = 1;
898 __setup("console=", console_setup);
901 * add_preferred_console - add a device to the list of preferred consoles.
904 * @options: options for this console
906 * The last preferred console added will be used for kernel messages
907 * and stdin/out/err for init. Normally this is used by console_setup
908 * above to handle user-supplied console arguments; however it can also
909 * be used by arch-specific code either to override the user or more
910 * commonly to provide a default console (ie from PROM variables) when
911 * the user has not supplied one.
913 int add_preferred_console(char *name, int idx, char *options)
915 return __add_preferred_console(name, idx, options, NULL);
918 int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options)
920 struct console_cmdline *c;
923 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
924 if (strcmp(console_cmdline[i].name, name) == 0 &&
925 console_cmdline[i].index == idx) {
926 c = &console_cmdline[i];
927 strlcpy(c->name, name_new, sizeof(c->name));
928 c->name[sizeof(c->name) - 1] = 0;
929 c->options = options;
937 int console_suspend_enabled = 1;
938 EXPORT_SYMBOL(console_suspend_enabled);
940 static int __init console_suspend_disable(char *str)
942 console_suspend_enabled = 0;
945 __setup("no_console_suspend", console_suspend_disable);
948 * suspend_console - suspend the console subsystem
950 * This disables printk() while we go into suspend states
952 void suspend_console(void)
954 if (!console_suspend_enabled)
956 printk("Suspending console(s)\n");
957 acquire_console_sem();
958 console_suspended = 1;
961 void resume_console(void)
963 if (!console_suspend_enabled)
965 console_suspended = 0;
966 release_console_sem();
970 * acquire_console_sem - lock the console system for exclusive use.
972 * Acquires a semaphore which guarantees that the caller has
973 * exclusive access to the console system and the console_drivers list.
975 * Can sleep, returns nothing.
977 void acquire_console_sem(void)
979 BUG_ON(in_interrupt());
980 if (console_suspended) {
981 down(&secondary_console_sem);
986 console_may_schedule = 1;
988 EXPORT_SYMBOL(acquire_console_sem);
990 int try_acquire_console_sem(void)
992 if (down_trylock(&console_sem))
995 console_may_schedule = 0;
998 EXPORT_SYMBOL(try_acquire_console_sem);
1000 int is_console_locked(void)
1002 return console_locked;
1005 void wake_up_klogd(void)
1007 if (!oops_in_progress && waitqueue_active(&log_wait))
1008 wake_up_interruptible(&log_wait);
1012 * release_console_sem - unlock the console system
1014 * Releases the semaphore which the caller holds on the console system
1015 * and the console driver list.
1017 * While the semaphore was held, console output may have been buffered
1018 * by printk(). If this is the case, release_console_sem() emits
1019 * the output prior to releasing the semaphore.
1021 * If there is output waiting for klogd, we wake it up.
1023 * release_console_sem() may be called from any context.
1025 void release_console_sem(void)
1027 unsigned long flags;
1028 unsigned _con_start, _log_end;
1029 unsigned wake_klogd = 0;
1031 if (console_suspended) {
1032 up(&secondary_console_sem);
1036 console_may_schedule = 0;
1039 spin_lock_irqsave(&logbuf_lock, flags);
1040 wake_klogd |= log_start - log_end;
1041 if (con_start == log_end)
1042 break; /* Nothing to print */
1043 _con_start = con_start;
1045 con_start = log_end; /* Flush */
1046 spin_unlock(&logbuf_lock);
1047 call_console_drivers(_con_start, _log_end);
1048 local_irq_restore(flags);
1052 spin_unlock_irqrestore(&logbuf_lock, flags);
1056 EXPORT_SYMBOL(release_console_sem);
1059 * console_conditional_schedule - yield the CPU if required
1061 * If the console code is currently allowed to sleep, and
1062 * if this CPU should yield the CPU to another task, do
1065 * Must be called within acquire_console_sem().
1067 void __sched console_conditional_schedule(void)
1069 if (console_may_schedule)
1072 EXPORT_SYMBOL(console_conditional_schedule);
1074 void console_print(const char *s)
1076 printk(KERN_EMERG "%s", s);
1078 EXPORT_SYMBOL(console_print);
1080 void console_unblank(void)
1085 * console_unblank can no longer be called in interrupt context unless
1086 * oops_in_progress is set to 1..
1088 if (oops_in_progress) {
1089 if (down_trylock(&console_sem) != 0)
1092 acquire_console_sem();
1095 console_may_schedule = 0;
1096 for (c = console_drivers; c != NULL; c = c->next)
1097 if ((c->flags & CON_ENABLED) && c->unblank)
1099 release_console_sem();
1103 * Return the console tty driver structure and its associated index
1105 struct tty_driver *console_device(int *index)
1108 struct tty_driver *driver = NULL;
1110 acquire_console_sem();
1111 for (c = console_drivers; c != NULL; c = c->next) {
1114 driver = c->device(c, index);
1118 release_console_sem();
1123 * Prevent further output on the passed console device so that (for example)
1124 * serial drivers can disable console output before suspending a port, and can
1125 * re-enable output afterwards.
1127 void console_stop(struct console *console)
1129 acquire_console_sem();
1130 console->flags &= ~CON_ENABLED;
1131 release_console_sem();
1133 EXPORT_SYMBOL(console_stop);
1135 void console_start(struct console *console)
1137 acquire_console_sem();
1138 console->flags |= CON_ENABLED;
1139 release_console_sem();
1141 EXPORT_SYMBOL(console_start);
1144 * The console driver calls this routine during kernel initialization
1145 * to register the console printing procedure with printk() and to
1146 * print any messages that were printed by the kernel before the
1147 * console driver was initialized.
1149 void register_console(struct console *console)
1152 unsigned long flags;
1153 struct console *bootconsole = NULL;
1155 if (console_drivers) {
1156 if (console->flags & CON_BOOT)
1158 if (console_drivers->flags & CON_BOOT)
1159 bootconsole = console_drivers;
1162 if (preferred_console < 0 || bootconsole || !console_drivers)
1163 preferred_console = selected_console;
1165 if (console->early_setup)
1166 console->early_setup();
1169 * See if we want to use this console driver. If we
1170 * didn't select a console we take the first one
1171 * that registers here.
1173 if (preferred_console < 0) {
1174 if (console->index < 0)
1176 if (console->setup == NULL ||
1177 console->setup(console, NULL) == 0) {
1178 console->flags |= CON_ENABLED | CON_CONSDEV;
1179 preferred_console = 0;
1184 * See if this console matches one we selected on
1187 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0];
1189 if (strcmp(console_cmdline[i].name, console->name) != 0)
1191 if (console->index >= 0 &&
1192 console->index != console_cmdline[i].index)
1194 if (console->index < 0)
1195 console->index = console_cmdline[i].index;
1196 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1197 if (console_cmdline[i].brl_options) {
1198 console->flags |= CON_BRL;
1199 braille_register_console(console,
1200 console_cmdline[i].index,
1201 console_cmdline[i].options,
1202 console_cmdline[i].brl_options);
1206 if (console->setup &&
1207 console->setup(console, console_cmdline[i].options) != 0)
1209 console->flags |= CON_ENABLED;
1210 console->index = console_cmdline[i].index;
1211 if (i == selected_console) {
1212 console->flags |= CON_CONSDEV;
1213 preferred_console = selected_console;
1218 if (!(console->flags & CON_ENABLED))
1221 if (bootconsole && (console->flags & CON_CONSDEV)) {
1222 printk(KERN_INFO "console handover: boot [%s%d] -> real [%s%d]\n",
1223 bootconsole->name, bootconsole->index,
1224 console->name, console->index);
1225 unregister_console(bootconsole);
1226 console->flags &= ~CON_PRINTBUFFER;
1228 printk(KERN_INFO "console [%s%d] enabled\n",
1229 console->name, console->index);
1233 * Put this console in the list - keep the
1234 * preferred driver at the head of the list.
1236 acquire_console_sem();
1237 if ((console->flags & CON_CONSDEV) || console_drivers == NULL) {
1238 console->next = console_drivers;
1239 console_drivers = console;
1241 console->next->flags &= ~CON_CONSDEV;
1243 console->next = console_drivers->next;
1244 console_drivers->next = console;
1246 if (console->flags & CON_PRINTBUFFER) {
1248 * release_console_sem() will print out the buffered messages
1251 spin_lock_irqsave(&logbuf_lock, flags);
1252 con_start = log_start;
1253 spin_unlock_irqrestore(&logbuf_lock, flags);
1255 release_console_sem();
1257 EXPORT_SYMBOL(register_console);
1259 int unregister_console(struct console *console)
1261 struct console *a, *b;
1264 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1265 if (console->flags & CON_BRL)
1266 return braille_unregister_console(console);
1269 acquire_console_sem();
1270 if (console_drivers == console) {
1271 console_drivers=console->next;
1273 } else if (console_drivers) {
1274 for (a=console_drivers->next, b=console_drivers ;
1275 a; b=a, a=b->next) {
1285 * If this isn't the last console and it has CON_CONSDEV set, we
1286 * need to set it on the next preferred console.
1288 if (console_drivers != NULL && console->flags & CON_CONSDEV)
1289 console_drivers->flags |= CON_CONSDEV;
1291 release_console_sem();
1294 EXPORT_SYMBOL(unregister_console);
1296 static int __init disable_boot_consoles(void)
1298 if (console_drivers != NULL) {
1299 if (console_drivers->flags & CON_BOOT) {
1300 printk(KERN_INFO "turn off boot console %s%d\n",
1301 console_drivers->name, console_drivers->index);
1302 return unregister_console(console_drivers);
1307 late_initcall(disable_boot_consoles);
1310 * tty_write_message - write a message to a certain tty, not just the console.
1311 * @tty: the destination tty_struct
1312 * @msg: the message to write
1314 * This is used for messages that need to be redirected to a specific tty.
1315 * We don't put it into the syslog queue right now maybe in the future if
1318 void tty_write_message(struct tty_struct *tty, char *msg)
1320 if (tty && tty->ops->write)
1321 tty->ops->write(tty, msg, strlen(msg));
1325 #if defined CONFIG_PRINTK
1327 * printk rate limiting, lifted from the networking subsystem.
1329 * This enforces a rate limit: not more than one kernel message
1330 * every printk_ratelimit_jiffies to make a denial-of-service
1331 * attack impossible.
1333 int __printk_ratelimit(int ratelimit_jiffies, int ratelimit_burst)
1335 return __ratelimit(ratelimit_jiffies, ratelimit_burst);
1337 EXPORT_SYMBOL(__printk_ratelimit);
1339 /* minimum time in jiffies between messages */
1340 int printk_ratelimit_jiffies = 5 * HZ;
1342 /* number of messages we send before ratelimiting */
1343 int printk_ratelimit_burst = 10;
1345 int printk_ratelimit(void)
1347 return __printk_ratelimit(printk_ratelimit_jiffies,
1348 printk_ratelimit_burst);
1350 EXPORT_SYMBOL(printk_ratelimit);
1353 * printk_timed_ratelimit - caller-controlled printk ratelimiting
1354 * @caller_jiffies: pointer to caller's state
1355 * @interval_msecs: minimum interval between prints
1357 * printk_timed_ratelimit() returns true if more than @interval_msecs
1358 * milliseconds have elapsed since the last time printk_timed_ratelimit()
1361 bool printk_timed_ratelimit(unsigned long *caller_jiffies,
1362 unsigned int interval_msecs)
1364 if (*caller_jiffies == 0 || time_after(jiffies, *caller_jiffies)) {
1365 *caller_jiffies = jiffies + msecs_to_jiffies(interval_msecs);
1370 EXPORT_SYMBOL(printk_timed_ratelimit);