PRCM: OMAP3: Fix to wrongly modified omap2_clk_wait_ready
[linux-ginger.git] / kernel / printk.c
blob03fe2b41e29be450fe16d22cc8fc533bf012bb59
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>
36 #include <asm/uaccess.h>
39 * Architectures can override it:
41 void __attribute__((weak)) early_printk(const char *fmt, ...)
45 #define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT)
47 #ifdef CONFIG_DEBUG_LL
48 extern void printascii(char *);
49 #endif
51 /* printk's without a loglevel use this.. */
52 #define DEFAULT_MESSAGE_LOGLEVEL 4 /* KERN_WARNING */
54 /* We show everything that is MORE important than this.. */
55 #define MINIMUM_CONSOLE_LOGLEVEL 1 /* Minimum loglevel we let people use */
56 #define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */
58 DECLARE_WAIT_QUEUE_HEAD(log_wait);
60 int console_printk[4] = {
61 DEFAULT_CONSOLE_LOGLEVEL, /* console_loglevel */
62 DEFAULT_MESSAGE_LOGLEVEL, /* default_message_loglevel */
63 MINIMUM_CONSOLE_LOGLEVEL, /* minimum_console_loglevel */
64 DEFAULT_CONSOLE_LOGLEVEL, /* default_console_loglevel */
68 * Low level drivers may need that to know if they can schedule in
69 * their unblank() callback or not. So let's export it.
71 int oops_in_progress;
72 EXPORT_SYMBOL(oops_in_progress);
75 * console_sem protects the console_drivers list, and also
76 * provides serialisation for access to the entire console
77 * driver system.
79 static DECLARE_MUTEX(console_sem);
80 static DECLARE_MUTEX(secondary_console_sem);
81 struct console *console_drivers;
83 * This is used for debugging the mess that is the VT code by
84 * keeping track if we have the console semaphore held. It's
85 * definitely not the perfect debug tool (we don't know if _WE_
86 * hold it are racing, but it helps tracking those weird code
87 * path in the console code where we end up in places I want
88 * locked without the console sempahore held
90 static int console_locked, console_suspended;
93 * logbuf_lock protects log_buf, log_start, log_end, con_start and logged_chars
94 * It is also used in interesting ways to provide interlocking in
95 * release_console_sem().
97 static DEFINE_SPINLOCK(logbuf_lock);
99 #define LOG_BUF_MASK (log_buf_len-1)
100 #define LOG_BUF(idx) (log_buf[(idx) & LOG_BUF_MASK])
103 * The indices into log_buf are not constrained to log_buf_len - they
104 * must be masked before subscripting
106 static unsigned log_start; /* Index into log_buf: next char to be read by syslog() */
107 static unsigned con_start; /* Index into log_buf: next char to be sent to consoles */
108 static unsigned log_end; /* Index into log_buf: most-recently-written-char + 1 */
111 * Array of consoles built from command line options (console=)
113 struct console_cmdline
115 char name[8]; /* Name of the driver */
116 int index; /* Minor dev. to use */
117 char *options; /* Options for the driver */
118 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
119 char *brl_options; /* Options for braille driver */
120 #endif
123 #define MAX_CMDLINECONSOLES 8
125 static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
126 static int selected_console = -1;
127 static int preferred_console = -1;
129 /* Flag: console code may call schedule() */
130 static int console_may_schedule;
132 #ifdef CONFIG_PRINTK
134 static char __log_buf[__LOG_BUF_LEN];
135 static char *log_buf = __log_buf;
136 static int log_buf_len = __LOG_BUF_LEN;
137 static unsigned logged_chars; /* Number of chars produced since last read+clear operation */
139 static int __init log_buf_len_setup(char *str)
141 unsigned size = memparse(str, &str);
142 unsigned long flags;
144 if (size)
145 size = roundup_pow_of_two(size);
146 if (size > log_buf_len) {
147 unsigned start, dest_idx, offset;
148 char *new_log_buf;
150 new_log_buf = alloc_bootmem(size);
151 if (!new_log_buf) {
152 printk(KERN_WARNING "log_buf_len: allocation failed\n");
153 goto out;
156 spin_lock_irqsave(&logbuf_lock, flags);
157 log_buf_len = size;
158 log_buf = new_log_buf;
160 offset = start = min(con_start, log_start);
161 dest_idx = 0;
162 while (start != log_end) {
163 log_buf[dest_idx] = __log_buf[start & (__LOG_BUF_LEN - 1)];
164 start++;
165 dest_idx++;
167 log_start -= offset;
168 con_start -= offset;
169 log_end -= offset;
170 spin_unlock_irqrestore(&logbuf_lock, flags);
172 printk(KERN_NOTICE "log_buf_len: %d\n", log_buf_len);
174 out:
175 return 1;
178 __setup("log_buf_len=", log_buf_len_setup);
180 #ifdef CONFIG_BOOT_PRINTK_DELAY
182 static unsigned int boot_delay; /* msecs delay after each printk during bootup */
183 static unsigned long long printk_delay_msec; /* per msec, based on boot_delay */
185 static int __init boot_delay_setup(char *str)
187 unsigned long lpj;
188 unsigned long long loops_per_msec;
190 lpj = preset_lpj ? preset_lpj : 1000000; /* some guess */
191 loops_per_msec = (unsigned long long)lpj / 1000 * HZ;
193 get_option(&str, &boot_delay);
194 if (boot_delay > 10 * 1000)
195 boot_delay = 0;
197 printk_delay_msec = loops_per_msec;
198 printk(KERN_DEBUG "boot_delay: %u, preset_lpj: %ld, lpj: %lu, "
199 "HZ: %d, printk_delay_msec: %llu\n",
200 boot_delay, preset_lpj, lpj, HZ, printk_delay_msec);
201 return 1;
203 __setup("boot_delay=", boot_delay_setup);
205 static void boot_delay_msec(void)
207 unsigned long long k;
208 unsigned long timeout;
210 if (boot_delay == 0 || system_state != SYSTEM_BOOTING)
211 return;
213 k = (unsigned long long)printk_delay_msec * boot_delay;
215 timeout = jiffies + msecs_to_jiffies(boot_delay);
216 while (k) {
217 k--;
218 cpu_relax();
220 * use (volatile) jiffies to prevent
221 * compiler reduction; loop termination via jiffies
222 * is secondary and may or may not happen.
224 if (time_after(jiffies, timeout))
225 break;
226 touch_nmi_watchdog();
229 #else
230 static inline void boot_delay_msec(void)
233 #endif
236 * Return the number of unread characters in the log buffer.
238 int log_buf_get_len(void)
240 return logged_chars;
244 * Copy a range of characters from the log buffer.
246 int log_buf_copy(char *dest, int idx, int len)
248 int ret, max;
249 bool took_lock = false;
251 if (!oops_in_progress) {
252 spin_lock_irq(&logbuf_lock);
253 took_lock = true;
256 max = log_buf_get_len();
257 if (idx < 0 || idx >= max) {
258 ret = -1;
259 } else {
260 if (len > max)
261 len = max;
262 ret = len;
263 idx += (log_end - max);
264 while (len-- > 0)
265 dest[len] = LOG_BUF(idx + len);
268 if (took_lock)
269 spin_unlock_irq(&logbuf_lock);
271 return ret;
275 * Extract a single character from the log buffer.
277 int log_buf_read(int idx)
279 char ret;
281 if (log_buf_copy(&ret, idx, 1) == 1)
282 return ret;
283 else
284 return -1;
288 * Commands to do_syslog:
290 * 0 -- Close the log. Currently a NOP.
291 * 1 -- Open the log. Currently a NOP.
292 * 2 -- Read from the log.
293 * 3 -- Read all messages remaining in the ring buffer.
294 * 4 -- Read and clear all messages remaining in the ring buffer
295 * 5 -- Clear ring buffer.
296 * 6 -- Disable printk's to console
297 * 7 -- Enable printk's to console
298 * 8 -- Set level of messages printed to console
299 * 9 -- Return number of unread characters in the log buffer
300 * 10 -- Return size of the log buffer
302 int do_syslog(int type, char __user *buf, int len)
304 unsigned i, j, limit, count;
305 int do_clear = 0;
306 char c;
307 int error = 0;
309 error = security_syslog(type);
310 if (error)
311 return error;
313 switch (type) {
314 case 0: /* Close log */
315 break;
316 case 1: /* Open log */
317 break;
318 case 2: /* Read from log */
319 error = -EINVAL;
320 if (!buf || len < 0)
321 goto out;
322 error = 0;
323 if (!len)
324 goto out;
325 if (!access_ok(VERIFY_WRITE, buf, len)) {
326 error = -EFAULT;
327 goto out;
329 error = wait_event_interruptible(log_wait,
330 (log_start - log_end));
331 if (error)
332 goto out;
333 i = 0;
334 spin_lock_irq(&logbuf_lock);
335 while (!error && (log_start != log_end) && i < len) {
336 c = LOG_BUF(log_start);
337 log_start++;
338 spin_unlock_irq(&logbuf_lock);
339 error = __put_user(c,buf);
340 buf++;
341 i++;
342 cond_resched();
343 spin_lock_irq(&logbuf_lock);
345 spin_unlock_irq(&logbuf_lock);
346 if (!error)
347 error = i;
348 break;
349 case 4: /* Read/clear last kernel messages */
350 do_clear = 1;
351 /* FALL THRU */
352 case 3: /* Read last kernel messages */
353 error = -EINVAL;
354 if (!buf || len < 0)
355 goto out;
356 error = 0;
357 if (!len)
358 goto out;
359 if (!access_ok(VERIFY_WRITE, buf, len)) {
360 error = -EFAULT;
361 goto out;
363 count = len;
364 if (count > log_buf_len)
365 count = log_buf_len;
366 spin_lock_irq(&logbuf_lock);
367 if (count > logged_chars)
368 count = logged_chars;
369 if (do_clear)
370 logged_chars = 0;
371 limit = log_end;
373 * __put_user() could sleep, and while we sleep
374 * printk() could overwrite the messages
375 * we try to copy to user space. Therefore
376 * the messages are copied in reverse. <manfreds>
378 for (i = 0; i < count && !error; i++) {
379 j = limit-1-i;
380 if (j + log_buf_len < log_end)
381 break;
382 c = LOG_BUF(j);
383 spin_unlock_irq(&logbuf_lock);
384 error = __put_user(c,&buf[count-1-i]);
385 cond_resched();
386 spin_lock_irq(&logbuf_lock);
388 spin_unlock_irq(&logbuf_lock);
389 if (error)
390 break;
391 error = i;
392 if (i != count) {
393 int offset = count-error;
394 /* buffer overflow during copy, correct user buffer. */
395 for (i = 0; i < error; i++) {
396 if (__get_user(c,&buf[i+offset]) ||
397 __put_user(c,&buf[i])) {
398 error = -EFAULT;
399 break;
401 cond_resched();
404 break;
405 case 5: /* Clear ring buffer */
406 logged_chars = 0;
407 break;
408 case 6: /* Disable logging to console */
409 console_loglevel = minimum_console_loglevel;
410 break;
411 case 7: /* Enable logging to console */
412 console_loglevel = default_console_loglevel;
413 break;
414 case 8: /* Set level of messages printed to console */
415 error = -EINVAL;
416 if (len < 1 || len > 8)
417 goto out;
418 if (len < minimum_console_loglevel)
419 len = minimum_console_loglevel;
420 console_loglevel = len;
421 error = 0;
422 break;
423 case 9: /* Number of chars in the log buffer */
424 error = log_end - log_start;
425 break;
426 case 10: /* Size of the log buffer */
427 error = log_buf_len;
428 break;
429 default:
430 error = -EINVAL;
431 break;
433 out:
434 return error;
437 asmlinkage long sys_syslog(int type, char __user *buf, int len)
439 return do_syslog(type, buf, len);
443 * Call the console drivers on a range of log_buf
445 static void __call_console_drivers(unsigned start, unsigned end)
447 struct console *con;
449 for (con = console_drivers; con; con = con->next) {
450 if ((con->flags & CON_ENABLED) && con->write &&
451 (cpu_online(smp_processor_id()) ||
452 (con->flags & CON_ANYTIME)))
453 con->write(con, &LOG_BUF(start), end - start);
457 static int __read_mostly ignore_loglevel;
459 static int __init ignore_loglevel_setup(char *str)
461 ignore_loglevel = 1;
462 printk(KERN_INFO "debug: ignoring loglevel setting.\n");
464 return 0;
467 early_param("ignore_loglevel", ignore_loglevel_setup);
470 * Write out chars from start to end - 1 inclusive
472 static void _call_console_drivers(unsigned start,
473 unsigned end, int msg_log_level)
475 if ((msg_log_level < console_loglevel || ignore_loglevel) &&
476 console_drivers && start != end) {
477 if ((start & LOG_BUF_MASK) > (end & LOG_BUF_MASK)) {
478 /* wrapped write */
479 __call_console_drivers(start & LOG_BUF_MASK,
480 log_buf_len);
481 __call_console_drivers(0, end & LOG_BUF_MASK);
482 } else {
483 __call_console_drivers(start, end);
489 * Call the console drivers, asking them to write out
490 * log_buf[start] to log_buf[end - 1].
491 * The console_sem must be held.
493 static void call_console_drivers(unsigned start, unsigned end)
495 unsigned cur_index, start_print;
496 static int msg_level = -1;
498 BUG_ON(((int)(start - end)) > 0);
500 cur_index = start;
501 start_print = start;
502 while (cur_index != end) {
503 if (msg_level < 0 && ((end - cur_index) > 2) &&
504 LOG_BUF(cur_index + 0) == '<' &&
505 LOG_BUF(cur_index + 1) >= '0' &&
506 LOG_BUF(cur_index + 1) <= '7' &&
507 LOG_BUF(cur_index + 2) == '>') {
508 msg_level = LOG_BUF(cur_index + 1) - '0';
509 cur_index += 3;
510 start_print = cur_index;
512 while (cur_index != end) {
513 char c = LOG_BUF(cur_index);
515 cur_index++;
516 if (c == '\n') {
517 if (msg_level < 0) {
519 * printk() has already given us loglevel tags in
520 * the buffer. This code is here in case the
521 * log buffer has wrapped right round and scribbled
522 * on those tags
524 msg_level = default_message_loglevel;
526 _call_console_drivers(start_print, cur_index, msg_level);
527 msg_level = -1;
528 start_print = cur_index;
529 break;
533 _call_console_drivers(start_print, end, msg_level);
536 static void emit_log_char(char c)
538 LOG_BUF(log_end) = c;
539 log_end++;
540 if (log_end - log_start > log_buf_len)
541 log_start = log_end - log_buf_len;
542 if (log_end - con_start > log_buf_len)
543 con_start = log_end - log_buf_len;
544 if (logged_chars < log_buf_len)
545 logged_chars++;
549 * Zap console related locks when oopsing. Only zap at most once
550 * every 10 seconds, to leave time for slow consoles to print a
551 * full oops.
553 static void zap_locks(void)
555 static unsigned long oops_timestamp;
557 if (time_after_eq(jiffies, oops_timestamp) &&
558 !time_after(jiffies, oops_timestamp + 30 * HZ))
559 return;
561 oops_timestamp = jiffies;
563 /* If a crash is occurring, make sure we can't deadlock */
564 spin_lock_init(&logbuf_lock);
565 /* And make sure that we print immediately */
566 init_MUTEX(&console_sem);
569 #if defined(CONFIG_PRINTK_TIME)
570 static int printk_time = 1;
571 #else
572 static int printk_time = 0;
573 #endif
574 module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR);
576 /* Check if we have any console registered that can be called early in boot. */
577 static int have_callable_console(void)
579 struct console *con;
581 for (con = console_drivers; con; con = con->next)
582 if (con->flags & CON_ANYTIME)
583 return 1;
585 return 0;
589 * printk - print a kernel message
590 * @fmt: format string
592 * This is printk(). It can be called from any context. We want it to work.
593 * Be aware of the fact that if oops_in_progress is not set, we might try to
594 * wake klogd up which could deadlock on runqueue lock if printk() is called
595 * from scheduler code.
597 * We try to grab the console_sem. If we succeed, it's easy - we log the output and
598 * call the console drivers. If we fail to get the semaphore we place the output
599 * into the log buffer and return. The current holder of the console_sem will
600 * notice the new output in release_console_sem() and will send it to the
601 * consoles before releasing the semaphore.
603 * One effect of this deferred printing is that code which calls printk() and
604 * then changes console_loglevel may break. This is because console_loglevel
605 * is inspected when the actual printing occurs.
607 * See also:
608 * printf(3)
611 asmlinkage int printk(const char *fmt, ...)
613 va_list args;
614 int r;
616 va_start(args, fmt);
617 r = vprintk(fmt, args);
618 va_end(args);
620 return r;
623 /* cpu currently holding logbuf_lock */
624 static volatile unsigned int printk_cpu = UINT_MAX;
627 * Can we actually use the console at this time on this cpu?
629 * Console drivers may assume that per-cpu resources have
630 * been allocated. So unless they're explicitly marked as
631 * being able to cope (CON_ANYTIME) don't call them until
632 * this CPU is officially up.
634 static inline int can_use_console(unsigned int cpu)
636 return cpu_online(cpu) || have_callable_console();
640 * Try to get console ownership to actually show the kernel
641 * messages from a 'printk'. Return true (and with the
642 * console_semaphore held, and 'console_locked' set) if it
643 * is successful, false otherwise.
645 * This gets called with the 'logbuf_lock' spinlock held and
646 * interrupts disabled. It should return with 'lockbuf_lock'
647 * released but interrupts still disabled.
649 static int acquire_console_semaphore_for_printk(unsigned int cpu)
651 int retval = 0;
653 if (!try_acquire_console_sem()) {
654 retval = 1;
657 * If we can't use the console, we need to release
658 * the console semaphore by hand to avoid flushing
659 * the buffer. We need to hold the console semaphore
660 * in order to do this test safely.
662 if (!can_use_console(cpu)) {
663 console_locked = 0;
664 up(&console_sem);
665 retval = 0;
668 printk_cpu = UINT_MAX;
669 spin_unlock(&logbuf_lock);
670 return retval;
673 const char printk_recursion_bug_msg [] =
674 KERN_CRIT "BUG: recent printk recursion!\n";
675 static int printk_recursion_bug;
677 asmlinkage int vprintk(const char *fmt, va_list args)
679 static int log_level_unknown = 1;
680 static char printk_buf[1024];
682 unsigned long flags;
683 int printed_len = 0;
684 int this_cpu;
685 char *p;
687 boot_delay_msec();
689 preempt_disable();
690 /* This stops the holder of console_sem just where we want him */
691 raw_local_irq_save(flags);
692 this_cpu = smp_processor_id();
695 * Ouch, printk recursed into itself!
697 if (unlikely(printk_cpu == this_cpu)) {
699 * If a crash is occurring during printk() on this CPU,
700 * then try to get the crash message out but make sure
701 * we can't deadlock. Otherwise just return to avoid the
702 * recursion and return - but flag the recursion so that
703 * it can be printed at the next appropriate moment:
705 if (!oops_in_progress) {
706 printk_recursion_bug = 1;
707 goto out_restore_irqs;
709 zap_locks();
712 lockdep_off();
713 spin_lock(&logbuf_lock);
714 printk_cpu = this_cpu;
716 if (printk_recursion_bug) {
717 printk_recursion_bug = 0;
718 strcpy(printk_buf, printk_recursion_bug_msg);
719 printed_len = sizeof(printk_recursion_bug_msg);
721 /* Emit the output into the temporary buffer */
722 printed_len += vscnprintf(printk_buf + printed_len,
723 sizeof(printk_buf) - printed_len, fmt, args);
725 #ifdef CONFIG_DEBUG_LL
726 printascii(printk_buf);
727 #endif
730 * Copy the output into log_buf. If the caller didn't provide
731 * appropriate log level tags, we insert them here
733 for (p = printk_buf; *p; p++) {
734 if (log_level_unknown) {
735 /* log_level_unknown signals the start of a new line */
736 if (printk_time) {
737 int loglev_char;
738 char tbuf[50], *tp;
739 unsigned tlen;
740 unsigned long long t;
741 unsigned long nanosec_rem;
744 * force the log level token to be
745 * before the time output.
747 if (p[0] == '<' && p[1] >='0' &&
748 p[1] <= '7' && p[2] == '>') {
749 loglev_char = p[1];
750 p += 3;
751 printed_len -= 3;
752 } else {
753 loglev_char = default_message_loglevel
754 + '0';
756 t = cpu_clock(printk_cpu);
757 nanosec_rem = do_div(t, 1000000000);
758 tlen = sprintf(tbuf,
759 "<%c>[%5lu.%06lu] ",
760 loglev_char,
761 (unsigned long)t,
762 nanosec_rem/1000);
764 for (tp = tbuf; tp < tbuf + tlen; tp++)
765 emit_log_char(*tp);
766 printed_len += tlen;
767 } else {
768 if (p[0] != '<' || p[1] < '0' ||
769 p[1] > '7' || p[2] != '>') {
770 emit_log_char('<');
771 emit_log_char(default_message_loglevel
772 + '0');
773 emit_log_char('>');
774 printed_len += 3;
777 log_level_unknown = 0;
778 if (!*p)
779 break;
781 emit_log_char(*p);
782 if (*p == '\n')
783 log_level_unknown = 1;
787 * Try to acquire and then immediately release the
788 * console semaphore. The release will do all the
789 * actual magic (print out buffers, wake up klogd,
790 * etc).
792 * The acquire_console_semaphore_for_printk() function
793 * will release 'logbuf_lock' regardless of whether it
794 * actually gets the semaphore or not.
796 if (acquire_console_semaphore_for_printk(this_cpu))
797 release_console_sem();
799 lockdep_on();
800 out_restore_irqs:
801 raw_local_irq_restore(flags);
803 preempt_enable();
804 return printed_len;
806 EXPORT_SYMBOL(printk);
807 EXPORT_SYMBOL(vprintk);
809 #else
811 asmlinkage long sys_syslog(int type, char __user *buf, int len)
813 return -ENOSYS;
816 static void call_console_drivers(unsigned start, unsigned end)
820 #endif
822 static int __add_preferred_console(char *name, int idx, char *options,
823 char *brl_options)
825 struct console_cmdline *c;
826 int i;
829 * See if this tty is not yet registered, and
830 * if we have a slot free.
832 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
833 if (strcmp(console_cmdline[i].name, name) == 0 &&
834 console_cmdline[i].index == idx) {
835 if (!brl_options)
836 selected_console = i;
837 return 0;
839 if (i == MAX_CMDLINECONSOLES)
840 return -E2BIG;
841 if (!brl_options)
842 selected_console = i;
843 c = &console_cmdline[i];
844 strlcpy(c->name, name, sizeof(c->name));
845 c->options = options;
846 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
847 c->brl_options = brl_options;
848 #endif
849 c->index = idx;
850 return 0;
853 * Set up a list of consoles. Called from init/main.c
855 static int __init console_setup(char *str)
857 char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for index */
858 char *s, *options, *brl_options = NULL;
859 int idx;
861 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
862 if (!memcmp(str, "brl,", 4)) {
863 brl_options = "";
864 str += 4;
865 } else if (!memcmp(str, "brl=", 4)) {
866 brl_options = str + 4;
867 str = strchr(brl_options, ',');
868 if (!str) {
869 printk(KERN_ERR "need port name after brl=\n");
870 return 1;
872 *(str++) = 0;
874 #endif
877 * Decode str into name, index, options.
879 if (str[0] >= '0' && str[0] <= '9') {
880 strcpy(buf, "ttyS");
881 strncpy(buf + 4, str, sizeof(buf) - 5);
882 } else {
883 strncpy(buf, str, sizeof(buf) - 1);
885 buf[sizeof(buf) - 1] = 0;
886 if ((options = strchr(str, ',')) != NULL)
887 *(options++) = 0;
888 #ifdef __sparc__
889 if (!strcmp(str, "ttya"))
890 strcpy(buf, "ttyS0");
891 if (!strcmp(str, "ttyb"))
892 strcpy(buf, "ttyS1");
893 #endif
894 for (s = buf; *s; s++)
895 if ((*s >= '0' && *s <= '9') || *s == ',')
896 break;
897 idx = simple_strtoul(s, NULL, 10);
898 *s = 0;
900 __add_preferred_console(buf, idx, options, brl_options);
901 return 1;
903 __setup("console=", console_setup);
906 * add_preferred_console - add a device to the list of preferred consoles.
907 * @name: device name
908 * @idx: device index
909 * @options: options for this console
911 * The last preferred console added will be used for kernel messages
912 * and stdin/out/err for init. Normally this is used by console_setup
913 * above to handle user-supplied console arguments; however it can also
914 * be used by arch-specific code either to override the user or more
915 * commonly to provide a default console (ie from PROM variables) when
916 * the user has not supplied one.
918 int add_preferred_console(char *name, int idx, char *options)
920 return __add_preferred_console(name, idx, options, NULL);
923 int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options)
925 struct console_cmdline *c;
926 int i;
928 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
929 if (strcmp(console_cmdline[i].name, name) == 0 &&
930 console_cmdline[i].index == idx) {
931 c = &console_cmdline[i];
932 strlcpy(c->name, name_new, sizeof(c->name));
933 c->name[sizeof(c->name) - 1] = 0;
934 c->options = options;
935 c->index = idx_new;
936 return i;
938 /* not found */
939 return -1;
942 int console_suspend_enabled = 1;
943 EXPORT_SYMBOL(console_suspend_enabled);
945 static int __init console_suspend_disable(char *str)
947 console_suspend_enabled = 0;
948 return 1;
950 __setup("no_console_suspend", console_suspend_disable);
953 * suspend_console - suspend the console subsystem
955 * This disables printk() while we go into suspend states
957 void suspend_console(void)
959 if (!console_suspend_enabled)
960 return;
961 printk("Suspending console(s)\n");
962 acquire_console_sem();
963 console_suspended = 1;
966 void resume_console(void)
968 if (!console_suspend_enabled)
969 return;
970 console_suspended = 0;
971 release_console_sem();
975 * acquire_console_sem - lock the console system for exclusive use.
977 * Acquires a semaphore which guarantees that the caller has
978 * exclusive access to the console system and the console_drivers list.
980 * Can sleep, returns nothing.
982 void acquire_console_sem(void)
984 BUG_ON(in_interrupt());
985 if (console_suspended) {
986 down(&secondary_console_sem);
987 return;
989 down(&console_sem);
990 console_locked = 1;
991 console_may_schedule = 1;
993 EXPORT_SYMBOL(acquire_console_sem);
995 int try_acquire_console_sem(void)
997 if (down_trylock(&console_sem))
998 return -1;
999 console_locked = 1;
1000 console_may_schedule = 0;
1001 return 0;
1003 EXPORT_SYMBOL(try_acquire_console_sem);
1005 int is_console_locked(void)
1007 return console_locked;
1010 void wake_up_klogd(void)
1012 if (!oops_in_progress && waitqueue_active(&log_wait))
1013 wake_up_interruptible(&log_wait);
1017 * release_console_sem - unlock the console system
1019 * Releases the semaphore which the caller holds on the console system
1020 * and the console driver list.
1022 * While the semaphore was held, console output may have been buffered
1023 * by printk(). If this is the case, release_console_sem() emits
1024 * the output prior to releasing the semaphore.
1026 * If there is output waiting for klogd, we wake it up.
1028 * release_console_sem() may be called from any context.
1030 void release_console_sem(void)
1032 unsigned long flags;
1033 unsigned _con_start, _log_end;
1034 unsigned wake_klogd = 0;
1036 if (console_suspended) {
1037 up(&secondary_console_sem);
1038 return;
1041 console_may_schedule = 0;
1043 for ( ; ; ) {
1044 spin_lock_irqsave(&logbuf_lock, flags);
1045 wake_klogd |= log_start - log_end;
1046 if (con_start == log_end)
1047 break; /* Nothing to print */
1048 _con_start = con_start;
1049 _log_end = log_end;
1050 con_start = log_end; /* Flush */
1051 spin_unlock(&logbuf_lock);
1052 call_console_drivers(_con_start, _log_end);
1053 local_irq_restore(flags);
1055 console_locked = 0;
1056 up(&console_sem);
1057 spin_unlock_irqrestore(&logbuf_lock, flags);
1058 if (wake_klogd)
1059 wake_up_klogd();
1061 EXPORT_SYMBOL(release_console_sem);
1064 * console_conditional_schedule - yield the CPU if required
1066 * If the console code is currently allowed to sleep, and
1067 * if this CPU should yield the CPU to another task, do
1068 * so here.
1070 * Must be called within acquire_console_sem().
1072 void __sched console_conditional_schedule(void)
1074 if (console_may_schedule)
1075 cond_resched();
1077 EXPORT_SYMBOL(console_conditional_schedule);
1079 void console_print(const char *s)
1081 printk(KERN_EMERG "%s", s);
1083 EXPORT_SYMBOL(console_print);
1085 void console_unblank(void)
1087 struct console *c;
1090 * console_unblank can no longer be called in interrupt context unless
1091 * oops_in_progress is set to 1..
1093 if (oops_in_progress) {
1094 if (down_trylock(&console_sem) != 0)
1095 return;
1096 } else
1097 acquire_console_sem();
1099 console_locked = 1;
1100 console_may_schedule = 0;
1101 for (c = console_drivers; c != NULL; c = c->next)
1102 if ((c->flags & CON_ENABLED) && c->unblank)
1103 c->unblank();
1104 release_console_sem();
1108 * Return the console tty driver structure and its associated index
1110 struct tty_driver *console_device(int *index)
1112 struct console *c;
1113 struct tty_driver *driver = NULL;
1115 acquire_console_sem();
1116 for (c = console_drivers; c != NULL; c = c->next) {
1117 if (!c->device)
1118 continue;
1119 driver = c->device(c, index);
1120 if (driver)
1121 break;
1123 release_console_sem();
1124 return driver;
1128 * Prevent further output on the passed console device so that (for example)
1129 * serial drivers can disable console output before suspending a port, and can
1130 * re-enable output afterwards.
1132 void console_stop(struct console *console)
1134 acquire_console_sem();
1135 console->flags &= ~CON_ENABLED;
1136 release_console_sem();
1138 EXPORT_SYMBOL(console_stop);
1140 void console_start(struct console *console)
1142 acquire_console_sem();
1143 console->flags |= CON_ENABLED;
1144 release_console_sem();
1146 EXPORT_SYMBOL(console_start);
1149 * The console driver calls this routine during kernel initialization
1150 * to register the console printing procedure with printk() and to
1151 * print any messages that were printed by the kernel before the
1152 * console driver was initialized.
1154 void register_console(struct console *console)
1156 int i;
1157 unsigned long flags;
1158 struct console *bootconsole = NULL;
1160 if (console_drivers) {
1161 if (console->flags & CON_BOOT)
1162 return;
1163 if (console_drivers->flags & CON_BOOT)
1164 bootconsole = console_drivers;
1167 if (preferred_console < 0 || bootconsole || !console_drivers)
1168 preferred_console = selected_console;
1170 if (console->early_setup)
1171 console->early_setup();
1174 * See if we want to use this console driver. If we
1175 * didn't select a console we take the first one
1176 * that registers here.
1178 if (preferred_console < 0) {
1179 if (console->index < 0)
1180 console->index = 0;
1181 if (console->setup == NULL ||
1182 console->setup(console, NULL) == 0) {
1183 console->flags |= CON_ENABLED | CON_CONSDEV;
1184 preferred_console = 0;
1189 * See if this console matches one we selected on
1190 * the command line.
1192 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0];
1193 i++) {
1194 if (strcmp(console_cmdline[i].name, console->name) != 0)
1195 continue;
1196 if (console->index >= 0 &&
1197 console->index != console_cmdline[i].index)
1198 continue;
1199 if (console->index < 0)
1200 console->index = console_cmdline[i].index;
1201 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1202 if (console_cmdline[i].brl_options) {
1203 console->flags |= CON_BRL;
1204 braille_register_console(console,
1205 console_cmdline[i].index,
1206 console_cmdline[i].options,
1207 console_cmdline[i].brl_options);
1208 return;
1210 #endif
1211 if (console->setup &&
1212 console->setup(console, console_cmdline[i].options) != 0)
1213 break;
1214 console->flags |= CON_ENABLED;
1215 console->index = console_cmdline[i].index;
1216 if (i == selected_console) {
1217 console->flags |= CON_CONSDEV;
1218 preferred_console = selected_console;
1220 break;
1223 if (!(console->flags & CON_ENABLED))
1224 return;
1226 if (bootconsole && (console->flags & CON_CONSDEV)) {
1227 printk(KERN_INFO "console handover: boot [%s%d] -> real [%s%d]\n",
1228 bootconsole->name, bootconsole->index,
1229 console->name, console->index);
1230 unregister_console(bootconsole);
1231 console->flags &= ~CON_PRINTBUFFER;
1232 } else {
1233 printk(KERN_INFO "console [%s%d] enabled\n",
1234 console->name, console->index);
1238 * Put this console in the list - keep the
1239 * preferred driver at the head of the list.
1241 acquire_console_sem();
1242 if ((console->flags & CON_CONSDEV) || console_drivers == NULL) {
1243 console->next = console_drivers;
1244 console_drivers = console;
1245 if (console->next)
1246 console->next->flags &= ~CON_CONSDEV;
1247 } else {
1248 console->next = console_drivers->next;
1249 console_drivers->next = console;
1251 if (console->flags & CON_PRINTBUFFER) {
1253 * release_console_sem() will print out the buffered messages
1254 * for us.
1256 spin_lock_irqsave(&logbuf_lock, flags);
1257 con_start = log_start;
1258 spin_unlock_irqrestore(&logbuf_lock, flags);
1260 release_console_sem();
1262 EXPORT_SYMBOL(register_console);
1264 int unregister_console(struct console *console)
1266 struct console *a, *b;
1267 int res = 1;
1269 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1270 if (console->flags & CON_BRL)
1271 return braille_unregister_console(console);
1272 #endif
1274 acquire_console_sem();
1275 if (console_drivers == console) {
1276 console_drivers=console->next;
1277 res = 0;
1278 } else if (console_drivers) {
1279 for (a=console_drivers->next, b=console_drivers ;
1280 a; b=a, a=b->next) {
1281 if (a == console) {
1282 b->next = a->next;
1283 res = 0;
1284 break;
1290 * If this isn't the last console and it has CON_CONSDEV set, we
1291 * need to set it on the next preferred console.
1293 if (console_drivers != NULL && console->flags & CON_CONSDEV)
1294 console_drivers->flags |= CON_CONSDEV;
1296 release_console_sem();
1297 return res;
1299 EXPORT_SYMBOL(unregister_console);
1301 static int __init disable_boot_consoles(void)
1303 if (console_drivers != NULL) {
1304 if (console_drivers->flags & CON_BOOT) {
1305 printk(KERN_INFO "turn off boot console %s%d\n",
1306 console_drivers->name, console_drivers->index);
1307 return unregister_console(console_drivers);
1310 return 0;
1312 late_initcall(disable_boot_consoles);
1315 * tty_write_message - write a message to a certain tty, not just the console.
1316 * @tty: the destination tty_struct
1317 * @msg: the message to write
1319 * This is used for messages that need to be redirected to a specific tty.
1320 * We don't put it into the syslog queue right now maybe in the future if
1321 * really needed.
1323 void tty_write_message(struct tty_struct *tty, char *msg)
1325 if (tty && tty->ops->write)
1326 tty->ops->write(tty, msg, strlen(msg));
1327 return;
1330 #if defined CONFIG_PRINTK
1332 * printk rate limiting, lifted from the networking subsystem.
1334 * This enforces a rate limit: not more than one kernel message
1335 * every printk_ratelimit_jiffies to make a denial-of-service
1336 * attack impossible.
1338 int __printk_ratelimit(int ratelimit_jiffies, int ratelimit_burst)
1340 return __ratelimit(ratelimit_jiffies, ratelimit_burst);
1342 EXPORT_SYMBOL(__printk_ratelimit);
1344 /* minimum time in jiffies between messages */
1345 int printk_ratelimit_jiffies = 5 * HZ;
1347 /* number of messages we send before ratelimiting */
1348 int printk_ratelimit_burst = 10;
1350 int printk_ratelimit(void)
1352 return __printk_ratelimit(printk_ratelimit_jiffies,
1353 printk_ratelimit_burst);
1355 EXPORT_SYMBOL(printk_ratelimit);
1358 * printk_timed_ratelimit - caller-controlled printk ratelimiting
1359 * @caller_jiffies: pointer to caller's state
1360 * @interval_msecs: minimum interval between prints
1362 * printk_timed_ratelimit() returns true if more than @interval_msecs
1363 * milliseconds have elapsed since the last time printk_timed_ratelimit()
1364 * returned true.
1366 bool printk_timed_ratelimit(unsigned long *caller_jiffies,
1367 unsigned int interval_msecs)
1369 if (*caller_jiffies == 0 || time_after(jiffies, *caller_jiffies)) {
1370 *caller_jiffies = jiffies + msecs_to_jiffies(interval_msecs);
1371 return true;
1373 return false;
1375 EXPORT_SYMBOL(printk_timed_ratelimit);
1376 #endif