update hdmi driver: support 480p
[firefly-linux-kernel-4.4.55.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
37 #include <asm/uaccess.h>
38
39 /*
40  * for_each_console() allows you to iterate on each console
41  */
42 #define for_each_console(con) \
43         for (con = console_drivers; con != NULL; con = con->next)
44
45 /*
46  * Architectures can override it:
47  */
48 void asmlinkage __attribute__((weak)) early_printk(const char *fmt, ...)
49 {
50 }
51
52 #define __LOG_BUF_LEN   (1 << CONFIG_LOG_BUF_SHIFT)
53
54 #ifdef        CONFIG_DEBUG_LL
55 extern void printascii(char *);
56 #endif
57
58 /* printk's without a loglevel use this.. */
59 #define DEFAULT_MESSAGE_LOGLEVEL 4 /* KERN_WARNING */
60
61 /* We show everything that is MORE important than this.. */
62 #define MINIMUM_CONSOLE_LOGLEVEL 1 /* Minimum loglevel we let people use */
63 #define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */
64
65 DECLARE_WAIT_QUEUE_HEAD(log_wait);
66
67 int console_printk[4] = {
68         DEFAULT_CONSOLE_LOGLEVEL,       /* console_loglevel */
69         DEFAULT_MESSAGE_LOGLEVEL,       /* default_message_loglevel */
70         MINIMUM_CONSOLE_LOGLEVEL,       /* minimum_console_loglevel */
71         DEFAULT_CONSOLE_LOGLEVEL,       /* default_console_loglevel */
72 };
73
74 static int saved_console_loglevel = -1;
75
76 /*
77  * Low level drivers may need that to know if they can schedule in
78  * their unblank() callback or not. So let's export it.
79  */
80 int oops_in_progress;
81 EXPORT_SYMBOL(oops_in_progress);
82
83 /*
84  * console_sem protects the console_drivers list, and also
85  * provides serialisation for access to the entire console
86  * driver system.
87  */
88 static DECLARE_MUTEX(console_sem);
89 struct console *console_drivers;
90 EXPORT_SYMBOL_GPL(console_drivers);
91
92 /*
93  * This is used for debugging the mess that is the VT code by
94  * keeping track if we have the console semaphore held. It's
95  * definitely not the perfect debug tool (we don't know if _WE_
96  * hold it are racing, but it helps tracking those weird code
97  * path in the console code where we end up in places I want
98  * locked without the console sempahore held
99  */
100 static int console_locked, console_suspended;
101
102 /*
103  * logbuf_lock protects log_buf, log_start, log_end, con_start and logged_chars
104  * It is also used in interesting ways to provide interlocking in
105  * release_console_sem().
106  */
107 static DEFINE_SPINLOCK(logbuf_lock);
108
109 #define LOG_BUF_MASK (log_buf_len-1)
110 #define LOG_BUF(idx) (log_buf[(idx) & LOG_BUF_MASK])
111
112 /*
113  * The indices into log_buf are not constrained to log_buf_len - they
114  * must be masked before subscripting
115  */
116 static unsigned log_start;      /* Index into log_buf: next char to be read by syslog() */
117 static unsigned con_start;      /* Index into log_buf: next char to be sent to consoles */
118 static unsigned log_end;        /* Index into log_buf: most-recently-written-char + 1 */
119
120 /*
121  *      Array of consoles built from command line options (console=)
122  */
123 struct console_cmdline
124 {
125         char    name[8];                        /* Name of the driver       */
126         int     index;                          /* Minor dev. to use        */
127         char    *options;                       /* Options for the driver   */
128 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
129         char    *brl_options;                   /* Options for braille driver */
130 #endif
131 };
132
133 #define MAX_CMDLINECONSOLES 8
134
135 static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
136 static int selected_console = -1;
137 static int preferred_console = -1;
138 int console_set_on_cmdline;
139 EXPORT_SYMBOL(console_set_on_cmdline);
140
141 /* Flag: console code may call schedule() */
142 static int console_may_schedule;
143
144 #ifdef CONFIG_PRINTK
145
146 static char __log_buf[__LOG_BUF_LEN];
147 static char *log_buf = __log_buf;
148 static int log_buf_len = __LOG_BUF_LEN;
149 static unsigned logged_chars; /* Number of chars produced since last read+clear operation */
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 unsigned 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 /*
311  * Commands to do_syslog:
312  *
313  *      0 -- Close the log.  Currently a NOP.
314  *      1 -- Open the log. Currently a NOP.
315  *      2 -- Read from the log.
316  *      3 -- Read all messages remaining in the ring buffer.
317  *      4 -- Read and clear all messages remaining in the ring buffer
318  *      5 -- Clear ring buffer.
319  *      6 -- Disable printk's to console
320  *      7 -- Enable printk's to console
321  *      8 -- Set level of messages printed to console
322  *      9 -- Return number of unread characters in the log buffer
323  *     10 -- Return size of the log buffer
324  */
325 int do_syslog(int type, char __user *buf, int len)
326 {
327         unsigned i, j, limit, count;
328         int do_clear = 0;
329         char c;
330         int error = 0;
331
332         error = security_syslog(type);
333         if (error)
334                 return error;
335
336         switch (type) {
337         case 0:         /* Close log */
338                 break;
339         case 1:         /* Open log */
340                 break;
341         case 2:         /* Read from log */
342                 error = -EINVAL;
343                 if (!buf || len < 0)
344                         goto out;
345                 error = 0;
346                 if (!len)
347                         goto out;
348                 if (!access_ok(VERIFY_WRITE, buf, len)) {
349                         error = -EFAULT;
350                         goto out;
351                 }
352                 error = wait_event_interruptible(log_wait,
353                                                         (log_start - log_end));
354                 if (error)
355                         goto out;
356                 i = 0;
357                 spin_lock_irq(&logbuf_lock);
358                 while (!error && (log_start != log_end) && i < len) {
359                         c = LOG_BUF(log_start);
360                         log_start++;
361                         spin_unlock_irq(&logbuf_lock);
362                         error = __put_user(c,buf);
363                         buf++;
364                         i++;
365                         cond_resched();
366                         spin_lock_irq(&logbuf_lock);
367                 }
368                 spin_unlock_irq(&logbuf_lock);
369                 if (!error)
370                         error = i;
371                 break;
372         case 4:         /* Read/clear last kernel messages */
373                 do_clear = 1;
374                 /* FALL THRU */
375         case 3:         /* Read last kernel messages */
376                 error = -EINVAL;
377                 if (!buf || len < 0)
378                         goto out;
379                 error = 0;
380                 if (!len)
381                         goto out;
382                 if (!access_ok(VERIFY_WRITE, buf, len)) {
383                         error = -EFAULT;
384                         goto out;
385                 }
386                 count = len;
387                 if (count > log_buf_len)
388                         count = log_buf_len;
389                 spin_lock_irq(&logbuf_lock);
390                 if (count > logged_chars)
391                         count = logged_chars;
392                 if (do_clear)
393                         logged_chars = 0;
394                 limit = log_end;
395                 /*
396                  * __put_user() could sleep, and while we sleep
397                  * printk() could overwrite the messages
398                  * we try to copy to user space. Therefore
399                  * the messages are copied in reverse. <manfreds>
400                  */
401                 for (i = 0; i < count && !error; i++) {
402                         j = limit-1-i;
403                         if (j + log_buf_len < log_end)
404                                 break;
405                         c = LOG_BUF(j);
406                         spin_unlock_irq(&logbuf_lock);
407                         error = __put_user(c,&buf[count-1-i]);
408                         cond_resched();
409                         spin_lock_irq(&logbuf_lock);
410                 }
411                 spin_unlock_irq(&logbuf_lock);
412                 if (error)
413                         break;
414                 error = i;
415                 if (i != count) {
416                         int offset = count-error;
417                         /* buffer overflow during copy, correct user buffer. */
418                         for (i = 0; i < error; i++) {
419                                 if (__get_user(c,&buf[i+offset]) ||
420                                     __put_user(c,&buf[i])) {
421                                         error = -EFAULT;
422                                         break;
423                                 }
424                                 cond_resched();
425                         }
426                 }
427                 break;
428         case 5:         /* Clear ring buffer */
429                 logged_chars = 0;
430                 break;
431         case 6:         /* Disable logging to console */
432                 if (saved_console_loglevel == -1)
433                         saved_console_loglevel = console_loglevel;
434                 console_loglevel = minimum_console_loglevel;
435                 break;
436         case 7:         /* Enable logging to console */
437                 if (saved_console_loglevel != -1) {
438                         console_loglevel = saved_console_loglevel;
439                         saved_console_loglevel = -1;
440                 }
441                 break;
442         case 8:         /* Set level of messages printed to console */
443                 error = -EINVAL;
444                 if (len < 1 || len > 8)
445                         goto out;
446                 if (len < minimum_console_loglevel)
447                         len = minimum_console_loglevel;
448                 console_loglevel = len;
449                 /* Implicitly re-enable logging to console */
450                 saved_console_loglevel = -1;
451                 error = 0;
452                 break;
453         case 9:         /* Number of chars in the log buffer */
454                 error = log_end - log_start;
455                 break;
456         case 10:        /* Size of the log buffer */
457                 error = log_buf_len;
458                 break;
459         default:
460                 error = -EINVAL;
461                 break;
462         }
463 out:
464         return error;
465 }
466
467 SYSCALL_DEFINE3(syslog, int, type, char __user *, buf, int, len)
468 {
469         return do_syslog(type, buf, len);
470 }
471
472 /*
473  * Call the console drivers on a range of log_buf
474  */
475 static void __call_console_drivers(unsigned start, unsigned end)
476 {
477         struct console *con;
478
479         for_each_console(con) {
480                 if ((con->flags & CON_ENABLED) && con->write &&
481                                 (cpu_online(smp_processor_id()) ||
482                                 (con->flags & CON_ANYTIME)))
483                         con->write(con, &LOG_BUF(start), end - start);
484         }
485 }
486
487 static int __read_mostly ignore_loglevel;
488
489 static int __init ignore_loglevel_setup(char *str)
490 {
491         ignore_loglevel = 1;
492         printk(KERN_INFO "debug: ignoring loglevel setting.\n");
493
494         return 0;
495 }
496
497 early_param("ignore_loglevel", ignore_loglevel_setup);
498
499 /*
500  * Write out chars from start to end - 1 inclusive
501  */
502 static void _call_console_drivers(unsigned start,
503                                 unsigned end, int msg_log_level)
504 {
505         if ((msg_log_level < console_loglevel || ignore_loglevel) &&
506                         console_drivers && start != end) {
507                 if ((start & LOG_BUF_MASK) > (end & LOG_BUF_MASK)) {
508                         /* wrapped write */
509                         __call_console_drivers(start & LOG_BUF_MASK,
510                                                 log_buf_len);
511                         __call_console_drivers(0, end & LOG_BUF_MASK);
512                 } else {
513                         __call_console_drivers(start, end);
514                 }
515         }
516 }
517
518 /*
519  * Call the console drivers, asking them to write out
520  * log_buf[start] to log_buf[end - 1].
521  * The console_sem must be held.
522  */
523 static void call_console_drivers(unsigned start, unsigned end)
524 {
525         unsigned cur_index, start_print;
526         static int msg_level = -1;
527
528         BUG_ON(((int)(start - end)) > 0);
529
530         cur_index = start;
531         start_print = start;
532         while (cur_index != end) {
533                 if (msg_level < 0 && ((end - cur_index) > 2) &&
534                                 LOG_BUF(cur_index + 0) == '<' &&
535                                 LOG_BUF(cur_index + 1) >= '0' &&
536                                 LOG_BUF(cur_index + 1) <= '7' &&
537                                 LOG_BUF(cur_index + 2) == '>') {
538                         msg_level = LOG_BUF(cur_index + 1) - '0';
539                         cur_index += 3;
540                         start_print = cur_index;
541                 }
542                 while (cur_index != end) {
543                         char c = LOG_BUF(cur_index);
544
545                         cur_index++;
546                         if (c == '\n') {
547                                 if (msg_level < 0) {
548                                         /*
549                                          * printk() has already given us loglevel tags in
550                                          * the buffer.  This code is here in case the
551                                          * log buffer has wrapped right round and scribbled
552                                          * on those tags
553                                          */
554                                         msg_level = default_message_loglevel;
555                                 }
556                                 _call_console_drivers(start_print, cur_index, msg_level);
557                                 msg_level = -1;
558                                 start_print = cur_index;
559                                 break;
560                         }
561                 }
562         }
563         _call_console_drivers(start_print, end, msg_level);
564 }
565
566 static void emit_log_char(char c)
567 {
568         LOG_BUF(log_end) = c;
569         log_end++;
570         if (log_end - log_start > log_buf_len)
571                 log_start = log_end - log_buf_len;
572         if (log_end - con_start > log_buf_len)
573                 con_start = log_end - log_buf_len;
574         if (logged_chars < log_buf_len)
575                 logged_chars++;
576 }
577
578 /*
579  * Zap console related locks when oopsing. Only zap at most once
580  * every 10 seconds, to leave time for slow consoles to print a
581  * full oops.
582  */
583 static void zap_locks(void)
584 {
585         static unsigned long oops_timestamp;
586
587         if (time_after_eq(jiffies, oops_timestamp) &&
588                         !time_after(jiffies, oops_timestamp + 30 * HZ))
589                 return;
590
591         oops_timestamp = jiffies;
592
593         /* If a crash is occurring, make sure we can't deadlock */
594         spin_lock_init(&logbuf_lock);
595         /* And make sure that we print immediately */
596         init_MUTEX(&console_sem);
597 }
598
599 #if defined(CONFIG_PRINTK_TIME)
600 static int printk_time = 1;
601 #else
602 static int printk_time = 0;
603 #endif
604 module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR);
605
606 /* Check if we have any console registered that can be called early in boot. */
607 static int have_callable_console(void)
608 {
609         struct console *con;
610
611         for_each_console(con)
612                 if (con->flags & CON_ANYTIME)
613                         return 1;
614
615         return 0;
616 }
617
618 /**
619  * printk - print a kernel message
620  * @fmt: format string
621  *
622  * This is printk().  It can be called from any context.  We want it to work.
623  *
624  * We try to grab the console_sem.  If we succeed, it's easy - we log the output and
625  * call the console drivers.  If we fail to get the semaphore we place the output
626  * into the log buffer and return.  The current holder of the console_sem will
627  * notice the new output in release_console_sem() and will send it to the
628  * consoles before releasing the semaphore.
629  *
630  * One effect of this deferred printing is that code which calls printk() and
631  * then changes console_loglevel may break. This is because console_loglevel
632  * is inspected when the actual printing occurs.
633  *
634  * See also:
635  * printf(3)
636  *
637  * See the vsnprintf() documentation for format string extensions over C99.
638  */
639
640 asmlinkage int printk(const char *fmt, ...)
641 {
642         va_list args;
643         int r;
644
645         va_start(args, fmt);
646         r = vprintk(fmt, args);
647         va_end(args);
648
649         return r;
650 }
651
652 /* cpu currently holding logbuf_lock */
653 static volatile unsigned int printk_cpu = UINT_MAX;
654
655 /*
656  * Can we actually use the console at this time on this cpu?
657  *
658  * Console drivers may assume that per-cpu resources have
659  * been allocated. So unless they're explicitly marked as
660  * being able to cope (CON_ANYTIME) don't call them until
661  * this CPU is officially up.
662  */
663 static inline int can_use_console(unsigned int cpu)
664 {
665         return cpu_online(cpu) || have_callable_console();
666 }
667
668 /*
669  * Try to get console ownership to actually show the kernel
670  * messages from a 'printk'. Return true (and with the
671  * console_semaphore held, and 'console_locked' set) if it
672  * is successful, false otherwise.
673  *
674  * This gets called with the 'logbuf_lock' spinlock held and
675  * interrupts disabled. It should return with 'lockbuf_lock'
676  * released but interrupts still disabled.
677  */
678 static int acquire_console_semaphore_for_printk(unsigned int cpu)
679 {
680         int retval = 0;
681
682         if (!try_acquire_console_sem()) {
683                 retval = 1;
684
685                 /*
686                  * If we can't use the console, we need to release
687                  * the console semaphore by hand to avoid flushing
688                  * the buffer. We need to hold the console semaphore
689                  * in order to do this test safely.
690                  */
691                 if (!can_use_console(cpu)) {
692                         console_locked = 0;
693                         up(&console_sem);
694                         retval = 0;
695                 }
696         }
697         printk_cpu = UINT_MAX;
698         spin_unlock(&logbuf_lock);
699         return retval;
700 }
701 static const char recursion_bug_msg [] =
702                 KERN_CRIT "BUG: recent printk recursion!\n";
703 static int recursion_bug;
704 static int new_text_line = 1;
705 static char printk_buf[1024];
706
707 int printk_delay_msec __read_mostly;
708
709 static inline void printk_delay(void)
710 {
711         if (unlikely(printk_delay_msec)) {
712                 int m = printk_delay_msec;
713
714                 while (m--) {
715                         mdelay(1);
716                         touch_nmi_watchdog();
717                 }
718         }
719 }
720
721 asmlinkage int vprintk(const char *fmt, va_list args)
722 {
723         int printed_len = 0;
724         int current_log_level = default_message_loglevel;
725         unsigned long flags;
726         int this_cpu;
727         char *p;
728
729         boot_delay_msec();
730         printk_delay();
731
732         preempt_disable();
733         /* This stops the holder of console_sem just where we want him */
734         raw_local_irq_save(flags);
735         this_cpu = smp_processor_id();
736
737         /*
738          * Ouch, printk recursed into itself!
739          */
740         if (unlikely(printk_cpu == this_cpu)) {
741                 /*
742                  * If a crash is occurring during printk() on this CPU,
743                  * then try to get the crash message out but make sure
744                  * we can't deadlock. Otherwise just return to avoid the
745                  * recursion and return - but flag the recursion so that
746                  * it can be printed at the next appropriate moment:
747                  */
748                 if (!oops_in_progress) {
749                         recursion_bug = 1;
750                         goto out_restore_irqs;
751                 }
752                 zap_locks();
753         }
754
755         lockdep_off();
756         spin_lock(&logbuf_lock);
757         printk_cpu = this_cpu;
758
759         if (recursion_bug) {
760                 recursion_bug = 0;
761                 strcpy(printk_buf, recursion_bug_msg);
762                 printed_len = strlen(recursion_bug_msg);
763         }
764         /* Emit the output into the temporary buffer */
765         printed_len += vscnprintf(printk_buf + printed_len,
766                                   sizeof(printk_buf) - printed_len, fmt, args);
767
768 #ifdef  CONFIG_DEBUG_LL
769         printascii(printk_buf);
770 #endif
771
772         p = printk_buf;
773
774         /* Do we have a loglevel in the string? */
775         if (p[0] == '<') {
776                 unsigned char c = p[1];
777                 if (c && p[2] == '>') {
778                         switch (c) {
779                         case '0' ... '7': /* loglevel */
780                                 current_log_level = c - '0';
781                         /* Fallthrough - make sure we're on a new line */
782                         case 'd': /* KERN_DEFAULT */
783                                 if (!new_text_line) {
784                                         emit_log_char('\n');
785                                         new_text_line = 1;
786                                 }
787                         /* Fallthrough - skip the loglevel */
788                         case 'c': /* KERN_CONT */
789                                 p += 3;
790                                 break;
791                         }
792                 }
793         }
794
795         /*
796          * Copy the output into log_buf.  If the caller didn't provide
797          * appropriate log level tags, we insert them here
798          */
799         for ( ; *p; p++) {
800                 if (new_text_line) {
801                         /* Always output the token */
802                         emit_log_char('<');
803                         emit_log_char(current_log_level + '0');
804                         emit_log_char('>');
805                         printed_len += 3;
806                         new_text_line = 0;
807
808                         if (printk_time) {
809                                 /* Follow the token with the time */
810                                 char tbuf[50], *tp;
811                                 unsigned tlen;
812                                 unsigned long long t;
813                                 unsigned long nanosec_rem;
814
815                                 t = cpu_clock(printk_cpu);
816                                 nanosec_rem = do_div(t, 1000000000);
817                                 tlen = sprintf(tbuf, "[%5lu.%06lu] ",
818                                                 (unsigned long) t,
819                                                 nanosec_rem / 1000);
820
821                                 for (tp = tbuf; tp < tbuf + tlen; tp++)
822                                         emit_log_char(*tp);
823                                 printed_len += tlen;
824                         }
825
826                         if (!*p)
827                                 break;
828                 }
829
830                 emit_log_char(*p);
831                 if (*p == '\n')
832                         new_text_line = 1;
833         }
834
835         /*
836          * Try to acquire and then immediately release the
837          * console semaphore. The release will do all the
838          * actual magic (print out buffers, wake up klogd,
839          * etc). 
840          *
841          * The acquire_console_semaphore_for_printk() function
842          * will release 'logbuf_lock' regardless of whether it
843          * actually gets the semaphore or not.
844          */
845         if (acquire_console_semaphore_for_printk(this_cpu))
846                 release_console_sem();
847
848         lockdep_on();
849 out_restore_irqs:
850         raw_local_irq_restore(flags);
851
852         preempt_enable();
853         return printed_len;
854 }
855 EXPORT_SYMBOL(printk);
856 EXPORT_SYMBOL(vprintk);
857
858 #else
859
860 static void call_console_drivers(unsigned start, unsigned end)
861 {
862 }
863
864 #endif
865
866 static int __add_preferred_console(char *name, int idx, char *options,
867                                    char *brl_options)
868 {
869         struct console_cmdline *c;
870         int i;
871
872         /*
873          *      See if this tty is not yet registered, and
874          *      if we have a slot free.
875          */
876         for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
877                 if (strcmp(console_cmdline[i].name, name) == 0 &&
878                           console_cmdline[i].index == idx) {
879                                 if (!brl_options)
880                                         selected_console = i;
881                                 return 0;
882                 }
883         if (i == MAX_CMDLINECONSOLES)
884                 return -E2BIG;
885         if (!brl_options)
886                 selected_console = i;
887         c = &console_cmdline[i];
888         strlcpy(c->name, name, sizeof(c->name));
889         c->options = options;
890 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
891         c->brl_options = brl_options;
892 #endif
893         c->index = idx;
894         return 0;
895 }
896 /*
897  * Set up a list of consoles.  Called from init/main.c
898  */
899 static int __init console_setup(char *str)
900 {
901         char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for index */
902         char *s, *options, *brl_options = NULL;
903         int idx;
904
905 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
906         if (!memcmp(str, "brl,", 4)) {
907                 brl_options = "";
908                 str += 4;
909         } else if (!memcmp(str, "brl=", 4)) {
910                 brl_options = str + 4;
911                 str = strchr(brl_options, ',');
912                 if (!str) {
913                         printk(KERN_ERR "need port name after brl=\n");
914                         return 1;
915                 }
916                 *(str++) = 0;
917         }
918 #endif
919
920         /*
921          * Decode str into name, index, options.
922          */
923         if (str[0] >= '0' && str[0] <= '9') {
924                 strcpy(buf, "ttyS");
925                 strncpy(buf + 4, str, sizeof(buf) - 5);
926         } else {
927                 strncpy(buf, str, sizeof(buf) - 1);
928         }
929         buf[sizeof(buf) - 1] = 0;
930         if ((options = strchr(str, ',')) != NULL)
931                 *(options++) = 0;
932 #ifdef __sparc__
933         if (!strcmp(str, "ttya"))
934                 strcpy(buf, "ttyS0");
935         if (!strcmp(str, "ttyb"))
936                 strcpy(buf, "ttyS1");
937 #endif
938         for (s = buf; *s; s++)
939                 if ((*s >= '0' && *s <= '9') || *s == ',')
940                         break;
941         idx = simple_strtoul(s, NULL, 10);
942         *s = 0;
943
944         __add_preferred_console(buf, idx, options, brl_options);
945         console_set_on_cmdline = 1;
946         return 1;
947 }
948 __setup("console=", console_setup);
949
950 /**
951  * add_preferred_console - add a device to the list of preferred consoles.
952  * @name: device name
953  * @idx: device index
954  * @options: options for this console
955  *
956  * The last preferred console added will be used for kernel messages
957  * and stdin/out/err for init.  Normally this is used by console_setup
958  * above to handle user-supplied console arguments; however it can also
959  * be used by arch-specific code either to override the user or more
960  * commonly to provide a default console (ie from PROM variables) when
961  * the user has not supplied one.
962  */
963 int add_preferred_console(char *name, int idx, char *options)
964 {
965         return __add_preferred_console(name, idx, options, NULL);
966 }
967
968 int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options)
969 {
970         struct console_cmdline *c;
971         int i;
972
973         for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
974                 if (strcmp(console_cmdline[i].name, name) == 0 &&
975                           console_cmdline[i].index == idx) {
976                                 c = &console_cmdline[i];
977                                 strlcpy(c->name, name_new, sizeof(c->name));
978                                 c->name[sizeof(c->name) - 1] = 0;
979                                 c->options = options;
980                                 c->index = idx_new;
981                                 return i;
982                 }
983         /* not found */
984         return -1;
985 }
986
987 int console_suspend_enabled = 1;
988 EXPORT_SYMBOL(console_suspend_enabled);
989
990 static int __init console_suspend_disable(char *str)
991 {
992         console_suspend_enabled = 0;
993         return 1;
994 }
995 __setup("no_console_suspend", console_suspend_disable);
996
997 /**
998  * suspend_console - suspend the console subsystem
999  *
1000  * This disables printk() while we go into suspend states
1001  */
1002 void suspend_console(void)
1003 {
1004         if (!console_suspend_enabled)
1005                 return;
1006         printk("Suspending console(s) (use no_console_suspend to debug)\n");
1007         acquire_console_sem();
1008         console_suspended = 1;
1009         up(&console_sem);
1010 }
1011
1012 void resume_console(void)
1013 {
1014         if (!console_suspend_enabled)
1015                 return;
1016         down(&console_sem);
1017         console_suspended = 0;
1018         release_console_sem();
1019 }
1020
1021 /**
1022  * acquire_console_sem - lock the console system for exclusive use.
1023  *
1024  * Acquires a semaphore which guarantees that the caller has
1025  * exclusive access to the console system and the console_drivers list.
1026  *
1027  * Can sleep, returns nothing.
1028  */
1029 void acquire_console_sem(void)
1030 {
1031         BUG_ON(in_interrupt());
1032         down(&console_sem);
1033         if (console_suspended)
1034                 return;
1035         console_locked = 1;
1036         console_may_schedule = 1;
1037 }
1038 EXPORT_SYMBOL(acquire_console_sem);
1039
1040 int try_acquire_console_sem(void)
1041 {
1042         if (down_trylock(&console_sem))
1043                 return -1;
1044         if (console_suspended) {
1045                 up(&console_sem);
1046                 return -1;
1047         }
1048         console_locked = 1;
1049         console_may_schedule = 0;
1050         return 0;
1051 }
1052 EXPORT_SYMBOL(try_acquire_console_sem);
1053
1054 int is_console_locked(void)
1055 {
1056         return console_locked;
1057 }
1058
1059 static DEFINE_PER_CPU(int, printk_pending);
1060
1061 void printk_tick(void)
1062 {
1063         if (__get_cpu_var(printk_pending)) {
1064                 __get_cpu_var(printk_pending) = 0;
1065                 wake_up_interruptible(&log_wait);
1066         }
1067 }
1068
1069 int printk_needs_cpu(int cpu)
1070 {
1071         if (unlikely(cpu_is_offline(cpu)))
1072                 printk_tick();
1073         return per_cpu(printk_pending, cpu);
1074 }
1075
1076 void wake_up_klogd(void)
1077 {
1078         if (waitqueue_active(&log_wait))
1079                 __raw_get_cpu_var(printk_pending) = 1;
1080 }
1081
1082 /**
1083  * release_console_sem - unlock the console system
1084  *
1085  * Releases the semaphore which the caller holds on the console system
1086  * and the console driver list.
1087  *
1088  * While the semaphore was held, console output may have been buffered
1089  * by printk().  If this is the case, release_console_sem() emits
1090  * the output prior to releasing the semaphore.
1091  *
1092  * If there is output waiting for klogd, we wake it up.
1093  *
1094  * release_console_sem() may be called from any context.
1095  */
1096 void release_console_sem(void)
1097 {
1098         unsigned long flags;
1099         unsigned _con_start, _log_end;
1100         unsigned wake_klogd = 0;
1101
1102         if (console_suspended) {
1103                 up(&console_sem);
1104                 return;
1105         }
1106
1107         console_may_schedule = 0;
1108
1109         for ( ; ; ) {
1110                 spin_lock_irqsave(&logbuf_lock, flags);
1111                 wake_klogd |= log_start - log_end;
1112                 if (con_start == log_end)
1113                         break;                  /* Nothing to print */
1114                 _con_start = con_start;
1115                 _log_end = log_end;
1116                 con_start = log_end;            /* Flush */
1117                 spin_unlock(&logbuf_lock);
1118                 stop_critical_timings();        /* don't trace print latency */
1119                 call_console_drivers(_con_start, _log_end);
1120                 start_critical_timings();
1121                 local_irq_restore(flags);
1122         }
1123         console_locked = 0;
1124         up(&console_sem);
1125         spin_unlock_irqrestore(&logbuf_lock, flags);
1126         if (wake_klogd)
1127                 wake_up_klogd();
1128 }
1129 EXPORT_SYMBOL(release_console_sem);
1130
1131 /**
1132  * console_conditional_schedule - yield the CPU if required
1133  *
1134  * If the console code is currently allowed to sleep, and
1135  * if this CPU should yield the CPU to another task, do
1136  * so here.
1137  *
1138  * Must be called within acquire_console_sem().
1139  */
1140 void __sched console_conditional_schedule(void)
1141 {
1142         if (console_may_schedule)
1143                 cond_resched();
1144 }
1145 EXPORT_SYMBOL(console_conditional_schedule);
1146
1147 void console_unblank(void)
1148 {
1149         struct console *c;
1150
1151         /*
1152          * console_unblank can no longer be called in interrupt context unless
1153          * oops_in_progress is set to 1..
1154          */
1155         if (oops_in_progress) {
1156                 if (down_trylock(&console_sem) != 0)
1157                         return;
1158         } else
1159                 acquire_console_sem();
1160
1161         console_locked = 1;
1162         console_may_schedule = 0;
1163         for_each_console(c)
1164                 if ((c->flags & CON_ENABLED) && c->unblank)
1165                         c->unblank();
1166         release_console_sem();
1167 }
1168
1169 /*
1170  * Return the console tty driver structure and its associated index
1171  */
1172 struct tty_driver *console_device(int *index)
1173 {
1174         struct console *c;
1175         struct tty_driver *driver = NULL;
1176
1177         acquire_console_sem();
1178         for_each_console(c) {
1179                 if (!c->device)
1180                         continue;
1181                 driver = c->device(c, index);
1182                 if (driver)
1183                         break;
1184         }
1185         release_console_sem();
1186         return driver;
1187 }
1188
1189 /*
1190  * Prevent further output on the passed console device so that (for example)
1191  * serial drivers can disable console output before suspending a port, and can
1192  * re-enable output afterwards.
1193  */
1194 void console_stop(struct console *console)
1195 {
1196         acquire_console_sem();
1197         console->flags &= ~CON_ENABLED;
1198         release_console_sem();
1199 }
1200 EXPORT_SYMBOL(console_stop);
1201
1202 void console_start(struct console *console)
1203 {
1204         acquire_console_sem();
1205         console->flags |= CON_ENABLED;
1206         release_console_sem();
1207 }
1208 EXPORT_SYMBOL(console_start);
1209
1210 /*
1211  * The console driver calls this routine during kernel initialization
1212  * to register the console printing procedure with printk() and to
1213  * print any messages that were printed by the kernel before the
1214  * console driver was initialized.
1215  *
1216  * This can happen pretty early during the boot process (because of
1217  * early_printk) - sometimes before setup_arch() completes - be careful
1218  * of what kernel features are used - they may not be initialised yet.
1219  *
1220  * There are two types of consoles - bootconsoles (early_printk) and
1221  * "real" consoles (everything which is not a bootconsole) which are
1222  * handled differently.
1223  *  - Any number of bootconsoles can be registered at any time.
1224  *  - As soon as a "real" console is registered, all bootconsoles
1225  *    will be unregistered automatically.
1226  *  - Once a "real" console is registered, any attempt to register a
1227  *    bootconsoles will be rejected
1228  */
1229 void register_console(struct console *newcon)
1230 {
1231         int i;
1232         unsigned long flags;
1233         struct console *bcon = NULL;
1234
1235         /*
1236          * before we register a new CON_BOOT console, make sure we don't
1237          * already have a valid console
1238          */
1239         if (console_drivers && newcon->flags & CON_BOOT) {
1240                 /* find the last or real console */
1241                 for_each_console(bcon) {
1242                         if (!(bcon->flags & CON_BOOT)) {
1243                                 printk(KERN_INFO "Too late to register bootconsole %s%d\n",
1244                                         newcon->name, newcon->index);
1245                                 return;
1246                         }
1247                 }
1248         }
1249
1250         if (console_drivers && console_drivers->flags & CON_BOOT)
1251                 bcon = console_drivers;
1252
1253         if (preferred_console < 0 || bcon || !console_drivers)
1254                 preferred_console = selected_console;
1255
1256         if (newcon->early_setup)
1257                 newcon->early_setup();
1258
1259         /*
1260          *      See if we want to use this console driver. If we
1261          *      didn't select a console we take the first one
1262          *      that registers here.
1263          */
1264         if (preferred_console < 0) {
1265                 if (newcon->index < 0)
1266                         newcon->index = 0;
1267                 if (newcon->setup == NULL ||
1268                     newcon->setup(newcon, NULL) == 0) {
1269                         newcon->flags |= CON_ENABLED;
1270                         if (newcon->device) {
1271                                 newcon->flags |= CON_CONSDEV;
1272                                 preferred_console = 0;
1273                         }
1274                 }
1275         }
1276
1277         /*
1278          *      See if this console matches one we selected on
1279          *      the command line.
1280          */
1281         for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0];
1282                         i++) {
1283                 if (strcmp(console_cmdline[i].name, newcon->name) != 0)
1284                         continue;
1285                 if (newcon->index >= 0 &&
1286                     newcon->index != console_cmdline[i].index)
1287                         continue;
1288                 if (newcon->index < 0)
1289                         newcon->index = console_cmdline[i].index;
1290 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1291                 if (console_cmdline[i].brl_options) {
1292                         newcon->flags |= CON_BRL;
1293                         braille_register_console(newcon,
1294                                         console_cmdline[i].index,
1295                                         console_cmdline[i].options,
1296                                         console_cmdline[i].brl_options);
1297                         return;
1298                 }
1299 #endif
1300                 if (newcon->setup &&
1301                     newcon->setup(newcon, console_cmdline[i].options) != 0)
1302                         break;
1303                 newcon->flags |= CON_ENABLED;
1304                 newcon->index = console_cmdline[i].index;
1305                 if (i == selected_console) {
1306                         newcon->flags |= CON_CONSDEV;
1307                         preferred_console = selected_console;
1308                 }
1309                 break;
1310         }
1311
1312         if (!(newcon->flags & CON_ENABLED))
1313                 return;
1314
1315         /*
1316          * If we have a bootconsole, and are switching to a real console,
1317          * don't print everything out again, since when the boot console, and
1318          * the real console are the same physical device, it's annoying to
1319          * see the beginning boot messages twice
1320          */
1321         if (bcon && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV))
1322                 newcon->flags &= ~CON_PRINTBUFFER;
1323
1324         /*
1325          *      Put this console in the list - keep the
1326          *      preferred driver at the head of the list.
1327          */
1328         acquire_console_sem();
1329         if ((newcon->flags & CON_CONSDEV) || console_drivers == NULL) {
1330                 newcon->next = console_drivers;
1331                 console_drivers = newcon;
1332                 if (newcon->next)
1333                         newcon->next->flags &= ~CON_CONSDEV;
1334         } else {
1335                 newcon->next = console_drivers->next;
1336                 console_drivers->next = newcon;
1337         }
1338         if (newcon->flags & CON_PRINTBUFFER) {
1339                 /*
1340                  * release_console_sem() will print out the buffered messages
1341                  * for us.
1342                  */
1343                 spin_lock_irqsave(&logbuf_lock, flags);
1344                 con_start = log_start;
1345                 spin_unlock_irqrestore(&logbuf_lock, flags);
1346         }
1347         release_console_sem();
1348
1349         /*
1350          * By unregistering the bootconsoles after we enable the real console
1351          * we get the "console xxx enabled" message on all the consoles -
1352          * boot consoles, real consoles, etc - this is to ensure that end
1353          * users know there might be something in the kernel's log buffer that
1354          * went to the bootconsole (that they do not see on the real console)
1355          */
1356         if (bcon && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV)) {
1357                 /* we need to iterate through twice, to make sure we print
1358                  * everything out, before we unregister the console(s)
1359                  */
1360                 printk(KERN_INFO "console [%s%d] enabled, bootconsole disabled\n",
1361                         newcon->name, newcon->index);
1362                 for_each_console(bcon)
1363                         if (bcon->flags & CON_BOOT)
1364                                 unregister_console(bcon);
1365         } else {
1366                 printk(KERN_INFO "%sconsole [%s%d] enabled\n",
1367                         (newcon->flags & CON_BOOT) ? "boot" : "" ,
1368                         newcon->name, newcon->index);
1369         }
1370 }
1371 EXPORT_SYMBOL(register_console);
1372
1373 int unregister_console(struct console *console)
1374 {
1375         struct console *a, *b;
1376         int res = 1;
1377
1378 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1379         if (console->flags & CON_BRL)
1380                 return braille_unregister_console(console);
1381 #endif
1382
1383         acquire_console_sem();
1384         if (console_drivers == console) {
1385                 console_drivers=console->next;
1386                 res = 0;
1387         } else if (console_drivers) {
1388                 for (a=console_drivers->next, b=console_drivers ;
1389                      a; b=a, a=b->next) {
1390                         if (a == console) {
1391                                 b->next = a->next;
1392                                 res = 0;
1393                                 break;
1394                         }
1395                 }
1396         }
1397
1398         /*
1399          * If this isn't the last console and it has CON_CONSDEV set, we
1400          * need to set it on the next preferred console.
1401          */
1402         if (console_drivers != NULL && console->flags & CON_CONSDEV)
1403                 console_drivers->flags |= CON_CONSDEV;
1404
1405         release_console_sem();
1406         return res;
1407 }
1408 EXPORT_SYMBOL(unregister_console);
1409
1410 static int __init disable_boot_consoles(void)
1411 {
1412         struct console *con;
1413
1414         for_each_console(con) {
1415                 if (con->flags & CON_BOOT) {
1416                         printk(KERN_INFO "turn off boot console %s%d\n",
1417                                 con->name, con->index);
1418                         unregister_console(con);
1419                 }
1420         }
1421         return 0;
1422 }
1423 late_initcall(disable_boot_consoles);
1424
1425 #if defined CONFIG_PRINTK
1426
1427 /*
1428  * printk rate limiting, lifted from the networking subsystem.
1429  *
1430  * This enforces a rate limit: not more than 10 kernel messages
1431  * every 5s to make a denial-of-service attack impossible.
1432  */
1433 DEFINE_RATELIMIT_STATE(printk_ratelimit_state, 5 * HZ, 10);
1434
1435 int printk_ratelimit(void)
1436 {
1437         return __ratelimit(&printk_ratelimit_state);
1438 }
1439 EXPORT_SYMBOL(printk_ratelimit);
1440
1441 /**
1442  * printk_timed_ratelimit - caller-controlled printk ratelimiting
1443  * @caller_jiffies: pointer to caller's state
1444  * @interval_msecs: minimum interval between prints
1445  *
1446  * printk_timed_ratelimit() returns true if more than @interval_msecs
1447  * milliseconds have elapsed since the last time printk_timed_ratelimit()
1448  * returned true.
1449  */
1450 bool printk_timed_ratelimit(unsigned long *caller_jiffies,
1451                         unsigned int interval_msecs)
1452 {
1453         if (*caller_jiffies == 0
1454                         || !time_in_range(jiffies, *caller_jiffies,
1455                                         *caller_jiffies
1456                                         + msecs_to_jiffies(interval_msecs))) {
1457                 *caller_jiffies = jiffies;
1458                 return true;
1459         }
1460         return false;
1461 }
1462 EXPORT_SYMBOL(printk_timed_ratelimit);
1463 #endif