binder: Make sure user page map is in sync with kernel map before changing it.
[linux-2.6/android.git] / kernel / printk.c
blob0b7bebf439ab8a2bc95f82ab3695a550c6d5ad64
1 /*
2 * linux/kernel/printk.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * Modified to make sys_syslog() more flexible: added commands to
7 * return the last 4k of kernel messages, regardless of whether
8 * they've been read or not. Added option to suppress kernel printk's
9 * to the console. Added hook for sending the console messages
10 * elsewhere, in preparation for a serial line console (someday).
11 * Ted Ts'o, 2/11/93.
12 * Modified for sysctl support, 1/8/97, Chris Horn.
13 * Fixed SMP synchronization, 08/08/99, Manfred Spraul
14 * manfred@colorfullife.com
15 * Rewrote bits to get rid of console_lock
16 * 01Mar01 Andrew Morton <andrewm@uow.edu.au>
19 #include <linux/kernel.h>
20 #include <linux/mm.h>
21 #include <linux/tty.h>
22 #include <linux/tty_driver.h>
23 #include <linux/console.h>
24 #include <linux/init.h>
25 #include <linux/jiffies.h>
26 #include <linux/nmi.h>
27 #include <linux/module.h>
28 #include <linux/moduleparam.h>
29 #include <linux/interrupt.h> /* For in_interrupt() */
30 #include <linux/delay.h>
31 #include <linux/smp.h>
32 #include <linux/security.h>
33 #include <linux/bootmem.h>
34 #include <linux/syscalls.h>
35 #include <linux/jiffies.h>
37 #include <asm/uaccess.h>
39 #define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT)
41 /* printk's without a loglevel use this.. */
42 #define DEFAULT_MESSAGE_LOGLEVEL 4 /* KERN_WARNING */
44 /* We show everything that is MORE important than this.. */
45 #define MINIMUM_CONSOLE_LOGLEVEL 1 /* Minimum loglevel we let people use */
46 #define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */
48 DECLARE_WAIT_QUEUE_HEAD(log_wait);
50 int console_printk[4] = {
51 DEFAULT_CONSOLE_LOGLEVEL, /* console_loglevel */
52 DEFAULT_MESSAGE_LOGLEVEL, /* default_message_loglevel */
53 MINIMUM_CONSOLE_LOGLEVEL, /* minimum_console_loglevel */
54 DEFAULT_CONSOLE_LOGLEVEL, /* default_console_loglevel */
58 * Low level drivers may need that to know if they can schedule in
59 * their unblank() callback or not. So let's export it.
61 int oops_in_progress;
62 EXPORT_SYMBOL(oops_in_progress);
65 * console_sem protects the console_drivers list, and also
66 * provides serialisation for access to the entire console
67 * driver system.
69 static DECLARE_MUTEX(console_sem);
70 struct console *console_drivers;
72 * This is used for debugging the mess that is the VT code by
73 * keeping track if we have the console semaphore held. It's
74 * definitely not the perfect debug tool (we don't know if _WE_
75 * hold it are racing, but it helps tracking those weird code
76 * path in the console code where we end up in places I want
77 * locked without the console sempahore held
79 static int console_locked, console_suspended;
82 * logbuf_lock protects log_buf, log_start, log_end, con_start and logged_chars
83 * It is also used in interesting ways to provide interlocking in
84 * release_console_sem().
86 static DEFINE_SPINLOCK(logbuf_lock);
88 #define LOG_BUF_MASK (log_buf_len-1)
89 #define LOG_BUF(idx) (log_buf[(idx) & LOG_BUF_MASK])
92 * The indices into log_buf are not constrained to log_buf_len - they
93 * must be masked before subscripting
95 static unsigned long log_start; /* Index into log_buf: next char to be read by syslog() */
96 static unsigned long con_start; /* Index into log_buf: next char to be sent to consoles */
97 static unsigned long log_end; /* Index into log_buf: most-recently-written-char + 1 */
100 * Array of consoles built from command line options (console=)
102 struct console_cmdline
104 char name[8]; /* Name of the driver */
105 int index; /* Minor dev. to use */
106 char *options; /* Options for the driver */
109 #define MAX_CMDLINECONSOLES 8
111 static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
112 static int selected_console = -1;
113 static int preferred_console = -1;
115 /* Flag: console code may call schedule() */
116 static int console_may_schedule;
118 #ifdef CONFIG_PRINTK
120 static char __log_buf[__LOG_BUF_LEN];
121 static char *log_buf = __log_buf;
122 static int log_buf_len = __LOG_BUF_LEN;
123 static unsigned long logged_chars; /* Number of chars produced since last read+clear operation */
125 static int __init log_buf_len_setup(char *str)
127 unsigned long size = memparse(str, &str);
128 unsigned long flags;
130 if (size)
131 size = roundup_pow_of_two(size);
132 if (size > log_buf_len) {
133 unsigned long start, dest_idx, offset;
134 char *new_log_buf;
136 new_log_buf = alloc_bootmem(size);
137 if (!new_log_buf) {
138 printk(KERN_WARNING "log_buf_len: allocation failed\n");
139 goto out;
142 spin_lock_irqsave(&logbuf_lock, flags);
143 log_buf_len = size;
144 log_buf = new_log_buf;
146 offset = start = min(con_start, log_start);
147 dest_idx = 0;
148 while (start != log_end) {
149 log_buf[dest_idx] = __log_buf[start & (__LOG_BUF_LEN - 1)];
150 start++;
151 dest_idx++;
153 log_start -= offset;
154 con_start -= offset;
155 log_end -= offset;
156 spin_unlock_irqrestore(&logbuf_lock, flags);
158 printk(KERN_NOTICE "log_buf_len: %d\n", log_buf_len);
160 out:
161 return 1;
164 __setup("log_buf_len=", log_buf_len_setup);
166 #ifdef CONFIG_BOOT_PRINTK_DELAY
168 static unsigned int boot_delay; /* msecs delay after each printk during bootup */
169 static unsigned long long printk_delay_msec; /* per msec, based on boot_delay */
171 static int __init boot_delay_setup(char *str)
173 unsigned long lpj;
174 unsigned long long loops_per_msec;
176 lpj = preset_lpj ? preset_lpj : 1000000; /* some guess */
177 loops_per_msec = (unsigned long long)lpj / 1000 * HZ;
179 get_option(&str, &boot_delay);
180 if (boot_delay > 10 * 1000)
181 boot_delay = 0;
183 printk_delay_msec = loops_per_msec;
184 printk(KERN_DEBUG "boot_delay: %u, preset_lpj: %ld, lpj: %lu, "
185 "HZ: %d, printk_delay_msec: %llu\n",
186 boot_delay, preset_lpj, lpj, HZ, printk_delay_msec);
187 return 1;
189 __setup("boot_delay=", boot_delay_setup);
191 static void boot_delay_msec(void)
193 unsigned long long k;
194 unsigned long timeout;
196 if (boot_delay == 0 || system_state != SYSTEM_BOOTING)
197 return;
199 k = (unsigned long long)printk_delay_msec * boot_delay;
201 timeout = jiffies + msecs_to_jiffies(boot_delay);
202 while (k) {
203 k--;
204 cpu_relax();
206 * use (volatile) jiffies to prevent
207 * compiler reduction; loop termination via jiffies
208 * is secondary and may or may not happen.
210 if (time_after(jiffies, timeout))
211 break;
212 touch_nmi_watchdog();
215 #else
216 static inline void boot_delay_msec(void)
219 #endif
222 * Return the number of unread characters in the log buffer.
224 int log_buf_get_len(void)
226 return logged_chars;
230 * Copy a range of characters from the log buffer.
232 int log_buf_copy(char *dest, int idx, int len)
234 int ret, max;
235 bool took_lock = false;
237 if (!oops_in_progress) {
238 spin_lock_irq(&logbuf_lock);
239 took_lock = true;
242 max = log_buf_get_len();
243 if (idx < 0 || idx >= max) {
244 ret = -1;
245 } else {
246 if (len > max - idx)
247 len = max - idx;
248 ret = len;
249 idx += (log_end - max);
250 while (len-- > 0)
251 dest[len] = LOG_BUF(idx + len);
254 if (took_lock)
255 spin_unlock_irq(&logbuf_lock);
257 return ret;
261 * Extract a single character from the log buffer.
263 int log_buf_read(int idx)
265 char ret;
267 if (log_buf_copy(&ret, idx, 1) == 1)
268 return ret;
269 else
270 return -1;
274 * Commands to do_syslog:
276 * 0 -- Close the log. Currently a NOP.
277 * 1 -- Open the log. Currently a NOP.
278 * 2 -- Read from the log.
279 * 3 -- Read all messages remaining in the ring buffer.
280 * 4 -- Read and clear all messages remaining in the ring buffer
281 * 5 -- Clear ring buffer.
282 * 6 -- Disable printk's to console
283 * 7 -- Enable printk's to console
284 * 8 -- Set level of messages printed to console
285 * 9 -- Return number of unread characters in the log buffer
286 * 10 -- Return size of the log buffer
288 int do_syslog(int type, char __user *buf, int len)
290 unsigned long i, j, limit, count;
291 int do_clear = 0;
292 char c;
293 int error = 0;
295 error = security_syslog(type);
296 if (error)
297 return error;
299 switch (type) {
300 case 0: /* Close log */
301 break;
302 case 1: /* Open log */
303 break;
304 case 2: /* Read from log */
305 error = -EINVAL;
306 if (!buf || len < 0)
307 goto out;
308 error = 0;
309 if (!len)
310 goto out;
311 if (!access_ok(VERIFY_WRITE, buf, len)) {
312 error = -EFAULT;
313 goto out;
315 error = wait_event_interruptible(log_wait,
316 (log_start - log_end));
317 if (error)
318 goto out;
319 i = 0;
320 spin_lock_irq(&logbuf_lock);
321 while (!error && (log_start != log_end) && i < len) {
322 c = LOG_BUF(log_start);
323 log_start++;
324 spin_unlock_irq(&logbuf_lock);
325 error = __put_user(c,buf);
326 buf++;
327 i++;
328 cond_resched();
329 spin_lock_irq(&logbuf_lock);
331 spin_unlock_irq(&logbuf_lock);
332 if (!error)
333 error = i;
334 break;
335 case 4: /* Read/clear last kernel messages */
336 do_clear = 1;
337 /* FALL THRU */
338 case 3: /* Read last kernel messages */
339 error = -EINVAL;
340 if (!buf || len < 0)
341 goto out;
342 error = 0;
343 if (!len)
344 goto out;
345 if (!access_ok(VERIFY_WRITE, buf, len)) {
346 error = -EFAULT;
347 goto out;
349 count = len;
350 if (count > log_buf_len)
351 count = log_buf_len;
352 spin_lock_irq(&logbuf_lock);
353 if (count > logged_chars)
354 count = logged_chars;
355 if (do_clear)
356 logged_chars = 0;
357 limit = log_end;
359 * __put_user() could sleep, and while we sleep
360 * printk() could overwrite the messages
361 * we try to copy to user space. Therefore
362 * the messages are copied in reverse. <manfreds>
364 for (i = 0; i < count && !error; i++) {
365 j = limit-1-i;
366 if (j + log_buf_len < log_end)
367 break;
368 c = LOG_BUF(j);
369 spin_unlock_irq(&logbuf_lock);
370 error = __put_user(c,&buf[count-1-i]);
371 cond_resched();
372 spin_lock_irq(&logbuf_lock);
374 spin_unlock_irq(&logbuf_lock);
375 if (error)
376 break;
377 error = i;
378 if (i != count) {
379 int offset = count-error;
380 /* buffer overflow during copy, correct user buffer. */
381 for (i = 0; i < error; i++) {
382 if (__get_user(c,&buf[i+offset]) ||
383 __put_user(c,&buf[i])) {
384 error = -EFAULT;
385 break;
387 cond_resched();
390 break;
391 case 5: /* Clear ring buffer */
392 logged_chars = 0;
393 break;
394 case 6: /* Disable logging to console */
395 console_loglevel = minimum_console_loglevel;
396 break;
397 case 7: /* Enable logging to console */
398 console_loglevel = default_console_loglevel;
399 break;
400 case 8: /* Set level of messages printed to console */
401 error = -EINVAL;
402 if (len < 1 || len > 8)
403 goto out;
404 if (len < minimum_console_loglevel)
405 len = minimum_console_loglevel;
406 console_loglevel = len;
407 error = 0;
408 break;
409 case 9: /* Number of chars in the log buffer */
410 error = log_end - log_start;
411 break;
412 case 10: /* Size of the log buffer */
413 error = log_buf_len;
414 break;
415 default:
416 error = -EINVAL;
417 break;
419 out:
420 return error;
423 asmlinkage long sys_syslog(int type, char __user *buf, int len)
425 return do_syslog(type, buf, len);
429 * Call the console drivers on a range of log_buf
431 static void __call_console_drivers(unsigned long start, unsigned long end)
433 struct console *con;
435 for (con = console_drivers; con; con = con->next) {
436 if ((con->flags & CON_ENABLED) && con->write &&
437 (cpu_online(smp_processor_id()) ||
438 (con->flags & CON_ANYTIME)))
439 con->write(con, &LOG_BUF(start), end - start);
443 static int __read_mostly ignore_loglevel;
445 static int __init ignore_loglevel_setup(char *str)
447 ignore_loglevel = 1;
448 printk(KERN_INFO "debug: ignoring loglevel setting.\n");
450 return 1;
453 __setup("ignore_loglevel", ignore_loglevel_setup);
456 * Write out chars from start to end - 1 inclusive
458 static void _call_console_drivers(unsigned long start,
459 unsigned long end, int msg_log_level)
461 if ((msg_log_level < console_loglevel || ignore_loglevel) &&
462 console_drivers && start != end) {
463 if ((start & LOG_BUF_MASK) > (end & LOG_BUF_MASK)) {
464 /* wrapped write */
465 __call_console_drivers(start & LOG_BUF_MASK,
466 log_buf_len);
467 __call_console_drivers(0, end & LOG_BUF_MASK);
468 } else {
469 __call_console_drivers(start, end);
475 * Call the console drivers, asking them to write out
476 * log_buf[start] to log_buf[end - 1].
477 * The console_sem must be held.
479 static void call_console_drivers(unsigned long start, unsigned long end)
481 unsigned long cur_index, start_print;
482 static int msg_level = -1;
484 BUG_ON(((long)(start - end)) > 0);
486 cur_index = start;
487 start_print = start;
488 while (cur_index != end) {
489 if (msg_level < 0 && ((end - cur_index) > 2) &&
490 LOG_BUF(cur_index + 0) == '<' &&
491 LOG_BUF(cur_index + 1) >= '0' &&
492 LOG_BUF(cur_index + 1) <= '7' &&
493 LOG_BUF(cur_index + 2) == '>') {
494 msg_level = LOG_BUF(cur_index + 1) - '0';
495 cur_index += 3;
496 start_print = cur_index;
498 while (cur_index != end) {
499 char c = LOG_BUF(cur_index);
501 cur_index++;
502 if (c == '\n') {
503 if (msg_level < 0) {
505 * printk() has already given us loglevel tags in
506 * the buffer. This code is here in case the
507 * log buffer has wrapped right round and scribbled
508 * on those tags
510 msg_level = default_message_loglevel;
512 _call_console_drivers(start_print, cur_index, msg_level);
513 msg_level = -1;
514 start_print = cur_index;
515 break;
519 _call_console_drivers(start_print, end, msg_level);
522 static void emit_log_char(char c)
524 LOG_BUF(log_end) = c;
525 log_end++;
526 if (log_end - log_start > log_buf_len)
527 log_start = log_end - log_buf_len;
528 if (log_end - con_start > log_buf_len)
529 con_start = log_end - log_buf_len;
530 if (logged_chars < log_buf_len)
531 logged_chars++;
535 * Zap console related locks when oopsing. Only zap at most once
536 * every 10 seconds, to leave time for slow consoles to print a
537 * full oops.
539 static void zap_locks(void)
541 static unsigned long oops_timestamp;
543 if (time_after_eq(jiffies, oops_timestamp) &&
544 !time_after(jiffies, oops_timestamp + 30 * HZ))
545 return;
547 oops_timestamp = jiffies;
549 /* If a crash is occurring, make sure we can't deadlock */
550 spin_lock_init(&logbuf_lock);
551 /* And make sure that we print immediately */
552 init_MUTEX(&console_sem);
555 #if defined(CONFIG_PRINTK_TIME)
556 static int printk_time = 1;
557 #else
558 static int printk_time = 0;
559 #endif
560 module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR);
562 static int __init printk_time_setup(char *str)
564 if (*str)
565 return 0;
566 printk_time = 1;
567 printk(KERN_NOTICE "The 'time' option is deprecated and "
568 "is scheduled for removal in early 2008\n");
569 printk(KERN_NOTICE "Use 'printk.time=<value>' instead\n");
570 return 1;
573 __setup("time", printk_time_setup);
575 __attribute__((weak)) unsigned long long printk_clock(void)
577 return sched_clock();
580 /* Check if we have any console registered that can be called early in boot. */
581 static int have_callable_console(void)
583 struct console *con;
585 for (con = console_drivers; con; con = con->next)
586 if (con->flags & CON_ANYTIME)
587 return 1;
589 return 0;
593 * printk - print a kernel message
594 * @fmt: format string
596 * This is printk(). It can be called from any context. We want it to work.
597 * Be aware of the fact that if oops_in_progress is not set, we might try to
598 * wake klogd up which could deadlock on runqueue lock if printk() is called
599 * from scheduler code.
601 * We try to grab the console_sem. If we succeed, it's easy - we log the output and
602 * call the console drivers. If we fail to get the semaphore we place the output
603 * into the log buffer and return. The current holder of the console_sem will
604 * notice the new output in release_console_sem() and will send it to the
605 * consoles before releasing the semaphore.
607 * One effect of this deferred printing is that code which calls printk() and
608 * then changes console_loglevel may break. This is because console_loglevel
609 * is inspected when the actual printing occurs.
611 * See also:
612 * printf(3)
615 asmlinkage int printk(const char *fmt, ...)
617 va_list args;
618 int r;
620 va_start(args, fmt);
621 r = vprintk(fmt, args);
622 va_end(args);
624 return r;
627 /* cpu currently holding logbuf_lock */
628 static volatile unsigned int printk_cpu = UINT_MAX;
630 asmlinkage int vprintk(const char *fmt, va_list args)
632 unsigned long flags;
633 int printed_len;
634 char *p;
635 static char printk_buf[1024];
636 static int log_level_unknown = 1;
638 boot_delay_msec();
640 preempt_disable();
641 if (unlikely(oops_in_progress) && printk_cpu == smp_processor_id())
642 /* If a crash is occurring during printk() on this CPU,
643 * make sure we can't deadlock */
644 zap_locks();
646 /* This stops the holder of console_sem just where we want him */
647 raw_local_irq_save(flags);
648 lockdep_off();
649 spin_lock(&logbuf_lock);
650 printk_cpu = smp_processor_id();
652 /* Emit the output into the temporary buffer */
653 printed_len = vscnprintf(printk_buf, sizeof(printk_buf), fmt, args);
656 * Copy the output into log_buf. If the caller didn't provide
657 * appropriate log level tags, we insert them here
659 for (p = printk_buf; *p; p++) {
660 if (log_level_unknown) {
661 /* log_level_unknown signals the start of a new line */
662 if (printk_time) {
663 int loglev_char;
664 char tbuf[50], *tp;
665 unsigned tlen;
666 unsigned long long t;
667 unsigned long nanosec_rem;
670 * force the log level token to be
671 * before the time output.
673 if (p[0] == '<' && p[1] >='0' &&
674 p[1] <= '7' && p[2] == '>') {
675 loglev_char = p[1];
676 p += 3;
677 printed_len -= 3;
678 } else {
679 loglev_char = default_message_loglevel
680 + '0';
682 t = printk_clock();
683 nanosec_rem = do_div(t, 1000000000);
684 tlen = sprintf(tbuf,
685 "<%c>[%5lu.%06lu] ",
686 loglev_char,
687 (unsigned long)t,
688 nanosec_rem/1000);
690 for (tp = tbuf; tp < tbuf + tlen; tp++)
691 emit_log_char(*tp);
692 printed_len += tlen;
693 } else {
694 if (p[0] != '<' || p[1] < '0' ||
695 p[1] > '7' || p[2] != '>') {
696 emit_log_char('<');
697 emit_log_char(default_message_loglevel
698 + '0');
699 emit_log_char('>');
700 printed_len += 3;
703 log_level_unknown = 0;
704 if (!*p)
705 break;
707 emit_log_char(*p);
708 if (*p == '\n')
709 log_level_unknown = 1;
712 if (!down_trylock(&console_sem)) {
713 if (console_suspended) {
714 up(&console_sem);
715 goto console_was_suspened;
719 * We own the drivers. We can drop the spinlock and
720 * let release_console_sem() print the text, maybe ...
722 console_locked = 1;
723 printk_cpu = UINT_MAX;
724 spin_unlock(&logbuf_lock);
727 * Console drivers may assume that per-cpu resources have
728 * been allocated. So unless they're explicitly marked as
729 * being able to cope (CON_ANYTIME) don't call them until
730 * this CPU is officially up.
732 if (cpu_online(smp_processor_id()) || have_callable_console()) {
733 console_may_schedule = 0;
734 release_console_sem();
735 } else {
736 /* Release by hand to avoid flushing the buffer. */
737 console_locked = 0;
738 up(&console_sem);
740 lockdep_on();
741 raw_local_irq_restore(flags);
742 } else {
743 console_was_suspened:
745 * Someone else owns the drivers. We drop the spinlock, which
746 * allows the semaphore holder to proceed and to call the
747 * console drivers with the output which we just produced.
749 printk_cpu = UINT_MAX;
750 spin_unlock(&logbuf_lock);
751 lockdep_on();
752 raw_local_irq_restore(flags);
755 preempt_enable();
756 return printed_len;
758 EXPORT_SYMBOL(printk);
759 EXPORT_SYMBOL(vprintk);
761 #else
763 asmlinkage long sys_syslog(int type, char __user *buf, int len)
765 return -ENOSYS;
768 static void call_console_drivers(unsigned long start, unsigned long end)
772 #endif
775 * Set up a list of consoles. Called from init/main.c
777 static int __init console_setup(char *str)
779 char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for index */
780 char *s, *options;
781 int idx;
784 * Decode str into name, index, options.
786 if (str[0] >= '0' && str[0] <= '9') {
787 strcpy(buf, "ttyS");
788 strncpy(buf + 4, str, sizeof(buf) - 5);
789 } else {
790 strncpy(buf, str, sizeof(buf) - 1);
792 buf[sizeof(buf) - 1] = 0;
793 if ((options = strchr(str, ',')) != NULL)
794 *(options++) = 0;
795 #ifdef __sparc__
796 if (!strcmp(str, "ttya"))
797 strcpy(buf, "ttyS0");
798 if (!strcmp(str, "ttyb"))
799 strcpy(buf, "ttyS1");
800 #endif
801 for (s = buf; *s; s++)
802 if ((*s >= '0' && *s <= '9') || *s == ',')
803 break;
804 idx = simple_strtoul(s, NULL, 10);
805 *s = 0;
807 add_preferred_console(buf, idx, options);
808 return 1;
810 __setup("console=", console_setup);
813 * add_preferred_console - add a device to the list of preferred consoles.
814 * @name: device name
815 * @idx: device index
816 * @options: options for this console
818 * The last preferred console added will be used for kernel messages
819 * and stdin/out/err for init. Normally this is used by console_setup
820 * above to handle user-supplied console arguments; however it can also
821 * be used by arch-specific code either to override the user or more
822 * commonly to provide a default console (ie from PROM variables) when
823 * the user has not supplied one.
825 int add_preferred_console(char *name, int idx, char *options)
827 struct console_cmdline *c;
828 int i;
831 * See if this tty is not yet registered, and
832 * if we have a slot free.
834 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
835 if (strcmp(console_cmdline[i].name, name) == 0 &&
836 console_cmdline[i].index == idx) {
837 selected_console = i;
838 return 0;
840 if (i == MAX_CMDLINECONSOLES)
841 return -E2BIG;
842 selected_console = i;
843 c = &console_cmdline[i];
844 memcpy(c->name, name, sizeof(c->name));
845 c->name[sizeof(c->name) - 1] = 0;
846 c->options = options;
847 c->index = idx;
848 return 0;
851 int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options)
853 struct console_cmdline *c;
854 int i;
856 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
857 if (strcmp(console_cmdline[i].name, name) == 0 &&
858 console_cmdline[i].index == idx) {
859 c = &console_cmdline[i];
860 memcpy(c->name, name_new, sizeof(c->name));
861 c->name[sizeof(c->name) - 1] = 0;
862 c->options = options;
863 c->index = idx_new;
864 return i;
866 /* not found */
867 return -1;
870 int console_suspend_enabled = 1;
871 EXPORT_SYMBOL(console_suspend_enabled);
873 static int __init console_suspend_disable(char *str)
875 console_suspend_enabled = 0;
876 return 1;
878 __setup("no_console_suspend", console_suspend_disable);
881 * suspend_console - suspend the console subsystem
883 * This disables printk() while we go into suspend states
885 void suspend_console(void)
887 if (!console_suspend_enabled)
888 return;
889 printk("Suspending console(s)\n");
890 acquire_console_sem();
891 console_suspended = 1;
892 up(&console_sem);
895 void resume_console(void)
897 if (!console_suspend_enabled)
898 return;
899 down(&console_sem);
900 console_suspended = 0;
901 release_console_sem();
905 * acquire_console_sem - lock the console system for exclusive use.
907 * Acquires a semaphore which guarantees that the caller has
908 * exclusive access to the console system and the console_drivers list.
910 * Can sleep, returns nothing.
912 void acquire_console_sem(void)
914 BUG_ON(in_interrupt());
915 down(&console_sem);
916 if (console_suspended)
917 return;
918 console_locked = 1;
919 console_may_schedule = 1;
921 EXPORT_SYMBOL(acquire_console_sem);
923 int try_acquire_console_sem(void)
925 if (down_trylock(&console_sem))
926 return -1;
927 if (console_suspended) {
928 up(&console_sem);
929 return -1;
931 console_locked = 1;
932 console_may_schedule = 0;
933 return 0;
935 EXPORT_SYMBOL(try_acquire_console_sem);
937 int is_console_locked(void)
939 return console_locked;
942 void wake_up_klogd(void)
944 if (!oops_in_progress && waitqueue_active(&log_wait))
945 wake_up_interruptible(&log_wait);
949 * release_console_sem - unlock the console system
951 * Releases the semaphore which the caller holds on the console system
952 * and the console driver list.
954 * While the semaphore was held, console output may have been buffered
955 * by printk(). If this is the case, release_console_sem() emits
956 * the output prior to releasing the semaphore.
958 * If there is output waiting for klogd, we wake it up.
960 * release_console_sem() may be called from any context.
962 void release_console_sem(void)
964 unsigned long flags;
965 unsigned long _con_start, _log_end;
966 unsigned long wake_klogd = 0;
968 if (console_suspended) {
969 up(&console_sem);
970 return;
973 console_may_schedule = 0;
975 for ( ; ; ) {
976 spin_lock_irqsave(&logbuf_lock, flags);
977 wake_klogd |= log_start - log_end;
978 if (con_start == log_end)
979 break; /* Nothing to print */
980 _con_start = con_start;
981 _log_end = log_end;
982 con_start = log_end; /* Flush */
983 spin_unlock(&logbuf_lock);
984 call_console_drivers(_con_start, _log_end);
985 local_irq_restore(flags);
987 console_locked = 0;
988 up(&console_sem);
989 spin_unlock_irqrestore(&logbuf_lock, flags);
990 if (wake_klogd)
991 wake_up_klogd();
993 EXPORT_SYMBOL(release_console_sem);
996 * console_conditional_schedule - yield the CPU if required
998 * If the console code is currently allowed to sleep, and
999 * if this CPU should yield the CPU to another task, do
1000 * so here.
1002 * Must be called within acquire_console_sem().
1004 void __sched console_conditional_schedule(void)
1006 if (console_may_schedule)
1007 cond_resched();
1009 EXPORT_SYMBOL(console_conditional_schedule);
1011 void console_print(const char *s)
1013 printk(KERN_EMERG "%s", s);
1015 EXPORT_SYMBOL(console_print);
1017 void console_unblank(void)
1019 struct console *c;
1022 * console_unblank can no longer be called in interrupt context unless
1023 * oops_in_progress is set to 1..
1025 if (oops_in_progress) {
1026 if (down_trylock(&console_sem) != 0)
1027 return;
1028 } else
1029 acquire_console_sem();
1031 console_locked = 1;
1032 console_may_schedule = 0;
1033 for (c = console_drivers; c != NULL; c = c->next)
1034 if ((c->flags & CON_ENABLED) && c->unblank)
1035 c->unblank();
1036 release_console_sem();
1040 * Return the console tty driver structure and its associated index
1042 struct tty_driver *console_device(int *index)
1044 struct console *c;
1045 struct tty_driver *driver = NULL;
1047 acquire_console_sem();
1048 for (c = console_drivers; c != NULL; c = c->next) {
1049 if (!c->device)
1050 continue;
1051 driver = c->device(c, index);
1052 if (driver)
1053 break;
1055 release_console_sem();
1056 return driver;
1060 * Prevent further output on the passed console device so that (for example)
1061 * serial drivers can disable console output before suspending a port, and can
1062 * re-enable output afterwards.
1064 void console_stop(struct console *console)
1066 acquire_console_sem();
1067 console->flags &= ~CON_ENABLED;
1068 release_console_sem();
1070 EXPORT_SYMBOL(console_stop);
1072 void console_start(struct console *console)
1074 acquire_console_sem();
1075 console->flags |= CON_ENABLED;
1076 release_console_sem();
1078 EXPORT_SYMBOL(console_start);
1081 * The console driver calls this routine during kernel initialization
1082 * to register the console printing procedure with printk() and to
1083 * print any messages that were printed by the kernel before the
1084 * console driver was initialized.
1086 void register_console(struct console *console)
1088 int i;
1089 unsigned long flags;
1090 struct console *bootconsole = NULL;
1092 if (console_drivers) {
1093 if (console->flags & CON_BOOT)
1094 return;
1095 if (console_drivers->flags & CON_BOOT)
1096 bootconsole = console_drivers;
1099 if (preferred_console < 0 || bootconsole || !console_drivers)
1100 preferred_console = selected_console;
1102 if (console->early_setup)
1103 console->early_setup();
1106 * See if we want to use this console driver. If we
1107 * didn't select a console we take the first one
1108 * that registers here.
1110 if (preferred_console < 0) {
1111 if (console->index < 0)
1112 console->index = 0;
1113 if (console->setup == NULL ||
1114 console->setup(console, NULL) == 0) {
1115 console->flags |= CON_ENABLED | CON_CONSDEV;
1116 preferred_console = 0;
1121 * See if this console matches one we selected on
1122 * the command line.
1124 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0];
1125 i++) {
1126 if (strcmp(console_cmdline[i].name, console->name) != 0)
1127 continue;
1128 if (console->index >= 0 &&
1129 console->index != console_cmdline[i].index)
1130 continue;
1131 if (console->index < 0)
1132 console->index = console_cmdline[i].index;
1133 if (console->setup &&
1134 console->setup(console, console_cmdline[i].options) != 0)
1135 break;
1136 console->flags |= CON_ENABLED;
1137 console->index = console_cmdline[i].index;
1138 if (i == selected_console) {
1139 console->flags |= CON_CONSDEV;
1140 preferred_console = selected_console;
1142 break;
1145 if (!(console->flags & CON_ENABLED))
1146 return;
1148 if (bootconsole && (console->flags & CON_CONSDEV)) {
1149 printk(KERN_INFO "console handover: boot [%s%d] -> real [%s%d]\n",
1150 bootconsole->name, bootconsole->index,
1151 console->name, console->index);
1152 unregister_console(bootconsole);
1153 console->flags &= ~CON_PRINTBUFFER;
1154 } else {
1155 printk(KERN_INFO "console [%s%d] enabled\n",
1156 console->name, console->index);
1160 * Put this console in the list - keep the
1161 * preferred driver at the head of the list.
1163 acquire_console_sem();
1164 if ((console->flags & CON_CONSDEV) || console_drivers == NULL) {
1165 console->next = console_drivers;
1166 console_drivers = console;
1167 if (console->next)
1168 console->next->flags &= ~CON_CONSDEV;
1169 } else {
1170 console->next = console_drivers->next;
1171 console_drivers->next = console;
1173 if (console->flags & CON_PRINTBUFFER) {
1175 * release_console_sem() will print out the buffered messages
1176 * for us.
1178 spin_lock_irqsave(&logbuf_lock, flags);
1179 con_start = log_start;
1180 spin_unlock_irqrestore(&logbuf_lock, flags);
1182 release_console_sem();
1184 EXPORT_SYMBOL(register_console);
1186 int unregister_console(struct console *console)
1188 struct console *a, *b;
1189 int res = 1;
1191 acquire_console_sem();
1192 if (console_drivers == console) {
1193 console_drivers=console->next;
1194 res = 0;
1195 } else if (console_drivers) {
1196 for (a=console_drivers->next, b=console_drivers ;
1197 a; b=a, a=b->next) {
1198 if (a == console) {
1199 b->next = a->next;
1200 res = 0;
1201 break;
1207 * If this isn't the last console and it has CON_CONSDEV set, we
1208 * need to set it on the next preferred console.
1210 if (console_drivers != NULL && console->flags & CON_CONSDEV)
1211 console_drivers->flags |= CON_CONSDEV;
1213 release_console_sem();
1214 return res;
1216 EXPORT_SYMBOL(unregister_console);
1218 static int __init disable_boot_consoles(void)
1220 if (console_drivers != NULL) {
1221 if (console_drivers->flags & CON_BOOT) {
1222 printk(KERN_INFO "turn off boot console %s%d\n",
1223 console_drivers->name, console_drivers->index);
1224 return unregister_console(console_drivers);
1227 return 0;
1229 late_initcall(disable_boot_consoles);
1232 * tty_write_message - write a message to a certain tty, not just the console.
1233 * @tty: the destination tty_struct
1234 * @msg: the message to write
1236 * This is used for messages that need to be redirected to a specific tty.
1237 * We don't put it into the syslog queue right now maybe in the future if
1238 * really needed.
1240 void tty_write_message(struct tty_struct *tty, char *msg)
1242 if (tty && tty->driver->write)
1243 tty->driver->write(tty, msg, strlen(msg));
1244 return;
1248 * printk rate limiting, lifted from the networking subsystem.
1250 * This enforces a rate limit: not more than one kernel message
1251 * every printk_ratelimit_jiffies to make a denial-of-service
1252 * attack impossible.
1254 int __printk_ratelimit(int ratelimit_jiffies, int ratelimit_burst)
1256 static DEFINE_SPINLOCK(ratelimit_lock);
1257 static unsigned long toks = 10 * 5 * HZ;
1258 static unsigned long last_msg;
1259 static int missed;
1260 unsigned long flags;
1261 unsigned long now = jiffies;
1263 spin_lock_irqsave(&ratelimit_lock, flags);
1264 toks += now - last_msg;
1265 last_msg = now;
1266 if (toks > (ratelimit_burst * ratelimit_jiffies))
1267 toks = ratelimit_burst * ratelimit_jiffies;
1268 if (toks >= ratelimit_jiffies) {
1269 int lost = missed;
1271 missed = 0;
1272 toks -= ratelimit_jiffies;
1273 spin_unlock_irqrestore(&ratelimit_lock, flags);
1274 if (lost)
1275 printk(KERN_WARNING "printk: %d messages suppressed.\n", lost);
1276 return 1;
1278 missed++;
1279 spin_unlock_irqrestore(&ratelimit_lock, flags);
1280 return 0;
1282 EXPORT_SYMBOL(__printk_ratelimit);
1284 /* minimum time in jiffies between messages */
1285 int printk_ratelimit_jiffies = 5 * HZ;
1287 /* number of messages we send before ratelimiting */
1288 int printk_ratelimit_burst = 10;
1290 int printk_ratelimit(void)
1292 return __printk_ratelimit(printk_ratelimit_jiffies,
1293 printk_ratelimit_burst);
1295 EXPORT_SYMBOL(printk_ratelimit);
1298 * printk_timed_ratelimit - caller-controlled printk ratelimiting
1299 * @caller_jiffies: pointer to caller's state
1300 * @interval_msecs: minimum interval between prints
1302 * printk_timed_ratelimit() returns true if more than @interval_msecs
1303 * milliseconds have elapsed since the last time printk_timed_ratelimit()
1304 * returned true.
1306 bool printk_timed_ratelimit(unsigned long *caller_jiffies,
1307 unsigned int interval_msecs)
1309 if (*caller_jiffies == 0 || time_after(jiffies, *caller_jiffies)) {
1310 *caller_jiffies = jiffies + msecs_to_jiffies(interval_msecs);
1311 return true;
1313 return false;
1315 EXPORT_SYMBOL(printk_timed_ratelimit);