replace some function names
[linux-2.6/zen-sources.git] / kernel / printk.c
blob8fe43730bada9fc504a314d5d4163d2c4d47a508
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
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>
36 #include <linux/suspend.h>
38 #include <asm/uaccess.h>
41 * Architectures can override it:
43 void asmlinkage __attribute__((weak)) early_printk(const char *fmt, ...)
47 #define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT)
49 /* printk's without a loglevel use this.. */
50 #define DEFAULT_MESSAGE_LOGLEVEL 4 /* KERN_WARNING */
52 /* We show everything that is MORE important than this.. */
53 #define MINIMUM_CONSOLE_LOGLEVEL 1 /* Minimum loglevel we let people use */
54 #define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */
56 DECLARE_WAIT_QUEUE_HEAD(log_wait);
58 int console_printk[4] = {
59 DEFAULT_CONSOLE_LOGLEVEL, /* console_loglevel */
60 DEFAULT_MESSAGE_LOGLEVEL, /* default_message_loglevel */
61 MINIMUM_CONSOLE_LOGLEVEL, /* minimum_console_loglevel */
62 DEFAULT_CONSOLE_LOGLEVEL, /* default_console_loglevel */
66 * Low level drivers may need that to know if they can schedule in
67 * their unblank() callback or not. So let's export it.
69 int oops_in_progress;
70 EXPORT_SYMBOL(oops_in_progress);
73 * console_sem protects the console_drivers list, and also
74 * provides serialisation for access to the entire console
75 * driver system.
77 static DECLARE_MUTEX(console_sem);
78 static DECLARE_MUTEX(secondary_console_sem);
79 struct console *console_drivers;
80 EXPORT_SYMBOL_GPL(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;
128 int console_set_on_cmdline;
129 EXPORT_SYMBOL(console_set_on_cmdline);
131 /* Flag: console code may call schedule() */
132 static int console_may_schedule;
134 #ifdef CONFIG_PRINTK
136 static char __log_buf[__LOG_BUF_LEN];
137 static char *log_buf = __log_buf;
138 static int log_buf_len = __LOG_BUF_LEN;
139 static unsigned logged_chars; /* Number of chars produced since last read+clear operation */
141 static int __init log_buf_len_setup(char *str)
143 unsigned size = memparse(str, &str);
144 unsigned long flags;
146 if (size)
147 size = roundup_pow_of_two(size);
148 if (size > log_buf_len) {
149 unsigned start, dest_idx, offset;
150 char *new_log_buf;
152 new_log_buf = alloc_bootmem(size);
153 if (!new_log_buf) {
154 printk(KERN_WARNING "log_buf_len: allocation failed\n");
155 goto out;
158 spin_lock_irqsave(&logbuf_lock, flags);
159 log_buf_len = size;
160 log_buf = new_log_buf;
162 offset = start = min(con_start, log_start);
163 dest_idx = 0;
164 while (start != log_end) {
165 log_buf[dest_idx] = __log_buf[start & (__LOG_BUF_LEN - 1)];
166 start++;
167 dest_idx++;
169 log_start -= offset;
170 con_start -= offset;
171 log_end -= offset;
172 spin_unlock_irqrestore(&logbuf_lock, flags);
174 printk(KERN_NOTICE "log_buf_len: %d\n", log_buf_len);
176 out:
177 return 1;
180 __setup("log_buf_len=", log_buf_len_setup);
182 #ifdef CONFIG_BOOT_PRINTK_DELAY
184 static unsigned int boot_delay; /* msecs delay after each printk during bootup */
185 static unsigned long long printk_delay_msec; /* per msec, based on boot_delay */
187 static int __init boot_delay_setup(char *str)
189 unsigned long lpj;
190 unsigned long long loops_per_msec;
192 lpj = preset_lpj ? preset_lpj : 1000000; /* some guess */
193 loops_per_msec = (unsigned long long)lpj / 1000 * HZ;
195 get_option(&str, &boot_delay);
196 if (boot_delay > 10 * 1000)
197 boot_delay = 0;
199 printk_delay_msec = loops_per_msec;
200 printk(KERN_DEBUG "boot_delay: %u, preset_lpj: %ld, lpj: %lu, "
201 "HZ: %d, printk_delay_msec: %llu\n",
202 boot_delay, preset_lpj, lpj, HZ, printk_delay_msec);
203 return 1;
205 __setup("boot_delay=", boot_delay_setup);
207 static void boot_delay_msec(void)
209 unsigned long long k;
210 unsigned long timeout;
212 if (boot_delay == 0 || system_state != SYSTEM_BOOTING)
213 return;
215 k = (unsigned long long)printk_delay_msec * boot_delay;
217 timeout = jiffies + msecs_to_jiffies(boot_delay);
218 while (k) {
219 k--;
220 cpu_relax();
222 * use (volatile) jiffies to prevent
223 * compiler reduction; loop termination via jiffies
224 * is secondary and may or may not happen.
226 if (time_after(jiffies, timeout))
227 break;
228 touch_nmi_watchdog();
231 #else
232 static inline void boot_delay_msec(void)
235 #endif
238 * Commands to do_syslog:
240 * 0 -- Close the log. Currently a NOP.
241 * 1 -- Open the log. Currently a NOP.
242 * 2 -- Read from the log.
243 * 3 -- Read all messages remaining in the ring buffer.
244 * 4 -- Read and clear all messages remaining in the ring buffer
245 * 5 -- Clear ring buffer.
246 * 6 -- Disable printk's to console
247 * 7 -- Enable printk's to console
248 * 8 -- Set level of messages printed to console
249 * 9 -- Return number of unread characters in the log buffer
250 * 10 -- Return size of the log buffer
252 int do_syslog(int type, char __user *buf, int len)
254 unsigned i, j, limit, count;
255 int do_clear = 0;
256 char c;
257 int error = 0;
259 error = security_syslog(type);
260 if (error)
261 return error;
263 switch (type) {
264 case 0: /* Close log */
265 break;
266 case 1: /* Open log */
267 break;
268 case 2: /* Read from log */
269 error = -EINVAL;
270 if (!buf || len < 0)
271 goto out;
272 error = 0;
273 if (!len)
274 goto out;
275 if (!access_ok(VERIFY_WRITE, buf, len)) {
276 error = -EFAULT;
277 goto out;
279 error = wait_event_interruptible(log_wait,
280 (log_start - log_end));
281 if (error)
282 goto out;
283 i = 0;
284 spin_lock_irq(&logbuf_lock);
285 while (!error && (log_start != log_end) && i < len) {
286 c = LOG_BUF(log_start);
287 log_start++;
288 spin_unlock_irq(&logbuf_lock);
289 error = __put_user(c,buf);
290 buf++;
291 i++;
292 cond_resched();
293 spin_lock_irq(&logbuf_lock);
295 spin_unlock_irq(&logbuf_lock);
296 if (!error)
297 error = i;
298 break;
299 case 4: /* Read/clear last kernel messages */
300 do_clear = 1;
301 /* FALL THRU */
302 case 3: /* Read last kernel messages */
303 error = -EINVAL;
304 if (!buf || len < 0)
305 goto out;
306 error = 0;
307 if (!len)
308 goto out;
309 if (!access_ok(VERIFY_WRITE, buf, len)) {
310 error = -EFAULT;
311 goto out;
313 count = len;
314 if (count > log_buf_len)
315 count = log_buf_len;
316 spin_lock_irq(&logbuf_lock);
317 if (count > logged_chars)
318 count = logged_chars;
319 if (do_clear)
320 logged_chars = 0;
321 limit = log_end;
323 * __put_user() could sleep, and while we sleep
324 * printk() could overwrite the messages
325 * we try to copy to user space. Therefore
326 * the messages are copied in reverse. <manfreds>
328 for (i = 0; i < count && !error; i++) {
329 j = limit-1-i;
330 if (j + log_buf_len < log_end)
331 break;
332 c = LOG_BUF(j);
333 spin_unlock_irq(&logbuf_lock);
334 error = __put_user(c,&buf[count-1-i]);
335 cond_resched();
336 spin_lock_irq(&logbuf_lock);
338 spin_unlock_irq(&logbuf_lock);
339 if (error)
340 break;
341 error = i;
342 if (i != count) {
343 int offset = count-error;
344 /* buffer overflow during copy, correct user buffer. */
345 for (i = 0; i < error; i++) {
346 if (__get_user(c,&buf[i+offset]) ||
347 __put_user(c,&buf[i])) {
348 error = -EFAULT;
349 break;
351 cond_resched();
354 break;
355 case 5: /* Clear ring buffer */
356 logged_chars = 0;
357 break;
358 case 6: /* Disable logging to console */
359 console_loglevel = minimum_console_loglevel;
360 break;
361 case 7: /* Enable logging to console */
362 console_loglevel = default_console_loglevel;
363 break;
364 case 8: /* Set level of messages printed to console */
365 error = -EINVAL;
366 if (len < 1 || len > 8)
367 goto out;
368 if (len < minimum_console_loglevel)
369 len = minimum_console_loglevel;
370 console_loglevel = len;
371 error = 0;
372 break;
373 case 9: /* Number of chars in the log buffer */
374 error = log_end - log_start;
375 break;
376 case 10: /* Size of the log buffer */
377 error = log_buf_len;
378 break;
379 default:
380 error = -EINVAL;
381 break;
383 out:
384 return error;
387 asmlinkage long sys_syslog(int type, char __user *buf, int len)
389 return do_syslog(type, buf, len);
393 * Call the console drivers on a range of log_buf
395 static void __call_console_drivers(unsigned start, unsigned end,
396 unsigned int loglevel)
398 struct console *con;
400 for (con = console_drivers; con; con = con->next) {
401 if ((con->flags & CON_ENABLED) && con->write &&
402 (cpu_online(smp_processor_id()) ||
403 (con->flags & CON_ANYTIME)))
404 con->write(con, &LOG_BUF(start), end - start, loglevel);
408 static int __read_mostly ignore_loglevel;
410 static int __init ignore_loglevel_setup(char *str)
412 ignore_loglevel = 1;
413 printk(KERN_INFO "debug: ignoring loglevel setting.\n");
415 return 0;
418 early_param("ignore_loglevel", ignore_loglevel_setup);
421 * Write out chars from start to end - 1 inclusive
423 static void _call_console_drivers(unsigned start,
424 unsigned end, int msg_log_level)
426 if ((msg_log_level < console_loglevel || ignore_loglevel) &&
427 console_drivers && start != end) {
428 if ((start & LOG_BUF_MASK) > (end & LOG_BUF_MASK)) {
429 /* wrapped write */
430 __call_console_drivers(start & LOG_BUF_MASK,
431 log_buf_len, msg_log_level);
432 __call_console_drivers(0, end & LOG_BUF_MASK,
433 msg_log_level);
434 } else {
435 __call_console_drivers(start, end, msg_log_level);
441 * Call the console drivers, asking them to write out
442 * log_buf[start] to log_buf[end - 1].
443 * The console_sem must be held.
445 static void call_console_drivers(unsigned start, unsigned end)
447 unsigned cur_index, start_print;
448 static int msg_level = -1;
450 BUG_ON(((int)(start - end)) > 0);
452 cur_index = start;
453 start_print = start;
454 while (cur_index != end) {
455 if (msg_level < 0 && ((end - cur_index) > 2) &&
456 LOG_BUF(cur_index + 0) == '<' &&
457 LOG_BUF(cur_index + 1) >= '0' &&
458 LOG_BUF(cur_index + 1) <= '7' &&
459 LOG_BUF(cur_index + 2) == '>') {
460 msg_level = LOG_BUF(cur_index + 1) - '0';
461 cur_index += 3;
462 start_print = cur_index;
464 while (cur_index != end) {
465 char c = LOG_BUF(cur_index);
467 cur_index++;
468 if (c == '\n') {
469 if (msg_level < 0) {
471 * printk() has already given us loglevel tags in
472 * the buffer. This code is here in case the
473 * log buffer has wrapped right round and scribbled
474 * on those tags
476 msg_level = default_message_loglevel;
478 _call_console_drivers(start_print, cur_index, msg_level);
479 msg_level = -1;
480 start_print = cur_index;
481 break;
485 _call_console_drivers(start_print, end, msg_level);
488 static void emit_log_char(char c)
490 LOG_BUF(log_end) = c;
491 log_end++;
492 if (log_end - log_start > log_buf_len)
493 log_start = log_end - log_buf_len;
494 if (log_end - con_start > log_buf_len)
495 con_start = log_end - log_buf_len;
496 if (logged_chars < log_buf_len)
497 logged_chars++;
501 * Zap console related locks when oopsing. Only zap at most once
502 * every 10 seconds, to leave time for slow consoles to print a
503 * full oops.
505 static void zap_locks(void)
507 static unsigned long oops_timestamp;
509 if (time_after_eq(jiffies, oops_timestamp) &&
510 !time_after(jiffies, oops_timestamp + 30 * HZ))
511 return;
513 oops_timestamp = jiffies;
515 /* If a crash is occurring, make sure we can't deadlock */
516 spin_lock_init(&logbuf_lock);
517 /* And make sure that we print immediately */
518 init_MUTEX(&console_sem);
521 #if defined(CONFIG_PRINTK_TIME)
522 static int printk_time = 1;
523 #else
524 static int printk_time = 0;
525 #endif
526 module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR);
528 /* Check if we have any console registered that can be called early in boot. */
529 static int have_callable_console(void)
531 struct console *con;
533 for (con = console_drivers; con; con = con->next)
534 if (con->flags & CON_ANYTIME)
535 return 1;
537 return 0;
541 * printk - print a kernel message
542 * @fmt: format string
544 * This is printk(). It can be called from any context. We want it to work.
546 * We try to grab the console_sem. If we succeed, it's easy - we log the output and
547 * call the console drivers. If we fail to get the semaphore we place the output
548 * into the log buffer and return. The current holder of the console_sem will
549 * notice the new output in release_console_sem() and will send it to the
550 * consoles before releasing the semaphore.
552 * One effect of this deferred printing is that code which calls printk() and
553 * then changes console_loglevel may break. This is because console_loglevel
554 * is inspected when the actual printing occurs.
556 * See also:
557 * printf(3)
559 * See the vsnprintf() documentation for format string extensions over C99.
562 asmlinkage int printk(const char *fmt, ...)
564 va_list args;
565 int r;
567 va_start(args, fmt);
568 r = vprintk(fmt, args);
569 va_end(args);
571 return r;
574 /* cpu currently holding logbuf_lock */
575 static volatile unsigned int printk_cpu = UINT_MAX;
578 * Can we actually use the console at this time on this cpu?
580 * Console drivers may assume that per-cpu resources have
581 * been allocated. So unless they're explicitly marked as
582 * being able to cope (CON_ANYTIME) don't call them until
583 * this CPU is officially up.
585 static inline int can_use_console(unsigned int cpu)
587 return cpu_online(cpu) || have_callable_console();
591 * Try to get console ownership to actually show the kernel
592 * messages from a 'printk'. Return true (and with the
593 * console_semaphore held, and 'console_locked' set) if it
594 * is successful, false otherwise.
596 * This gets called with the 'logbuf_lock' spinlock held and
597 * interrupts disabled. It should return with 'lockbuf_lock'
598 * released but interrupts still disabled.
600 static int acquire_console_semaphore_for_printk(unsigned int cpu)
602 int retval = 0;
604 if (!try_acquire_console_sem()) {
605 retval = 1;
608 * If we can't use the console, we need to release
609 * the console semaphore by hand to avoid flushing
610 * the buffer. We need to hold the console semaphore
611 * in order to do this test safely.
613 if (!can_use_console(cpu)) {
614 console_locked = 0;
615 up(&console_sem);
616 retval = 0;
619 printk_cpu = UINT_MAX;
620 spin_unlock(&logbuf_lock);
621 return retval;
623 static const char recursion_bug_msg [] =
624 KERN_CRIT "BUG: recent printk recursion!\n";
625 static int recursion_bug;
626 static int new_text_line = 1;
627 static char printk_buf[1024];
629 asmlinkage int vprintk(const char *fmt, va_list args)
631 int printed_len = 0;
632 int current_log_level = default_message_loglevel;
633 unsigned long flags;
634 int this_cpu;
635 char *p;
637 boot_delay_msec();
639 preempt_disable();
640 /* This stops the holder of console_sem just where we want him */
641 raw_local_irq_save(flags);
642 this_cpu = smp_processor_id();
645 * Ouch, printk recursed into itself!
647 if (unlikely(printk_cpu == this_cpu)) {
649 * If a crash is occurring during printk() on this CPU,
650 * then try to get the crash message out but make sure
651 * we can't deadlock. Otherwise just return to avoid the
652 * recursion and return - but flag the recursion so that
653 * it can be printed at the next appropriate moment:
655 if (!oops_in_progress) {
656 recursion_bug = 1;
657 goto out_restore_irqs;
659 zap_locks();
662 lockdep_off();
663 spin_lock(&logbuf_lock);
664 printk_cpu = this_cpu;
666 if (recursion_bug) {
667 recursion_bug = 0;
668 strcpy(printk_buf, recursion_bug_msg);
669 printed_len = sizeof(recursion_bug_msg);
671 /* Emit the output into the temporary buffer */
672 printed_len += vscnprintf(printk_buf + printed_len,
673 sizeof(printk_buf) - printed_len, fmt, args);
677 * Copy the output into log_buf. If the caller didn't provide
678 * appropriate log level tags, we insert them here
680 for (p = printk_buf; *p; p++) {
681 if (new_text_line) {
682 /* If a token, set current_log_level and skip over */
683 if (p[0] == '<' && p[1] >= '0' && p[1] <= '7' &&
684 p[2] == '>') {
685 current_log_level = p[1] - '0';
686 p += 3;
687 printed_len -= 3;
690 /* Always output the token */
691 emit_log_char('<');
692 emit_log_char(current_log_level + '0');
693 emit_log_char('>');
694 printed_len += 3;
695 new_text_line = 0;
697 if (printk_time) {
698 /* Follow the token with the time */
699 char tbuf[50], *tp;
700 unsigned tlen;
701 unsigned long long t;
702 unsigned long nanosec_rem;
704 t = cpu_clock(printk_cpu);
705 nanosec_rem = do_div(t, 1000000000);
706 tlen = sprintf(tbuf, "[%5lu.%06lu] ",
707 (unsigned long) t,
708 nanosec_rem / 1000);
710 for (tp = tbuf; tp < tbuf + tlen; tp++)
711 emit_log_char(*tp);
712 printed_len += tlen;
715 if (!*p)
716 break;
719 emit_log_char(*p);
720 if (*p == '\n')
721 new_text_line = 1;
725 * Try to acquire and then immediately release the
726 * console semaphore. The release will do all the
727 * actual magic (print out buffers, wake up klogd,
728 * etc).
730 * The acquire_console_semaphore_for_printk() function
731 * will release 'logbuf_lock' regardless of whether it
732 * actually gets the semaphore or not.
734 if (acquire_console_semaphore_for_printk(this_cpu))
735 release_console_sem();
737 lockdep_on();
738 out_restore_irqs:
739 raw_local_irq_restore(flags);
741 preempt_enable();
742 return printed_len;
744 EXPORT_SYMBOL(printk);
745 EXPORT_SYMBOL(vprintk);
747 #else
749 asmlinkage long sys_syslog(int type, char __user *buf, int len)
751 return -ENOSYS;
754 static void call_console_drivers(unsigned start, unsigned end)
758 #endif
760 static int __add_preferred_console(char *name, int idx, char *options,
761 char *brl_options)
763 struct console_cmdline *c;
764 int i;
767 * See if this tty is not yet registered, and
768 * if we have a slot free.
770 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
771 if (strcmp(console_cmdline[i].name, name) == 0 &&
772 console_cmdline[i].index == idx) {
773 if (!brl_options)
774 selected_console = i;
775 return 0;
777 if (i == MAX_CMDLINECONSOLES)
778 return -E2BIG;
779 if (!brl_options)
780 selected_console = i;
781 c = &console_cmdline[i];
782 strlcpy(c->name, name, sizeof(c->name));
783 c->options = options;
784 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
785 c->brl_options = brl_options;
786 #endif
787 c->index = idx;
788 return 0;
791 * Set up a list of consoles. Called from init/main.c
793 static int __init console_setup(char *str)
795 char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for index */
796 char *s, *options, *brl_options = NULL;
797 int idx;
799 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
800 if (!memcmp(str, "brl,", 4)) {
801 brl_options = "";
802 str += 4;
803 } else if (!memcmp(str, "brl=", 4)) {
804 brl_options = str + 4;
805 str = strchr(brl_options, ',');
806 if (!str) {
807 printk(KERN_ERR "need port name after brl=\n");
808 return 1;
810 *(str++) = 0;
812 #endif
815 * Decode str into name, index, options.
817 if (str[0] >= '0' && str[0] <= '9') {
818 strcpy(buf, "ttyS");
819 strncpy(buf + 4, str, sizeof(buf) - 5);
820 } else {
821 strncpy(buf, str, sizeof(buf) - 1);
823 buf[sizeof(buf) - 1] = 0;
824 if ((options = strchr(str, ',')) != NULL)
825 *(options++) = 0;
826 #ifdef __sparc__
827 if (!strcmp(str, "ttya"))
828 strcpy(buf, "ttyS0");
829 if (!strcmp(str, "ttyb"))
830 strcpy(buf, "ttyS1");
831 #endif
832 for (s = buf; *s; s++)
833 if ((*s >= '0' && *s <= '9') || *s == ',')
834 break;
835 idx = simple_strtoul(s, NULL, 10);
836 *s = 0;
838 __add_preferred_console(buf, idx, options, brl_options);
839 console_set_on_cmdline = 1;
840 return 1;
842 __setup("console=", console_setup);
845 * add_preferred_console - add a device to the list of preferred consoles.
846 * @name: device name
847 * @idx: device index
848 * @options: options for this console
850 * The last preferred console added will be used for kernel messages
851 * and stdin/out/err for init. Normally this is used by console_setup
852 * above to handle user-supplied console arguments; however it can also
853 * be used by arch-specific code either to override the user or more
854 * commonly to provide a default console (ie from PROM variables) when
855 * the user has not supplied one.
857 int add_preferred_console(char *name, int idx, char *options)
859 return __add_preferred_console(name, idx, options, NULL);
862 int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options)
864 struct console_cmdline *c;
865 int i;
867 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
868 if (strcmp(console_cmdline[i].name, name) == 0 &&
869 console_cmdline[i].index == idx) {
870 c = &console_cmdline[i];
871 strlcpy(c->name, name_new, sizeof(c->name));
872 c->name[sizeof(c->name) - 1] = 0;
873 c->options = options;
874 c->index = idx_new;
875 return i;
877 /* not found */
878 return -1;
881 int console_suspend_enabled = 1;
882 EXPORT_SYMBOL(console_suspend_enabled);
884 static int __init console_suspend_disable(char *str)
886 console_suspend_enabled = 0;
887 return 1;
889 __setup("no_console_suspend", console_suspend_disable);
892 * suspend_console - suspend the console subsystem
894 * This disables printk() while we go into suspend states
896 void suspend_console(void)
898 if (!console_suspend_enabled)
899 return;
900 printk("Suspending console(s) (use no_console_suspend to debug)\n");
901 acquire_console_sem();
902 console_suspended = 1;
904 EXPORT_IF_TOI_MODULAR(suspend_console);
906 void resume_console(void)
908 if (!console_suspend_enabled)
909 return;
910 console_suspended = 0;
911 release_console_sem();
913 EXPORT_IF_TOI_MODULAR(resume_console);
916 * acquire_console_sem - lock the console system for exclusive use.
918 * Acquires a semaphore which guarantees that the caller has
919 * exclusive access to the console system and the console_drivers list.
921 * Can sleep, returns nothing.
923 void acquire_console_sem(void)
925 BUG_ON(in_interrupt());
926 if (console_suspended) {
927 down(&secondary_console_sem);
928 return;
930 down(&console_sem);
931 console_locked = 1;
932 console_may_schedule = 1;
934 EXPORT_SYMBOL(acquire_console_sem);
936 int try_acquire_console_sem(void)
938 if (down_trylock(&console_sem))
939 return -1;
940 console_locked = 1;
941 console_may_schedule = 0;
942 return 0;
944 EXPORT_SYMBOL(try_acquire_console_sem);
946 int is_console_locked(void)
948 return console_locked;
951 static DEFINE_PER_CPU(int, printk_pending);
953 void printk_tick(void)
955 if (__get_cpu_var(printk_pending)) {
956 __get_cpu_var(printk_pending) = 0;
957 wake_up_interruptible(&log_wait);
961 int printk_needs_cpu(int cpu)
963 return per_cpu(printk_pending, cpu);
966 void wake_up_klogd(void)
968 if (waitqueue_active(&log_wait))
969 __raw_get_cpu_var(printk_pending) = 1;
973 * release_console_sem - unlock the console system
975 * Releases the semaphore which the caller holds on the console system
976 * and the console driver list.
978 * While the semaphore was held, console output may have been buffered
979 * by printk(). If this is the case, release_console_sem() emits
980 * the output prior to releasing the semaphore.
982 * If there is output waiting for klogd, we wake it up.
984 * release_console_sem() may be called from any context.
986 void release_console_sem(void)
988 unsigned long flags;
989 unsigned _con_start, _log_end;
990 unsigned wake_klogd = 0;
992 if (console_suspended) {
993 up(&secondary_console_sem);
994 return;
997 console_may_schedule = 0;
999 for ( ; ; ) {
1000 spin_lock_irqsave(&logbuf_lock, flags);
1001 wake_klogd |= log_start - log_end;
1002 if (con_start == log_end)
1003 break; /* Nothing to print */
1004 _con_start = con_start;
1005 _log_end = log_end;
1006 con_start = log_end; /* Flush */
1007 spin_unlock(&logbuf_lock);
1008 stop_critical_timings(); /* don't trace print latency */
1009 call_console_drivers(_con_start, _log_end);
1010 start_critical_timings();
1011 local_irq_restore(flags);
1013 console_locked = 0;
1014 up(&console_sem);
1015 spin_unlock_irqrestore(&logbuf_lock, flags);
1016 if (wake_klogd)
1017 wake_up_klogd();
1019 EXPORT_SYMBOL(release_console_sem);
1022 * console_conditional_schedule - yield the CPU if required
1024 * If the console code is currently allowed to sleep, and
1025 * if this CPU should yield the CPU to another task, do
1026 * so here.
1028 * Must be called within acquire_console_sem().
1030 void __sched console_conditional_schedule(void)
1032 if (console_may_schedule)
1033 cond_resched();
1035 EXPORT_SYMBOL(console_conditional_schedule);
1037 void console_print(const char *s)
1039 printk(KERN_EMERG "%s", s);
1041 EXPORT_SYMBOL(console_print);
1043 void console_unblank(void)
1045 struct console *c;
1048 * console_unblank can no longer be called in interrupt context unless
1049 * oops_in_progress is set to 1..
1051 if (oops_in_progress) {
1052 if (down_trylock(&console_sem) != 0)
1053 return;
1054 } else
1055 acquire_console_sem();
1057 console_locked = 1;
1058 console_may_schedule = 0;
1059 for (c = console_drivers; c != NULL; c = c->next)
1060 if ((c->flags & CON_ENABLED) && c->unblank)
1061 c->unblank();
1062 release_console_sem();
1066 * Return the console tty driver structure and its associated index
1068 struct tty_driver *console_device(int *index)
1070 struct console *c;
1071 struct tty_driver *driver = NULL;
1073 acquire_console_sem();
1074 for (c = console_drivers; c != NULL; c = c->next) {
1075 if (!c->device)
1076 continue;
1077 driver = c->device(c, index);
1078 if (driver)
1079 break;
1081 release_console_sem();
1082 return driver;
1086 * Prevent further output on the passed console device so that (for example)
1087 * serial drivers can disable console output before suspending a port, and can
1088 * re-enable output afterwards.
1090 void console_stop(struct console *console)
1092 acquire_console_sem();
1093 console->flags &= ~CON_ENABLED;
1094 release_console_sem();
1096 EXPORT_SYMBOL(console_stop);
1098 void console_start(struct console *console)
1100 acquire_console_sem();
1101 console->flags |= CON_ENABLED;
1102 release_console_sem();
1104 EXPORT_SYMBOL(console_start);
1107 * The console driver calls this routine during kernel initialization
1108 * to register the console printing procedure with printk() and to
1109 * print any messages that were printed by the kernel before the
1110 * console driver was initialized.
1112 void register_console(struct console *console)
1114 int i;
1115 unsigned long flags;
1116 struct console *bootconsole = NULL;
1118 if (console_drivers) {
1119 if (console->flags & CON_BOOT)
1120 return;
1121 if (console_drivers->flags & CON_BOOT)
1122 bootconsole = console_drivers;
1125 if (preferred_console < 0 || bootconsole || !console_drivers)
1126 preferred_console = selected_console;
1128 if (console->early_setup)
1129 console->early_setup();
1132 * See if we want to use this console driver. If we
1133 * didn't select a console we take the first one
1134 * that registers here.
1136 if (preferred_console < 0) {
1137 if (console->index < 0)
1138 console->index = 0;
1139 if (console->setup == NULL ||
1140 console->setup(console, NULL) == 0) {
1141 console->flags |= CON_ENABLED;
1142 if (console->device) {
1143 console->flags |= CON_CONSDEV;
1144 preferred_console = 0;
1150 * See if this console matches one we selected on
1151 * the command line.
1153 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0];
1154 i++) {
1155 if (strcmp(console_cmdline[i].name, console->name) != 0)
1156 continue;
1157 if (console->index >= 0 &&
1158 console->index != console_cmdline[i].index)
1159 continue;
1160 if (console->index < 0)
1161 console->index = console_cmdline[i].index;
1162 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1163 if (console_cmdline[i].brl_options) {
1164 console->flags |= CON_BRL;
1165 braille_register_console(console,
1166 console_cmdline[i].index,
1167 console_cmdline[i].options,
1168 console_cmdline[i].brl_options);
1169 return;
1171 #endif
1172 if (console->setup &&
1173 console->setup(console, console_cmdline[i].options) != 0)
1174 break;
1175 console->flags |= CON_ENABLED;
1176 console->index = console_cmdline[i].index;
1177 if (i == selected_console) {
1178 console->flags |= CON_CONSDEV;
1179 preferred_console = selected_console;
1181 break;
1184 if (!(console->flags & CON_ENABLED))
1185 return;
1187 if (bootconsole && (console->flags & CON_CONSDEV)) {
1188 printk(KERN_INFO "console handover: boot [%s%d] -> real [%s%d]\n",
1189 bootconsole->name, bootconsole->index,
1190 console->name, console->index);
1191 unregister_console(bootconsole);
1192 console->flags &= ~CON_PRINTBUFFER;
1193 } else {
1194 printk(KERN_INFO "console [%s%d] enabled\n",
1195 console->name, console->index);
1199 * Put this console in the list - keep the
1200 * preferred driver at the head of the list.
1202 acquire_console_sem();
1203 if ((console->flags & CON_CONSDEV) || console_drivers == NULL) {
1204 console->next = console_drivers;
1205 console_drivers = console;
1206 if (console->next)
1207 console->next->flags &= ~CON_CONSDEV;
1208 } else {
1209 console->next = console_drivers->next;
1210 console_drivers->next = console;
1212 if (console->flags & CON_PRINTBUFFER) {
1214 * release_console_sem() will print out the buffered messages
1215 * for us.
1217 spin_lock_irqsave(&logbuf_lock, flags);
1218 con_start = log_start;
1219 spin_unlock_irqrestore(&logbuf_lock, flags);
1221 release_console_sem();
1223 EXPORT_SYMBOL(register_console);
1225 int unregister_console(struct console *console)
1227 struct console *a, *b;
1228 int res = 1;
1230 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1231 if (console->flags & CON_BRL)
1232 return braille_unregister_console(console);
1233 #endif
1235 acquire_console_sem();
1236 if (console_drivers == console) {
1237 console_drivers=console->next;
1238 res = 0;
1239 } else if (console_drivers) {
1240 for (a=console_drivers->next, b=console_drivers ;
1241 a; b=a, a=b->next) {
1242 if (a == console) {
1243 b->next = a->next;
1244 res = 0;
1245 break;
1251 * If this isn't the last console and it has CON_CONSDEV set, we
1252 * need to set it on the next preferred console.
1254 if (console_drivers != NULL && console->flags & CON_CONSDEV)
1255 console_drivers->flags |= CON_CONSDEV;
1257 release_console_sem();
1258 return res;
1260 EXPORT_SYMBOL(unregister_console);
1262 static int __init disable_boot_consoles(void)
1264 if (console_drivers != NULL) {
1265 if (console_drivers->flags & CON_BOOT) {
1266 printk(KERN_INFO "turn off boot console %s%d\n",
1267 console_drivers->name, console_drivers->index);
1268 return unregister_console(console_drivers);
1271 return 0;
1273 late_initcall(disable_boot_consoles);
1275 #if defined CONFIG_PRINTK
1278 * printk rate limiting, lifted from the networking subsystem.
1280 * This enforces a rate limit: not more than 10 kernel messages
1281 * every 5s to make a denial-of-service attack impossible.
1283 DEFINE_RATELIMIT_STATE(printk_ratelimit_state, 5 * HZ, 10);
1285 int printk_ratelimit(void)
1287 return __ratelimit(&printk_ratelimit_state);
1289 EXPORT_SYMBOL(printk_ratelimit);
1292 * printk_timed_ratelimit - caller-controlled printk ratelimiting
1293 * @caller_jiffies: pointer to caller's state
1294 * @interval_msecs: minimum interval between prints
1296 * printk_timed_ratelimit() returns true if more than @interval_msecs
1297 * milliseconds have elapsed since the last time printk_timed_ratelimit()
1298 * returned true.
1300 bool printk_timed_ratelimit(unsigned long *caller_jiffies,
1301 unsigned int interval_msecs)
1303 if (*caller_jiffies == 0 || time_after(jiffies, *caller_jiffies)) {
1304 *caller_jiffies = jiffies + msecs_to_jiffies(interval_msecs);
1305 return true;
1307 return false;
1309 EXPORT_SYMBOL(printk_timed_ratelimit);
1310 #endif