nozomi driver update
[linux-2.6/mini2440.git] / kernel / printk.c
blob29ae1e99cde08b247aa106a2939496abf3b3c871
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>
40 * Architectures can override it:
42 void __attribute__((weak)) early_printk(const char *fmt, ...)
46 #define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT)
48 /* printk's without a loglevel use this.. */
49 #define DEFAULT_MESSAGE_LOGLEVEL 4 /* KERN_WARNING */
51 /* We show everything that is MORE important than this.. */
52 #define MINIMUM_CONSOLE_LOGLEVEL 1 /* Minimum loglevel we let people use */
53 #define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */
55 DECLARE_WAIT_QUEUE_HEAD(log_wait);
57 int console_printk[4] = {
58 DEFAULT_CONSOLE_LOGLEVEL, /* console_loglevel */
59 DEFAULT_MESSAGE_LOGLEVEL, /* default_message_loglevel */
60 MINIMUM_CONSOLE_LOGLEVEL, /* minimum_console_loglevel */
61 DEFAULT_CONSOLE_LOGLEVEL, /* default_console_loglevel */
65 * Low level drivers may need that to know if they can schedule in
66 * their unblank() callback or not. So let's export it.
68 int oops_in_progress;
69 EXPORT_SYMBOL(oops_in_progress);
72 * console_sem protects the console_drivers list, and also
73 * provides serialisation for access to the entire console
74 * driver system.
76 static DECLARE_MUTEX(console_sem);
77 static DECLARE_MUTEX(secondary_console_sem);
78 struct console *console_drivers;
80 * This is used for debugging the mess that is the VT code by
81 * keeping track if we have the console semaphore held. It's
82 * definitely not the perfect debug tool (we don't know if _WE_
83 * hold it are racing, but it helps tracking those weird code
84 * path in the console code where we end up in places I want
85 * locked without the console sempahore held
87 static int console_locked, console_suspended;
90 * logbuf_lock protects log_buf, log_start, log_end, con_start and logged_chars
91 * It is also used in interesting ways to provide interlocking in
92 * release_console_sem().
94 static DEFINE_SPINLOCK(logbuf_lock);
96 #define LOG_BUF_MASK (log_buf_len-1)
97 #define LOG_BUF(idx) (log_buf[(idx) & LOG_BUF_MASK])
100 * The indices into log_buf are not constrained to log_buf_len - they
101 * must be masked before subscripting
103 static unsigned long log_start; /* Index into log_buf: next char to be read by syslog() */
104 static unsigned long con_start; /* Index into log_buf: next char to be sent to consoles */
105 static unsigned long log_end; /* Index into log_buf: most-recently-written-char + 1 */
108 * Array of consoles built from command line options (console=)
110 struct console_cmdline
112 char name[8]; /* Name of the driver */
113 int index; /* Minor dev. to use */
114 char *options; /* Options for the driver */
117 #define MAX_CMDLINECONSOLES 8
119 static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
120 static int selected_console = -1;
121 static int preferred_console = -1;
123 /* Flag: console code may call schedule() */
124 static int console_may_schedule;
126 #ifdef CONFIG_PRINTK
128 static char __log_buf[__LOG_BUF_LEN];
129 static char *log_buf = __log_buf;
130 static int log_buf_len = __LOG_BUF_LEN;
131 static unsigned long logged_chars; /* Number of chars produced since last read+clear operation */
133 static int __init log_buf_len_setup(char *str)
135 unsigned long size = memparse(str, &str);
136 unsigned long flags;
138 if (size)
139 size = roundup_pow_of_two(size);
140 if (size > log_buf_len) {
141 unsigned long start, dest_idx, offset;
142 char *new_log_buf;
144 new_log_buf = alloc_bootmem(size);
145 if (!new_log_buf) {
146 printk(KERN_WARNING "log_buf_len: allocation failed\n");
147 goto out;
150 spin_lock_irqsave(&logbuf_lock, flags);
151 log_buf_len = size;
152 log_buf = new_log_buf;
154 offset = start = min(con_start, log_start);
155 dest_idx = 0;
156 while (start != log_end) {
157 log_buf[dest_idx] = __log_buf[start & (__LOG_BUF_LEN - 1)];
158 start++;
159 dest_idx++;
161 log_start -= offset;
162 con_start -= offset;
163 log_end -= offset;
164 spin_unlock_irqrestore(&logbuf_lock, flags);
166 printk(KERN_NOTICE "log_buf_len: %d\n", log_buf_len);
168 out:
169 return 1;
172 __setup("log_buf_len=", log_buf_len_setup);
174 #ifdef CONFIG_BOOT_PRINTK_DELAY
176 static unsigned int boot_delay; /* msecs delay after each printk during bootup */
177 static unsigned long long printk_delay_msec; /* per msec, based on boot_delay */
179 static int __init boot_delay_setup(char *str)
181 unsigned long lpj;
182 unsigned long long loops_per_msec;
184 lpj = preset_lpj ? preset_lpj : 1000000; /* some guess */
185 loops_per_msec = (unsigned long long)lpj / 1000 * HZ;
187 get_option(&str, &boot_delay);
188 if (boot_delay > 10 * 1000)
189 boot_delay = 0;
191 printk_delay_msec = loops_per_msec;
192 printk(KERN_DEBUG "boot_delay: %u, preset_lpj: %ld, lpj: %lu, "
193 "HZ: %d, printk_delay_msec: %llu\n",
194 boot_delay, preset_lpj, lpj, HZ, printk_delay_msec);
195 return 1;
197 __setup("boot_delay=", boot_delay_setup);
199 static void boot_delay_msec(void)
201 unsigned long long k;
202 unsigned long timeout;
204 if (boot_delay == 0 || system_state != SYSTEM_BOOTING)
205 return;
207 k = (unsigned long long)printk_delay_msec * boot_delay;
209 timeout = jiffies + msecs_to_jiffies(boot_delay);
210 while (k) {
211 k--;
212 cpu_relax();
214 * use (volatile) jiffies to prevent
215 * compiler reduction; loop termination via jiffies
216 * is secondary and may or may not happen.
218 if (time_after(jiffies, timeout))
219 break;
220 touch_nmi_watchdog();
223 #else
224 static inline void boot_delay_msec(void)
227 #endif
230 * Return the number of unread characters in the log buffer.
232 int log_buf_get_len(void)
234 return logged_chars;
238 * Copy a range of characters from the log buffer.
240 int log_buf_copy(char *dest, int idx, int len)
242 int ret, max;
243 bool took_lock = false;
245 if (!oops_in_progress) {
246 spin_lock_irq(&logbuf_lock);
247 took_lock = true;
250 max = log_buf_get_len();
251 if (idx < 0 || idx >= max) {
252 ret = -1;
253 } else {
254 if (len > max)
255 len = max;
256 ret = len;
257 idx += (log_end - max);
258 while (len-- > 0)
259 dest[len] = LOG_BUF(idx + len);
262 if (took_lock)
263 spin_unlock_irq(&logbuf_lock);
265 return ret;
269 * Extract a single character from the log buffer.
271 int log_buf_read(int idx)
273 char ret;
275 if (log_buf_copy(&ret, idx, 1) == 1)
276 return ret;
277 else
278 return -1;
282 * Commands to do_syslog:
284 * 0 -- Close the log. Currently a NOP.
285 * 1 -- Open the log. Currently a NOP.
286 * 2 -- Read from the log.
287 * 3 -- Read all messages remaining in the ring buffer.
288 * 4 -- Read and clear all messages remaining in the ring buffer
289 * 5 -- Clear ring buffer.
290 * 6 -- Disable printk's to console
291 * 7 -- Enable printk's to console
292 * 8 -- Set level of messages printed to console
293 * 9 -- Return number of unread characters in the log buffer
294 * 10 -- Return size of the log buffer
296 int do_syslog(int type, char __user *buf, int len)
298 unsigned long i, j, limit, count;
299 int do_clear = 0;
300 char c;
301 int error = 0;
303 error = security_syslog(type);
304 if (error)
305 return error;
307 switch (type) {
308 case 0: /* Close log */
309 break;
310 case 1: /* Open log */
311 break;
312 case 2: /* Read from log */
313 error = -EINVAL;
314 if (!buf || len < 0)
315 goto out;
316 error = 0;
317 if (!len)
318 goto out;
319 if (!access_ok(VERIFY_WRITE, buf, len)) {
320 error = -EFAULT;
321 goto out;
323 error = wait_event_interruptible(log_wait,
324 (log_start - log_end));
325 if (error)
326 goto out;
327 i = 0;
328 spin_lock_irq(&logbuf_lock);
329 while (!error && (log_start != log_end) && i < len) {
330 c = LOG_BUF(log_start);
331 log_start++;
332 spin_unlock_irq(&logbuf_lock);
333 error = __put_user(c,buf);
334 buf++;
335 i++;
336 cond_resched();
337 spin_lock_irq(&logbuf_lock);
339 spin_unlock_irq(&logbuf_lock);
340 if (!error)
341 error = i;
342 break;
343 case 4: /* Read/clear last kernel messages */
344 do_clear = 1;
345 /* FALL THRU */
346 case 3: /* Read last kernel messages */
347 error = -EINVAL;
348 if (!buf || len < 0)
349 goto out;
350 error = 0;
351 if (!len)
352 goto out;
353 if (!access_ok(VERIFY_WRITE, buf, len)) {
354 error = -EFAULT;
355 goto out;
357 count = len;
358 if (count > log_buf_len)
359 count = log_buf_len;
360 spin_lock_irq(&logbuf_lock);
361 if (count > logged_chars)
362 count = logged_chars;
363 if (do_clear)
364 logged_chars = 0;
365 limit = log_end;
367 * __put_user() could sleep, and while we sleep
368 * printk() could overwrite the messages
369 * we try to copy to user space. Therefore
370 * the messages are copied in reverse. <manfreds>
372 for (i = 0; i < count && !error; i++) {
373 j = limit-1-i;
374 if (j + log_buf_len < log_end)
375 break;
376 c = LOG_BUF(j);
377 spin_unlock_irq(&logbuf_lock);
378 error = __put_user(c,&buf[count-1-i]);
379 cond_resched();
380 spin_lock_irq(&logbuf_lock);
382 spin_unlock_irq(&logbuf_lock);
383 if (error)
384 break;
385 error = i;
386 if (i != count) {
387 int offset = count-error;
388 /* buffer overflow during copy, correct user buffer. */
389 for (i = 0; i < error; i++) {
390 if (__get_user(c,&buf[i+offset]) ||
391 __put_user(c,&buf[i])) {
392 error = -EFAULT;
393 break;
395 cond_resched();
398 break;
399 case 5: /* Clear ring buffer */
400 logged_chars = 0;
401 break;
402 case 6: /* Disable logging to console */
403 console_loglevel = minimum_console_loglevel;
404 break;
405 case 7: /* Enable logging to console */
406 console_loglevel = default_console_loglevel;
407 break;
408 case 8: /* Set level of messages printed to console */
409 error = -EINVAL;
410 if (len < 1 || len > 8)
411 goto out;
412 if (len < minimum_console_loglevel)
413 len = minimum_console_loglevel;
414 console_loglevel = len;
415 error = 0;
416 break;
417 case 9: /* Number of chars in the log buffer */
418 error = log_end - log_start;
419 break;
420 case 10: /* Size of the log buffer */
421 error = log_buf_len;
422 break;
423 default:
424 error = -EINVAL;
425 break;
427 out:
428 return error;
431 asmlinkage long sys_syslog(int type, char __user *buf, int len)
433 return do_syslog(type, buf, len);
437 * Call the console drivers on a range of log_buf
439 static void __call_console_drivers(unsigned long start, unsigned long end)
441 struct console *con;
443 for (con = console_drivers; con; con = con->next) {
444 if ((con->flags & CON_ENABLED) && con->write &&
445 (cpu_online(smp_processor_id()) ||
446 (con->flags & CON_ANYTIME)))
447 con->write(con, &LOG_BUF(start), end - start);
451 static int __read_mostly ignore_loglevel;
453 static int __init ignore_loglevel_setup(char *str)
455 ignore_loglevel = 1;
456 printk(KERN_INFO "debug: ignoring loglevel setting.\n");
458 return 0;
461 early_param("ignore_loglevel", ignore_loglevel_setup);
464 * Write out chars from start to end - 1 inclusive
466 static void _call_console_drivers(unsigned long start,
467 unsigned long end, int msg_log_level)
469 if ((msg_log_level < console_loglevel || ignore_loglevel) &&
470 console_drivers && start != end) {
471 if ((start & LOG_BUF_MASK) > (end & LOG_BUF_MASK)) {
472 /* wrapped write */
473 __call_console_drivers(start & LOG_BUF_MASK,
474 log_buf_len);
475 __call_console_drivers(0, end & LOG_BUF_MASK);
476 } else {
477 __call_console_drivers(start, end);
483 * Call the console drivers, asking them to write out
484 * log_buf[start] to log_buf[end - 1].
485 * The console_sem must be held.
487 static void call_console_drivers(unsigned long start, unsigned long end)
489 unsigned long cur_index, start_print;
490 static int msg_level = -1;
492 BUG_ON(((long)(start - end)) > 0);
494 cur_index = start;
495 start_print = start;
496 while (cur_index != end) {
497 if (msg_level < 0 && ((end - cur_index) > 2) &&
498 LOG_BUF(cur_index + 0) == '<' &&
499 LOG_BUF(cur_index + 1) >= '0' &&
500 LOG_BUF(cur_index + 1) <= '7' &&
501 LOG_BUF(cur_index + 2) == '>') {
502 msg_level = LOG_BUF(cur_index + 1) - '0';
503 cur_index += 3;
504 start_print = cur_index;
506 while (cur_index != end) {
507 char c = LOG_BUF(cur_index);
509 cur_index++;
510 if (c == '\n') {
511 if (msg_level < 0) {
513 * printk() has already given us loglevel tags in
514 * the buffer. This code is here in case the
515 * log buffer has wrapped right round and scribbled
516 * on those tags
518 msg_level = default_message_loglevel;
520 _call_console_drivers(start_print, cur_index, msg_level);
521 msg_level = -1;
522 start_print = cur_index;
523 break;
527 _call_console_drivers(start_print, end, msg_level);
530 static void emit_log_char(char c)
532 LOG_BUF(log_end) = c;
533 log_end++;
534 if (log_end - log_start > log_buf_len)
535 log_start = log_end - log_buf_len;
536 if (log_end - con_start > log_buf_len)
537 con_start = log_end - log_buf_len;
538 if (logged_chars < log_buf_len)
539 logged_chars++;
543 * Zap console related locks when oopsing. Only zap at most once
544 * every 10 seconds, to leave time for slow consoles to print a
545 * full oops.
547 static void zap_locks(void)
549 static unsigned long oops_timestamp;
551 if (time_after_eq(jiffies, oops_timestamp) &&
552 !time_after(jiffies, oops_timestamp + 30 * HZ))
553 return;
555 oops_timestamp = jiffies;
557 /* If a crash is occurring, make sure we can't deadlock */
558 spin_lock_init(&logbuf_lock);
559 /* And make sure that we print immediately */
560 init_MUTEX(&console_sem);
563 #if defined(CONFIG_PRINTK_TIME)
564 static int printk_time = 1;
565 #else
566 static int printk_time = 0;
567 #endif
568 module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR);
570 static int __init printk_time_setup(char *str)
572 if (*str)
573 return 0;
574 printk_time = 1;
575 printk(KERN_NOTICE "The 'time' option is deprecated and "
576 "is scheduled for removal in early 2008\n");
577 printk(KERN_NOTICE "Use 'printk.time=<value>' instead\n");
578 return 1;
581 __setup("time", printk_time_setup);
583 /* Check if we have any console registered that can be called early in boot. */
584 static int have_callable_console(void)
586 struct console *con;
588 for (con = console_drivers; con; con = con->next)
589 if (con->flags & CON_ANYTIME)
590 return 1;
592 return 0;
596 * printk - print a kernel message
597 * @fmt: format string
599 * This is printk(). It can be called from any context. We want it to work.
600 * Be aware of the fact that if oops_in_progress is not set, we might try to
601 * wake klogd up which could deadlock on runqueue lock if printk() is called
602 * from scheduler code.
604 * We try to grab the console_sem. If we succeed, it's easy - we log the output and
605 * call the console drivers. If we fail to get the semaphore we place the output
606 * into the log buffer and return. The current holder of the console_sem will
607 * notice the new output in release_console_sem() and will send it to the
608 * consoles before releasing the semaphore.
610 * One effect of this deferred printing is that code which calls printk() and
611 * then changes console_loglevel may break. This is because console_loglevel
612 * is inspected when the actual printing occurs.
614 * See also:
615 * printf(3)
618 asmlinkage int printk(const char *fmt, ...)
620 va_list args;
621 int r;
623 va_start(args, fmt);
624 r = vprintk(fmt, args);
625 va_end(args);
627 return r;
630 /* cpu currently holding logbuf_lock */
631 static volatile unsigned int printk_cpu = UINT_MAX;
633 const char printk_recursion_bug_msg [] =
634 KERN_CRIT "BUG: recent printk recursion!\n";
635 static int printk_recursion_bug;
637 asmlinkage int vprintk(const char *fmt, va_list args)
639 static int log_level_unknown = 1;
640 static char printk_buf[1024];
642 unsigned long flags;
643 int printed_len = 0;
644 int this_cpu;
645 char *p;
647 boot_delay_msec();
649 preempt_disable();
650 /* This stops the holder of console_sem just where we want him */
651 raw_local_irq_save(flags);
652 this_cpu = smp_processor_id();
655 * Ouch, printk recursed into itself!
657 if (unlikely(printk_cpu == this_cpu)) {
659 * If a crash is occurring during printk() on this CPU,
660 * then try to get the crash message out but make sure
661 * we can't deadlock. Otherwise just return to avoid the
662 * recursion and return - but flag the recursion so that
663 * it can be printed at the next appropriate moment:
665 if (!oops_in_progress) {
666 printk_recursion_bug = 1;
667 goto out_restore_irqs;
669 zap_locks();
672 lockdep_off();
673 spin_lock(&logbuf_lock);
674 printk_cpu = this_cpu;
676 if (printk_recursion_bug) {
677 printk_recursion_bug = 0;
678 strcpy(printk_buf, printk_recursion_bug_msg);
679 printed_len = sizeof(printk_recursion_bug_msg);
681 /* Emit the output into the temporary buffer */
682 printed_len += vscnprintf(printk_buf + printed_len,
683 sizeof(printk_buf), fmt, args);
686 * Copy the output into log_buf. If the caller didn't provide
687 * appropriate log level tags, we insert them here
689 for (p = printk_buf; *p; p++) {
690 if (log_level_unknown) {
691 /* log_level_unknown signals the start of a new line */
692 if (printk_time) {
693 int loglev_char;
694 char tbuf[50], *tp;
695 unsigned tlen;
696 unsigned long long t;
697 unsigned long nanosec_rem;
700 * force the log level token to be
701 * before the time output.
703 if (p[0] == '<' && p[1] >='0' &&
704 p[1] <= '7' && p[2] == '>') {
705 loglev_char = p[1];
706 p += 3;
707 printed_len -= 3;
708 } else {
709 loglev_char = default_message_loglevel
710 + '0';
712 t = cpu_clock(printk_cpu);
713 nanosec_rem = do_div(t, 1000000000);
714 tlen = sprintf(tbuf,
715 "<%c>[%5lu.%06lu] ",
716 loglev_char,
717 (unsigned long)t,
718 nanosec_rem/1000);
720 for (tp = tbuf; tp < tbuf + tlen; tp++)
721 emit_log_char(*tp);
722 printed_len += tlen;
723 } else {
724 if (p[0] != '<' || p[1] < '0' ||
725 p[1] > '7' || p[2] != '>') {
726 emit_log_char('<');
727 emit_log_char(default_message_loglevel
728 + '0');
729 emit_log_char('>');
730 printed_len += 3;
733 log_level_unknown = 0;
734 if (!*p)
735 break;
737 emit_log_char(*p);
738 if (*p == '\n')
739 log_level_unknown = 1;
742 if (!down_trylock(&console_sem)) {
744 * We own the drivers. We can drop the spinlock and
745 * let release_console_sem() print the text, maybe ...
747 console_locked = 1;
748 printk_cpu = UINT_MAX;
749 spin_unlock(&logbuf_lock);
752 * Console drivers may assume that per-cpu resources have
753 * been allocated. So unless they're explicitly marked as
754 * being able to cope (CON_ANYTIME) don't call them until
755 * this CPU is officially up.
757 if (cpu_online(smp_processor_id()) || have_callable_console()) {
758 console_may_schedule = 0;
759 release_console_sem();
760 } else {
761 /* Release by hand to avoid flushing the buffer. */
762 console_locked = 0;
763 up(&console_sem);
765 lockdep_on();
766 raw_local_irq_restore(flags);
767 } else {
769 * Someone else owns the drivers. We drop the spinlock, which
770 * allows the semaphore holder to proceed and to call the
771 * console drivers with the output which we just produced.
773 printk_cpu = UINT_MAX;
774 spin_unlock(&logbuf_lock);
775 lockdep_on();
776 out_restore_irqs:
777 raw_local_irq_restore(flags);
780 preempt_enable();
781 return printed_len;
783 EXPORT_SYMBOL(printk);
784 EXPORT_SYMBOL(vprintk);
786 #else
788 asmlinkage long sys_syslog(int type, char __user *buf, int len)
790 return -ENOSYS;
793 static void call_console_drivers(unsigned long start, unsigned long end)
797 #endif
800 * Set up a list of consoles. Called from init/main.c
802 static int __init console_setup(char *str)
804 char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for index */
805 char *s, *options;
806 int idx;
809 * Decode str into name, index, options.
811 if (str[0] >= '0' && str[0] <= '9') {
812 strcpy(buf, "ttyS");
813 strncpy(buf + 4, str, sizeof(buf) - 5);
814 } else {
815 strncpy(buf, str, sizeof(buf) - 1);
817 buf[sizeof(buf) - 1] = 0;
818 if ((options = strchr(str, ',')) != NULL)
819 *(options++) = 0;
820 #ifdef __sparc__
821 if (!strcmp(str, "ttya"))
822 strcpy(buf, "ttyS0");
823 if (!strcmp(str, "ttyb"))
824 strcpy(buf, "ttyS1");
825 #endif
826 for (s = buf; *s; s++)
827 if ((*s >= '0' && *s <= '9') || *s == ',')
828 break;
829 idx = simple_strtoul(s, NULL, 10);
830 *s = 0;
832 add_preferred_console(buf, idx, options);
833 return 1;
835 __setup("console=", console_setup);
838 * add_preferred_console - add a device to the list of preferred consoles.
839 * @name: device name
840 * @idx: device index
841 * @options: options for this console
843 * The last preferred console added will be used for kernel messages
844 * and stdin/out/err for init. Normally this is used by console_setup
845 * above to handle user-supplied console arguments; however it can also
846 * be used by arch-specific code either to override the user or more
847 * commonly to provide a default console (ie from PROM variables) when
848 * the user has not supplied one.
850 int add_preferred_console(char *name, int idx, char *options)
852 struct console_cmdline *c;
853 int i;
856 * See if this tty is not yet registered, and
857 * if we have a slot free.
859 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
860 if (strcmp(console_cmdline[i].name, name) == 0 &&
861 console_cmdline[i].index == idx) {
862 selected_console = i;
863 return 0;
865 if (i == MAX_CMDLINECONSOLES)
866 return -E2BIG;
867 selected_console = i;
868 c = &console_cmdline[i];
869 memcpy(c->name, name, sizeof(c->name));
870 c->name[sizeof(c->name) - 1] = 0;
871 c->options = options;
872 c->index = idx;
873 return 0;
876 int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options)
878 struct console_cmdline *c;
879 int i;
881 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
882 if (strcmp(console_cmdline[i].name, name) == 0 &&
883 console_cmdline[i].index == idx) {
884 c = &console_cmdline[i];
885 memcpy(c->name, name_new, sizeof(c->name));
886 c->name[sizeof(c->name) - 1] = 0;
887 c->options = options;
888 c->index = idx_new;
889 return i;
891 /* not found */
892 return -1;
895 int console_suspend_enabled = 1;
896 EXPORT_SYMBOL(console_suspend_enabled);
898 static int __init console_suspend_disable(char *str)
900 console_suspend_enabled = 0;
901 return 1;
903 __setup("no_console_suspend", console_suspend_disable);
906 * suspend_console - suspend the console subsystem
908 * This disables printk() while we go into suspend states
910 void suspend_console(void)
912 if (!console_suspend_enabled)
913 return;
914 printk("Suspending console(s)\n");
915 acquire_console_sem();
916 console_suspended = 1;
919 void resume_console(void)
921 if (!console_suspend_enabled)
922 return;
923 console_suspended = 0;
924 release_console_sem();
928 * acquire_console_sem - lock the console system for exclusive use.
930 * Acquires a semaphore which guarantees that the caller has
931 * exclusive access to the console system and the console_drivers list.
933 * Can sleep, returns nothing.
935 void acquire_console_sem(void)
937 BUG_ON(in_interrupt());
938 if (console_suspended) {
939 down(&secondary_console_sem);
940 return;
942 down(&console_sem);
943 console_locked = 1;
944 console_may_schedule = 1;
946 EXPORT_SYMBOL(acquire_console_sem);
948 int try_acquire_console_sem(void)
950 if (down_trylock(&console_sem))
951 return -1;
952 console_locked = 1;
953 console_may_schedule = 0;
954 return 0;
956 EXPORT_SYMBOL(try_acquire_console_sem);
958 int is_console_locked(void)
960 return console_locked;
963 void wake_up_klogd(void)
965 if (!oops_in_progress && waitqueue_active(&log_wait))
966 wake_up_interruptible(&log_wait);
970 * release_console_sem - unlock the console system
972 * Releases the semaphore which the caller holds on the console system
973 * and the console driver list.
975 * While the semaphore was held, console output may have been buffered
976 * by printk(). If this is the case, release_console_sem() emits
977 * the output prior to releasing the semaphore.
979 * If there is output waiting for klogd, we wake it up.
981 * release_console_sem() may be called from any context.
983 void release_console_sem(void)
985 unsigned long flags;
986 unsigned long _con_start, _log_end;
987 unsigned long wake_klogd = 0;
989 if (console_suspended) {
990 up(&secondary_console_sem);
991 return;
994 console_may_schedule = 0;
996 for ( ; ; ) {
997 spin_lock_irqsave(&logbuf_lock, flags);
998 wake_klogd |= log_start - log_end;
999 if (con_start == log_end)
1000 break; /* Nothing to print */
1001 _con_start = con_start;
1002 _log_end = log_end;
1003 con_start = log_end; /* Flush */
1004 spin_unlock(&logbuf_lock);
1005 call_console_drivers(_con_start, _log_end);
1006 local_irq_restore(flags);
1008 console_locked = 0;
1009 up(&console_sem);
1010 spin_unlock_irqrestore(&logbuf_lock, flags);
1011 if (wake_klogd)
1012 wake_up_klogd();
1014 EXPORT_SYMBOL(release_console_sem);
1017 * console_conditional_schedule - yield the CPU if required
1019 * If the console code is currently allowed to sleep, and
1020 * if this CPU should yield the CPU to another task, do
1021 * so here.
1023 * Must be called within acquire_console_sem().
1025 void __sched console_conditional_schedule(void)
1027 if (console_may_schedule)
1028 cond_resched();
1030 EXPORT_SYMBOL(console_conditional_schedule);
1032 void console_print(const char *s)
1034 printk(KERN_EMERG "%s", s);
1036 EXPORT_SYMBOL(console_print);
1038 void console_unblank(void)
1040 struct console *c;
1043 * console_unblank can no longer be called in interrupt context unless
1044 * oops_in_progress is set to 1..
1046 if (oops_in_progress) {
1047 if (down_trylock(&console_sem) != 0)
1048 return;
1049 } else
1050 acquire_console_sem();
1052 console_locked = 1;
1053 console_may_schedule = 0;
1054 for (c = console_drivers; c != NULL; c = c->next)
1055 if ((c->flags & CON_ENABLED) && c->unblank)
1056 c->unblank();
1057 release_console_sem();
1061 * Return the console tty driver structure and its associated index
1063 struct tty_driver *console_device(int *index)
1065 struct console *c;
1066 struct tty_driver *driver = NULL;
1068 acquire_console_sem();
1069 for (c = console_drivers; c != NULL; c = c->next) {
1070 if (!c->device)
1071 continue;
1072 driver = c->device(c, index);
1073 if (driver)
1074 break;
1076 release_console_sem();
1077 return driver;
1081 * Prevent further output on the passed console device so that (for example)
1082 * serial drivers can disable console output before suspending a port, and can
1083 * re-enable output afterwards.
1085 void console_stop(struct console *console)
1087 acquire_console_sem();
1088 console->flags &= ~CON_ENABLED;
1089 release_console_sem();
1091 EXPORT_SYMBOL(console_stop);
1093 void console_start(struct console *console)
1095 acquire_console_sem();
1096 console->flags |= CON_ENABLED;
1097 release_console_sem();
1099 EXPORT_SYMBOL(console_start);
1102 * The console driver calls this routine during kernel initialization
1103 * to register the console printing procedure with printk() and to
1104 * print any messages that were printed by the kernel before the
1105 * console driver was initialized.
1107 void register_console(struct console *console)
1109 int i;
1110 unsigned long flags;
1111 struct console *bootconsole = NULL;
1113 if (console_drivers) {
1114 if (console->flags & CON_BOOT)
1115 return;
1116 if (console_drivers->flags & CON_BOOT)
1117 bootconsole = console_drivers;
1120 if (preferred_console < 0 || bootconsole || !console_drivers)
1121 preferred_console = selected_console;
1123 if (console->early_setup)
1124 console->early_setup();
1127 * See if we want to use this console driver. If we
1128 * didn't select a console we take the first one
1129 * that registers here.
1131 if (preferred_console < 0) {
1132 if (console->index < 0)
1133 console->index = 0;
1134 if (console->setup == NULL ||
1135 console->setup(console, NULL) == 0) {
1136 console->flags |= CON_ENABLED | CON_CONSDEV;
1137 preferred_console = 0;
1142 * See if this console matches one we selected on
1143 * the command line.
1145 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0];
1146 i++) {
1147 if (strcmp(console_cmdline[i].name, console->name) != 0)
1148 continue;
1149 if (console->index >= 0 &&
1150 console->index != console_cmdline[i].index)
1151 continue;
1152 if (console->index < 0)
1153 console->index = console_cmdline[i].index;
1154 if (console->setup &&
1155 console->setup(console, console_cmdline[i].options) != 0)
1156 break;
1157 console->flags |= CON_ENABLED;
1158 console->index = console_cmdline[i].index;
1159 if (i == selected_console) {
1160 console->flags |= CON_CONSDEV;
1161 preferred_console = selected_console;
1163 break;
1166 if (!(console->flags & CON_ENABLED))
1167 return;
1169 if (bootconsole && (console->flags & CON_CONSDEV)) {
1170 printk(KERN_INFO "console handover: boot [%s%d] -> real [%s%d]\n",
1171 bootconsole->name, bootconsole->index,
1172 console->name, console->index);
1173 unregister_console(bootconsole);
1174 console->flags &= ~CON_PRINTBUFFER;
1175 } else {
1176 printk(KERN_INFO "console [%s%d] enabled\n",
1177 console->name, console->index);
1181 * Put this console in the list - keep the
1182 * preferred driver at the head of the list.
1184 acquire_console_sem();
1185 if ((console->flags & CON_CONSDEV) || console_drivers == NULL) {
1186 console->next = console_drivers;
1187 console_drivers = console;
1188 if (console->next)
1189 console->next->flags &= ~CON_CONSDEV;
1190 } else {
1191 console->next = console_drivers->next;
1192 console_drivers->next = console;
1194 if (console->flags & CON_PRINTBUFFER) {
1196 * release_console_sem() will print out the buffered messages
1197 * for us.
1199 spin_lock_irqsave(&logbuf_lock, flags);
1200 con_start = log_start;
1201 spin_unlock_irqrestore(&logbuf_lock, flags);
1203 release_console_sem();
1205 EXPORT_SYMBOL(register_console);
1207 int unregister_console(struct console *console)
1209 struct console *a, *b;
1210 int res = 1;
1212 acquire_console_sem();
1213 if (console_drivers == console) {
1214 console_drivers=console->next;
1215 res = 0;
1216 } else if (console_drivers) {
1217 for (a=console_drivers->next, b=console_drivers ;
1218 a; b=a, a=b->next) {
1219 if (a == console) {
1220 b->next = a->next;
1221 res = 0;
1222 break;
1228 * If this isn't the last console and it has CON_CONSDEV set, we
1229 * need to set it on the next preferred console.
1231 if (console_drivers != NULL && console->flags & CON_CONSDEV)
1232 console_drivers->flags |= CON_CONSDEV;
1234 release_console_sem();
1235 return res;
1237 EXPORT_SYMBOL(unregister_console);
1239 static int __init disable_boot_consoles(void)
1241 if (console_drivers != NULL) {
1242 if (console_drivers->flags & CON_BOOT) {
1243 printk(KERN_INFO "turn off boot console %s%d\n",
1244 console_drivers->name, console_drivers->index);
1245 return unregister_console(console_drivers);
1248 return 0;
1250 late_initcall(disable_boot_consoles);
1253 * tty_write_message - write a message to a certain tty, not just the console.
1254 * @tty: the destination tty_struct
1255 * @msg: the message to write
1257 * This is used for messages that need to be redirected to a specific tty.
1258 * We don't put it into the syslog queue right now maybe in the future if
1259 * really needed.
1261 void tty_write_message(struct tty_struct *tty, char *msg)
1263 if (tty && tty->driver->write)
1264 tty->driver->write(tty, msg, strlen(msg));
1265 return;
1269 * printk rate limiting, lifted from the networking subsystem.
1271 * This enforces a rate limit: not more than one kernel message
1272 * every printk_ratelimit_jiffies to make a denial-of-service
1273 * attack impossible.
1275 int __printk_ratelimit(int ratelimit_jiffies, int ratelimit_burst)
1277 static DEFINE_SPINLOCK(ratelimit_lock);
1278 static unsigned long toks = 10 * 5 * HZ;
1279 static unsigned long last_msg;
1280 static int missed;
1281 unsigned long flags;
1282 unsigned long now = jiffies;
1284 spin_lock_irqsave(&ratelimit_lock, flags);
1285 toks += now - last_msg;
1286 last_msg = now;
1287 if (toks > (ratelimit_burst * ratelimit_jiffies))
1288 toks = ratelimit_burst * ratelimit_jiffies;
1289 if (toks >= ratelimit_jiffies) {
1290 int lost = missed;
1292 missed = 0;
1293 toks -= ratelimit_jiffies;
1294 spin_unlock_irqrestore(&ratelimit_lock, flags);
1295 if (lost)
1296 printk(KERN_WARNING "printk: %d messages suppressed.\n", lost);
1297 return 1;
1299 missed++;
1300 spin_unlock_irqrestore(&ratelimit_lock, flags);
1301 return 0;
1303 EXPORT_SYMBOL(__printk_ratelimit);
1305 /* minimum time in jiffies between messages */
1306 int printk_ratelimit_jiffies = 5 * HZ;
1308 /* number of messages we send before ratelimiting */
1309 int printk_ratelimit_burst = 10;
1311 int printk_ratelimit(void)
1313 return __printk_ratelimit(printk_ratelimit_jiffies,
1314 printk_ratelimit_burst);
1316 EXPORT_SYMBOL(printk_ratelimit);
1319 * printk_timed_ratelimit - caller-controlled printk ratelimiting
1320 * @caller_jiffies: pointer to caller's state
1321 * @interval_msecs: minimum interval between prints
1323 * printk_timed_ratelimit() returns true if more than @interval_msecs
1324 * milliseconds have elapsed since the last time printk_timed_ratelimit()
1325 * returned true.
1327 bool printk_timed_ratelimit(unsigned long *caller_jiffies,
1328 unsigned int interval_msecs)
1330 if (*caller_jiffies == 0 || time_after(jiffies, *caller_jiffies)) {
1331 *caller_jiffies = jiffies + msecs_to_jiffies(interval_msecs);
1332 return true;
1334 return false;
1336 EXPORT_SYMBOL(printk_timed_ratelimit);