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