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