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