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