printk: convert byte-buffer to variable-length record buffer
[pandora-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/memblock.h>
35 #include <linux/syscalls.h>
36 #include <linux/kexec.h>
37 #include <linux/kdb.h>
38 #include <linux/ratelimit.h>
39 #include <linux/kmsg_dump.h>
40 #include <linux/syslog.h>
41 #include <linux/cpu.h>
42 #include <linux/notifier.h>
43 #include <linux/rculist.h>
44
45 #include <asm/uaccess.h>
46
47 #define CREATE_TRACE_POINTS
48 #include <trace/events/printk.h>
49
50 /*
51  * Architectures can override it:
52  */
53 void asmlinkage __attribute__((weak)) early_printk(const char *fmt, ...)
54 {
55 }
56
57 /* printk's without a loglevel use this.. */
58 #define DEFAULT_MESSAGE_LOGLEVEL CONFIG_DEFAULT_MESSAGE_LOGLEVEL
59
60 /* We show everything that is MORE important than this.. */
61 #define MINIMUM_CONSOLE_LOGLEVEL 1 /* Minimum loglevel we let people use */
62 #define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */
63
64 DECLARE_WAIT_QUEUE_HEAD(log_wait);
65
66 int console_printk[4] = {
67         DEFAULT_CONSOLE_LOGLEVEL,       /* console_loglevel */
68         DEFAULT_MESSAGE_LOGLEVEL,       /* default_message_loglevel */
69         MINIMUM_CONSOLE_LOGLEVEL,       /* minimum_console_loglevel */
70         DEFAULT_CONSOLE_LOGLEVEL,       /* default_console_loglevel */
71 };
72
73 /*
74  * Low level drivers may need that to know if they can schedule in
75  * their unblank() callback or not. So let's export it.
76  */
77 int oops_in_progress;
78 EXPORT_SYMBOL(oops_in_progress);
79
80 /*
81  * console_sem protects the console_drivers list, and also
82  * provides serialisation for access to the entire console
83  * driver system.
84  */
85 static DEFINE_SEMAPHORE(console_sem);
86 struct console *console_drivers;
87 EXPORT_SYMBOL_GPL(console_drivers);
88
89 /*
90  * This is used for debugging the mess that is the VT code by
91  * keeping track if we have the console semaphore held. It's
92  * definitely not the perfect debug tool (we don't know if _WE_
93  * hold it are racing, but it helps tracking those weird code
94  * path in the console code where we end up in places I want
95  * locked without the console sempahore held
96  */
97 static int console_locked, console_suspended;
98
99 /*
100  * If exclusive_console is non-NULL then only this console is to be printed to.
101  */
102 static struct console *exclusive_console;
103
104 /*
105  *      Array of consoles built from command line options (console=)
106  */
107 struct console_cmdline
108 {
109         char    name[8];                        /* Name of the driver       */
110         int     index;                          /* Minor dev. to use        */
111         char    *options;                       /* Options for the driver   */
112 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
113         char    *brl_options;                   /* Options for braille driver */
114 #endif
115 };
116
117 #define MAX_CMDLINECONSOLES 8
118
119 static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
120 static int selected_console = -1;
121 static int preferred_console = -1;
122 int console_set_on_cmdline;
123 EXPORT_SYMBOL(console_set_on_cmdline);
124
125 /* Flag: console code may call schedule() */
126 static int console_may_schedule;
127
128 #ifdef CONFIG_PRINTK
129 /*
130  * The printk log buffer consists of a chain of concatenated variable
131  * length records. Every record starts with a record header, containing
132  * the overall length of the record.
133  *
134  * The heads to the first and last entry in the buffer, as well as the
135  * sequence numbers of these both entries are maintained when messages
136  * are stored..
137  *
138  * If the heads indicate available messages, the length in the header
139  * tells the start next message. A length == 0 for the next message
140  * indicates a wrap-around to the beginning of the buffer.
141  *
142  * Every record carries the monotonic timestamp in microseconds, as well as
143  * the standard userspace syslog level and syslog facility. The usual
144  * kernel messages use LOG_KERN; userspace-injected messages always carry
145  * a matching syslog facility, by default LOG_USER. The origin of every
146  * message can be reliably determined that way.
147  *
148  * The human readable log message directly follows the message header. The
149  * length of the message text is stored in the header, the stored message
150  * is not terminated.
151  *
152  */
153
154 struct log {
155         u64 ts_nsec;            /* timestamp in nanoseconds */
156         u16 len;                /* length of entire record */
157         u16 text_len;           /* length of text buffer */
158         u16 dict_len;           /* length of dictionary buffer */
159         u16 level;              /* syslog level + facility */
160 };
161
162 /*
163  * The logbuf_lock protects kmsg buffer, indices, counters. It is also
164  * used in interesting ways to provide interlocking in console_unlock();
165  */
166 static DEFINE_RAW_SPINLOCK(logbuf_lock);
167
168 /* cpu currently holding logbuf_lock */
169 static volatile unsigned int logbuf_cpu = UINT_MAX;
170
171 #define LOG_LINE_MAX 1024
172
173 /* record buffer */
174 #define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT)
175 static char __log_buf[__LOG_BUF_LEN];
176 static char *log_buf = __log_buf;
177 static u32 log_buf_len = __LOG_BUF_LEN;
178
179 /* index and sequence number of the first record stored in the buffer */
180 static u64 log_first_seq;
181 static u32 log_first_idx;
182
183 /* index and sequence number of the next record to store in the buffer */
184 static u64 log_next_seq;
185 static u32 log_next_idx;
186
187 /* the next printk record to read after the last 'clear' command */
188 static u64 clear_seq;
189 static u32 clear_idx;
190
191 /* the next printk record to read by syslog(READ) or /proc/kmsg */
192 static u64 syslog_seq;
193 static u32 syslog_idx;
194
195 /* human readable text of the record */
196 static char *log_text(const struct log *msg)
197 {
198         return (char *)msg + sizeof(struct log);
199 }
200
201 /* optional key/value pair dictionary attached to the record */
202 static char *log_dict(const struct log *msg)
203 {
204         return (char *)msg + sizeof(struct log) + msg->text_len;
205 }
206
207 /* get record by index; idx must point to valid msg */
208 static struct log *log_from_idx(u32 idx)
209 {
210         struct log *msg = (struct log *)(log_buf + idx);
211
212         /*
213          * A length == 0 record is the end of buffer marker. Wrap around and
214          * read the message at the start of the buffer.
215          */
216         if (!msg->len)
217                 return (struct log *)log_buf;
218         return msg;
219 }
220
221 /* get next record; idx must point to valid msg */
222 static u32 log_next(u32 idx)
223 {
224         struct log *msg = (struct log *)(log_buf + idx);
225
226         /* length == 0 indicates the end of the buffer; wrap */
227         /*
228          * A length == 0 record is the end of buffer marker. Wrap around and
229          * read the message at the start of the buffer as *this* one, and
230          * return the one after that.
231          */
232         if (!msg->len) {
233                 msg = (struct log *)log_buf;
234                 return msg->len;
235         }
236         return idx + msg->len;
237 }
238
239 #if !defined(CONFIG_64BIT) || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
240 #define LOG_ALIGN 4
241 #else
242 #define LOG_ALIGN 8
243 #endif
244
245 /* insert record into the buffer, discard old ones, update heads */
246 static void log_store(int facility, int level,
247                       const char *dict, u16 dict_len,
248                       const char *text, u16 text_len)
249 {
250         struct log *msg;
251         u32 size, pad_len;
252
253         /* number of '\0' padding bytes to next message */
254         size = sizeof(struct log) + text_len + dict_len;
255         pad_len = (-size) & (LOG_ALIGN - 1);
256         size += pad_len;
257
258         while (log_first_seq < log_next_seq) {
259                 u32 free;
260
261                 if (log_next_idx > log_first_idx)
262                         free = max(log_buf_len - log_next_idx, log_first_idx);
263                 else
264                         free = log_first_idx - log_next_idx;
265
266                 if (free > size + sizeof(struct log))
267                         break;
268
269                 /* drop old messages until we have enough contiuous space */
270                 log_first_idx = log_next(log_first_idx);
271                 log_first_seq++;
272         }
273
274         if (log_next_idx + size + sizeof(struct log) >= log_buf_len) {
275                 /*
276                  * This message + an additional empty header does not fit
277                  * at the end of the buffer. Add an empty header with len == 0
278                  * to signify a wrap around.
279                  */
280                 memset(log_buf + log_next_idx, 0, sizeof(struct log));
281                 log_next_idx = 0;
282         }
283
284         /* fill message */
285         msg = (struct log *)(log_buf + log_next_idx);
286         memcpy(log_text(msg), text, text_len);
287         msg->text_len = text_len;
288         memcpy(log_dict(msg), dict, dict_len);
289         msg->dict_len = dict_len;
290         msg->level = (facility << 3) | (level & 7);
291         msg->ts_nsec = local_clock();
292         memset(log_dict(msg) + dict_len, 0, pad_len);
293         msg->len = sizeof(struct log) + text_len + dict_len + pad_len;
294
295         /* insert message */
296         log_next_idx += msg->len;
297         log_next_seq++;
298 }
299
300 #ifdef CONFIG_KEXEC
301 /*
302  * This appends the listed symbols to /proc/vmcoreinfo
303  *
304  * /proc/vmcoreinfo is used by various utiilties, like crash and makedumpfile to
305  * obtain access to symbols that are otherwise very difficult to locate.  These
306  * symbols are specifically used so that utilities can access and extract the
307  * dmesg log from a vmcore file after a crash.
308  */
309 void log_buf_kexec_setup(void)
310 {
311         VMCOREINFO_SYMBOL(log_buf);
312         VMCOREINFO_SYMBOL(log_buf_len);
313         VMCOREINFO_SYMBOL(log_first_idx);
314         VMCOREINFO_SYMBOL(log_next_idx);
315 }
316 #endif
317
318 /* requested log_buf_len from kernel cmdline */
319 static unsigned long __initdata new_log_buf_len;
320
321 /* save requested log_buf_len since it's too early to process it */
322 static int __init log_buf_len_setup(char *str)
323 {
324         unsigned size = memparse(str, &str);
325
326         if (size)
327                 size = roundup_pow_of_two(size);
328         if (size > log_buf_len)
329                 new_log_buf_len = size;
330
331         return 0;
332 }
333 early_param("log_buf_len", log_buf_len_setup);
334
335 void __init setup_log_buf(int early)
336 {
337         unsigned long flags;
338         char *new_log_buf;
339         int free;
340
341         if (!new_log_buf_len)
342                 return;
343
344         if (early) {
345                 unsigned long mem;
346
347                 mem = memblock_alloc(new_log_buf_len, PAGE_SIZE);
348                 if (!mem)
349                         return;
350                 new_log_buf = __va(mem);
351         } else {
352                 new_log_buf = alloc_bootmem_nopanic(new_log_buf_len);
353         }
354
355         if (unlikely(!new_log_buf)) {
356                 pr_err("log_buf_len: %ld bytes not available\n",
357                         new_log_buf_len);
358                 return;
359         }
360
361         raw_spin_lock_irqsave(&logbuf_lock, flags);
362         log_buf_len = new_log_buf_len;
363         log_buf = new_log_buf;
364         new_log_buf_len = 0;
365         free = __LOG_BUF_LEN - log_next_idx;
366         memcpy(log_buf, __log_buf, __LOG_BUF_LEN);
367         raw_spin_unlock_irqrestore(&logbuf_lock, flags);
368
369         pr_info("log_buf_len: %d\n", log_buf_len);
370         pr_info("early log buf free: %d(%d%%)\n",
371                 free, (free * 100) / __LOG_BUF_LEN);
372 }
373
374 #ifdef CONFIG_BOOT_PRINTK_DELAY
375
376 static int boot_delay; /* msecs delay after each printk during bootup */
377 static unsigned long long loops_per_msec;       /* based on boot_delay */
378
379 static int __init boot_delay_setup(char *str)
380 {
381         unsigned long lpj;
382
383         lpj = preset_lpj ? preset_lpj : 1000000;        /* some guess */
384         loops_per_msec = (unsigned long long)lpj / 1000 * HZ;
385
386         get_option(&str, &boot_delay);
387         if (boot_delay > 10 * 1000)
388                 boot_delay = 0;
389
390         pr_debug("boot_delay: %u, preset_lpj: %ld, lpj: %lu, "
391                 "HZ: %d, loops_per_msec: %llu\n",
392                 boot_delay, preset_lpj, lpj, HZ, loops_per_msec);
393         return 1;
394 }
395 __setup("boot_delay=", boot_delay_setup);
396
397 static void boot_delay_msec(void)
398 {
399         unsigned long long k;
400         unsigned long timeout;
401
402         if (boot_delay == 0 || system_state != SYSTEM_BOOTING)
403                 return;
404
405         k = (unsigned long long)loops_per_msec * boot_delay;
406
407         timeout = jiffies + msecs_to_jiffies(boot_delay);
408         while (k) {
409                 k--;
410                 cpu_relax();
411                 /*
412                  * use (volatile) jiffies to prevent
413                  * compiler reduction; loop termination via jiffies
414                  * is secondary and may or may not happen.
415                  */
416                 if (time_after(jiffies, timeout))
417                         break;
418                 touch_nmi_watchdog();
419         }
420 }
421 #else
422 static inline void boot_delay_msec(void)
423 {
424 }
425 #endif
426
427 #ifdef CONFIG_SECURITY_DMESG_RESTRICT
428 int dmesg_restrict = 1;
429 #else
430 int dmesg_restrict;
431 #endif
432
433 static int syslog_action_restricted(int type)
434 {
435         if (dmesg_restrict)
436                 return 1;
437         /* Unless restricted, we allow "read all" and "get buffer size" for everybody */
438         return type != SYSLOG_ACTION_READ_ALL && type != SYSLOG_ACTION_SIZE_BUFFER;
439 }
440
441 static int check_syslog_permissions(int type, bool from_file)
442 {
443         /*
444          * If this is from /proc/kmsg and we've already opened it, then we've
445          * already done the capabilities checks at open time.
446          */
447         if (from_file && type != SYSLOG_ACTION_OPEN)
448                 return 0;
449
450         if (syslog_action_restricted(type)) {
451                 if (capable(CAP_SYSLOG))
452                         return 0;
453                 /* For historical reasons, accept CAP_SYS_ADMIN too, with a warning */
454                 if (capable(CAP_SYS_ADMIN)) {
455                         printk_once(KERN_WARNING "%s (%d): "
456                                  "Attempt to access syslog with CAP_SYS_ADMIN "
457                                  "but no CAP_SYSLOG (deprecated).\n",
458                                  current->comm, task_pid_nr(current));
459                         return 0;
460                 }
461                 return -EPERM;
462         }
463         return 0;
464 }
465
466 #if defined(CONFIG_PRINTK_TIME)
467 static bool printk_time = 1;
468 #else
469 static bool printk_time;
470 #endif
471 module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR);
472
473 static int syslog_print_line(u32 idx, char *text, size_t size)
474 {
475         struct log *msg;
476         size_t len;
477
478         msg = log_from_idx(idx);
479         if (!text) {
480                 /* calculate length only */
481                 len = 3;
482
483                 if (msg->level > 9)
484                         len++;
485                 if (msg->level > 99)
486                         len++;
487
488                 if (printk_time)
489                         len += 15;
490
491                 len += msg->text_len;
492                 len++;
493                 return len;
494         }
495
496         len = sprintf(text, "<%u>", msg->level);
497
498         if (printk_time) {
499                 unsigned long long t = msg->ts_nsec;
500                 unsigned long rem_ns = do_div(t, 1000000000);
501
502                 len += sprintf(text + len, "[%5lu.%06lu] ",
503                                    (unsigned long) t, rem_ns / 1000);
504         }
505
506         if (len + msg->text_len > size)
507                 return -EINVAL;
508         memcpy(text + len, log_text(msg), msg->text_len);
509         len += msg->text_len;
510         text[len++] = '\n';
511         return len;
512 }
513
514 static int syslog_print(char __user *buf, int size)
515 {
516         char *text;
517         int len;
518
519         text = kmalloc(LOG_LINE_MAX, GFP_KERNEL);
520         if (!text)
521                 return -ENOMEM;
522
523         raw_spin_lock_irq(&logbuf_lock);
524         if (syslog_seq < log_first_seq) {
525                 /* messages are gone, move to first one */
526                 syslog_seq = log_first_seq;
527                 syslog_idx = log_first_idx;
528         }
529         len = syslog_print_line(syslog_idx, text, LOG_LINE_MAX);
530         syslog_idx = log_next(syslog_idx);
531         syslog_seq++;
532         raw_spin_unlock_irq(&logbuf_lock);
533
534         if (len > 0 && copy_to_user(buf, text, len))
535                 len = -EFAULT;
536
537         kfree(text);
538         return len;
539 }
540
541 static int syslog_print_all(char __user *buf, int size, bool clear)
542 {
543         char *text;
544         int len = 0;
545
546         text = kmalloc(LOG_LINE_MAX, GFP_KERNEL);
547         if (!text)
548                 return -ENOMEM;
549
550         raw_spin_lock_irq(&logbuf_lock);
551         if (buf) {
552                 u64 next_seq;
553                 u64 seq;
554                 u32 idx;
555
556                 if (clear_seq < log_first_seq) {
557                         /* messages are gone, move to first available one */
558                         clear_seq = log_first_seq;
559                         clear_idx = log_first_idx;
560                 }
561
562                 /*
563                  * Find first record that fits, including all following records,
564                  * into the user-provided buffer for this dump.
565                 */
566                 seq = clear_seq;
567                 idx = clear_idx;
568                 while (seq < log_next_seq) {
569                         len += syslog_print_line(idx, NULL, 0);
570                         idx = log_next(idx);
571                         seq++;
572                 }
573                 seq = clear_seq;
574                 idx = clear_idx;
575                 while (len > size && seq < log_next_seq) {
576                         len -= syslog_print_line(idx, NULL, 0);
577                         idx = log_next(idx);
578                         seq++;
579                 }
580
581                 /* last message in this dump */
582                 next_seq = log_next_seq;
583
584                 len = 0;
585                 while (len >= 0 && seq < next_seq) {
586                         int textlen;
587
588                         textlen = syslog_print_line(idx, text, LOG_LINE_MAX);
589                         if (textlen < 0) {
590                                 len = textlen;
591                                 break;
592                         }
593                         idx = log_next(idx);
594                         seq++;
595
596                         raw_spin_unlock_irq(&logbuf_lock);
597                         if (copy_to_user(buf + len, text, textlen))
598                                 len = -EFAULT;
599                         else
600                                 len += textlen;
601                         raw_spin_lock_irq(&logbuf_lock);
602
603                         if (seq < log_first_seq) {
604                                 /* messages are gone, move to next one */
605                                 seq = log_first_seq;
606                                 idx = log_first_idx;
607                         }
608                 }
609         }
610
611         if (clear) {
612                 clear_seq = log_next_seq;
613                 clear_idx = log_next_idx;
614         }
615         raw_spin_unlock_irq(&logbuf_lock);
616
617         kfree(text);
618         return len;
619 }
620
621 int do_syslog(int type, char __user *buf, int len, bool from_file)
622 {
623         bool clear = false;
624         static int saved_console_loglevel = -1;
625         int error;
626
627         error = check_syslog_permissions(type, from_file);
628         if (error)
629                 goto out;
630
631         error = security_syslog(type);
632         if (error)
633                 return error;
634
635         switch (type) {
636         case SYSLOG_ACTION_CLOSE:       /* Close log */
637                 break;
638         case SYSLOG_ACTION_OPEN:        /* Open log */
639                 break;
640         case SYSLOG_ACTION_READ:        /* Read from log */
641                 error = -EINVAL;
642                 if (!buf || len < 0)
643                         goto out;
644                 error = 0;
645                 if (!len)
646                         goto out;
647                 if (!access_ok(VERIFY_WRITE, buf, len)) {
648                         error = -EFAULT;
649                         goto out;
650                 }
651                 error = wait_event_interruptible(log_wait,
652                                                  syslog_seq != log_next_seq);
653                 if (error)
654                         goto out;
655                 error = syslog_print(buf, len);
656                 break;
657         /* Read/clear last kernel messages */
658         case SYSLOG_ACTION_READ_CLEAR:
659                 clear = true;
660                 /* FALL THRU */
661         /* Read last kernel messages */
662         case SYSLOG_ACTION_READ_ALL:
663                 error = -EINVAL;
664                 if (!buf || len < 0)
665                         goto out;
666                 error = 0;
667                 if (!len)
668                         goto out;
669                 if (!access_ok(VERIFY_WRITE, buf, len)) {
670                         error = -EFAULT;
671                         goto out;
672                 }
673                 error = syslog_print_all(buf, len, clear);
674                 break;
675         /* Clear ring buffer */
676         case SYSLOG_ACTION_CLEAR:
677                 syslog_print_all(NULL, 0, true);
678         /* Disable logging to console */
679         case SYSLOG_ACTION_CONSOLE_OFF:
680                 if (saved_console_loglevel == -1)
681                         saved_console_loglevel = console_loglevel;
682                 console_loglevel = minimum_console_loglevel;
683                 break;
684         /* Enable logging to console */
685         case SYSLOG_ACTION_CONSOLE_ON:
686                 if (saved_console_loglevel != -1) {
687                         console_loglevel = saved_console_loglevel;
688                         saved_console_loglevel = -1;
689                 }
690                 break;
691         /* Set level of messages printed to console */
692         case SYSLOG_ACTION_CONSOLE_LEVEL:
693                 error = -EINVAL;
694                 if (len < 1 || len > 8)
695                         goto out;
696                 if (len < minimum_console_loglevel)
697                         len = minimum_console_loglevel;
698                 console_loglevel = len;
699                 /* Implicitly re-enable logging to console */
700                 saved_console_loglevel = -1;
701                 error = 0;
702                 break;
703         /* Number of chars in the log buffer */
704         case SYSLOG_ACTION_SIZE_UNREAD:
705                 raw_spin_lock_irq(&logbuf_lock);
706                 if (syslog_seq < log_first_seq) {
707                         /* messages are gone, move to first one */
708                         syslog_seq = log_first_seq;
709                         syslog_idx = log_first_idx;
710                 }
711                 if (from_file) {
712                         /*
713                          * Short-cut for poll(/"proc/kmsg") which simply checks
714                          * for pending data, not the size; return the count of
715                          * records, not the length.
716                          */
717                         error = log_next_idx - syslog_idx;
718                 } else {
719                         u64 seq;
720                         u32 idx;
721
722                         error = 0;
723                         seq = syslog_seq;
724                         idx = syslog_idx;
725                         while (seq < log_next_seq) {
726                                 error += syslog_print_line(idx, NULL, 0);
727                                 idx = log_next(idx);
728                                 seq++;
729                         }
730                 }
731                 raw_spin_unlock_irq(&logbuf_lock);
732                 break;
733         /* Size of the log buffer */
734         case SYSLOG_ACTION_SIZE_BUFFER:
735                 error = log_buf_len;
736                 break;
737         default:
738                 error = -EINVAL;
739                 break;
740         }
741 out:
742         return error;
743 }
744
745 SYSCALL_DEFINE3(syslog, int, type, char __user *, buf, int, len)
746 {
747         return do_syslog(type, buf, len, SYSLOG_FROM_CALL);
748 }
749
750 #ifdef  CONFIG_KGDB_KDB
751 /* kdb dmesg command needs access to the syslog buffer.  do_syslog()
752  * uses locks so it cannot be used during debugging.  Just tell kdb
753  * where the start and end of the physical and logical logs are.  This
754  * is equivalent to do_syslog(3).
755  */
756 void kdb_syslog_data(char *syslog_data[4])
757 {
758         syslog_data[0] = log_buf;
759         syslog_data[1] = log_buf + log_buf_len;
760         syslog_data[2] = log_buf + log_first_idx;
761         syslog_data[3] = log_buf + log_next_idx;
762 }
763 #endif  /* CONFIG_KGDB_KDB */
764
765 static bool __read_mostly ignore_loglevel;
766
767 static int __init ignore_loglevel_setup(char *str)
768 {
769         ignore_loglevel = 1;
770         printk(KERN_INFO "debug: ignoring loglevel setting.\n");
771
772         return 0;
773 }
774
775 early_param("ignore_loglevel", ignore_loglevel_setup);
776 module_param(ignore_loglevel, bool, S_IRUGO | S_IWUSR);
777 MODULE_PARM_DESC(ignore_loglevel, "ignore loglevel setting, to"
778         "print all kernel messages to the console.");
779
780 /*
781  * Call the console drivers, asking them to write out
782  * log_buf[start] to log_buf[end - 1].
783  * The console_lock must be held.
784  */
785 static void call_console_drivers(int level, const char *text, size_t len)
786 {
787         struct console *con;
788
789         trace_console(text, 0, len, len);
790
791         if (level >= console_loglevel && !ignore_loglevel)
792                 return;
793         if (!console_drivers)
794                 return;
795
796         for_each_console(con) {
797                 if (exclusive_console && con != exclusive_console)
798                         continue;
799                 if (!(con->flags & CON_ENABLED))
800                         continue;
801                 if (!con->write)
802                         continue;
803                 if (!cpu_online(smp_processor_id()) &&
804                     !(con->flags & CON_ANYTIME))
805                         continue;
806                 con->write(con, text, len);
807         }
808 }
809
810 /*
811  * Zap console related locks when oopsing. Only zap at most once
812  * every 10 seconds, to leave time for slow consoles to print a
813  * full oops.
814  */
815 static void zap_locks(void)
816 {
817         static unsigned long oops_timestamp;
818
819         if (time_after_eq(jiffies, oops_timestamp) &&
820                         !time_after(jiffies, oops_timestamp + 30 * HZ))
821                 return;
822
823         oops_timestamp = jiffies;
824
825         debug_locks_off();
826         /* If a crash is occurring, make sure we can't deadlock */
827         raw_spin_lock_init(&logbuf_lock);
828         /* And make sure that we print immediately */
829         sema_init(&console_sem, 1);
830 }
831
832 /* Check if we have any console registered that can be called early in boot. */
833 static int have_callable_console(void)
834 {
835         struct console *con;
836
837         for_each_console(con)
838                 if (con->flags & CON_ANYTIME)
839                         return 1;
840
841         return 0;
842 }
843
844 /*
845  * Can we actually use the console at this time on this cpu?
846  *
847  * Console drivers may assume that per-cpu resources have
848  * been allocated. So unless they're explicitly marked as
849  * being able to cope (CON_ANYTIME) don't call them until
850  * this CPU is officially up.
851  */
852 static inline int can_use_console(unsigned int cpu)
853 {
854         return cpu_online(cpu) || have_callable_console();
855 }
856
857 /*
858  * Try to get console ownership to actually show the kernel
859  * messages from a 'printk'. Return true (and with the
860  * console_lock held, and 'console_locked' set) if it
861  * is successful, false otherwise.
862  *
863  * This gets called with the 'logbuf_lock' spinlock held and
864  * interrupts disabled. It should return with 'lockbuf_lock'
865  * released but interrupts still disabled.
866  */
867 static int console_trylock_for_printk(unsigned int cpu)
868         __releases(&logbuf_lock)
869 {
870         int retval = 0, wake = 0;
871
872         if (console_trylock()) {
873                 retval = 1;
874
875                 /*
876                  * If we can't use the console, we need to release
877                  * the console semaphore by hand to avoid flushing
878                  * the buffer. We need to hold the console semaphore
879                  * in order to do this test safely.
880                  */
881                 if (!can_use_console(cpu)) {
882                         console_locked = 0;
883                         wake = 1;
884                         retval = 0;
885                 }
886         }
887         logbuf_cpu = UINT_MAX;
888         if (wake)
889                 up(&console_sem);
890         raw_spin_unlock(&logbuf_lock);
891         return retval;
892 }
893
894 int printk_delay_msec __read_mostly;
895
896 static inline void printk_delay(void)
897 {
898         if (unlikely(printk_delay_msec)) {
899                 int m = printk_delay_msec;
900
901                 while (m--) {
902                         mdelay(1);
903                         touch_nmi_watchdog();
904                 }
905         }
906 }
907
908 asmlinkage int vprintk_emit(int facility, int level,
909                             const char *dict, size_t dictlen,
910                             const char *fmt, va_list args)
911 {
912         static int recursion_bug;
913         static char buf[LOG_LINE_MAX];
914         static size_t buflen;
915         static int buflevel;
916         static char textbuf[LOG_LINE_MAX];
917         char *text = textbuf;
918         size_t textlen;
919         unsigned long flags;
920         int this_cpu;
921         bool newline = false;
922         bool cont = false;
923         int printed_len = 0;
924
925         boot_delay_msec();
926         printk_delay();
927
928         /* This stops the holder of console_sem just where we want him */
929         local_irq_save(flags);
930         this_cpu = smp_processor_id();
931
932         /*
933          * Ouch, printk recursed into itself!
934          */
935         if (unlikely(logbuf_cpu == this_cpu)) {
936                 /*
937                  * If a crash is occurring during printk() on this CPU,
938                  * then try to get the crash message out but make sure
939                  * we can't deadlock. Otherwise just return to avoid the
940                  * recursion and return - but flag the recursion so that
941                  * it can be printed at the next appropriate moment:
942                  */
943                 if (!oops_in_progress && !lockdep_recursing(current)) {
944                         recursion_bug = 1;
945                         goto out_restore_irqs;
946                 }
947                 zap_locks();
948         }
949
950         lockdep_off();
951         raw_spin_lock(&logbuf_lock);
952         logbuf_cpu = this_cpu;
953
954         if (recursion_bug) {
955                 static const char recursion_msg[] =
956                         "BUG: recent printk recursion!";
957
958                 recursion_bug = 0;
959                 printed_len += strlen(recursion_msg);
960                 /* emit KERN_CRIT message */
961                 log_store(0, 2, NULL, 0, recursion_msg, printed_len);
962         }
963
964         /*
965          * The printf needs to come first; we need the syslog
966          * prefix which might be passed-in as a parameter.
967          */
968         textlen = vscnprintf(text, sizeof(textbuf), fmt, args);
969
970         /* mark and strip a trailing newline */
971         if (textlen && text[textlen-1] == '\n') {
972                 textlen--;
973                 newline = true;
974         }
975
976         /* strip syslog prefix and extract log level or flags */
977         if (text[0] == '<' && text[1] && text[2] == '>') {
978                 switch (text[1]) {
979                 case '0' ... '7':
980                         if (level == -1)
981                                 level = text[1] - '0';
982                         text += 3;
983                         textlen -= 3;
984                         break;
985                 case 'c':       /* KERN_CONT */
986                         cont = true;
987                 case 'd':       /* KERN_DEFAULT */
988                         text += 3;
989                         textlen -= 3;
990                         break;
991                 }
992         }
993
994         if (buflen && (!cont || dict)) {
995                 /* no continuation; flush existing buffer */
996                 log_store(facility, buflevel, NULL, 0, buf, buflen);
997                 printed_len += buflen;
998                 buflen = 0;
999         }
1000
1001         if (buflen == 0) {
1002                 /* remember level for first message in the buffer */
1003                 if (level == -1)
1004                         buflevel = default_message_loglevel;
1005                 else
1006                         buflevel = level;
1007         }
1008
1009         if (buflen || !newline) {
1010                 /* append to existing buffer, or buffer until next message */
1011                 if (buflen + textlen > sizeof(buf))
1012                         textlen = sizeof(buf) - buflen;
1013                 memcpy(buf + buflen, text, textlen);
1014                 buflen += textlen;
1015         }
1016
1017         if (newline) {
1018                 /* end of line; flush buffer */
1019                 if (buflen) {
1020                         log_store(facility, buflevel,
1021                                   dict, dictlen, buf, buflen);
1022                         printed_len += buflen;
1023                         buflen = 0;
1024                 } else {
1025                         log_store(facility, buflevel,
1026                                   dict, dictlen, text, textlen);
1027                         printed_len += textlen;
1028                 }
1029         }
1030
1031         /*
1032          * Try to acquire and then immediately release the console semaphore.
1033          * The release will print out buffers and wake up /dev/kmsg and syslog()
1034          * users.
1035          *
1036          * The console_trylock_for_printk() function will release 'logbuf_lock'
1037          * regardless of whether it actually gets the console semaphore or not.
1038          */
1039         if (console_trylock_for_printk(this_cpu))
1040                 console_unlock();
1041
1042         lockdep_on();
1043 out_restore_irqs:
1044         local_irq_restore(flags);
1045
1046         return printed_len;
1047 }
1048 EXPORT_SYMBOL(vprintk_emit);
1049
1050 asmlinkage int vprintk(const char *fmt, va_list args)
1051 {
1052         return vprintk_emit(0, -1, NULL, 0, fmt, args);
1053 }
1054 EXPORT_SYMBOL(vprintk);
1055
1056 asmlinkage int printk_emit(int facility, int level,
1057                            const char *dict, size_t dictlen,
1058                            const char *fmt, ...)
1059 {
1060         va_list args;
1061         int r;
1062
1063         va_start(args, fmt);
1064         r = vprintk_emit(facility, level, dict, dictlen, fmt, args);
1065         va_end(args);
1066
1067         return r;
1068 }
1069 EXPORT_SYMBOL(printk_emit);
1070
1071 /**
1072  * printk - print a kernel message
1073  * @fmt: format string
1074  *
1075  * This is printk(). It can be called from any context. We want it to work.
1076  *
1077  * We try to grab the console_lock. If we succeed, it's easy - we log the
1078  * output and call the console drivers.  If we fail to get the semaphore, we
1079  * place the output into the log buffer and return. The current holder of
1080  * the console_sem will notice the new output in console_unlock(); and will
1081  * send it to the consoles before releasing the lock.
1082  *
1083  * One effect of this deferred printing is that code which calls printk() and
1084  * then changes console_loglevel may break. This is because console_loglevel
1085  * is inspected when the actual printing occurs.
1086  *
1087  * See also:
1088  * printf(3)
1089  *
1090  * See the vsnprintf() documentation for format string extensions over C99.
1091  */
1092 asmlinkage int printk(const char *fmt, ...)
1093 {
1094         va_list args;
1095         int r;
1096
1097 #ifdef CONFIG_KGDB_KDB
1098         if (unlikely(kdb_trap_printk)) {
1099                 va_start(args, fmt);
1100                 r = vkdb_printf(fmt, args);
1101                 va_end(args);
1102                 return r;
1103         }
1104 #endif
1105         va_start(args, fmt);
1106         r = vprintk_emit(0, -1, NULL, 0, fmt, args);
1107         va_end(args);
1108
1109         return r;
1110 }
1111 EXPORT_SYMBOL(printk);
1112 #else
1113
1114 static void call_console_drivers(int level, const char *text, size_t len)
1115 {
1116 }
1117
1118 #endif
1119
1120 static int __add_preferred_console(char *name, int idx, char *options,
1121                                    char *brl_options)
1122 {
1123         struct console_cmdline *c;
1124         int i;
1125
1126         /*
1127          *      See if this tty is not yet registered, and
1128          *      if we have a slot free.
1129          */
1130         for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
1131                 if (strcmp(console_cmdline[i].name, name) == 0 &&
1132                           console_cmdline[i].index == idx) {
1133                                 if (!brl_options)
1134                                         selected_console = i;
1135                                 return 0;
1136                 }
1137         if (i == MAX_CMDLINECONSOLES)
1138                 return -E2BIG;
1139         if (!brl_options)
1140                 selected_console = i;
1141         c = &console_cmdline[i];
1142         strlcpy(c->name, name, sizeof(c->name));
1143         c->options = options;
1144 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1145         c->brl_options = brl_options;
1146 #endif
1147         c->index = idx;
1148         return 0;
1149 }
1150 /*
1151  * Set up a list of consoles.  Called from init/main.c
1152  */
1153 static int __init console_setup(char *str)
1154 {
1155         char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for index */
1156         char *s, *options, *brl_options = NULL;
1157         int idx;
1158
1159 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1160         if (!memcmp(str, "brl,", 4)) {
1161                 brl_options = "";
1162                 str += 4;
1163         } else if (!memcmp(str, "brl=", 4)) {
1164                 brl_options = str + 4;
1165                 str = strchr(brl_options, ',');
1166                 if (!str) {
1167                         printk(KERN_ERR "need port name after brl=\n");
1168                         return 1;
1169                 }
1170                 *(str++) = 0;
1171         }
1172 #endif
1173
1174         /*
1175          * Decode str into name, index, options.
1176          */
1177         if (str[0] >= '0' && str[0] <= '9') {
1178                 strcpy(buf, "ttyS");
1179                 strncpy(buf + 4, str, sizeof(buf) - 5);
1180         } else {
1181                 strncpy(buf, str, sizeof(buf) - 1);
1182         }
1183         buf[sizeof(buf) - 1] = 0;
1184         if ((options = strchr(str, ',')) != NULL)
1185                 *(options++) = 0;
1186 #ifdef __sparc__
1187         if (!strcmp(str, "ttya"))
1188                 strcpy(buf, "ttyS0");
1189         if (!strcmp(str, "ttyb"))
1190                 strcpy(buf, "ttyS1");
1191 #endif
1192         for (s = buf; *s; s++)
1193                 if ((*s >= '0' && *s <= '9') || *s == ',')
1194                         break;
1195         idx = simple_strtoul(s, NULL, 10);
1196         *s = 0;
1197
1198         __add_preferred_console(buf, idx, options, brl_options);
1199         console_set_on_cmdline = 1;
1200         return 1;
1201 }
1202 __setup("console=", console_setup);
1203
1204 /**
1205  * add_preferred_console - add a device to the list of preferred consoles.
1206  * @name: device name
1207  * @idx: device index
1208  * @options: options for this console
1209  *
1210  * The last preferred console added will be used for kernel messages
1211  * and stdin/out/err for init.  Normally this is used by console_setup
1212  * above to handle user-supplied console arguments; however it can also
1213  * be used by arch-specific code either to override the user or more
1214  * commonly to provide a default console (ie from PROM variables) when
1215  * the user has not supplied one.
1216  */
1217 int add_preferred_console(char *name, int idx, char *options)
1218 {
1219         return __add_preferred_console(name, idx, options, NULL);
1220 }
1221
1222 int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options)
1223 {
1224         struct console_cmdline *c;
1225         int i;
1226
1227         for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
1228                 if (strcmp(console_cmdline[i].name, name) == 0 &&
1229                           console_cmdline[i].index == idx) {
1230                                 c = &console_cmdline[i];
1231                                 strlcpy(c->name, name_new, sizeof(c->name));
1232                                 c->name[sizeof(c->name) - 1] = 0;
1233                                 c->options = options;
1234                                 c->index = idx_new;
1235                                 return i;
1236                 }
1237         /* not found */
1238         return -1;
1239 }
1240
1241 bool console_suspend_enabled = 1;
1242 EXPORT_SYMBOL(console_suspend_enabled);
1243
1244 static int __init console_suspend_disable(char *str)
1245 {
1246         console_suspend_enabled = 0;
1247         return 1;
1248 }
1249 __setup("no_console_suspend", console_suspend_disable);
1250 module_param_named(console_suspend, console_suspend_enabled,
1251                 bool, S_IRUGO | S_IWUSR);
1252 MODULE_PARM_DESC(console_suspend, "suspend console during suspend"
1253         " and hibernate operations");
1254
1255 /**
1256  * suspend_console - suspend the console subsystem
1257  *
1258  * This disables printk() while we go into suspend states
1259  */
1260 void suspend_console(void)
1261 {
1262         if (!console_suspend_enabled)
1263                 return;
1264         printk("Suspending console(s) (use no_console_suspend to debug)\n");
1265         console_lock();
1266         console_suspended = 1;
1267         up(&console_sem);
1268 }
1269
1270 void resume_console(void)
1271 {
1272         if (!console_suspend_enabled)
1273                 return;
1274         down(&console_sem);
1275         console_suspended = 0;
1276         console_unlock();
1277 }
1278
1279 /**
1280  * console_cpu_notify - print deferred console messages after CPU hotplug
1281  * @self: notifier struct
1282  * @action: CPU hotplug event
1283  * @hcpu: unused
1284  *
1285  * If printk() is called from a CPU that is not online yet, the messages
1286  * will be spooled but will not show up on the console.  This function is
1287  * called when a new CPU comes online (or fails to come up), and ensures
1288  * that any such output gets printed.
1289  */
1290 static int __cpuinit console_cpu_notify(struct notifier_block *self,
1291         unsigned long action, void *hcpu)
1292 {
1293         switch (action) {
1294         case CPU_ONLINE:
1295         case CPU_DEAD:
1296         case CPU_DYING:
1297         case CPU_DOWN_FAILED:
1298         case CPU_UP_CANCELED:
1299                 console_lock();
1300                 console_unlock();
1301         }
1302         return NOTIFY_OK;
1303 }
1304
1305 /**
1306  * console_lock - lock the console system for exclusive use.
1307  *
1308  * Acquires a lock which guarantees that the caller has
1309  * exclusive access to the console system and the console_drivers list.
1310  *
1311  * Can sleep, returns nothing.
1312  */
1313 void console_lock(void)
1314 {
1315         BUG_ON(in_interrupt());
1316         down(&console_sem);
1317         if (console_suspended)
1318                 return;
1319         console_locked = 1;
1320         console_may_schedule = 1;
1321 }
1322 EXPORT_SYMBOL(console_lock);
1323
1324 /**
1325  * console_trylock - try to lock the console system for exclusive use.
1326  *
1327  * Tried to acquire a lock which guarantees that the caller has
1328  * exclusive access to the console system and the console_drivers list.
1329  *
1330  * returns 1 on success, and 0 on failure to acquire the lock.
1331  */
1332 int console_trylock(void)
1333 {
1334         if (down_trylock(&console_sem))
1335                 return 0;
1336         if (console_suspended) {
1337                 up(&console_sem);
1338                 return 0;
1339         }
1340         console_locked = 1;
1341         console_may_schedule = 0;
1342         return 1;
1343 }
1344 EXPORT_SYMBOL(console_trylock);
1345
1346 int is_console_locked(void)
1347 {
1348         return console_locked;
1349 }
1350
1351 /*
1352  * Delayed printk version, for scheduler-internal messages:
1353  */
1354 #define PRINTK_BUF_SIZE         512
1355
1356 #define PRINTK_PENDING_WAKEUP   0x01
1357 #define PRINTK_PENDING_SCHED    0x02
1358
1359 static DEFINE_PER_CPU(int, printk_pending);
1360 static DEFINE_PER_CPU(char [PRINTK_BUF_SIZE], printk_sched_buf);
1361
1362 void printk_tick(void)
1363 {
1364         if (__this_cpu_read(printk_pending)) {
1365                 int pending = __this_cpu_xchg(printk_pending, 0);
1366                 if (pending & PRINTK_PENDING_SCHED) {
1367                         char *buf = __get_cpu_var(printk_sched_buf);
1368                         printk(KERN_WARNING "[sched_delayed] %s", buf);
1369                 }
1370                 if (pending & PRINTK_PENDING_WAKEUP)
1371                         wake_up_interruptible(&log_wait);
1372         }
1373 }
1374
1375 int printk_needs_cpu(int cpu)
1376 {
1377         if (cpu_is_offline(cpu))
1378                 printk_tick();
1379         return __this_cpu_read(printk_pending);
1380 }
1381
1382 void wake_up_klogd(void)
1383 {
1384         if (waitqueue_active(&log_wait))
1385                 this_cpu_or(printk_pending, PRINTK_PENDING_WAKEUP);
1386 }
1387
1388 /* the next printk record to write to the console */
1389 static u64 console_seq;
1390 static u32 console_idx;
1391
1392 /**
1393  * console_unlock - unlock the console system
1394  *
1395  * Releases the console_lock which the caller holds on the console system
1396  * and the console driver list.
1397  *
1398  * While the console_lock was held, console output may have been buffered
1399  * by printk().  If this is the case, console_unlock(); emits
1400  * the output prior to releasing the lock.
1401  *
1402  * If there is output waiting, we wake it /dev/kmsg and syslog() users.
1403  *
1404  * console_unlock(); may be called from any context.
1405  */
1406 void console_unlock(void)
1407 {
1408         static u64 seen_seq;
1409         unsigned long flags;
1410         bool wake_klogd = false;
1411         bool retry;
1412
1413         if (console_suspended) {
1414                 up(&console_sem);
1415                 return;
1416         }
1417
1418         console_may_schedule = 0;
1419
1420 again:
1421         for (;;) {
1422                 struct log *msg;
1423                 static char text[LOG_LINE_MAX];
1424                 size_t len;
1425                 int level;
1426
1427                 raw_spin_lock_irqsave(&logbuf_lock, flags);
1428                 if (seen_seq != log_next_seq) {
1429                         wake_klogd = true;
1430                         seen_seq = log_next_seq;
1431                 }
1432
1433                 if (console_seq < log_first_seq) {
1434                         /* messages are gone, move to first one */
1435                         console_seq = log_first_seq;
1436                         console_idx = log_first_idx;
1437                 }
1438
1439                 if (console_seq == log_next_seq)
1440                         break;
1441
1442                 msg = log_from_idx(console_idx);
1443                 level = msg->level & 7;
1444                 len = msg->text_len;
1445                 if (len+1 >= sizeof(text))
1446                         len = sizeof(text)-1;
1447                 memcpy(text, log_text(msg), len);
1448                 text[len++] = '\n';
1449
1450                 console_idx = log_next(console_idx);
1451                 console_seq++;
1452                 raw_spin_unlock(&logbuf_lock);
1453
1454                 stop_critical_timings();        /* don't trace print latency */
1455                 call_console_drivers(level, text, len);
1456                 start_critical_timings();
1457                 local_irq_restore(flags);
1458         }
1459         console_locked = 0;
1460
1461         /* Release the exclusive_console once it is used */
1462         if (unlikely(exclusive_console))
1463                 exclusive_console = NULL;
1464
1465         raw_spin_unlock(&logbuf_lock);
1466
1467         up(&console_sem);
1468
1469         /*
1470          * Someone could have filled up the buffer again, so re-check if there's
1471          * something to flush. In case we cannot trylock the console_sem again,
1472          * there's a new owner and the console_unlock() from them will do the
1473          * flush, no worries.
1474          */
1475         raw_spin_lock(&logbuf_lock);
1476         retry = console_seq != log_next_seq;
1477         raw_spin_unlock_irqrestore(&logbuf_lock, flags);
1478
1479         if (retry && console_trylock())
1480                 goto again;
1481
1482         if (wake_klogd)
1483                 wake_up_klogd();
1484 }
1485 EXPORT_SYMBOL(console_unlock);
1486
1487 /**
1488  * console_conditional_schedule - yield the CPU if required
1489  *
1490  * If the console code is currently allowed to sleep, and
1491  * if this CPU should yield the CPU to another task, do
1492  * so here.
1493  *
1494  * Must be called within console_lock();.
1495  */
1496 void __sched console_conditional_schedule(void)
1497 {
1498         if (console_may_schedule)
1499                 cond_resched();
1500 }
1501 EXPORT_SYMBOL(console_conditional_schedule);
1502
1503 void console_unblank(void)
1504 {
1505         struct console *c;
1506
1507         /*
1508          * console_unblank can no longer be called in interrupt context unless
1509          * oops_in_progress is set to 1..
1510          */
1511         if (oops_in_progress) {
1512                 if (down_trylock(&console_sem) != 0)
1513                         return;
1514         } else
1515                 console_lock();
1516
1517         console_locked = 1;
1518         console_may_schedule = 0;
1519         for_each_console(c)
1520                 if ((c->flags & CON_ENABLED) && c->unblank)
1521                         c->unblank();
1522         console_unlock();
1523 }
1524
1525 /*
1526  * Return the console tty driver structure and its associated index
1527  */
1528 struct tty_driver *console_device(int *index)
1529 {
1530         struct console *c;
1531         struct tty_driver *driver = NULL;
1532
1533         console_lock();
1534         for_each_console(c) {
1535                 if (!c->device)
1536                         continue;
1537                 driver = c->device(c, index);
1538                 if (driver)
1539                         break;
1540         }
1541         console_unlock();
1542         return driver;
1543 }
1544
1545 /*
1546  * Prevent further output on the passed console device so that (for example)
1547  * serial drivers can disable console output before suspending a port, and can
1548  * re-enable output afterwards.
1549  */
1550 void console_stop(struct console *console)
1551 {
1552         console_lock();
1553         console->flags &= ~CON_ENABLED;
1554         console_unlock();
1555 }
1556 EXPORT_SYMBOL(console_stop);
1557
1558 void console_start(struct console *console)
1559 {
1560         console_lock();
1561         console->flags |= CON_ENABLED;
1562         console_unlock();
1563 }
1564 EXPORT_SYMBOL(console_start);
1565
1566 static int __read_mostly keep_bootcon;
1567
1568 static int __init keep_bootcon_setup(char *str)
1569 {
1570         keep_bootcon = 1;
1571         printk(KERN_INFO "debug: skip boot console de-registration.\n");
1572
1573         return 0;
1574 }
1575
1576 early_param("keep_bootcon", keep_bootcon_setup);
1577
1578 /*
1579  * The console driver calls this routine during kernel initialization
1580  * to register the console printing procedure with printk() and to
1581  * print any messages that were printed by the kernel before the
1582  * console driver was initialized.
1583  *
1584  * This can happen pretty early during the boot process (because of
1585  * early_printk) - sometimes before setup_arch() completes - be careful
1586  * of what kernel features are used - they may not be initialised yet.
1587  *
1588  * There are two types of consoles - bootconsoles (early_printk) and
1589  * "real" consoles (everything which is not a bootconsole) which are
1590  * handled differently.
1591  *  - Any number of bootconsoles can be registered at any time.
1592  *  - As soon as a "real" console is registered, all bootconsoles
1593  *    will be unregistered automatically.
1594  *  - Once a "real" console is registered, any attempt to register a
1595  *    bootconsoles will be rejected
1596  */
1597 void register_console(struct console *newcon)
1598 {
1599         int i;
1600         unsigned long flags;
1601         struct console *bcon = NULL;
1602
1603         /*
1604          * before we register a new CON_BOOT console, make sure we don't
1605          * already have a valid console
1606          */
1607         if (console_drivers && newcon->flags & CON_BOOT) {
1608                 /* find the last or real console */
1609                 for_each_console(bcon) {
1610                         if (!(bcon->flags & CON_BOOT)) {
1611                                 printk(KERN_INFO "Too late to register bootconsole %s%d\n",
1612                                         newcon->name, newcon->index);
1613                                 return;
1614                         }
1615                 }
1616         }
1617
1618         if (console_drivers && console_drivers->flags & CON_BOOT)
1619                 bcon = console_drivers;
1620
1621         if (preferred_console < 0 || bcon || !console_drivers)
1622                 preferred_console = selected_console;
1623
1624         if (newcon->early_setup)
1625                 newcon->early_setup();
1626
1627         /*
1628          *      See if we want to use this console driver. If we
1629          *      didn't select a console we take the first one
1630          *      that registers here.
1631          */
1632         if (preferred_console < 0) {
1633                 if (newcon->index < 0)
1634                         newcon->index = 0;
1635                 if (newcon->setup == NULL ||
1636                     newcon->setup(newcon, NULL) == 0) {
1637                         newcon->flags |= CON_ENABLED;
1638                         if (newcon->device) {
1639                                 newcon->flags |= CON_CONSDEV;
1640                                 preferred_console = 0;
1641                         }
1642                 }
1643         }
1644
1645         /*
1646          *      See if this console matches one we selected on
1647          *      the command line.
1648          */
1649         for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0];
1650                         i++) {
1651                 if (strcmp(console_cmdline[i].name, newcon->name) != 0)
1652                         continue;
1653                 if (newcon->index >= 0 &&
1654                     newcon->index != console_cmdline[i].index)
1655                         continue;
1656                 if (newcon->index < 0)
1657                         newcon->index = console_cmdline[i].index;
1658 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1659                 if (console_cmdline[i].brl_options) {
1660                         newcon->flags |= CON_BRL;
1661                         braille_register_console(newcon,
1662                                         console_cmdline[i].index,
1663                                         console_cmdline[i].options,
1664                                         console_cmdline[i].brl_options);
1665                         return;
1666                 }
1667 #endif
1668                 if (newcon->setup &&
1669                     newcon->setup(newcon, console_cmdline[i].options) != 0)
1670                         break;
1671                 newcon->flags |= CON_ENABLED;
1672                 newcon->index = console_cmdline[i].index;
1673                 if (i == selected_console) {
1674                         newcon->flags |= CON_CONSDEV;
1675                         preferred_console = selected_console;
1676                 }
1677                 break;
1678         }
1679
1680         if (!(newcon->flags & CON_ENABLED))
1681                 return;
1682
1683         /*
1684          * If we have a bootconsole, and are switching to a real console,
1685          * don't print everything out again, since when the boot console, and
1686          * the real console are the same physical device, it's annoying to
1687          * see the beginning boot messages twice
1688          */
1689         if (bcon && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV))
1690                 newcon->flags &= ~CON_PRINTBUFFER;
1691
1692         /*
1693          *      Put this console in the list - keep the
1694          *      preferred driver at the head of the list.
1695          */
1696         console_lock();
1697         if ((newcon->flags & CON_CONSDEV) || console_drivers == NULL) {
1698                 newcon->next = console_drivers;
1699                 console_drivers = newcon;
1700                 if (newcon->next)
1701                         newcon->next->flags &= ~CON_CONSDEV;
1702         } else {
1703                 newcon->next = console_drivers->next;
1704                 console_drivers->next = newcon;
1705         }
1706         if (newcon->flags & CON_PRINTBUFFER) {
1707                 /*
1708                  * console_unlock(); will print out the buffered messages
1709                  * for us.
1710                  */
1711                 raw_spin_lock_irqsave(&logbuf_lock, flags);
1712                 console_seq = syslog_seq;
1713                 console_idx = syslog_idx;
1714                 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
1715                 /*
1716                  * We're about to replay the log buffer.  Only do this to the
1717                  * just-registered console to avoid excessive message spam to
1718                  * the already-registered consoles.
1719                  */
1720                 exclusive_console = newcon;
1721         }
1722         console_unlock();
1723         console_sysfs_notify();
1724
1725         /*
1726          * By unregistering the bootconsoles after we enable the real console
1727          * we get the "console xxx enabled" message on all the consoles -
1728          * boot consoles, real consoles, etc - this is to ensure that end
1729          * users know there might be something in the kernel's log buffer that
1730          * went to the bootconsole (that they do not see on the real console)
1731          */
1732         if (bcon &&
1733             ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV) &&
1734             !keep_bootcon) {
1735                 /* we need to iterate through twice, to make sure we print
1736                  * everything out, before we unregister the console(s)
1737                  */
1738                 printk(KERN_INFO "console [%s%d] enabled, bootconsole disabled\n",
1739                         newcon->name, newcon->index);
1740                 for_each_console(bcon)
1741                         if (bcon->flags & CON_BOOT)
1742                                 unregister_console(bcon);
1743         } else {
1744                 printk(KERN_INFO "%sconsole [%s%d] enabled\n",
1745                         (newcon->flags & CON_BOOT) ? "boot" : "" ,
1746                         newcon->name, newcon->index);
1747         }
1748 }
1749 EXPORT_SYMBOL(register_console);
1750
1751 int unregister_console(struct console *console)
1752 {
1753         struct console *a, *b;
1754         int res = 1;
1755
1756 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1757         if (console->flags & CON_BRL)
1758                 return braille_unregister_console(console);
1759 #endif
1760
1761         console_lock();
1762         if (console_drivers == console) {
1763                 console_drivers=console->next;
1764                 res = 0;
1765         } else if (console_drivers) {
1766                 for (a=console_drivers->next, b=console_drivers ;
1767                      a; b=a, a=b->next) {
1768                         if (a == console) {
1769                                 b->next = a->next;
1770                                 res = 0;
1771                                 break;
1772                         }
1773                 }
1774         }
1775
1776         /*
1777          * If this isn't the last console and it has CON_CONSDEV set, we
1778          * need to set it on the next preferred console.
1779          */
1780         if (console_drivers != NULL && console->flags & CON_CONSDEV)
1781                 console_drivers->flags |= CON_CONSDEV;
1782
1783         console_unlock();
1784         console_sysfs_notify();
1785         return res;
1786 }
1787 EXPORT_SYMBOL(unregister_console);
1788
1789 static int __init printk_late_init(void)
1790 {
1791         struct console *con;
1792
1793         for_each_console(con) {
1794                 if (!keep_bootcon && con->flags & CON_BOOT) {
1795                         printk(KERN_INFO "turn off boot console %s%d\n",
1796                                 con->name, con->index);
1797                         unregister_console(con);
1798                 }
1799         }
1800         hotcpu_notifier(console_cpu_notify, 0);
1801         return 0;
1802 }
1803 late_initcall(printk_late_init);
1804
1805 #if defined CONFIG_PRINTK
1806
1807 int printk_sched(const char *fmt, ...)
1808 {
1809         unsigned long flags;
1810         va_list args;
1811         char *buf;
1812         int r;
1813
1814         local_irq_save(flags);
1815         buf = __get_cpu_var(printk_sched_buf);
1816
1817         va_start(args, fmt);
1818         r = vsnprintf(buf, PRINTK_BUF_SIZE, fmt, args);
1819         va_end(args);
1820
1821         __this_cpu_or(printk_pending, PRINTK_PENDING_SCHED);
1822         local_irq_restore(flags);
1823
1824         return r;
1825 }
1826
1827 /*
1828  * printk rate limiting, lifted from the networking subsystem.
1829  *
1830  * This enforces a rate limit: not more than 10 kernel messages
1831  * every 5s to make a denial-of-service attack impossible.
1832  */
1833 DEFINE_RATELIMIT_STATE(printk_ratelimit_state, 5 * HZ, 10);
1834
1835 int __printk_ratelimit(const char *func)
1836 {
1837         return ___ratelimit(&printk_ratelimit_state, func);
1838 }
1839 EXPORT_SYMBOL(__printk_ratelimit);
1840
1841 /**
1842  * printk_timed_ratelimit - caller-controlled printk ratelimiting
1843  * @caller_jiffies: pointer to caller's state
1844  * @interval_msecs: minimum interval between prints
1845  *
1846  * printk_timed_ratelimit() returns true if more than @interval_msecs
1847  * milliseconds have elapsed since the last time printk_timed_ratelimit()
1848  * returned true.
1849  */
1850 bool printk_timed_ratelimit(unsigned long *caller_jiffies,
1851                         unsigned int interval_msecs)
1852 {
1853         if (*caller_jiffies == 0
1854                         || !time_in_range(jiffies, *caller_jiffies,
1855                                         *caller_jiffies
1856                                         + msecs_to_jiffies(interval_msecs))) {
1857                 *caller_jiffies = jiffies;
1858                 return true;
1859         }
1860         return false;
1861 }
1862 EXPORT_SYMBOL(printk_timed_ratelimit);
1863
1864 static DEFINE_SPINLOCK(dump_list_lock);
1865 static LIST_HEAD(dump_list);
1866
1867 /**
1868  * kmsg_dump_register - register a kernel log dumper.
1869  * @dumper: pointer to the kmsg_dumper structure
1870  *
1871  * Adds a kernel log dumper to the system. The dump callback in the
1872  * structure will be called when the kernel oopses or panics and must be
1873  * set. Returns zero on success and %-EINVAL or %-EBUSY otherwise.
1874  */
1875 int kmsg_dump_register(struct kmsg_dumper *dumper)
1876 {
1877         unsigned long flags;
1878         int err = -EBUSY;
1879
1880         /* The dump callback needs to be set */
1881         if (!dumper->dump)
1882                 return -EINVAL;
1883
1884         spin_lock_irqsave(&dump_list_lock, flags);
1885         /* Don't allow registering multiple times */
1886         if (!dumper->registered) {
1887                 dumper->registered = 1;
1888                 list_add_tail_rcu(&dumper->list, &dump_list);
1889                 err = 0;
1890         }
1891         spin_unlock_irqrestore(&dump_list_lock, flags);
1892
1893         return err;
1894 }
1895 EXPORT_SYMBOL_GPL(kmsg_dump_register);
1896
1897 /**
1898  * kmsg_dump_unregister - unregister a kmsg dumper.
1899  * @dumper: pointer to the kmsg_dumper structure
1900  *
1901  * Removes a dump device from the system. Returns zero on success and
1902  * %-EINVAL otherwise.
1903  */
1904 int kmsg_dump_unregister(struct kmsg_dumper *dumper)
1905 {
1906         unsigned long flags;
1907         int err = -EINVAL;
1908
1909         spin_lock_irqsave(&dump_list_lock, flags);
1910         if (dumper->registered) {
1911                 dumper->registered = 0;
1912                 list_del_rcu(&dumper->list);
1913                 err = 0;
1914         }
1915         spin_unlock_irqrestore(&dump_list_lock, flags);
1916         synchronize_rcu();
1917
1918         return err;
1919 }
1920 EXPORT_SYMBOL_GPL(kmsg_dump_unregister);
1921
1922 static bool always_kmsg_dump;
1923 module_param_named(always_kmsg_dump, always_kmsg_dump, bool, S_IRUGO | S_IWUSR);
1924
1925 /**
1926  * kmsg_dump - dump kernel log to kernel message dumpers.
1927  * @reason: the reason (oops, panic etc) for dumping
1928  *
1929  * Iterate through each of the dump devices and call the oops/panic
1930  * callbacks with the log buffer.
1931  */
1932 void kmsg_dump(enum kmsg_dump_reason reason)
1933 {
1934         u64 idx;
1935         struct kmsg_dumper *dumper;
1936         const char *s1, *s2;
1937         unsigned long l1, l2;
1938         unsigned long flags;
1939
1940         if ((reason > KMSG_DUMP_OOPS) && !always_kmsg_dump)
1941                 return;
1942
1943         /* Theoretically, the log could move on after we do this, but
1944            there's not a lot we can do about that. The new messages
1945            will overwrite the start of what we dump. */
1946
1947         raw_spin_lock_irqsave(&logbuf_lock, flags);
1948         if (syslog_seq < log_first_seq)
1949                 idx = syslog_idx;
1950         else
1951                 idx = log_first_idx;
1952
1953         if (idx > log_next_idx) {
1954                 s1 = log_buf;
1955                 l1 = log_next_idx;
1956
1957                 s2 = log_buf + idx;
1958                 l2 = log_buf_len - idx;
1959         } else {
1960                 s1 = "";
1961                 l1 = 0;
1962
1963                 s2 = log_buf + idx;
1964                 l2 = log_next_idx - idx;
1965         }
1966         raw_spin_unlock_irqrestore(&logbuf_lock, flags);
1967
1968         rcu_read_lock();
1969         list_for_each_entry_rcu(dumper, &dump_list, list)
1970                 dumper->dump(dumper, reason, s1, l1, s2, l2);
1971         rcu_read_unlock();
1972 }
1973 #endif