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