OSDN Git Service

HID: hid-multitouch: add support for a new Lumio dual-touch panel
[android-x86/kernel.git] / kernel / printk.c
1 /*
2  *  linux/kernel/printk.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  *
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
17  */
18
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/kexec.h>
36 #include <linux/kdb.h>
37 #include <linux/ratelimit.h>
38 #include <linux/kmsg_dump.h>
39 #include <linux/syslog.h>
40 #include <linux/cpu.h>
41 #include <linux/notifier.h>
42 #include <linux/rculist.h>
43
44 #include <asm/uaccess.h>
45
46 /*
47  * Architectures can override it:
48  */
49 void asmlinkage __attribute__((weak)) early_printk(const char *fmt, ...)
50 {
51 }
52
53 #define __LOG_BUF_LEN   (1 << CONFIG_LOG_BUF_SHIFT)
54
55 #ifdef        CONFIG_DEBUG_LL
56 extern void printascii(char *);
57 #endif
58
59 /* printk's without a loglevel use this.. */
60 #define DEFAULT_MESSAGE_LOGLEVEL 4 /* KERN_WARNING */
61
62 /* We show everything that is MORE important than this.. */
63 #define MINIMUM_CONSOLE_LOGLEVEL 1 /* Minimum loglevel we let people use */
64 #define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */
65
66 DECLARE_WAIT_QUEUE_HEAD(log_wait);
67
68 int console_printk[4] = {
69         DEFAULT_CONSOLE_LOGLEVEL,       /* console_loglevel */
70         DEFAULT_MESSAGE_LOGLEVEL,       /* default_message_loglevel */
71         MINIMUM_CONSOLE_LOGLEVEL,       /* minimum_console_loglevel */
72         DEFAULT_CONSOLE_LOGLEVEL,       /* default_console_loglevel */
73 };
74
75 /*
76  * Low level drivers may need that to know if they can schedule in
77  * their unblank() callback or not. So let's export it.
78  */
79 int oops_in_progress;
80 EXPORT_SYMBOL(oops_in_progress);
81
82 /*
83  * console_sem protects the console_drivers list, and also
84  * provides serialisation for access to the entire console
85  * driver system.
86  */
87 static DEFINE_SEMAPHORE(console_sem);
88 struct console *console_drivers;
89 EXPORT_SYMBOL_GPL(console_drivers);
90
91 /*
92  * This is used for debugging the mess that is the VT code by
93  * keeping track if we have the console semaphore held. It's
94  * definitely not the perfect debug tool (we don't know if _WE_
95  * hold it are racing, but it helps tracking those weird code
96  * path in the console code where we end up in places I want
97  * locked without the console sempahore held
98  */
99 static int console_locked, console_suspended;
100
101 /*
102  * logbuf_lock protects log_buf, log_start, log_end, con_start and logged_chars
103  * It is also used in interesting ways to provide interlocking in
104  * console_unlock();.
105  */
106 static DEFINE_SPINLOCK(logbuf_lock);
107
108 #define LOG_BUF_MASK (log_buf_len-1)
109 #define LOG_BUF(idx) (log_buf[(idx) & LOG_BUF_MASK])
110
111 /*
112  * The indices into log_buf are not constrained to log_buf_len - they
113  * must be masked before subscripting
114  */
115 static unsigned log_start;      /* Index into log_buf: next char to be read by syslog() */
116 static unsigned con_start;      /* Index into log_buf: next char to be sent to consoles */
117 static unsigned log_end;        /* Index into log_buf: most-recently-written-char + 1 */
118
119 /*
120  *      Array of consoles built from command line options (console=)
121  */
122 struct console_cmdline
123 {
124         char    name[8];                        /* Name of the driver       */
125         int     index;                          /* Minor dev. to use        */
126         char    *options;                       /* Options for the driver   */
127 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
128         char    *brl_options;                   /* Options for braille driver */
129 #endif
130 };
131
132 #define MAX_CMDLINECONSOLES 8
133
134 static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
135 static int selected_console = -1;
136 static int preferred_console = -1;
137 int console_set_on_cmdline;
138 EXPORT_SYMBOL(console_set_on_cmdline);
139
140 /* Flag: console code may call schedule() */
141 static int console_may_schedule;
142
143 #ifdef CONFIG_PRINTK
144
145 static char __log_buf[__LOG_BUF_LEN];
146 static char *log_buf = __log_buf;
147 static int log_buf_len = __LOG_BUF_LEN;
148 static unsigned logged_chars; /* Number of chars produced since last read+clear operation */
149 static int saved_console_loglevel = -1;
150
151 #ifdef CONFIG_KEXEC
152 /*
153  * This appends the listed symbols to /proc/vmcoreinfo
154  *
155  * /proc/vmcoreinfo is used by various utiilties, like crash and makedumpfile to
156  * obtain access to symbols that are otherwise very difficult to locate.  These
157  * symbols are specifically used so that utilities can access and extract the
158  * dmesg log from a vmcore file after a crash.
159  */
160 void log_buf_kexec_setup(void)
161 {
162         VMCOREINFO_SYMBOL(log_buf);
163         VMCOREINFO_SYMBOL(log_end);
164         VMCOREINFO_SYMBOL(log_buf_len);
165         VMCOREINFO_SYMBOL(logged_chars);
166 }
167 #endif
168
169 static int __init log_buf_len_setup(char *str)
170 {
171         unsigned size = memparse(str, &str);
172         unsigned long flags;
173
174         if (size)
175                 size = roundup_pow_of_two(size);
176         if (size > log_buf_len) {
177                 unsigned start, dest_idx, offset;
178                 char *new_log_buf;
179
180                 new_log_buf = alloc_bootmem(size);
181                 if (!new_log_buf) {
182                         printk(KERN_WARNING "log_buf_len: allocation failed\n");
183                         goto out;
184                 }
185
186                 spin_lock_irqsave(&logbuf_lock, flags);
187                 log_buf_len = size;
188                 log_buf = new_log_buf;
189
190                 offset = start = min(con_start, log_start);
191                 dest_idx = 0;
192                 while (start != log_end) {
193                         log_buf[dest_idx] = __log_buf[start & (__LOG_BUF_LEN - 1)];
194                         start++;
195                         dest_idx++;
196                 }
197                 log_start -= offset;
198                 con_start -= offset;
199                 log_end -= offset;
200                 spin_unlock_irqrestore(&logbuf_lock, flags);
201
202                 printk(KERN_NOTICE "log_buf_len: %d\n", log_buf_len);
203         }
204 out:
205         return 1;
206 }
207
208 __setup("log_buf_len=", log_buf_len_setup);
209
210 #ifdef CONFIG_BOOT_PRINTK_DELAY
211
212 static int boot_delay; /* msecs delay after each printk during bootup */
213 static unsigned long long loops_per_msec;       /* based on boot_delay */
214
215 static int __init boot_delay_setup(char *str)
216 {
217         unsigned long lpj;
218
219         lpj = preset_lpj ? preset_lpj : 1000000;        /* some guess */
220         loops_per_msec = (unsigned long long)lpj / 1000 * HZ;
221
222         get_option(&str, &boot_delay);
223         if (boot_delay > 10 * 1000)
224                 boot_delay = 0;
225
226         pr_debug("boot_delay: %u, preset_lpj: %ld, lpj: %lu, "
227                 "HZ: %d, loops_per_msec: %llu\n",
228                 boot_delay, preset_lpj, lpj, HZ, loops_per_msec);
229         return 1;
230 }
231 __setup("boot_delay=", boot_delay_setup);
232
233 static void boot_delay_msec(void)
234 {
235         unsigned long long k;
236         unsigned long timeout;
237
238         if (boot_delay == 0 || system_state != SYSTEM_BOOTING)
239                 return;
240
241         k = (unsigned long long)loops_per_msec * boot_delay;
242
243         timeout = jiffies + msecs_to_jiffies(boot_delay);
244         while (k) {
245                 k--;
246                 cpu_relax();
247                 /*
248                  * use (volatile) jiffies to prevent
249                  * compiler reduction; loop termination via jiffies
250                  * is secondary and may or may not happen.
251                  */
252                 if (time_after(jiffies, timeout))
253                         break;
254                 touch_nmi_watchdog();
255         }
256 }
257 #else
258 static inline void boot_delay_msec(void)
259 {
260 }
261 #endif
262
263 /*
264  * Return the number of unread characters in the log buffer.
265  */
266 static int log_buf_get_len(void)
267 {
268         return logged_chars;
269 }
270
271 /*
272  * Clears the ring-buffer
273  */
274 void log_buf_clear(void)
275 {
276         logged_chars = 0;
277 }
278
279 /*
280  * Copy a range of characters from the log buffer.
281  */
282 int log_buf_copy(char *dest, int idx, int len)
283 {
284         int ret, max;
285         bool took_lock = false;
286
287         if (!oops_in_progress) {
288                 spin_lock_irq(&logbuf_lock);
289                 took_lock = true;
290         }
291
292         max = log_buf_get_len();
293         if (idx < 0 || idx >= max) {
294                 ret = -1;
295         } else {
296                 if (len > max - idx)
297                         len = max - idx;
298                 ret = len;
299                 idx += (log_end - max);
300                 while (len-- > 0)
301                         dest[len] = LOG_BUF(idx + len);
302         }
303
304         if (took_lock)
305                 spin_unlock_irq(&logbuf_lock);
306
307         return ret;
308 }
309
310 #ifdef CONFIG_SECURITY_DMESG_RESTRICT
311 int dmesg_restrict = 1;
312 #else
313 int dmesg_restrict;
314 #endif
315
316 static int syslog_action_restricted(int type)
317 {
318         if (dmesg_restrict)
319                 return 1;
320         /* Unless restricted, we allow "read all" and "get buffer size" for everybody */
321         return type != SYSLOG_ACTION_READ_ALL && type != SYSLOG_ACTION_SIZE_BUFFER;
322 }
323
324 static int check_syslog_permissions(int type, bool from_file)
325 {
326         /*
327          * If this is from /proc/kmsg and we've already opened it, then we've
328          * already done the capabilities checks at open time.
329          */
330         if (from_file && type != SYSLOG_ACTION_OPEN)
331                 return 0;
332
333         if (syslog_action_restricted(type)) {
334                 if (capable(CAP_SYSLOG))
335                         return 0;
336                 /* For historical reasons, accept CAP_SYS_ADMIN too, with a warning */
337                 if (capable(CAP_SYS_ADMIN)) {
338                         WARN_ONCE(1, "Attempt to access syslog with CAP_SYS_ADMIN "
339                                  "but no CAP_SYSLOG (deprecated).\n");
340                         return 0;
341                 }
342                 return -EPERM;
343         }
344         return 0;
345 }
346
347 int do_syslog(int type, char __user *buf, int len, bool from_file)
348 {
349         unsigned i, j, limit, count;
350         int do_clear = 0;
351         char c;
352         int error;
353
354         error = check_syslog_permissions(type, from_file);
355         if (error)
356                 goto out;
357
358         error = security_syslog(type);
359         if (error)
360                 return error;
361
362         switch (type) {
363         case SYSLOG_ACTION_CLOSE:       /* Close log */
364                 break;
365         case SYSLOG_ACTION_OPEN:        /* Open log */
366                 break;
367         case SYSLOG_ACTION_READ:        /* Read from log */
368                 error = -EINVAL;
369                 if (!buf || len < 0)
370                         goto out;
371                 error = 0;
372                 if (!len)
373                         goto out;
374                 if (!access_ok(VERIFY_WRITE, buf, len)) {
375                         error = -EFAULT;
376                         goto out;
377                 }
378                 error = wait_event_interruptible(log_wait,
379                                                         (log_start - log_end));
380                 if (error)
381                         goto out;
382                 i = 0;
383                 spin_lock_irq(&logbuf_lock);
384                 while (!error && (log_start != log_end) && i < len) {
385                         c = LOG_BUF(log_start);
386                         log_start++;
387                         spin_unlock_irq(&logbuf_lock);
388                         error = __put_user(c,buf);
389                         buf++;
390                         i++;
391                         cond_resched();
392                         spin_lock_irq(&logbuf_lock);
393                 }
394                 spin_unlock_irq(&logbuf_lock);
395                 if (!error)
396                         error = i;
397                 break;
398         /* Read/clear last kernel messages */
399         case SYSLOG_ACTION_READ_CLEAR:
400                 do_clear = 1;
401                 /* FALL THRU */
402         /* Read last kernel messages */
403         case SYSLOG_ACTION_READ_ALL:
404                 error = -EINVAL;
405                 if (!buf || len < 0)
406                         goto out;
407                 error = 0;
408                 if (!len)
409                         goto out;
410                 if (!access_ok(VERIFY_WRITE, buf, len)) {
411                         error = -EFAULT;
412                         goto out;
413                 }
414                 count = len;
415                 if (count > log_buf_len)
416                         count = log_buf_len;
417                 spin_lock_irq(&logbuf_lock);
418                 if (count > logged_chars)
419                         count = logged_chars;
420                 if (do_clear)
421                         logged_chars = 0;
422                 limit = log_end;
423                 /*
424                  * __put_user() could sleep, and while we sleep
425                  * printk() could overwrite the messages
426                  * we try to copy to user space. Therefore
427                  * the messages are copied in reverse. <manfreds>
428                  */
429                 for (i = 0; i < count && !error; i++) {
430                         j = limit-1-i;
431                         if (j + log_buf_len < log_end)
432                                 break;
433                         c = LOG_BUF(j);
434                         spin_unlock_irq(&logbuf_lock);
435                         error = __put_user(c,&buf[count-1-i]);
436                         cond_resched();
437                         spin_lock_irq(&logbuf_lock);
438                 }
439                 spin_unlock_irq(&logbuf_lock);
440                 if (error)
441                         break;
442                 error = i;
443                 if (i != count) {
444                         int offset = count-error;
445                         /* buffer overflow during copy, correct user buffer. */
446                         for (i = 0; i < error; i++) {
447                                 if (__get_user(c,&buf[i+offset]) ||
448                                     __put_user(c,&buf[i])) {
449                                         error = -EFAULT;
450                                         break;
451                                 }
452                                 cond_resched();
453                         }
454                 }
455                 break;
456         /* Clear ring buffer */
457         case SYSLOG_ACTION_CLEAR:
458                 logged_chars = 0;
459                 break;
460         /* Disable logging to console */
461         case SYSLOG_ACTION_CONSOLE_OFF:
462                 if (saved_console_loglevel == -1)
463                         saved_console_loglevel = console_loglevel;
464                 console_loglevel = minimum_console_loglevel;
465                 break;
466         /* Enable logging to console */
467         case SYSLOG_ACTION_CONSOLE_ON:
468                 if (saved_console_loglevel != -1) {
469                         console_loglevel = saved_console_loglevel;
470                         saved_console_loglevel = -1;
471                 }
472                 break;
473         /* Set level of messages printed to console */
474         case SYSLOG_ACTION_CONSOLE_LEVEL:
475                 error = -EINVAL;
476                 if (len < 1 || len > 8)
477                         goto out;
478                 if (len < minimum_console_loglevel)
479                         len = minimum_console_loglevel;
480                 console_loglevel = len;
481                 /* Implicitly re-enable logging to console */
482                 saved_console_loglevel = -1;
483                 error = 0;
484                 break;
485         /* Number of chars in the log buffer */
486         case SYSLOG_ACTION_SIZE_UNREAD:
487                 error = log_end - log_start;
488                 break;
489         /* Size of the log buffer */
490         case SYSLOG_ACTION_SIZE_BUFFER:
491                 error = log_buf_len;
492                 break;
493         default:
494                 error = -EINVAL;
495                 break;
496         }
497 out:
498         return error;
499 }
500
501 SYSCALL_DEFINE3(syslog, int, type, char __user *, buf, int, len)
502 {
503         return do_syslog(type, buf, len, SYSLOG_FROM_CALL);
504 }
505
506 #ifdef  CONFIG_KGDB_KDB
507 /* kdb dmesg command needs access to the syslog buffer.  do_syslog()
508  * uses locks so it cannot be used during debugging.  Just tell kdb
509  * where the start and end of the physical and logical logs are.  This
510  * is equivalent to do_syslog(3).
511  */
512 void kdb_syslog_data(char *syslog_data[4])
513 {
514         syslog_data[0] = log_buf;
515         syslog_data[1] = log_buf + log_buf_len;
516         syslog_data[2] = log_buf + log_end -
517                 (logged_chars < log_buf_len ? logged_chars : log_buf_len);
518         syslog_data[3] = log_buf + log_end;
519 }
520 #endif  /* CONFIG_KGDB_KDB */
521
522 /*
523  * Call the console drivers on a range of log_buf
524  */
525 static void __call_console_drivers(unsigned start, unsigned end)
526 {
527         struct console *con;
528
529         for_each_console(con) {
530                 if ((con->flags & CON_ENABLED) && con->write &&
531                                 (cpu_online(smp_processor_id()) ||
532                                 (con->flags & CON_ANYTIME)))
533                         con->write(con, &LOG_BUF(start), end - start);
534         }
535 }
536
537 static int __read_mostly ignore_loglevel;
538
539 static int __init ignore_loglevel_setup(char *str)
540 {
541         ignore_loglevel = 1;
542         printk(KERN_INFO "debug: ignoring loglevel setting.\n");
543
544         return 0;
545 }
546
547 early_param("ignore_loglevel", ignore_loglevel_setup);
548
549 /*
550  * Write out chars from start to end - 1 inclusive
551  */
552 static void _call_console_drivers(unsigned start,
553                                 unsigned end, int msg_log_level)
554 {
555         if ((msg_log_level < console_loglevel || ignore_loglevel) &&
556                         console_drivers && start != end) {
557                 if ((start & LOG_BUF_MASK) > (end & LOG_BUF_MASK)) {
558                         /* wrapped write */
559                         __call_console_drivers(start & LOG_BUF_MASK,
560                                                 log_buf_len);
561                         __call_console_drivers(0, end & LOG_BUF_MASK);
562                 } else {
563                         __call_console_drivers(start, end);
564                 }
565         }
566 }
567
568 /*
569  * Call the console drivers, asking them to write out
570  * log_buf[start] to log_buf[end - 1].
571  * The console_lock must be held.
572  */
573 static void call_console_drivers(unsigned start, unsigned end)
574 {
575         unsigned cur_index, start_print;
576         static int msg_level = -1;
577
578         BUG_ON(((int)(start - end)) > 0);
579
580         cur_index = start;
581         start_print = start;
582         while (cur_index != end) {
583                 if (msg_level < 0 && ((end - cur_index) > 2) &&
584                                 LOG_BUF(cur_index + 0) == '<' &&
585                                 LOG_BUF(cur_index + 1) >= '0' &&
586                                 LOG_BUF(cur_index + 1) <= '7' &&
587                                 LOG_BUF(cur_index + 2) == '>') {
588                         msg_level = LOG_BUF(cur_index + 1) - '0';
589                         cur_index += 3;
590                         start_print = cur_index;
591                 }
592                 while (cur_index != end) {
593                         char c = LOG_BUF(cur_index);
594
595                         cur_index++;
596                         if (c == '\n') {
597                                 if (msg_level < 0) {
598                                         /*
599                                          * printk() has already given us loglevel tags in
600                                          * the buffer.  This code is here in case the
601                                          * log buffer has wrapped right round and scribbled
602                                          * on those tags
603                                          */
604                                         msg_level = default_message_loglevel;
605                                 }
606                                 _call_console_drivers(start_print, cur_index, msg_level);
607                                 msg_level = -1;
608                                 start_print = cur_index;
609                                 break;
610                         }
611                 }
612         }
613         _call_console_drivers(start_print, end, msg_level);
614 }
615
616 static void emit_log_char(char c)
617 {
618         LOG_BUF(log_end) = c;
619         log_end++;
620         if (log_end - log_start > log_buf_len)
621                 log_start = log_end - log_buf_len;
622         if (log_end - con_start > log_buf_len)
623                 con_start = log_end - log_buf_len;
624         if (logged_chars < log_buf_len)
625                 logged_chars++;
626 }
627
628 /*
629  * Zap console related locks when oopsing. Only zap at most once
630  * every 10 seconds, to leave time for slow consoles to print a
631  * full oops.
632  */
633 static void zap_locks(void)
634 {
635         static unsigned long oops_timestamp;
636
637         if (time_after_eq(jiffies, oops_timestamp) &&
638                         !time_after(jiffies, oops_timestamp + 30 * HZ))
639                 return;
640
641         oops_timestamp = jiffies;
642
643         /* If a crash is occurring, make sure we can't deadlock */
644         spin_lock_init(&logbuf_lock);
645         /* And make sure that we print immediately */
646         sema_init(&console_sem, 1);
647 }
648
649 #if defined(CONFIG_PRINTK_TIME)
650 static int printk_time = 1;
651 #else
652 static int printk_time = 0;
653 #endif
654 module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR);
655
656 /* Check if we have any console registered that can be called early in boot. */
657 static int have_callable_console(void)
658 {
659         struct console *con;
660
661         for_each_console(con)
662                 if (con->flags & CON_ANYTIME)
663                         return 1;
664
665         return 0;
666 }
667
668 /**
669  * printk - print a kernel message
670  * @fmt: format string
671  *
672  * This is printk().  It can be called from any context.  We want it to work.
673  *
674  * We try to grab the console_lock.  If we succeed, it's easy - we log the output and
675  * call the console drivers.  If we fail to get the semaphore we place the output
676  * into the log buffer and return.  The current holder of the console_sem will
677  * notice the new output in console_unlock(); and will send it to the
678  * consoles before releasing the lock.
679  *
680  * One effect of this deferred printing is that code which calls printk() and
681  * then changes console_loglevel may break. This is because console_loglevel
682  * is inspected when the actual printing occurs.
683  *
684  * See also:
685  * printf(3)
686  *
687  * See the vsnprintf() documentation for format string extensions over C99.
688  */
689
690 asmlinkage int printk(const char *fmt, ...)
691 {
692         va_list args;
693         int r;
694
695 #ifdef CONFIG_KGDB_KDB
696         if (unlikely(kdb_trap_printk)) {
697                 va_start(args, fmt);
698                 r = vkdb_printf(fmt, args);
699                 va_end(args);
700                 return r;
701         }
702 #endif
703         va_start(args, fmt);
704         r = vprintk(fmt, args);
705         va_end(args);
706
707         return r;
708 }
709
710 /* cpu currently holding logbuf_lock */
711 static volatile unsigned int printk_cpu = UINT_MAX;
712
713 /*
714  * Can we actually use the console at this time on this cpu?
715  *
716  * Console drivers may assume that per-cpu resources have
717  * been allocated. So unless they're explicitly marked as
718  * being able to cope (CON_ANYTIME) don't call them until
719  * this CPU is officially up.
720  */
721 static inline int can_use_console(unsigned int cpu)
722 {
723         return cpu_online(cpu) || have_callable_console();
724 }
725
726 /*
727  * Try to get console ownership to actually show the kernel
728  * messages from a 'printk'. Return true (and with the
729  * console_lock held, and 'console_locked' set) if it
730  * is successful, false otherwise.
731  *
732  * This gets called with the 'logbuf_lock' spinlock held and
733  * interrupts disabled. It should return with 'lockbuf_lock'
734  * released but interrupts still disabled.
735  */
736 static int console_trylock_for_printk(unsigned int cpu)
737         __releases(&logbuf_lock)
738 {
739         int retval = 0;
740
741         if (console_trylock()) {
742                 retval = 1;
743
744                 /*
745                  * If we can't use the console, we need to release
746                  * the console semaphore by hand to avoid flushing
747                  * the buffer. We need to hold the console semaphore
748                  * in order to do this test safely.
749                  */
750                 if (!can_use_console(cpu)) {
751                         console_locked = 0;
752                         up(&console_sem);
753                         retval = 0;
754                 }
755         }
756         printk_cpu = UINT_MAX;
757         spin_unlock(&logbuf_lock);
758         return retval;
759 }
760 static const char recursion_bug_msg [] =
761                 KERN_CRIT "BUG: recent printk recursion!\n";
762 static int recursion_bug;
763 static int new_text_line = 1;
764 static char printk_buf[1024];
765
766 int printk_delay_msec __read_mostly;
767
768 static inline void printk_delay(void)
769 {
770         if (unlikely(printk_delay_msec)) {
771                 int m = printk_delay_msec;
772
773                 while (m--) {
774                         mdelay(1);
775                         touch_nmi_watchdog();
776                 }
777         }
778 }
779
780 asmlinkage int vprintk(const char *fmt, va_list args)
781 {
782         int printed_len = 0;
783         int current_log_level = default_message_loglevel;
784         unsigned long flags;
785         int this_cpu;
786         char *p;
787
788         boot_delay_msec();
789         printk_delay();
790
791         preempt_disable();
792         /* This stops the holder of console_sem just where we want him */
793         raw_local_irq_save(flags);
794         this_cpu = smp_processor_id();
795
796         /*
797          * Ouch, printk recursed into itself!
798          */
799         if (unlikely(printk_cpu == this_cpu)) {
800                 /*
801                  * If a crash is occurring during printk() on this CPU,
802                  * then try to get the crash message out but make sure
803                  * we can't deadlock. Otherwise just return to avoid the
804                  * recursion and return - but flag the recursion so that
805                  * it can be printed at the next appropriate moment:
806                  */
807                 if (!oops_in_progress) {
808                         recursion_bug = 1;
809                         goto out_restore_irqs;
810                 }
811                 zap_locks();
812         }
813
814         lockdep_off();
815         spin_lock(&logbuf_lock);
816         printk_cpu = this_cpu;
817
818         if (recursion_bug) {
819                 recursion_bug = 0;
820                 strcpy(printk_buf, recursion_bug_msg);
821                 printed_len = strlen(recursion_bug_msg);
822         }
823         /* Emit the output into the temporary buffer */
824         printed_len += vscnprintf(printk_buf + printed_len,
825                                   sizeof(printk_buf) - printed_len, fmt, args);
826
827 #ifdef  CONFIG_DEBUG_LL
828         printascii(printk_buf);
829 #endif
830
831         p = printk_buf;
832
833         /* Do we have a loglevel in the string? */
834         if (p[0] == '<') {
835                 unsigned char c = p[1];
836                 if (c && p[2] == '>') {
837                         switch (c) {
838                         case '0' ... '7': /* loglevel */
839                                 current_log_level = c - '0';
840                         /* Fallthrough - make sure we're on a new line */
841                         case 'd': /* KERN_DEFAULT */
842                                 if (!new_text_line) {
843                                         emit_log_char('\n');
844                                         new_text_line = 1;
845                                 }
846                         /* Fallthrough - skip the loglevel */
847                         case 'c': /* KERN_CONT */
848                                 p += 3;
849                                 break;
850                         }
851                 }
852         }
853
854         /*
855          * Copy the output into log_buf.  If the caller didn't provide
856          * appropriate log level tags, we insert them here
857          */
858         for ( ; *p; p++) {
859                 if (new_text_line) {
860                         /* Always output the token */
861                         emit_log_char('<');
862                         emit_log_char(current_log_level + '0');
863                         emit_log_char('>');
864                         printed_len += 3;
865                         new_text_line = 0;
866
867                         if (printk_time) {
868                                 /* Follow the token with the time */
869                                 char tbuf[50], *tp;
870                                 unsigned tlen;
871                                 unsigned long long t;
872                                 unsigned long nanosec_rem;
873
874                                 t = cpu_clock(printk_cpu);
875                                 nanosec_rem = do_div(t, 1000000000);
876                                 tlen = sprintf(tbuf, "[%5lu.%06lu] ",
877                                                 (unsigned long) t,
878                                                 nanosec_rem / 1000);
879
880                                 for (tp = tbuf; tp < tbuf + tlen; tp++)
881                                         emit_log_char(*tp);
882                                 printed_len += tlen;
883                         }
884
885                         if (!*p)
886                                 break;
887                 }
888
889                 emit_log_char(*p);
890                 if (*p == '\n')
891                         new_text_line = 1;
892         }
893
894         /*
895          * Try to acquire and then immediately release the
896          * console semaphore. The release will do all the
897          * actual magic (print out buffers, wake up klogd,
898          * etc). 
899          *
900          * The console_trylock_for_printk() function
901          * will release 'logbuf_lock' regardless of whether it
902          * actually gets the semaphore or not.
903          */
904         if (console_trylock_for_printk(this_cpu))
905                 console_unlock();
906
907         lockdep_on();
908 out_restore_irqs:
909         raw_local_irq_restore(flags);
910
911         preempt_enable();
912         return printed_len;
913 }
914 EXPORT_SYMBOL(printk);
915 EXPORT_SYMBOL(vprintk);
916
917 #else
918
919 static void call_console_drivers(unsigned start, unsigned end)
920 {
921 }
922
923 #endif
924
925 static int __add_preferred_console(char *name, int idx, char *options,
926                                    char *brl_options)
927 {
928         struct console_cmdline *c;
929         int i;
930
931         /*
932          *      See if this tty is not yet registered, and
933          *      if we have a slot free.
934          */
935         for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
936                 if (strcmp(console_cmdline[i].name, name) == 0 &&
937                           console_cmdline[i].index == idx) {
938                                 if (!brl_options)
939                                         selected_console = i;
940                                 return 0;
941                 }
942         if (i == MAX_CMDLINECONSOLES)
943                 return -E2BIG;
944         if (!brl_options)
945                 selected_console = i;
946         c = &console_cmdline[i];
947         strlcpy(c->name, name, sizeof(c->name));
948         c->options = options;
949 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
950         c->brl_options = brl_options;
951 #endif
952         c->index = idx;
953         return 0;
954 }
955 /*
956  * Set up a list of consoles.  Called from init/main.c
957  */
958 static int __init console_setup(char *str)
959 {
960         char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for index */
961         char *s, *options, *brl_options = NULL;
962         int idx;
963
964 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
965         if (!memcmp(str, "brl,", 4)) {
966                 brl_options = "";
967                 str += 4;
968         } else if (!memcmp(str, "brl=", 4)) {
969                 brl_options = str + 4;
970                 str = strchr(brl_options, ',');
971                 if (!str) {
972                         printk(KERN_ERR "need port name after brl=\n");
973                         return 1;
974                 }
975                 *(str++) = 0;
976         }
977 #endif
978
979         /*
980          * Decode str into name, index, options.
981          */
982         if (str[0] >= '0' && str[0] <= '9') {
983                 strcpy(buf, "ttyS");
984                 strncpy(buf + 4, str, sizeof(buf) - 5);
985         } else {
986                 strncpy(buf, str, sizeof(buf) - 1);
987         }
988         buf[sizeof(buf) - 1] = 0;
989         if ((options = strchr(str, ',')) != NULL)
990                 *(options++) = 0;
991 #ifdef __sparc__
992         if (!strcmp(str, "ttya"))
993                 strcpy(buf, "ttyS0");
994         if (!strcmp(str, "ttyb"))
995                 strcpy(buf, "ttyS1");
996 #endif
997         for (s = buf; *s; s++)
998                 if ((*s >= '0' && *s <= '9') || *s == ',')
999                         break;
1000         idx = simple_strtoul(s, NULL, 10);
1001         *s = 0;
1002
1003         __add_preferred_console(buf, idx, options, brl_options);
1004         console_set_on_cmdline = 1;
1005         return 1;
1006 }
1007 __setup("console=", console_setup);
1008
1009 /**
1010  * add_preferred_console - add a device to the list of preferred consoles.
1011  * @name: device name
1012  * @idx: device index
1013  * @options: options for this console
1014  *
1015  * The last preferred console added will be used for kernel messages
1016  * and stdin/out/err for init.  Normally this is used by console_setup
1017  * above to handle user-supplied console arguments; however it can also
1018  * be used by arch-specific code either to override the user or more
1019  * commonly to provide a default console (ie from PROM variables) when
1020  * the user has not supplied one.
1021  */
1022 int add_preferred_console(char *name, int idx, char *options)
1023 {
1024         return __add_preferred_console(name, idx, options, NULL);
1025 }
1026
1027 int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options)
1028 {
1029         struct console_cmdline *c;
1030         int i;
1031
1032         for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
1033                 if (strcmp(console_cmdline[i].name, name) == 0 &&
1034                           console_cmdline[i].index == idx) {
1035                                 c = &console_cmdline[i];
1036                                 strlcpy(c->name, name_new, sizeof(c->name));
1037                                 c->name[sizeof(c->name) - 1] = 0;
1038                                 c->options = options;
1039                                 c->index = idx_new;
1040                                 return i;
1041                 }
1042         /* not found */
1043         return -1;
1044 }
1045
1046 int console_suspend_enabled = 1;
1047 EXPORT_SYMBOL(console_suspend_enabled);
1048
1049 static int __init console_suspend_disable(char *str)
1050 {
1051         console_suspend_enabled = 0;
1052         return 1;
1053 }
1054 __setup("no_console_suspend", console_suspend_disable);
1055
1056 /**
1057  * suspend_console - suspend the console subsystem
1058  *
1059  * This disables printk() while we go into suspend states
1060  */
1061 void suspend_console(void)
1062 {
1063         if (!console_suspend_enabled)
1064                 return;
1065         printk("Suspending console(s) (use no_console_suspend to debug)\n");
1066         console_lock();
1067         console_suspended = 1;
1068         up(&console_sem);
1069 }
1070
1071 void resume_console(void)
1072 {
1073         if (!console_suspend_enabled)
1074                 return;
1075         down(&console_sem);
1076         console_suspended = 0;
1077         console_unlock();
1078 }
1079
1080 /**
1081  * console_cpu_notify - print deferred console messages after CPU hotplug
1082  * @self: notifier struct
1083  * @action: CPU hotplug event
1084  * @hcpu: unused
1085  *
1086  * If printk() is called from a CPU that is not online yet, the messages
1087  * will be spooled but will not show up on the console.  This function is
1088  * called when a new CPU comes online (or fails to come up), and ensures
1089  * that any such output gets printed.
1090  */
1091 static int __cpuinit console_cpu_notify(struct notifier_block *self,
1092         unsigned long action, void *hcpu)
1093 {
1094         switch (action) {
1095         case CPU_ONLINE:
1096         case CPU_DEAD:
1097         case CPU_DYING:
1098         case CPU_DOWN_FAILED:
1099         case CPU_UP_CANCELED:
1100                 console_lock();
1101                 console_unlock();
1102         }
1103         return NOTIFY_OK;
1104 }
1105
1106 /**
1107  * console_lock - lock the console system for exclusive use.
1108  *
1109  * Acquires a lock which guarantees that the caller has
1110  * exclusive access to the console system and the console_drivers list.
1111  *
1112  * Can sleep, returns nothing.
1113  */
1114 void console_lock(void)
1115 {
1116         BUG_ON(in_interrupt());
1117         down(&console_sem);
1118         if (console_suspended)
1119                 return;
1120         console_locked = 1;
1121         console_may_schedule = 1;
1122 }
1123 EXPORT_SYMBOL(console_lock);
1124
1125 /**
1126  * console_trylock - try to lock the console system for exclusive use.
1127  *
1128  * Tried to acquire a lock which guarantees that the caller has
1129  * exclusive access to the console system and the console_drivers list.
1130  *
1131  * returns 1 on success, and 0 on failure to acquire the lock.
1132  */
1133 int console_trylock(void)
1134 {
1135         if (down_trylock(&console_sem))
1136                 return 0;
1137         if (console_suspended) {
1138                 up(&console_sem);
1139                 return 0;
1140         }
1141         console_locked = 1;
1142         console_may_schedule = 0;
1143         return 1;
1144 }
1145 EXPORT_SYMBOL(console_trylock);
1146
1147 int is_console_locked(void)
1148 {
1149         return console_locked;
1150 }
1151
1152 static DEFINE_PER_CPU(int, printk_pending);
1153
1154 void printk_tick(void)
1155 {
1156         if (__this_cpu_read(printk_pending)) {
1157                 __this_cpu_write(printk_pending, 0);
1158                 wake_up_interruptible(&log_wait);
1159         }
1160 }
1161
1162 int printk_needs_cpu(int cpu)
1163 {
1164         if (cpu_is_offline(cpu))
1165                 printk_tick();
1166         return __this_cpu_read(printk_pending);
1167 }
1168
1169 void wake_up_klogd(void)
1170 {
1171         if (waitqueue_active(&log_wait))
1172                 this_cpu_write(printk_pending, 1);
1173 }
1174
1175 /**
1176  * console_unlock - unlock the console system
1177  *
1178  * Releases the console_lock which the caller holds on the console system
1179  * and the console driver list.
1180  *
1181  * While the console_lock was held, console output may have been buffered
1182  * by printk().  If this is the case, console_unlock(); emits
1183  * the output prior to releasing the lock.
1184  *
1185  * If there is output waiting for klogd, we wake it up.
1186  *
1187  * console_unlock(); may be called from any context.
1188  */
1189 void console_unlock(void)
1190 {
1191         unsigned long flags;
1192         unsigned _con_start, _log_end;
1193         unsigned wake_klogd = 0;
1194
1195         if (console_suspended) {
1196                 up(&console_sem);
1197                 return;
1198         }
1199
1200         console_may_schedule = 0;
1201
1202         for ( ; ; ) {
1203                 spin_lock_irqsave(&logbuf_lock, flags);
1204                 wake_klogd |= log_start - log_end;
1205                 if (con_start == log_end)
1206                         break;                  /* Nothing to print */
1207                 _con_start = con_start;
1208                 _log_end = log_end;
1209                 con_start = log_end;            /* Flush */
1210                 spin_unlock(&logbuf_lock);
1211                 stop_critical_timings();        /* don't trace print latency */
1212                 call_console_drivers(_con_start, _log_end);
1213                 start_critical_timings();
1214                 local_irq_restore(flags);
1215         }
1216         console_locked = 0;
1217         up(&console_sem);
1218         spin_unlock_irqrestore(&logbuf_lock, flags);
1219         if (wake_klogd)
1220                 wake_up_klogd();
1221 }
1222 EXPORT_SYMBOL(console_unlock);
1223
1224 /**
1225  * console_conditional_schedule - yield the CPU if required
1226  *
1227  * If the console code is currently allowed to sleep, and
1228  * if this CPU should yield the CPU to another task, do
1229  * so here.
1230  *
1231  * Must be called within console_lock();.
1232  */
1233 void __sched console_conditional_schedule(void)
1234 {
1235         if (console_may_schedule)
1236                 cond_resched();
1237 }
1238 EXPORT_SYMBOL(console_conditional_schedule);
1239
1240 void console_unblank(void)
1241 {
1242         struct console *c;
1243
1244         /*
1245          * console_unblank can no longer be called in interrupt context unless
1246          * oops_in_progress is set to 1..
1247          */
1248         if (oops_in_progress) {
1249                 if (down_trylock(&console_sem) != 0)
1250                         return;
1251         } else
1252                 console_lock();
1253
1254         console_locked = 1;
1255         console_may_schedule = 0;
1256         for_each_console(c)
1257                 if ((c->flags & CON_ENABLED) && c->unblank)
1258                         c->unblank();
1259         console_unlock();
1260 }
1261
1262 /*
1263  * Return the console tty driver structure and its associated index
1264  */
1265 struct tty_driver *console_device(int *index)
1266 {
1267         struct console *c;
1268         struct tty_driver *driver = NULL;
1269
1270         console_lock();
1271         for_each_console(c) {
1272                 if (!c->device)
1273                         continue;
1274                 driver = c->device(c, index);
1275                 if (driver)
1276                         break;
1277         }
1278         console_unlock();
1279         return driver;
1280 }
1281
1282 /*
1283  * Prevent further output on the passed console device so that (for example)
1284  * serial drivers can disable console output before suspending a port, and can
1285  * re-enable output afterwards.
1286  */
1287 void console_stop(struct console *console)
1288 {
1289         console_lock();
1290         console->flags &= ~CON_ENABLED;
1291         console_unlock();
1292 }
1293 EXPORT_SYMBOL(console_stop);
1294
1295 void console_start(struct console *console)
1296 {
1297         console_lock();
1298         console->flags |= CON_ENABLED;
1299         console_unlock();
1300 }
1301 EXPORT_SYMBOL(console_start);
1302
1303 /*
1304  * The console driver calls this routine during kernel initialization
1305  * to register the console printing procedure with printk() and to
1306  * print any messages that were printed by the kernel before the
1307  * console driver was initialized.
1308  *
1309  * This can happen pretty early during the boot process (because of
1310  * early_printk) - sometimes before setup_arch() completes - be careful
1311  * of what kernel features are used - they may not be initialised yet.
1312  *
1313  * There are two types of consoles - bootconsoles (early_printk) and
1314  * "real" consoles (everything which is not a bootconsole) which are
1315  * handled differently.
1316  *  - Any number of bootconsoles can be registered at any time.
1317  *  - As soon as a "real" console is registered, all bootconsoles
1318  *    will be unregistered automatically.
1319  *  - Once a "real" console is registered, any attempt to register a
1320  *    bootconsoles will be rejected
1321  */
1322 void register_console(struct console *newcon)
1323 {
1324         int i;
1325         unsigned long flags;
1326         struct console *bcon = NULL;
1327
1328         /*
1329          * before we register a new CON_BOOT console, make sure we don't
1330          * already have a valid console
1331          */
1332         if (console_drivers && newcon->flags & CON_BOOT) {
1333                 /* find the last or real console */
1334                 for_each_console(bcon) {
1335                         if (!(bcon->flags & CON_BOOT)) {
1336                                 printk(KERN_INFO "Too late to register bootconsole %s%d\n",
1337                                         newcon->name, newcon->index);
1338                                 return;
1339                         }
1340                 }
1341         }
1342
1343         if (console_drivers && console_drivers->flags & CON_BOOT)
1344                 bcon = console_drivers;
1345
1346         if (preferred_console < 0 || bcon || !console_drivers)
1347                 preferred_console = selected_console;
1348
1349         if (newcon->early_setup)
1350                 newcon->early_setup();
1351
1352         /*
1353          *      See if we want to use this console driver. If we
1354          *      didn't select a console we take the first one
1355          *      that registers here.
1356          */
1357         if (preferred_console < 0) {
1358                 if (newcon->index < 0)
1359                         newcon->index = 0;
1360                 if (newcon->setup == NULL ||
1361                     newcon->setup(newcon, NULL) == 0) {
1362                         newcon->flags |= CON_ENABLED;
1363                         if (newcon->device) {
1364                                 newcon->flags |= CON_CONSDEV;
1365                                 preferred_console = 0;
1366                         }
1367                 }
1368         }
1369
1370         /*
1371          *      See if this console matches one we selected on
1372          *      the command line.
1373          */
1374         for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0];
1375                         i++) {
1376                 if (strcmp(console_cmdline[i].name, newcon->name) != 0)
1377                         continue;
1378                 if (newcon->index >= 0 &&
1379                     newcon->index != console_cmdline[i].index)
1380                         continue;
1381                 if (newcon->index < 0)
1382                         newcon->index = console_cmdline[i].index;
1383 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1384                 if (console_cmdline[i].brl_options) {
1385                         newcon->flags |= CON_BRL;
1386                         braille_register_console(newcon,
1387                                         console_cmdline[i].index,
1388                                         console_cmdline[i].options,
1389                                         console_cmdline[i].brl_options);
1390                         return;
1391                 }
1392 #endif
1393                 if (newcon->setup &&
1394                     newcon->setup(newcon, console_cmdline[i].options) != 0)
1395                         break;
1396                 newcon->flags |= CON_ENABLED;
1397                 newcon->index = console_cmdline[i].index;
1398                 if (i == selected_console) {
1399                         newcon->flags |= CON_CONSDEV;
1400                         preferred_console = selected_console;
1401                 }
1402                 break;
1403         }
1404
1405         if (!(newcon->flags & CON_ENABLED))
1406                 return;
1407
1408         /*
1409          * If we have a bootconsole, and are switching to a real console,
1410          * don't print everything out again, since when the boot console, and
1411          * the real console are the same physical device, it's annoying to
1412          * see the beginning boot messages twice
1413          */
1414         if (bcon && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV))
1415                 newcon->flags &= ~CON_PRINTBUFFER;
1416
1417         /*
1418          *      Put this console in the list - keep the
1419          *      preferred driver at the head of the list.
1420          */
1421         console_lock();
1422         if ((newcon->flags & CON_CONSDEV) || console_drivers == NULL) {
1423                 newcon->next = console_drivers;
1424                 console_drivers = newcon;
1425                 if (newcon->next)
1426                         newcon->next->flags &= ~CON_CONSDEV;
1427         } else {
1428                 newcon->next = console_drivers->next;
1429                 console_drivers->next = newcon;
1430         }
1431         if (newcon->flags & CON_PRINTBUFFER) {
1432                 /*
1433                  * console_unlock(); will print out the buffered messages
1434                  * for us.
1435                  */
1436                 spin_lock_irqsave(&logbuf_lock, flags);
1437                 con_start = log_start;
1438                 spin_unlock_irqrestore(&logbuf_lock, flags);
1439         }
1440         console_unlock();
1441         console_sysfs_notify();
1442
1443         /*
1444          * By unregistering the bootconsoles after we enable the real console
1445          * we get the "console xxx enabled" message on all the consoles -
1446          * boot consoles, real consoles, etc - this is to ensure that end
1447          * users know there might be something in the kernel's log buffer that
1448          * went to the bootconsole (that they do not see on the real console)
1449          */
1450         if (bcon && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV)) {
1451                 /* we need to iterate through twice, to make sure we print
1452                  * everything out, before we unregister the console(s)
1453                  */
1454                 printk(KERN_INFO "console [%s%d] enabled, bootconsole disabled\n",
1455                         newcon->name, newcon->index);
1456                 for_each_console(bcon)
1457                         if (bcon->flags & CON_BOOT)
1458                                 unregister_console(bcon);
1459         } else {
1460                 printk(KERN_INFO "%sconsole [%s%d] enabled\n",
1461                         (newcon->flags & CON_BOOT) ? "boot" : "" ,
1462                         newcon->name, newcon->index);
1463         }
1464 }
1465 EXPORT_SYMBOL(register_console);
1466
1467 int unregister_console(struct console *console)
1468 {
1469         struct console *a, *b;
1470         int res = 1;
1471
1472 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1473         if (console->flags & CON_BRL)
1474                 return braille_unregister_console(console);
1475 #endif
1476
1477         console_lock();
1478         if (console_drivers == console) {
1479                 console_drivers=console->next;
1480                 res = 0;
1481         } else if (console_drivers) {
1482                 for (a=console_drivers->next, b=console_drivers ;
1483                      a; b=a, a=b->next) {
1484                         if (a == console) {
1485                                 b->next = a->next;
1486                                 res = 0;
1487                                 break;
1488                         }
1489                 }
1490         }
1491
1492         /*
1493          * If this isn't the last console and it has CON_CONSDEV set, we
1494          * need to set it on the next preferred console.
1495          */
1496         if (console_drivers != NULL && console->flags & CON_CONSDEV)
1497                 console_drivers->flags |= CON_CONSDEV;
1498
1499         console_unlock();
1500         console_sysfs_notify();
1501         return res;
1502 }
1503 EXPORT_SYMBOL(unregister_console);
1504
1505 static int __init printk_late_init(void)
1506 {
1507         struct console *con;
1508
1509         for_each_console(con) {
1510                 if (con->flags & CON_BOOT) {
1511                         printk(KERN_INFO "turn off boot console %s%d\n",
1512                                 con->name, con->index);
1513                         unregister_console(con);
1514                 }
1515         }
1516         hotcpu_notifier(console_cpu_notify, 0);
1517         return 0;
1518 }
1519 late_initcall(printk_late_init);
1520
1521 #if defined CONFIG_PRINTK
1522
1523 /*
1524  * printk rate limiting, lifted from the networking subsystem.
1525  *
1526  * This enforces a rate limit: not more than 10 kernel messages
1527  * every 5s to make a denial-of-service attack impossible.
1528  */
1529 DEFINE_RATELIMIT_STATE(printk_ratelimit_state, 5 * HZ, 10);
1530
1531 int __printk_ratelimit(const char *func)
1532 {
1533         return ___ratelimit(&printk_ratelimit_state, func);
1534 }
1535 EXPORT_SYMBOL(__printk_ratelimit);
1536
1537 /**
1538  * printk_timed_ratelimit - caller-controlled printk ratelimiting
1539  * @caller_jiffies: pointer to caller's state
1540  * @interval_msecs: minimum interval between prints
1541  *
1542  * printk_timed_ratelimit() returns true if more than @interval_msecs
1543  * milliseconds have elapsed since the last time printk_timed_ratelimit()
1544  * returned true.
1545  */
1546 bool printk_timed_ratelimit(unsigned long *caller_jiffies,
1547                         unsigned int interval_msecs)
1548 {
1549         if (*caller_jiffies == 0
1550                         || !time_in_range(jiffies, *caller_jiffies,
1551                                         *caller_jiffies
1552                                         + msecs_to_jiffies(interval_msecs))) {
1553                 *caller_jiffies = jiffies;
1554                 return true;
1555         }
1556         return false;
1557 }
1558 EXPORT_SYMBOL(printk_timed_ratelimit);
1559
1560 static DEFINE_SPINLOCK(dump_list_lock);
1561 static LIST_HEAD(dump_list);
1562
1563 /**
1564  * kmsg_dump_register - register a kernel log dumper.
1565  * @dumper: pointer to the kmsg_dumper structure
1566  *
1567  * Adds a kernel log dumper to the system. The dump callback in the
1568  * structure will be called when the kernel oopses or panics and must be
1569  * set. Returns zero on success and %-EINVAL or %-EBUSY otherwise.
1570  */
1571 int kmsg_dump_register(struct kmsg_dumper *dumper)
1572 {
1573         unsigned long flags;
1574         int err = -EBUSY;
1575
1576         /* The dump callback needs to be set */
1577         if (!dumper->dump)
1578                 return -EINVAL;
1579
1580         spin_lock_irqsave(&dump_list_lock, flags);
1581         /* Don't allow registering multiple times */
1582         if (!dumper->registered) {
1583                 dumper->registered = 1;
1584                 list_add_tail_rcu(&dumper->list, &dump_list);
1585                 err = 0;
1586         }
1587         spin_unlock_irqrestore(&dump_list_lock, flags);
1588
1589         return err;
1590 }
1591 EXPORT_SYMBOL_GPL(kmsg_dump_register);
1592
1593 /**
1594  * kmsg_dump_unregister - unregister a kmsg dumper.
1595  * @dumper: pointer to the kmsg_dumper structure
1596  *
1597  * Removes a dump device from the system. Returns zero on success and
1598  * %-EINVAL otherwise.
1599  */
1600 int kmsg_dump_unregister(struct kmsg_dumper *dumper)
1601 {
1602         unsigned long flags;
1603         int err = -EINVAL;
1604
1605         spin_lock_irqsave(&dump_list_lock, flags);
1606         if (dumper->registered) {
1607                 dumper->registered = 0;
1608                 list_del_rcu(&dumper->list);
1609                 err = 0;
1610         }
1611         spin_unlock_irqrestore(&dump_list_lock, flags);
1612         synchronize_rcu();
1613
1614         return err;
1615 }
1616 EXPORT_SYMBOL_GPL(kmsg_dump_unregister);
1617
1618 /**
1619  * kmsg_dump - dump kernel log to kernel message dumpers.
1620  * @reason: the reason (oops, panic etc) for dumping
1621  *
1622  * Iterate through each of the dump devices and call the oops/panic
1623  * callbacks with the log buffer.
1624  */
1625 void kmsg_dump(enum kmsg_dump_reason reason)
1626 {
1627         unsigned long end;
1628         unsigned chars;
1629         struct kmsg_dumper *dumper;
1630         const char *s1, *s2;
1631         unsigned long l1, l2;
1632         unsigned long flags;
1633
1634         /* Theoretically, the log could move on after we do this, but
1635            there's not a lot we can do about that. The new messages
1636            will overwrite the start of what we dump. */
1637         spin_lock_irqsave(&logbuf_lock, flags);
1638         end = log_end & LOG_BUF_MASK;
1639         chars = logged_chars;
1640         spin_unlock_irqrestore(&logbuf_lock, flags);
1641
1642         if (chars > end) {
1643                 s1 = log_buf + log_buf_len - chars + end;
1644                 l1 = chars - end;
1645
1646                 s2 = log_buf;
1647                 l2 = end;
1648         } else {
1649                 s1 = "";
1650                 l1 = 0;
1651
1652                 s2 = log_buf + end - chars;
1653                 l2 = chars;
1654         }
1655
1656         rcu_read_lock();
1657         list_for_each_entry_rcu(dumper, &dump_list, list)
1658                 dumper->dump(dumper, reason, s1, l1, s2, l2);
1659         rcu_read_unlock();
1660 }
1661 #endif