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