rockchip: video: fix logo display when switch screen
[firefly-linux-kernel-4.4.55.git] / kernel / printk.c
1 /*
2  *  linux/kernel/printk.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  *
6  * Modified to make sys_syslog() more flexible: added commands to
7  * return the last 4k of kernel messages, regardless of whether
8  * they've been read or not.  Added option to suppress kernel printk's
9  * to the console.  Added hook for sending the console messages
10  * elsewhere, in preparation for a serial line console (someday).
11  * Ted Ts'o, 2/11/93.
12  * Modified for sysctl support, 1/8/97, Chris Horn.
13  * Fixed SMP synchronization, 08/08/99, Manfred Spraul
14  *     manfred@colorfullife.com
15  * Rewrote bits to get rid of console_lock
16  *      01Mar01 Andrew Morton
17  */
18
19 #include <linux/kernel.h>
20 #include <linux/mm.h>
21 #include <linux/tty.h>
22 #include <linux/tty_driver.h>
23 #include <linux/console.h>
24 #include <linux/init.h>
25 #include <linux/jiffies.h>
26 #include <linux/nmi.h>
27 #include <linux/module.h>
28 #include <linux/moduleparam.h>
29 #include <linux/interrupt.h>                    /* For in_interrupt() */
30 #include <linux/delay.h>
31 #include <linux/smp.h>
32 #include <linux/security.h>
33 #include <linux/bootmem.h>
34 #include <linux/memblock.h>
35 #include <linux/aio.h>
36 #include <linux/syscalls.h>
37 #include <linux/kexec.h>
38 #include <linux/kdb.h>
39 #include <linux/ratelimit.h>
40 #include <linux/kmsg_dump.h>
41 #include <linux/syslog.h>
42 #include <linux/cpu.h>
43 #include <linux/notifier.h>
44 #include <linux/rculist.h>
45 #include <linux/poll.h>
46 #include <linux/irq_work.h>
47 #include <linux/utsname.h>
48
49 #include <asm/uaccess.h>
50
51 #define CREATE_TRACE_POINTS
52 #include <trace/events/printk.h>
53
54 #ifdef CONFIG_EARLY_PRINTK_DIRECT
55 extern void printascii(char *);
56 #endif
57
58 /* printk's without a loglevel use this.. */
59 #define DEFAULT_MESSAGE_LOGLEVEL CONFIG_DEFAULT_MESSAGE_LOGLEVEL
60
61 /* We show everything that is MORE important than this.. */
62 #define MINIMUM_CONSOLE_LOGLEVEL 1 /* Minimum loglevel we let people use */
63 #define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */
64
65 int console_printk[4] = {
66         DEFAULT_CONSOLE_LOGLEVEL,       /* console_loglevel */
67         DEFAULT_MESSAGE_LOGLEVEL,       /* default_message_loglevel */
68         MINIMUM_CONSOLE_LOGLEVEL,       /* minimum_console_loglevel */
69         DEFAULT_CONSOLE_LOGLEVEL,       /* default_console_loglevel */
70 };
71
72 /*
73  * Low level drivers may need that to know if they can schedule in
74  * their unblank() callback or not. So let's export it.
75  */
76 int oops_in_progress;
77 EXPORT_SYMBOL(oops_in_progress);
78
79 /*
80  * console_sem protects the console_drivers list, and also
81  * provides serialisation for access to the entire console
82  * driver system.
83  */
84 static DEFINE_SEMAPHORE(console_sem);
85 struct console *console_drivers;
86 EXPORT_SYMBOL_GPL(console_drivers);
87
88 #ifdef CONFIG_LOCKDEP
89 static struct lockdep_map console_lock_dep_map = {
90         .name = "console_lock"
91 };
92 #endif
93
94 /*
95  * This is used for debugging the mess that is the VT code by
96  * keeping track if we have the console semaphore held. It's
97  * definitely not the perfect debug tool (we don't know if _WE_
98  * hold it are racing, but it helps tracking those weird code
99  * path in the console code where we end up in places I want
100  * locked without the console sempahore held
101  */
102 static int console_locked, console_suspended;
103
104 /*
105  * If exclusive_console is non-NULL then only this console is to be printed to.
106  */
107 static struct console *exclusive_console;
108
109 /*
110  *      Array of consoles built from command line options (console=)
111  */
112 struct console_cmdline
113 {
114         char    name[8];                        /* Name of the driver       */
115         int     index;                          /* Minor dev. to use        */
116         char    *options;                       /* Options for the driver   */
117 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
118         char    *brl_options;                   /* Options for braille driver */
119 #endif
120 };
121
122 #define MAX_CMDLINECONSOLES 8
123
124 static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
125 static int selected_console = -1;
126 static int preferred_console = -1;
127 int console_set_on_cmdline;
128 EXPORT_SYMBOL(console_set_on_cmdline);
129
130 /* Flag: console code may call schedule() */
131 static int console_may_schedule;
132
133 /*
134  * The printk log buffer consists of a chain of concatenated variable
135  * length records. Every record starts with a record header, containing
136  * the overall length of the record.
137  *
138  * The heads to the first and last entry in the buffer, as well as the
139  * sequence numbers of these both entries are maintained when messages
140  * are stored..
141  *
142  * If the heads indicate available messages, the length in the header
143  * tells the start next message. A length == 0 for the next message
144  * indicates a wrap-around to the beginning of the buffer.
145  *
146  * Every record carries the monotonic timestamp in microseconds, as well as
147  * the standard userspace syslog level and syslog facility. The usual
148  * kernel messages use LOG_KERN; userspace-injected messages always carry
149  * a matching syslog facility, by default LOG_USER. The origin of every
150  * message can be reliably determined that way.
151  *
152  * The human readable log message directly follows the message header. The
153  * length of the message text is stored in the header, the stored message
154  * is not terminated.
155  *
156  * Optionally, a message can carry a dictionary of properties (key/value pairs),
157  * to provide userspace with a machine-readable message context.
158  *
159  * Examples for well-defined, commonly used property names are:
160  *   DEVICE=b12:8               device identifier
161  *                                b12:8         block dev_t
162  *                                c127:3        char dev_t
163  *                                n8            netdev ifindex
164  *                                +sound:card0  subsystem:devname
165  *   SUBSYSTEM=pci              driver-core subsystem name
166  *
167  * Valid characters in property names are [a-zA-Z0-9.-_]. The plain text value
168  * follows directly after a '=' character. Every property is terminated by
169  * a '\0' character. The last property is not terminated.
170  *
171  * Example of a message structure:
172  *   0000  ff 8f 00 00 00 00 00 00      monotonic time in nsec
173  *   0008  34 00                        record is 52 bytes long
174  *   000a        0b 00                  text is 11 bytes long
175  *   000c              1f 00            dictionary is 23 bytes long
176  *   000e                    03 00      LOG_KERN (facility) LOG_ERR (level)
177  *   0010  69 74 27 73 20 61 20 6c      "it's a l"
178  *         69 6e 65                     "ine"
179  *   001b           44 45 56 49 43      "DEVIC"
180  *         45 3d 62 38 3a 32 00 44      "E=b8:2\0D"
181  *         52 49 56 45 52 3d 62 75      "RIVER=bu"
182  *         67                           "g"
183  *   0032     00 00 00                  padding to next message header
184  *
185  * The 'struct log' buffer header must never be directly exported to
186  * userspace, it is a kernel-private implementation detail that might
187  * need to be changed in the future, when the requirements change.
188  *
189  * /dev/kmsg exports the structured data in the following line format:
190  *   "level,sequnum,timestamp;<message text>\n"
191  *
192  * The optional key/value pairs are attached as continuation lines starting
193  * with a space character and terminated by a newline. All possible
194  * non-prinatable characters are escaped in the "\xff" notation.
195  *
196  * Users of the export format should ignore possible additional values
197  * separated by ',', and find the message after the ';' character.
198  */
199
200 enum log_flags {
201         LOG_NOCONS      = 1,    /* already flushed, do not print to console */
202         LOG_NEWLINE     = 2,    /* text ended with a newline */
203         LOG_PREFIX      = 4,    /* text started with a prefix */
204         LOG_CONT        = 8,    /* text is a fragment of a continuation line */
205 };
206
207 struct log {
208         u64 ts_nsec;            /* timestamp in nanoseconds */
209         u16 len;                /* length of entire record */
210         u16 text_len;           /* length of text buffer */
211         u16 dict_len;           /* length of dictionary buffer */
212         u8 facility;            /* syslog facility */
213         u8 flags:5;             /* internal record flags */
214         u8 level:3;             /* syslog level */
215 };
216
217 /*
218  * The logbuf_lock protects kmsg buffer, indices, counters. It is also
219  * used in interesting ways to provide interlocking in console_unlock();
220  */
221 static DEFINE_RAW_SPINLOCK(logbuf_lock);
222
223 #ifdef CONFIG_PRINTK
224 DECLARE_WAIT_QUEUE_HEAD(log_wait);
225 /* the next printk record to read by syslog(READ) or /proc/kmsg */
226 static u64 syslog_seq;
227 static u32 syslog_idx;
228 static enum log_flags syslog_prev;
229 static size_t syslog_partial;
230
231 /* index and sequence number of the first record stored in the buffer */
232 static u64 log_first_seq;
233 static u32 log_first_idx;
234
235 /* index and sequence number of the next record to store in the buffer */
236 static u64 log_next_seq;
237 static u32 log_next_idx;
238
239 /* the next printk record to write to the console */
240 static u64 console_seq;
241 static u32 console_idx;
242 static enum log_flags console_prev;
243
244 /* the next printk record to read after the last 'clear' command */
245 static u64 clear_seq;
246 static u32 clear_idx;
247
248 #define PREFIX_MAX              32
249 #define LOG_LINE_MAX            1024 - PREFIX_MAX
250
251 /* record buffer */
252 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
253 #define LOG_ALIGN 4
254 #else
255 #define LOG_ALIGN __alignof__(struct log)
256 #endif
257 #define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT)
258 static char __log_buf[__LOG_BUF_LEN] __aligned(LOG_ALIGN);
259 static char *log_buf = __log_buf;
260 static u32 log_buf_len = __LOG_BUF_LEN;
261
262 /* cpu currently holding logbuf_lock */
263 static volatile unsigned int logbuf_cpu = UINT_MAX;
264
265 /* human readable text of the record */
266 static char *log_text(const struct log *msg)
267 {
268         return (char *)msg + sizeof(struct log);
269 }
270
271 /* optional key/value pair dictionary attached to the record */
272 static char *log_dict(const struct log *msg)
273 {
274         return (char *)msg + sizeof(struct log) + msg->text_len;
275 }
276
277 /* get record by index; idx must point to valid msg */
278 static struct log *log_from_idx(u32 idx)
279 {
280         struct log *msg = (struct log *)(log_buf + idx);
281
282         /*
283          * A length == 0 record is the end of buffer marker. Wrap around and
284          * read the message at the start of the buffer.
285          */
286         if (!msg->len)
287                 return (struct log *)log_buf;
288         return msg;
289 }
290
291 /* get next record; idx must point to valid msg */
292 static u32 log_next(u32 idx)
293 {
294         struct log *msg = (struct log *)(log_buf + idx);
295
296         /* length == 0 indicates the end of the buffer; wrap */
297         /*
298          * A length == 0 record is the end of buffer marker. Wrap around and
299          * read the message at the start of the buffer as *this* one, and
300          * return the one after that.
301          */
302         if (!msg->len) {
303                 msg = (struct log *)log_buf;
304                 return msg->len;
305         }
306         return idx + msg->len;
307 }
308
309 #ifdef CONFIG_RK_LAST_LOG
310 extern void rk_last_log_text(char *text, size_t size);
311 static char rk_text[1024];
312 static size_t msg_print_text(const struct log *msg, enum log_flags prev,
313                              bool syslog, char *buf, size_t size);
314 #endif
315 /* insert record into the buffer, discard old ones, update heads */
316 static void log_store(int facility, int level,
317                       enum log_flags flags, u64 ts_nsec,
318                       const char *dict, u16 dict_len,
319                       const char *text, u16 text_len)
320 {
321         struct log *msg;
322         u32 size, pad_len;
323
324         /* number of '\0' padding bytes to next message */
325         size = sizeof(struct log) + text_len + dict_len;
326         pad_len = (-size) & (LOG_ALIGN - 1);
327         size += pad_len;
328
329         while (log_first_seq < log_next_seq) {
330                 u32 free;
331
332                 if (log_next_idx > log_first_idx)
333                         free = max(log_buf_len - log_next_idx, log_first_idx);
334                 else
335                         free = log_first_idx - log_next_idx;
336
337                 if (free > size + sizeof(struct log))
338                         break;
339
340                 /* drop old messages until we have enough contiuous space */
341                 log_first_idx = log_next(log_first_idx);
342                 log_first_seq++;
343         }
344
345         if (log_next_idx + size + sizeof(struct log) >= log_buf_len) {
346                 /*
347                  * This message + an additional empty header does not fit
348                  * at the end of the buffer. Add an empty header with len == 0
349                  * to signify a wrap around.
350                  */
351                 memset(log_buf + log_next_idx, 0, sizeof(struct log));
352                 log_next_idx = 0;
353         }
354
355         /* fill message */
356         msg = (struct log *)(log_buf + log_next_idx);
357         memcpy(log_text(msg), text, text_len);
358         msg->text_len = text_len;
359         memcpy(log_dict(msg), dict, dict_len);
360         msg->dict_len = dict_len;
361         msg->facility = facility;
362         msg->level = level & 7;
363         msg->flags = flags & 0x1f;
364         if (ts_nsec > 0)
365                 msg->ts_nsec = ts_nsec;
366         else
367                 msg->ts_nsec = local_clock();
368         memset(log_dict(msg) + dict_len, 0, pad_len);
369         msg->len = sizeof(struct log) + text_len + dict_len + pad_len;
370
371 #ifdef CONFIG_RK_LAST_LOG
372         size = msg_print_text(msg, msg->flags, true, rk_text, sizeof(rk_text));
373         rk_last_log_text(rk_text, size);
374 #endif
375         /* insert message */
376         log_next_idx += msg->len;
377         log_next_seq++;
378 }
379
380 #ifdef CONFIG_SECURITY_DMESG_RESTRICT
381 int dmesg_restrict = 1;
382 #else
383 int dmesg_restrict;
384 #endif
385
386 static int syslog_action_restricted(int type)
387 {
388         if (dmesg_restrict)
389                 return 1;
390         /*
391          * Unless restricted, we allow "read all" and "get buffer size"
392          * for everybody.
393          */
394         return type != SYSLOG_ACTION_READ_ALL &&
395                type != SYSLOG_ACTION_SIZE_BUFFER;
396 }
397
398 static int check_syslog_permissions(int type, bool from_file)
399 {
400         /*
401          * If this is from /proc/kmsg and we've already opened it, then we've
402          * already done the capabilities checks at open time.
403          */
404         if (from_file && type != SYSLOG_ACTION_OPEN)
405                 return 0;
406
407         if (syslog_action_restricted(type)) {
408                 if (capable(CAP_SYSLOG))
409                         return 0;
410                 /*
411                  * For historical reasons, accept CAP_SYS_ADMIN too, with
412                  * a warning.
413                  */
414                 if (capable(CAP_SYS_ADMIN)) {
415                         pr_warn_once("%s (%d): Attempt to access syslog with "
416                                      "CAP_SYS_ADMIN but no CAP_SYSLOG "
417                                      "(deprecated).\n",
418                                  current->comm, task_pid_nr(current));
419                         return 0;
420                 }
421                 return -EPERM;
422         }
423         return security_syslog(type);
424 }
425
426
427 /* /dev/kmsg - userspace message inject/listen interface */
428 struct devkmsg_user {
429         u64 seq;
430         u32 idx;
431         enum log_flags prev;
432         struct mutex lock;
433         char buf[8192];
434 };
435
436 static ssize_t devkmsg_writev(struct kiocb *iocb, const struct iovec *iv,
437                               unsigned long count, loff_t pos)
438 {
439         char *buf, *line;
440         int i;
441         int level = default_message_loglevel;
442         int facility = 1;       /* LOG_USER */
443         size_t len = iov_length(iv, count);
444         ssize_t ret = len;
445
446         if (len > LOG_LINE_MAX)
447                 return -EINVAL;
448         buf = kmalloc(len+1, GFP_KERNEL);
449         if (buf == NULL)
450                 return -ENOMEM;
451
452         line = buf;
453         for (i = 0; i < count; i++) {
454                 if (copy_from_user(line, iv[i].iov_base, iv[i].iov_len)) {
455                         ret = -EFAULT;
456                         goto out;
457                 }
458                 line += iv[i].iov_len;
459         }
460
461         /*
462          * Extract and skip the syslog prefix <[0-9]*>. Coming from userspace
463          * the decimal value represents 32bit, the lower 3 bit are the log
464          * level, the rest are the log facility.
465          *
466          * If no prefix or no userspace facility is specified, we
467          * enforce LOG_USER, to be able to reliably distinguish
468          * kernel-generated messages from userspace-injected ones.
469          */
470         line = buf;
471         if (line[0] == '<') {
472                 char *endp = NULL;
473
474                 i = simple_strtoul(line+1, &endp, 10);
475                 if (endp && endp[0] == '>') {
476                         level = i & 7;
477                         if (i >> 3)
478                                 facility = i >> 3;
479                         endp++;
480                         len -= endp - line;
481                         line = endp;
482                 }
483         }
484         line[len] = '\0';
485
486         printk_emit(facility, level, NULL, 0, "%s", line);
487 out:
488         kfree(buf);
489         return ret;
490 }
491
492 static ssize_t devkmsg_read(struct file *file, char __user *buf,
493                             size_t count, loff_t *ppos)
494 {
495         struct devkmsg_user *user = file->private_data;
496         struct log *msg;
497         u64 ts_usec;
498         size_t i;
499         char cont = '-';
500         size_t len;
501         ssize_t ret;
502
503         if (!user)
504                 return -EBADF;
505
506         ret = mutex_lock_interruptible(&user->lock);
507         if (ret)
508                 return ret;
509         raw_spin_lock_irq(&logbuf_lock);
510         while (user->seq == log_next_seq) {
511                 if (file->f_flags & O_NONBLOCK) {
512                         ret = -EAGAIN;
513                         raw_spin_unlock_irq(&logbuf_lock);
514                         goto out;
515                 }
516
517                 raw_spin_unlock_irq(&logbuf_lock);
518                 ret = wait_event_interruptible(log_wait,
519                                                user->seq != log_next_seq);
520                 if (ret)
521                         goto out;
522                 raw_spin_lock_irq(&logbuf_lock);
523         }
524
525         if (user->seq < log_first_seq) {
526                 /* our last seen message is gone, return error and reset */
527                 user->idx = log_first_idx;
528                 user->seq = log_first_seq;
529                 ret = -EPIPE;
530                 raw_spin_unlock_irq(&logbuf_lock);
531                 goto out;
532         }
533
534         msg = log_from_idx(user->idx);
535         ts_usec = msg->ts_nsec;
536         do_div(ts_usec, 1000);
537
538         /*
539          * If we couldn't merge continuation line fragments during the print,
540          * export the stored flags to allow an optional external merge of the
541          * records. Merging the records isn't always neccessarily correct, like
542          * when we hit a race during printing. In most cases though, it produces
543          * better readable output. 'c' in the record flags mark the first
544          * fragment of a line, '+' the following.
545          */
546         if (msg->flags & LOG_CONT && !(user->prev & LOG_CONT))
547                 cont = 'c';
548         else if ((msg->flags & LOG_CONT) ||
549                  ((user->prev & LOG_CONT) && !(msg->flags & LOG_PREFIX)))
550                 cont = '+';
551
552         len = sprintf(user->buf, "%u,%llu,%llu,%c;",
553                       (msg->facility << 3) | msg->level,
554                       user->seq, ts_usec, cont);
555         user->prev = msg->flags;
556
557         /* escape non-printable characters */
558         for (i = 0; i < msg->text_len; i++) {
559                 unsigned char c = log_text(msg)[i];
560
561                 if (c < ' ' || c >= 127 || c == '\\')
562                         len += sprintf(user->buf + len, "\\x%02x", c);
563                 else
564                         user->buf[len++] = c;
565         }
566         user->buf[len++] = '\n';
567
568         if (msg->dict_len) {
569                 bool line = true;
570
571                 for (i = 0; i < msg->dict_len; i++) {
572                         unsigned char c = log_dict(msg)[i];
573
574                         if (line) {
575                                 user->buf[len++] = ' ';
576                                 line = false;
577                         }
578
579                         if (c == '\0') {
580                                 user->buf[len++] = '\n';
581                                 line = true;
582                                 continue;
583                         }
584
585                         if (c < ' ' || c >= 127 || c == '\\') {
586                                 len += sprintf(user->buf + len, "\\x%02x", c);
587                                 continue;
588                         }
589
590                         user->buf[len++] = c;
591                 }
592                 user->buf[len++] = '\n';
593         }
594
595         user->idx = log_next(user->idx);
596         user->seq++;
597         raw_spin_unlock_irq(&logbuf_lock);
598
599         if (len > count) {
600                 ret = -EINVAL;
601                 goto out;
602         }
603
604         if (copy_to_user(buf, user->buf, len)) {
605                 ret = -EFAULT;
606                 goto out;
607         }
608         ret = len;
609 out:
610         mutex_unlock(&user->lock);
611         return ret;
612 }
613
614 static loff_t devkmsg_llseek(struct file *file, loff_t offset, int whence)
615 {
616         struct devkmsg_user *user = file->private_data;
617         loff_t ret = 0;
618
619         if (!user)
620                 return -EBADF;
621         if (offset)
622                 return -ESPIPE;
623
624         raw_spin_lock_irq(&logbuf_lock);
625         switch (whence) {
626         case SEEK_SET:
627                 /* the first record */
628                 user->idx = log_first_idx;
629                 user->seq = log_first_seq;
630                 break;
631         case SEEK_DATA:
632                 /*
633                  * The first record after the last SYSLOG_ACTION_CLEAR,
634                  * like issued by 'dmesg -c'. Reading /dev/kmsg itself
635                  * changes no global state, and does not clear anything.
636                  */
637                 user->idx = clear_idx;
638                 user->seq = clear_seq;
639                 break;
640         case SEEK_END:
641                 /* after the last record */
642                 user->idx = log_next_idx;
643                 user->seq = log_next_seq;
644                 break;
645         default:
646                 ret = -EINVAL;
647         }
648         raw_spin_unlock_irq(&logbuf_lock);
649         return ret;
650 }
651
652 static unsigned int devkmsg_poll(struct file *file, poll_table *wait)
653 {
654         struct devkmsg_user *user = file->private_data;
655         int ret = 0;
656
657         if (!user)
658                 return POLLERR|POLLNVAL;
659
660         poll_wait(file, &log_wait, wait);
661
662         raw_spin_lock_irq(&logbuf_lock);
663         if (user->seq < log_next_seq) {
664                 /* return error when data has vanished underneath us */
665                 if (user->seq < log_first_seq)
666                         ret = POLLIN|POLLRDNORM|POLLERR|POLLPRI;
667                 else
668                         ret = POLLIN|POLLRDNORM;
669         }
670         raw_spin_unlock_irq(&logbuf_lock);
671
672         return ret;
673 }
674
675 static int devkmsg_open(struct inode *inode, struct file *file)
676 {
677         struct devkmsg_user *user;
678         int err;
679
680         /* write-only does not need any file context */
681         if ((file->f_flags & O_ACCMODE) == O_WRONLY)
682                 return 0;
683
684         err = check_syslog_permissions(SYSLOG_ACTION_READ_ALL,
685                                        SYSLOG_FROM_READER);
686         if (err)
687                 return err;
688
689         user = kmalloc(sizeof(struct devkmsg_user), GFP_KERNEL);
690         if (!user)
691                 return -ENOMEM;
692
693         mutex_init(&user->lock);
694
695         raw_spin_lock_irq(&logbuf_lock);
696         user->idx = log_first_idx;
697         user->seq = log_first_seq;
698         raw_spin_unlock_irq(&logbuf_lock);
699
700         file->private_data = user;
701         return 0;
702 }
703
704 static int devkmsg_release(struct inode *inode, struct file *file)
705 {
706         struct devkmsg_user *user = file->private_data;
707
708         if (!user)
709                 return 0;
710
711         mutex_destroy(&user->lock);
712         kfree(user);
713         return 0;
714 }
715
716 const struct file_operations kmsg_fops = {
717         .open = devkmsg_open,
718         .read = devkmsg_read,
719         .aio_write = devkmsg_writev,
720         .llseek = devkmsg_llseek,
721         .poll = devkmsg_poll,
722         .release = devkmsg_release,
723 };
724
725 #ifdef CONFIG_KEXEC
726 /*
727  * This appends the listed symbols to /proc/vmcoreinfo
728  *
729  * /proc/vmcoreinfo is used by various utiilties, like crash and makedumpfile to
730  * obtain access to symbols that are otherwise very difficult to locate.  These
731  * symbols are specifically used so that utilities can access and extract the
732  * dmesg log from a vmcore file after a crash.
733  */
734 void log_buf_kexec_setup(void)
735 {
736         VMCOREINFO_SYMBOL(log_buf);
737         VMCOREINFO_SYMBOL(log_buf_len);
738         VMCOREINFO_SYMBOL(log_first_idx);
739         VMCOREINFO_SYMBOL(log_next_idx);
740         /*
741          * Export struct log size and field offsets. User space tools can
742          * parse it and detect any changes to structure down the line.
743          */
744         VMCOREINFO_STRUCT_SIZE(log);
745         VMCOREINFO_OFFSET(log, ts_nsec);
746         VMCOREINFO_OFFSET(log, len);
747         VMCOREINFO_OFFSET(log, text_len);
748         VMCOREINFO_OFFSET(log, dict_len);
749 }
750 #endif
751
752 /* requested log_buf_len from kernel cmdline */
753 static unsigned long __initdata new_log_buf_len;
754
755 /* save requested log_buf_len since it's too early to process it */
756 static int __init log_buf_len_setup(char *str)
757 {
758         unsigned size = memparse(str, &str);
759
760         if (size)
761                 size = roundup_pow_of_two(size);
762         if (size > log_buf_len)
763                 new_log_buf_len = size;
764
765         return 0;
766 }
767 early_param("log_buf_len", log_buf_len_setup);
768
769 void __init setup_log_buf(int early)
770 {
771         unsigned long flags;
772         char *new_log_buf;
773         int free;
774
775         if (!new_log_buf_len)
776                 return;
777
778         if (early) {
779                 unsigned long mem;
780
781                 mem = memblock_alloc(new_log_buf_len, PAGE_SIZE);
782                 if (!mem)
783                         return;
784                 new_log_buf = __va(mem);
785         } else {
786                 new_log_buf = alloc_bootmem_nopanic(new_log_buf_len);
787         }
788
789         if (unlikely(!new_log_buf)) {
790                 pr_err("log_buf_len: %ld bytes not available\n",
791                         new_log_buf_len);
792                 return;
793         }
794
795         raw_spin_lock_irqsave(&logbuf_lock, flags);
796         log_buf_len = new_log_buf_len;
797         log_buf = new_log_buf;
798         new_log_buf_len = 0;
799         free = __LOG_BUF_LEN - log_next_idx;
800         memcpy(log_buf, __log_buf, __LOG_BUF_LEN);
801         raw_spin_unlock_irqrestore(&logbuf_lock, flags);
802
803         pr_info("log_buf_len: %d\n", log_buf_len);
804         pr_info("early log buf free: %d(%d%%)\n",
805                 free, (free * 100) / __LOG_BUF_LEN);
806 }
807
808 static bool __read_mostly ignore_loglevel;
809
810 static int __init ignore_loglevel_setup(char *str)
811 {
812         ignore_loglevel = 1;
813         printk(KERN_INFO "debug: ignoring loglevel setting.\n");
814
815         return 0;
816 }
817
818 early_param("ignore_loglevel", ignore_loglevel_setup);
819 module_param(ignore_loglevel, bool, S_IRUGO | S_IWUSR);
820 MODULE_PARM_DESC(ignore_loglevel, "ignore loglevel setting, to"
821         "print all kernel messages to the console.");
822
823 #ifdef CONFIG_BOOT_PRINTK_DELAY
824
825 static int boot_delay; /* msecs delay after each printk during bootup */
826 static unsigned long long loops_per_msec;       /* based on boot_delay */
827
828 static int __init boot_delay_setup(char *str)
829 {
830         unsigned long lpj;
831
832         lpj = preset_lpj ? preset_lpj : 1000000;        /* some guess */
833         loops_per_msec = (unsigned long long)lpj / 1000 * HZ;
834
835         get_option(&str, &boot_delay);
836         if (boot_delay > 10 * 1000)
837                 boot_delay = 0;
838
839         pr_debug("boot_delay: %u, preset_lpj: %ld, lpj: %lu, "
840                 "HZ: %d, loops_per_msec: %llu\n",
841                 boot_delay, preset_lpj, lpj, HZ, loops_per_msec);
842         return 1;
843 }
844 __setup("boot_delay=", boot_delay_setup);
845
846 static void boot_delay_msec(int level)
847 {
848         unsigned long long k;
849         unsigned long timeout;
850
851         if ((boot_delay == 0 || system_state != SYSTEM_BOOTING)
852                 || (level >= console_loglevel && !ignore_loglevel)) {
853                 return;
854         }
855
856         k = (unsigned long long)loops_per_msec * boot_delay;
857
858         timeout = jiffies + msecs_to_jiffies(boot_delay);
859         while (k) {
860                 k--;
861                 cpu_relax();
862                 /*
863                  * use (volatile) jiffies to prevent
864                  * compiler reduction; loop termination via jiffies
865                  * is secondary and may or may not happen.
866                  */
867                 if (time_after(jiffies, timeout))
868                         break;
869                 touch_nmi_watchdog();
870         }
871 }
872 #else
873 static inline void boot_delay_msec(int level)
874 {
875 }
876 #endif
877
878 #if defined(CONFIG_PRINTK_TIME)
879 static bool printk_time = 1;
880 #else
881 static bool printk_time;
882 #endif
883 module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR);
884
885 static size_t print_time(u64 ts, char *buf)
886 {
887         unsigned long rem_nsec;
888
889         if (!printk_time)
890                 return 0;
891
892         rem_nsec = do_div(ts, 1000000000);
893
894         if (!buf)
895                 return snprintf(NULL, 0, "[%5lu.000000] ", (unsigned long)ts);
896
897         return sprintf(buf, "[%5lu.%06lu] ",
898                        (unsigned long)ts, rem_nsec / 1000);
899 }
900
901 static size_t print_prefix(const struct log *msg, bool syslog, char *buf)
902 {
903         size_t len = 0;
904         unsigned int prefix = (msg->facility << 3) | msg->level;
905
906         if (syslog) {
907                 if (buf) {
908                         len += sprintf(buf, "<%u>", prefix);
909                 } else {
910                         len += 3;
911                         if (prefix > 999)
912                                 len += 3;
913                         else if (prefix > 99)
914                                 len += 2;
915                         else if (prefix > 9)
916                                 len++;
917                 }
918         }
919
920         len += print_time(msg->ts_nsec, buf ? buf + len : NULL);
921         return len;
922 }
923
924 static size_t msg_print_text(const struct log *msg, enum log_flags prev,
925                              bool syslog, char *buf, size_t size)
926 {
927         const char *text = log_text(msg);
928         size_t text_size = msg->text_len;
929         bool prefix = true;
930         bool newline = true;
931         size_t len = 0;
932
933         if ((prev & LOG_CONT) && !(msg->flags & LOG_PREFIX))
934                 prefix = false;
935
936         if (msg->flags & LOG_CONT) {
937                 if ((prev & LOG_CONT) && !(prev & LOG_NEWLINE))
938                         prefix = false;
939
940                 if (!(msg->flags & LOG_NEWLINE))
941                         newline = false;
942         }
943
944         do {
945                 const char *next = memchr(text, '\n', text_size);
946                 size_t text_len;
947
948                 if (next) {
949                         text_len = next - text;
950                         next++;
951                         text_size -= next - text;
952                 } else {
953                         text_len = text_size;
954                 }
955
956                 if (buf) {
957                         if (print_prefix(msg, syslog, NULL) +
958                             text_len + 1 >= size - len)
959                                 break;
960
961                         if (prefix)
962                                 len += print_prefix(msg, syslog, buf + len);
963                         memcpy(buf + len, text, text_len);
964                         len += text_len;
965                         if (next || newline)
966                                 buf[len++] = '\n';
967                 } else {
968                         /* SYSLOG_ACTION_* buffer size only calculation */
969                         if (prefix)
970                                 len += print_prefix(msg, syslog, NULL);
971                         len += text_len;
972                         if (next || newline)
973                                 len++;
974                 }
975
976                 prefix = true;
977                 text = next;
978         } while (text);
979
980         return len;
981 }
982
983 static int syslog_print(char __user *buf, int size)
984 {
985         char *text;
986         struct log *msg;
987         int len = 0;
988
989         text = kmalloc(LOG_LINE_MAX + PREFIX_MAX, GFP_KERNEL);
990         if (!text)
991                 return -ENOMEM;
992
993         while (size > 0) {
994                 size_t n;
995                 size_t skip;
996
997                 raw_spin_lock_irq(&logbuf_lock);
998                 if (syslog_seq < log_first_seq) {
999                         /* messages are gone, move to first one */
1000                         syslog_seq = log_first_seq;
1001                         syslog_idx = log_first_idx;
1002                         syslog_prev = 0;
1003                         syslog_partial = 0;
1004                 }
1005                 if (syslog_seq == log_next_seq) {
1006                         raw_spin_unlock_irq(&logbuf_lock);
1007                         break;
1008                 }
1009
1010                 skip = syslog_partial;
1011                 msg = log_from_idx(syslog_idx);
1012                 n = msg_print_text(msg, syslog_prev, true, text,
1013                                    LOG_LINE_MAX + PREFIX_MAX);
1014                 if (n - syslog_partial <= size) {
1015                         /* message fits into buffer, move forward */
1016                         syslog_idx = log_next(syslog_idx);
1017                         syslog_seq++;
1018                         syslog_prev = msg->flags;
1019                         n -= syslog_partial;
1020                         syslog_partial = 0;
1021                 } else if (!len){
1022                         /* partial read(), remember position */
1023                         n = size;
1024                         syslog_partial += n;
1025                 } else
1026                         n = 0;
1027                 raw_spin_unlock_irq(&logbuf_lock);
1028
1029                 if (!n)
1030                         break;
1031
1032                 if (copy_to_user(buf, text + skip, n)) {
1033                         if (!len)
1034                                 len = -EFAULT;
1035                         break;
1036                 }
1037
1038                 len += n;
1039                 size -= n;
1040                 buf += n;
1041         }
1042
1043         kfree(text);
1044         return len;
1045 }
1046
1047 static int syslog_print_all(char __user *buf, int size, bool clear)
1048 {
1049         char *text;
1050         int len = 0;
1051
1052         text = kmalloc(LOG_LINE_MAX + PREFIX_MAX, GFP_KERNEL);
1053         if (!text)
1054                 return -ENOMEM;
1055
1056         raw_spin_lock_irq(&logbuf_lock);
1057         if (buf) {
1058                 u64 next_seq;
1059                 u64 seq;
1060                 u32 idx;
1061                 enum log_flags prev;
1062
1063                 if (clear_seq < log_first_seq) {
1064                         /* messages are gone, move to first available one */
1065                         clear_seq = log_first_seq;
1066                         clear_idx = log_first_idx;
1067                 }
1068
1069                 /*
1070                  * Find first record that fits, including all following records,
1071                  * into the user-provided buffer for this dump.
1072                  */
1073                 seq = clear_seq;
1074                 idx = clear_idx;
1075                 prev = 0;
1076                 while (seq < log_next_seq) {
1077                         struct log *msg = log_from_idx(idx);
1078
1079                         len += msg_print_text(msg, prev, true, NULL, 0);
1080                         prev = msg->flags;
1081                         idx = log_next(idx);
1082                         seq++;
1083                 }
1084
1085                 /* move first record forward until length fits into the buffer */
1086                 seq = clear_seq;
1087                 idx = clear_idx;
1088                 prev = 0;
1089                 while (len > size && seq < log_next_seq) {
1090                         struct log *msg = log_from_idx(idx);
1091
1092                         len -= msg_print_text(msg, prev, true, NULL, 0);
1093                         prev = msg->flags;
1094                         idx = log_next(idx);
1095                         seq++;
1096                 }
1097
1098                 /* last message fitting into this dump */
1099                 next_seq = log_next_seq;
1100
1101                 len = 0;
1102                 prev = 0;
1103                 while (len >= 0 && seq < next_seq) {
1104                         struct log *msg = log_from_idx(idx);
1105                         int textlen;
1106
1107                         textlen = msg_print_text(msg, prev, true, text,
1108                                                  LOG_LINE_MAX + PREFIX_MAX);
1109                         if (textlen < 0) {
1110                                 len = textlen;
1111                                 break;
1112                         }
1113                         idx = log_next(idx);
1114                         seq++;
1115                         prev = msg->flags;
1116
1117                         raw_spin_unlock_irq(&logbuf_lock);
1118                         if (copy_to_user(buf + len, text, textlen))
1119                                 len = -EFAULT;
1120                         else
1121                                 len += textlen;
1122                         raw_spin_lock_irq(&logbuf_lock);
1123
1124                         if (seq < log_first_seq) {
1125                                 /* messages are gone, move to next one */
1126                                 seq = log_first_seq;
1127                                 idx = log_first_idx;
1128                                 prev = 0;
1129                         }
1130                 }
1131         }
1132
1133         if (clear) {
1134                 clear_seq = log_next_seq;
1135                 clear_idx = log_next_idx;
1136         }
1137         raw_spin_unlock_irq(&logbuf_lock);
1138
1139         kfree(text);
1140         return len;
1141 }
1142
1143 int do_syslog(int type, char __user *buf, int len, bool from_file)
1144 {
1145         bool clear = false;
1146         static int saved_console_loglevel = -1;
1147         int error;
1148
1149         error = check_syslog_permissions(type, from_file);
1150         if (error)
1151                 goto out;
1152
1153         error = security_syslog(type);
1154         if (error)
1155                 return error;
1156
1157         switch (type) {
1158         case SYSLOG_ACTION_CLOSE:       /* Close log */
1159                 break;
1160         case SYSLOG_ACTION_OPEN:        /* Open log */
1161                 break;
1162         case SYSLOG_ACTION_READ:        /* Read from log */
1163                 error = -EINVAL;
1164                 if (!buf || len < 0)
1165                         goto out;
1166                 error = 0;
1167                 if (!len)
1168                         goto out;
1169                 if (!access_ok(VERIFY_WRITE, buf, len)) {
1170                         error = -EFAULT;
1171                         goto out;
1172                 }
1173                 error = wait_event_interruptible(log_wait,
1174                                                  syslog_seq != log_next_seq);
1175                 if (error)
1176                         goto out;
1177                 error = syslog_print(buf, len);
1178                 break;
1179         /* Read/clear last kernel messages */
1180         case SYSLOG_ACTION_READ_CLEAR:
1181                 clear = true;
1182                 /* FALL THRU */
1183         /* Read last kernel messages */
1184         case SYSLOG_ACTION_READ_ALL:
1185                 error = -EINVAL;
1186                 if (!buf || len < 0)
1187                         goto out;
1188                 error = 0;
1189                 if (!len)
1190                         goto out;
1191                 if (!access_ok(VERIFY_WRITE, buf, len)) {
1192                         error = -EFAULT;
1193                         goto out;
1194                 }
1195                 error = syslog_print_all(buf, len, clear);
1196                 break;
1197         /* Clear ring buffer */
1198         case SYSLOG_ACTION_CLEAR:
1199                 syslog_print_all(NULL, 0, true);
1200                 break;
1201         /* Disable logging to console */
1202         case SYSLOG_ACTION_CONSOLE_OFF:
1203                 if (saved_console_loglevel == -1)
1204                         saved_console_loglevel = console_loglevel;
1205                 console_loglevel = minimum_console_loglevel;
1206                 break;
1207         /* Enable logging to console */
1208         case SYSLOG_ACTION_CONSOLE_ON:
1209                 if (saved_console_loglevel != -1) {
1210                         console_loglevel = saved_console_loglevel;
1211                         saved_console_loglevel = -1;
1212                 }
1213                 break;
1214         /* Set level of messages printed to console */
1215         case SYSLOG_ACTION_CONSOLE_LEVEL:
1216                 error = -EINVAL;
1217                 if (len < 1 || len > 8)
1218                         goto out;
1219                 if (len < minimum_console_loglevel)
1220                         len = minimum_console_loglevel;
1221                 console_loglevel = len;
1222                 /* Implicitly re-enable logging to console */
1223                 saved_console_loglevel = -1;
1224                 error = 0;
1225                 break;
1226         /* Number of chars in the log buffer */
1227         case SYSLOG_ACTION_SIZE_UNREAD:
1228                 raw_spin_lock_irq(&logbuf_lock);
1229                 if (syslog_seq < log_first_seq) {
1230                         /* messages are gone, move to first one */
1231                         syslog_seq = log_first_seq;
1232                         syslog_idx = log_first_idx;
1233                         syslog_prev = 0;
1234                         syslog_partial = 0;
1235                 }
1236                 if (from_file) {
1237                         /*
1238                          * Short-cut for poll(/"proc/kmsg") which simply checks
1239                          * for pending data, not the size; return the count of
1240                          * records, not the length.
1241                          */
1242                         error = log_next_idx - syslog_idx;
1243                 } else {
1244                         u64 seq = syslog_seq;
1245                         u32 idx = syslog_idx;
1246                         enum log_flags prev = syslog_prev;
1247
1248                         error = 0;
1249                         while (seq < log_next_seq) {
1250                                 struct log *msg = log_from_idx(idx);
1251
1252                                 error += msg_print_text(msg, prev, true, NULL, 0);
1253                                 idx = log_next(idx);
1254                                 seq++;
1255                                 prev = msg->flags;
1256                         }
1257                         error -= syslog_partial;
1258                 }
1259                 raw_spin_unlock_irq(&logbuf_lock);
1260                 break;
1261         /* Size of the log buffer */
1262         case SYSLOG_ACTION_SIZE_BUFFER:
1263                 error = log_buf_len;
1264                 break;
1265         default:
1266                 error = -EINVAL;
1267                 break;
1268         }
1269 out:
1270         return error;
1271 }
1272
1273 SYSCALL_DEFINE3(syslog, int, type, char __user *, buf, int, len)
1274 {
1275         return do_syslog(type, buf, len, SYSLOG_FROM_READER);
1276 }
1277
1278 /*
1279  * Call the console drivers, asking them to write out
1280  * log_buf[start] to log_buf[end - 1].
1281  * The console_lock must be held.
1282  */
1283 static void call_console_drivers(int level, const char *text, size_t len)
1284 {
1285         struct console *con;
1286
1287         trace_console(text, len);
1288
1289         if (level >= console_loglevel && !ignore_loglevel)
1290                 return;
1291         if (!console_drivers)
1292                 return;
1293
1294         for_each_console(con) {
1295                 if (exclusive_console && con != exclusive_console)
1296                         continue;
1297                 if (!(con->flags & CON_ENABLED))
1298                         continue;
1299                 if (!con->write)
1300                         continue;
1301                 if (!cpu_online(smp_processor_id()) &&
1302                     !(con->flags & CON_ANYTIME))
1303                         continue;
1304                 con->write(con, text, len);
1305         }
1306 }
1307
1308 /*
1309  * Zap console related locks when oopsing. Only zap at most once
1310  * every 10 seconds, to leave time for slow consoles to print a
1311  * full oops.
1312  */
1313 static void zap_locks(void)
1314 {
1315         static unsigned long oops_timestamp;
1316
1317         if (time_after_eq(jiffies, oops_timestamp) &&
1318                         !time_after(jiffies, oops_timestamp + 30 * HZ))
1319                 return;
1320
1321         oops_timestamp = jiffies;
1322
1323         debug_locks_off();
1324         /* If a crash is occurring, make sure we can't deadlock */
1325         raw_spin_lock_init(&logbuf_lock);
1326         /* And make sure that we print immediately */
1327         sema_init(&console_sem, 1);
1328 }
1329
1330 /* Check if we have any console registered that can be called early in boot. */
1331 static int have_callable_console(void)
1332 {
1333         struct console *con;
1334
1335         for_each_console(con)
1336                 if (con->flags & CON_ANYTIME)
1337                         return 1;
1338
1339         return 0;
1340 }
1341
1342 /*
1343  * Can we actually use the console at this time on this cpu?
1344  *
1345  * Console drivers may assume that per-cpu resources have
1346  * been allocated. So unless they're explicitly marked as
1347  * being able to cope (CON_ANYTIME) don't call them until
1348  * this CPU is officially up.
1349  */
1350 static inline int can_use_console(unsigned int cpu)
1351 {
1352         return cpu_online(cpu) || have_callable_console();
1353 }
1354
1355 /*
1356  * Try to get console ownership to actually show the kernel
1357  * messages from a 'printk'. Return true (and with the
1358  * console_lock held, and 'console_locked' set) if it
1359  * is successful, false otherwise.
1360  *
1361  * This gets called with the 'logbuf_lock' spinlock held and
1362  * interrupts disabled. It should return with 'lockbuf_lock'
1363  * released but interrupts still disabled.
1364  */
1365 static int console_trylock_for_printk(unsigned int cpu)
1366         __releases(&logbuf_lock)
1367 {
1368         int retval = 0, wake = 0;
1369
1370         if (console_trylock()) {
1371                 retval = 1;
1372
1373                 /*
1374                  * If we can't use the console, we need to release
1375                  * the console semaphore by hand to avoid flushing
1376                  * the buffer. We need to hold the console semaphore
1377                  * in order to do this test safely.
1378                  */
1379                 if (!can_use_console(cpu)) {
1380                         console_locked = 0;
1381                         wake = 1;
1382                         retval = 0;
1383                 }
1384         }
1385         logbuf_cpu = UINT_MAX;
1386         raw_spin_unlock(&logbuf_lock);
1387         if (wake)
1388                 up(&console_sem);
1389         return retval;
1390 }
1391
1392 int printk_delay_msec __read_mostly;
1393
1394 static inline void printk_delay(void)
1395 {
1396         if (unlikely(printk_delay_msec)) {
1397                 int m = printk_delay_msec;
1398
1399                 while (m--) {
1400                         mdelay(1);
1401                         touch_nmi_watchdog();
1402                 }
1403         }
1404 }
1405
1406 /*
1407  * Continuation lines are buffered, and not committed to the record buffer
1408  * until the line is complete, or a race forces it. The line fragments
1409  * though, are printed immediately to the consoles to ensure everything has
1410  * reached the console in case of a kernel crash.
1411  */
1412 static struct cont {
1413         char buf[LOG_LINE_MAX];
1414         size_t len;                     /* length == 0 means unused buffer */
1415         size_t cons;                    /* bytes written to console */
1416         struct task_struct *owner;      /* task of first print*/
1417         u64 ts_nsec;                    /* time of first print */
1418         u8 level;                       /* log level of first message */
1419         u8 facility;                    /* log level of first message */
1420         enum log_flags flags;           /* prefix, newline flags */
1421         bool flushed:1;                 /* buffer sealed and committed */
1422 } cont;
1423
1424 static void cont_flush(enum log_flags flags)
1425 {
1426         if (cont.flushed)
1427                 return;
1428         if (cont.len == 0)
1429                 return;
1430
1431         if (cont.cons) {
1432                 /*
1433                  * If a fragment of this line was directly flushed to the
1434                  * console; wait for the console to pick up the rest of the
1435                  * line. LOG_NOCONS suppresses a duplicated output.
1436                  */
1437                 log_store(cont.facility, cont.level, flags | LOG_NOCONS,
1438                           cont.ts_nsec, NULL, 0, cont.buf, cont.len);
1439                 cont.flags = flags;
1440                 cont.flushed = true;
1441         } else {
1442                 /*
1443                  * If no fragment of this line ever reached the console,
1444                  * just submit it to the store and free the buffer.
1445                  */
1446                 log_store(cont.facility, cont.level, flags, 0,
1447                           NULL, 0, cont.buf, cont.len);
1448                 cont.len = 0;
1449         }
1450 }
1451
1452 static bool cont_add(int facility, int level, const char *text, size_t len)
1453 {
1454         if (cont.len && cont.flushed)
1455                 return false;
1456
1457         if (cont.len + len > sizeof(cont.buf)) {
1458                 /* the line gets too long, split it up in separate records */
1459                 cont_flush(LOG_CONT);
1460                 return false;
1461         }
1462
1463         if (!cont.len) {
1464                 cont.facility = facility;
1465                 cont.level = level;
1466                 cont.owner = current;
1467                 cont.ts_nsec = local_clock();
1468                 cont.flags = 0;
1469                 cont.cons = 0;
1470                 cont.flushed = false;
1471         }
1472
1473         memcpy(cont.buf + cont.len, text, len);
1474         cont.len += len;
1475
1476         if (cont.len > (sizeof(cont.buf) * 80) / 100)
1477                 cont_flush(LOG_CONT);
1478
1479         return true;
1480 }
1481
1482 static size_t cont_print_text(char *text, size_t size)
1483 {
1484         size_t textlen = 0;
1485         size_t len;
1486
1487         if (cont.cons == 0 && (console_prev & LOG_NEWLINE)) {
1488                 textlen += print_time(cont.ts_nsec, text);
1489                 size -= textlen;
1490         }
1491
1492         len = cont.len - cont.cons;
1493         if (len > 0) {
1494                 if (len+1 > size)
1495                         len = size-1;
1496                 memcpy(text + textlen, cont.buf + cont.cons, len);
1497                 textlen += len;
1498                 cont.cons = cont.len;
1499         }
1500
1501         if (cont.flushed) {
1502                 if (cont.flags & LOG_NEWLINE)
1503                         text[textlen++] = '\n';
1504                 /* got everything, release buffer */
1505                 cont.len = 0;
1506         }
1507         return textlen;
1508 }
1509
1510 asmlinkage int vprintk_emit(int facility, int level,
1511                             const char *dict, size_t dictlen,
1512                             const char *fmt, va_list args)
1513 {
1514         static int recursion_bug;
1515         static char textbuf[LOG_LINE_MAX];
1516         char *text = textbuf;
1517         size_t text_len;
1518         enum log_flags lflags = 0;
1519         unsigned long flags;
1520         int this_cpu;
1521         int printed_len = 0;
1522
1523         boot_delay_msec(level);
1524         printk_delay();
1525
1526         /* This stops the holder of console_sem just where we want him */
1527         local_irq_save(flags);
1528         this_cpu = smp_processor_id();
1529
1530         /*
1531          * Ouch, printk recursed into itself!
1532          */
1533         if (unlikely(logbuf_cpu == this_cpu)) {
1534                 /*
1535                  * If a crash is occurring during printk() on this CPU,
1536                  * then try to get the crash message out but make sure
1537                  * we can't deadlock. Otherwise just return to avoid the
1538                  * recursion and return - but flag the recursion so that
1539                  * it can be printed at the next appropriate moment:
1540                  */
1541                 if (!oops_in_progress && !lockdep_recursing(current)) {
1542                         recursion_bug = 1;
1543                         goto out_restore_irqs;
1544                 }
1545                 zap_locks();
1546         }
1547
1548         lockdep_off();
1549         raw_spin_lock(&logbuf_lock);
1550         logbuf_cpu = this_cpu;
1551
1552         if (recursion_bug) {
1553                 static const char recursion_msg[] =
1554                         "BUG: recent printk recursion!";
1555
1556                 recursion_bug = 0;
1557                 printed_len += strlen(recursion_msg);
1558                 /* emit KERN_CRIT message */
1559                 log_store(0, 2, LOG_PREFIX|LOG_NEWLINE, 0,
1560                           NULL, 0, recursion_msg, printed_len);
1561         }
1562
1563         /*
1564          * The printf needs to come first; we need the syslog
1565          * prefix which might be passed-in as a parameter.
1566          */
1567         text_len = vscnprintf(text, sizeof(textbuf), fmt, args);
1568
1569         /* mark and strip a trailing newline */
1570         if (text_len && text[text_len-1] == '\n') {
1571                 text_len--;
1572                 lflags |= LOG_NEWLINE;
1573         }
1574
1575         /* strip kernel syslog prefix and extract log level or control flags */
1576         if (facility == 0) {
1577                 int kern_level = printk_get_level(text);
1578
1579                 if (kern_level) {
1580                         const char *end_of_header = printk_skip_level(text);
1581                         switch (kern_level) {
1582                         case '0' ... '7':
1583                                 if (level == -1)
1584                                         level = kern_level - '0';
1585                         case 'd':       /* KERN_DEFAULT */
1586                                 lflags |= LOG_PREFIX;
1587                         case 'c':       /* KERN_CONT */
1588                                 break;
1589                         }
1590                         text_len -= end_of_header - text;
1591                         text = (char *)end_of_header;
1592                 }
1593         }
1594
1595 #ifdef CONFIG_EARLY_PRINTK_DIRECT
1596         printascii(text);
1597 #endif
1598
1599         if (level == -1)
1600                 level = default_message_loglevel;
1601
1602         if (dict)
1603                 lflags |= LOG_PREFIX|LOG_NEWLINE;
1604
1605         if (!(lflags & LOG_NEWLINE)) {
1606                 /*
1607                  * Flush the conflicting buffer. An earlier newline was missing,
1608                  * or another task also prints continuation lines.
1609                  */
1610                 if (cont.len && (lflags & LOG_PREFIX || cont.owner != current))
1611                         cont_flush(LOG_NEWLINE);
1612
1613                 /* buffer line if possible, otherwise store it right away */
1614                 if (!cont_add(facility, level, text, text_len))
1615                         log_store(facility, level, lflags | LOG_CONT, 0,
1616                                   dict, dictlen, text, text_len);
1617         } else {
1618                 bool stored = false;
1619
1620                 /*
1621                  * If an earlier newline was missing and it was the same task,
1622                  * either merge it with the current buffer and flush, or if
1623                  * there was a race with interrupts (prefix == true) then just
1624                  * flush it out and store this line separately.
1625                  */
1626                 if (cont.len && cont.owner == current) {
1627                         if (!(lflags & LOG_PREFIX))
1628                                 stored = cont_add(facility, level, text, text_len);
1629                         cont_flush(LOG_NEWLINE);
1630                 }
1631
1632                 if (!stored)
1633                         log_store(facility, level, lflags, 0,
1634                                   dict, dictlen, text, text_len);
1635         }
1636         printed_len += text_len;
1637
1638         /*
1639          * Try to acquire and then immediately release the console semaphore.
1640          * The release will print out buffers and wake up /dev/kmsg and syslog()
1641          * users.
1642          *
1643          * The console_trylock_for_printk() function will release 'logbuf_lock'
1644          * regardless of whether it actually gets the console semaphore or not.
1645          */
1646         if (console_trylock_for_printk(this_cpu))
1647                 console_unlock();
1648
1649         lockdep_on();
1650 out_restore_irqs:
1651         local_irq_restore(flags);
1652
1653         return printed_len;
1654 }
1655 EXPORT_SYMBOL(vprintk_emit);
1656
1657 asmlinkage int vprintk(const char *fmt, va_list args)
1658 {
1659         return vprintk_emit(0, -1, NULL, 0, fmt, args);
1660 }
1661 EXPORT_SYMBOL(vprintk);
1662
1663 asmlinkage int printk_emit(int facility, int level,
1664                            const char *dict, size_t dictlen,
1665                            const char *fmt, ...)
1666 {
1667         va_list args;
1668         int r;
1669
1670         va_start(args, fmt);
1671         r = vprintk_emit(facility, level, dict, dictlen, fmt, args);
1672         va_end(args);
1673
1674         return r;
1675 }
1676 EXPORT_SYMBOL(printk_emit);
1677
1678 /**
1679  * printk - print a kernel message
1680  * @fmt: format string
1681  *
1682  * This is printk(). It can be called from any context. We want it to work.
1683  *
1684  * We try to grab the console_lock. If we succeed, it's easy - we log the
1685  * output and call the console drivers.  If we fail to get the semaphore, we
1686  * place the output into the log buffer and return. The current holder of
1687  * the console_sem will notice the new output in console_unlock(); and will
1688  * send it to the consoles before releasing the lock.
1689  *
1690  * One effect of this deferred printing is that code which calls printk() and
1691  * then changes console_loglevel may break. This is because console_loglevel
1692  * is inspected when the actual printing occurs.
1693  *
1694  * See also:
1695  * printf(3)
1696  *
1697  * See the vsnprintf() documentation for format string extensions over C99.
1698  */
1699 asmlinkage int printk(const char *fmt, ...)
1700 {
1701         va_list args;
1702         int r;
1703
1704 #ifdef CONFIG_KGDB_KDB
1705         if (unlikely(kdb_trap_printk)) {
1706                 va_start(args, fmt);
1707                 r = vkdb_printf(fmt, args);
1708                 va_end(args);
1709                 return r;
1710         }
1711 #endif
1712         va_start(args, fmt);
1713         r = vprintk_emit(0, -1, NULL, 0, fmt, args);
1714         va_end(args);
1715
1716         return r;
1717 }
1718 EXPORT_SYMBOL(printk);
1719
1720 #if defined(CONFIG_RK_DEBUG_UART) && (CONFIG_RK_DEBUG_UART >= 0)
1721 void console_disable_suspend(void)
1722 {
1723         console_suspended = 0;
1724 }
1725 #endif
1726 #else /* CONFIG_PRINTK */
1727
1728 #define LOG_LINE_MAX            0
1729 #define PREFIX_MAX              0
1730 #define LOG_LINE_MAX 0
1731 static u64 syslog_seq;
1732 static u32 syslog_idx;
1733 static u64 console_seq;
1734 static u32 console_idx;
1735 static enum log_flags syslog_prev;
1736 static u64 log_first_seq;
1737 static u32 log_first_idx;
1738 static u64 log_next_seq;
1739 static enum log_flags console_prev;
1740 static struct cont {
1741         size_t len;
1742         size_t cons;
1743         u8 level;
1744         bool flushed:1;
1745 } cont;
1746 static struct log *log_from_idx(u32 idx) { return NULL; }
1747 static u32 log_next(u32 idx) { return 0; }
1748 static void call_console_drivers(int level, const char *text, size_t len) {}
1749 static size_t msg_print_text(const struct log *msg, enum log_flags prev,
1750                              bool syslog, char *buf, size_t size) { return 0; }
1751 static size_t cont_print_text(char *text, size_t size) { return 0; }
1752
1753 #endif /* CONFIG_PRINTK */
1754
1755 #ifdef CONFIG_EARLY_PRINTK
1756 struct console *early_console;
1757
1758 void early_vprintk(const char *fmt, va_list ap)
1759 {
1760         if (early_console) {
1761                 char buf[512];
1762                 int n = vscnprintf(buf, sizeof(buf), fmt, ap);
1763
1764                 early_console->write(early_console, buf, n);
1765         }
1766 }
1767
1768 asmlinkage void early_printk(const char *fmt, ...)
1769 {
1770         va_list ap;
1771
1772         va_start(ap, fmt);
1773         early_vprintk(fmt, ap);
1774         va_end(ap);
1775 }
1776 #endif
1777
1778 static int __add_preferred_console(char *name, int idx, char *options,
1779                                    char *brl_options)
1780 {
1781         struct console_cmdline *c;
1782         int i;
1783
1784         /*
1785          *      See if this tty is not yet registered, and
1786          *      if we have a slot free.
1787          */
1788         for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
1789                 if (strcmp(console_cmdline[i].name, name) == 0 &&
1790                           console_cmdline[i].index == idx) {
1791                                 if (!brl_options)
1792                                         selected_console = i;
1793                                 return 0;
1794                 }
1795         if (i == MAX_CMDLINECONSOLES)
1796                 return -E2BIG;
1797         if (!brl_options)
1798                 selected_console = i;
1799         c = &console_cmdline[i];
1800         strlcpy(c->name, name, sizeof(c->name));
1801         c->options = options;
1802 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1803         c->brl_options = brl_options;
1804 #endif
1805         c->index = idx;
1806         return 0;
1807 }
1808 /*
1809  * Set up a list of consoles.  Called from init/main.c
1810  */
1811 static int __init console_setup(char *str)
1812 {
1813         char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for index */
1814         char *s, *options, *brl_options = NULL;
1815         int idx;
1816
1817 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1818         if (!memcmp(str, "brl,", 4)) {
1819                 brl_options = "";
1820                 str += 4;
1821         } else if (!memcmp(str, "brl=", 4)) {
1822                 brl_options = str + 4;
1823                 str = strchr(brl_options, ',');
1824                 if (!str) {
1825                         printk(KERN_ERR "need port name after brl=\n");
1826                         return 1;
1827                 }
1828                 *(str++) = 0;
1829         }
1830 #endif
1831
1832         /*
1833          * Decode str into name, index, options.
1834          */
1835         if (str[0] >= '0' && str[0] <= '9') {
1836                 strcpy(buf, "ttyS");
1837                 strncpy(buf + 4, str, sizeof(buf) - 5);
1838         } else {
1839                 strncpy(buf, str, sizeof(buf) - 1);
1840         }
1841         buf[sizeof(buf) - 1] = 0;
1842         if ((options = strchr(str, ',')) != NULL)
1843                 *(options++) = 0;
1844 #ifdef __sparc__
1845         if (!strcmp(str, "ttya"))
1846                 strcpy(buf, "ttyS0");
1847         if (!strcmp(str, "ttyb"))
1848                 strcpy(buf, "ttyS1");
1849 #endif
1850         for (s = buf; *s; s++)
1851                 if ((*s >= '0' && *s <= '9') || *s == ',')
1852                         break;
1853         idx = simple_strtoul(s, NULL, 10);
1854         *s = 0;
1855
1856         __add_preferred_console(buf, idx, options, brl_options);
1857         console_set_on_cmdline = 1;
1858         return 1;
1859 }
1860 __setup("console=", console_setup);
1861
1862 /**
1863  * add_preferred_console - add a device to the list of preferred consoles.
1864  * @name: device name
1865  * @idx: device index
1866  * @options: options for this console
1867  *
1868  * The last preferred console added will be used for kernel messages
1869  * and stdin/out/err for init.  Normally this is used by console_setup
1870  * above to handle user-supplied console arguments; however it can also
1871  * be used by arch-specific code either to override the user or more
1872  * commonly to provide a default console (ie from PROM variables) when
1873  * the user has not supplied one.
1874  */
1875 int add_preferred_console(char *name, int idx, char *options)
1876 {
1877         return __add_preferred_console(name, idx, options, NULL);
1878 }
1879
1880 int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options)
1881 {
1882         struct console_cmdline *c;
1883         int i;
1884
1885         for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
1886                 if (strcmp(console_cmdline[i].name, name) == 0 &&
1887                           console_cmdline[i].index == idx) {
1888                                 c = &console_cmdline[i];
1889                                 strlcpy(c->name, name_new, sizeof(c->name));
1890                                 c->name[sizeof(c->name) - 1] = 0;
1891                                 c->options = options;
1892                                 c->index = idx_new;
1893                                 return i;
1894                 }
1895         /* not found */
1896         return -1;
1897 }
1898
1899 bool console_suspend_enabled = 1;
1900 EXPORT_SYMBOL(console_suspend_enabled);
1901
1902 static int __init console_suspend_disable(char *str)
1903 {
1904         console_suspend_enabled = 0;
1905         return 1;
1906 }
1907 __setup("no_console_suspend", console_suspend_disable);
1908 module_param_named(console_suspend, console_suspend_enabled,
1909                 bool, S_IRUGO | S_IWUSR);
1910 MODULE_PARM_DESC(console_suspend, "suspend console during suspend"
1911         " and hibernate operations");
1912
1913 /**
1914  * suspend_console - suspend the console subsystem
1915  *
1916  * This disables printk() while we go into suspend states
1917  */
1918 void suspend_console(void)
1919 {
1920         if (!console_suspend_enabled)
1921                 return;
1922         printk("Suspending console(s) (use no_console_suspend to debug)\n");
1923         console_lock();
1924         console_suspended = 1;
1925         up(&console_sem);
1926 }
1927
1928 void resume_console(void)
1929 {
1930         if (!console_suspend_enabled)
1931                 return;
1932         down(&console_sem);
1933         console_suspended = 0;
1934         console_unlock();
1935 }
1936
1937 /**
1938  * console_cpu_notify - print deferred console messages after CPU hotplug
1939  * @self: notifier struct
1940  * @action: CPU hotplug event
1941  * @hcpu: unused
1942  *
1943  * If printk() is called from a CPU that is not online yet, the messages
1944  * will be spooled but will not show up on the console.  This function is
1945  * called when a new CPU comes online (or fails to come up), and ensures
1946  * that any such output gets printed.
1947  */
1948 static int __cpuinit console_cpu_notify(struct notifier_block *self,
1949         unsigned long action, void *hcpu)
1950 {
1951         switch (action) {
1952         case CPU_ONLINE:
1953         case CPU_DEAD:
1954         case CPU_DOWN_FAILED:
1955         case CPU_UP_CANCELED:
1956                 console_lock();
1957                 console_unlock();
1958         }
1959         return NOTIFY_OK;
1960 }
1961
1962 /**
1963  * console_lock - lock the console system for exclusive use.
1964  *
1965  * Acquires a lock which guarantees that the caller has
1966  * exclusive access to the console system and the console_drivers list.
1967  *
1968  * Can sleep, returns nothing.
1969  */
1970 void console_lock(void)
1971 {
1972         might_sleep();
1973
1974         down(&console_sem);
1975         if (console_suspended)
1976                 return;
1977         console_locked = 1;
1978         console_may_schedule = 1;
1979         mutex_acquire(&console_lock_dep_map, 0, 0, _RET_IP_);
1980 }
1981 EXPORT_SYMBOL(console_lock);
1982
1983 /**
1984  * console_trylock - try to lock the console system for exclusive use.
1985  *
1986  * Tried to acquire a lock which guarantees that the caller has
1987  * exclusive access to the console system and the console_drivers list.
1988  *
1989  * returns 1 on success, and 0 on failure to acquire the lock.
1990  */
1991 int console_trylock(void)
1992 {
1993         if (down_trylock(&console_sem))
1994                 return 0;
1995         if (console_suspended) {
1996                 up(&console_sem);
1997                 return 0;
1998         }
1999         console_locked = 1;
2000         console_may_schedule = 0;
2001         mutex_acquire(&console_lock_dep_map, 0, 1, _RET_IP_);
2002         return 1;
2003 }
2004 EXPORT_SYMBOL(console_trylock);
2005
2006 int is_console_locked(void)
2007 {
2008         return console_locked;
2009 }
2010
2011 static void console_cont_flush(char *text, size_t size)
2012 {
2013         unsigned long flags;
2014         size_t len;
2015
2016         raw_spin_lock_irqsave(&logbuf_lock, flags);
2017
2018         if (!cont.len)
2019                 goto out;
2020
2021         /*
2022          * We still queue earlier records, likely because the console was
2023          * busy. The earlier ones need to be printed before this one, we
2024          * did not flush any fragment so far, so just let it queue up.
2025          */
2026         if (console_seq < log_next_seq && !cont.cons)
2027                 goto out;
2028
2029         len = cont_print_text(text, size);
2030         raw_spin_unlock(&logbuf_lock);
2031         stop_critical_timings();
2032         call_console_drivers(cont.level, text, len);
2033         start_critical_timings();
2034         local_irq_restore(flags);
2035         return;
2036 out:
2037         raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2038 }
2039
2040 /**
2041  * console_unlock - unlock the console system
2042  *
2043  * Releases the console_lock which the caller holds on the console system
2044  * and the console driver list.
2045  *
2046  * While the console_lock was held, console output may have been buffered
2047  * by printk().  If this is the case, console_unlock(); emits
2048  * the output prior to releasing the lock.
2049  *
2050  * If there is output waiting, we wake /dev/kmsg and syslog() users.
2051  *
2052  * console_unlock(); may be called from any context.
2053  */
2054 void console_unlock(void)
2055 {
2056         static char text[LOG_LINE_MAX + PREFIX_MAX];
2057         static u64 seen_seq;
2058         unsigned long flags;
2059         bool wake_klogd = false;
2060         bool retry;
2061
2062         if (console_suspended) {
2063                 up(&console_sem);
2064                 return;
2065         }
2066
2067         console_may_schedule = 0;
2068
2069         /* flush buffered message fragment immediately to console */
2070         console_cont_flush(text, sizeof(text));
2071 again:
2072         for (;;) {
2073                 struct log *msg;
2074                 size_t len;
2075                 int level;
2076
2077                 raw_spin_lock_irqsave(&logbuf_lock, flags);
2078                 if (seen_seq != log_next_seq) {
2079                         wake_klogd = true;
2080                         seen_seq = log_next_seq;
2081                 }
2082
2083                 if (console_seq < log_first_seq) {
2084                         /* messages are gone, move to first one */
2085                         console_seq = log_first_seq;
2086                         console_idx = log_first_idx;
2087                         console_prev = 0;
2088                 }
2089 skip:
2090                 if (console_seq == log_next_seq)
2091                         break;
2092
2093                 msg = log_from_idx(console_idx);
2094                 if (msg->flags & LOG_NOCONS) {
2095                         /*
2096                          * Skip record we have buffered and already printed
2097                          * directly to the console when we received it.
2098                          */
2099                         console_idx = log_next(console_idx);
2100                         console_seq++;
2101                         /*
2102                          * We will get here again when we register a new
2103                          * CON_PRINTBUFFER console. Clear the flag so we
2104                          * will properly dump everything later.
2105                          */
2106                         msg->flags &= ~LOG_NOCONS;
2107                         console_prev = msg->flags;
2108                         goto skip;
2109                 }
2110
2111                 level = msg->level;
2112                 len = msg_print_text(msg, console_prev, false,
2113                                      text, sizeof(text));
2114                 console_idx = log_next(console_idx);
2115                 console_seq++;
2116                 console_prev = msg->flags;
2117                 raw_spin_unlock(&logbuf_lock);
2118
2119                 stop_critical_timings();        /* don't trace print latency */
2120                 call_console_drivers(level, text, len);
2121                 start_critical_timings();
2122                 local_irq_restore(flags);
2123         }
2124         console_locked = 0;
2125         mutex_release(&console_lock_dep_map, 1, _RET_IP_);
2126
2127         /* Release the exclusive_console once it is used */
2128         if (unlikely(exclusive_console))
2129                 exclusive_console = NULL;
2130
2131         raw_spin_unlock(&logbuf_lock);
2132
2133         up(&console_sem);
2134
2135         /*
2136          * Someone could have filled up the buffer again, so re-check if there's
2137          * something to flush. In case we cannot trylock the console_sem again,
2138          * there's a new owner and the console_unlock() from them will do the
2139          * flush, no worries.
2140          */
2141         raw_spin_lock(&logbuf_lock);
2142         retry = console_seq != log_next_seq;
2143         raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2144
2145         if (retry && console_trylock())
2146                 goto again;
2147
2148         if (wake_klogd)
2149                 wake_up_klogd();
2150 }
2151 EXPORT_SYMBOL(console_unlock);
2152
2153 /**
2154  * console_conditional_schedule - yield the CPU if required
2155  *
2156  * If the console code is currently allowed to sleep, and
2157  * if this CPU should yield the CPU to another task, do
2158  * so here.
2159  *
2160  * Must be called within console_lock();.
2161  */
2162 void __sched console_conditional_schedule(void)
2163 {
2164         if (console_may_schedule)
2165                 cond_resched();
2166 }
2167 EXPORT_SYMBOL(console_conditional_schedule);
2168
2169 void console_unblank(void)
2170 {
2171         struct console *c;
2172
2173         /*
2174          * console_unblank can no longer be called in interrupt context unless
2175          * oops_in_progress is set to 1..
2176          */
2177         if (oops_in_progress) {
2178                 if (down_trylock(&console_sem) != 0)
2179                         return;
2180         } else
2181                 console_lock();
2182
2183         console_locked = 1;
2184         console_may_schedule = 0;
2185         for_each_console(c)
2186                 if ((c->flags & CON_ENABLED) && c->unblank)
2187                         c->unblank();
2188         console_unlock();
2189 }
2190
2191 /*
2192  * Return the console tty driver structure and its associated index
2193  */
2194 struct tty_driver *console_device(int *index)
2195 {
2196         struct console *c;
2197         struct tty_driver *driver = NULL;
2198
2199         console_lock();
2200         for_each_console(c) {
2201                 if (!c->device)
2202                         continue;
2203                 driver = c->device(c, index);
2204                 if (driver)
2205                         break;
2206         }
2207         console_unlock();
2208         return driver;
2209 }
2210
2211 /*
2212  * Prevent further output on the passed console device so that (for example)
2213  * serial drivers can disable console output before suspending a port, and can
2214  * re-enable output afterwards.
2215  */
2216 void console_stop(struct console *console)
2217 {
2218         console_lock();
2219         console->flags &= ~CON_ENABLED;
2220         console_unlock();
2221 }
2222 EXPORT_SYMBOL(console_stop);
2223
2224 void console_start(struct console *console)
2225 {
2226         console_lock();
2227         console->flags |= CON_ENABLED;
2228         console_unlock();
2229 }
2230 EXPORT_SYMBOL(console_start);
2231
2232 static int __read_mostly keep_bootcon;
2233
2234 static int __init keep_bootcon_setup(char *str)
2235 {
2236         keep_bootcon = 1;
2237         printk(KERN_INFO "debug: skip boot console de-registration.\n");
2238
2239         return 0;
2240 }
2241
2242 early_param("keep_bootcon", keep_bootcon_setup);
2243
2244 /*
2245  * The console driver calls this routine during kernel initialization
2246  * to register the console printing procedure with printk() and to
2247  * print any messages that were printed by the kernel before the
2248  * console driver was initialized.
2249  *
2250  * This can happen pretty early during the boot process (because of
2251  * early_printk) - sometimes before setup_arch() completes - be careful
2252  * of what kernel features are used - they may not be initialised yet.
2253  *
2254  * There are two types of consoles - bootconsoles (early_printk) and
2255  * "real" consoles (everything which is not a bootconsole) which are
2256  * handled differently.
2257  *  - Any number of bootconsoles can be registered at any time.
2258  *  - As soon as a "real" console is registered, all bootconsoles
2259  *    will be unregistered automatically.
2260  *  - Once a "real" console is registered, any attempt to register a
2261  *    bootconsoles will be rejected
2262  */
2263 void register_console(struct console *newcon)
2264 {
2265         int i;
2266         unsigned long flags;
2267         struct console *bcon = NULL;
2268
2269         /*
2270          * before we register a new CON_BOOT console, make sure we don't
2271          * already have a valid console
2272          */
2273         if (console_drivers && newcon->flags & CON_BOOT) {
2274                 /* find the last or real console */
2275                 for_each_console(bcon) {
2276                         if (!(bcon->flags & CON_BOOT)) {
2277                                 printk(KERN_INFO "Too late to register bootconsole %s%d\n",
2278                                         newcon->name, newcon->index);
2279                                 return;
2280                         }
2281                 }
2282         }
2283
2284         if (console_drivers && console_drivers->flags & CON_BOOT)
2285                 bcon = console_drivers;
2286
2287         if (preferred_console < 0 || bcon || !console_drivers)
2288                 preferred_console = selected_console;
2289
2290         if (newcon->early_setup)
2291                 newcon->early_setup();
2292
2293         /*
2294          *      See if we want to use this console driver. If we
2295          *      didn't select a console we take the first one
2296          *      that registers here.
2297          */
2298         if (preferred_console < 0) {
2299                 if (newcon->index < 0)
2300                         newcon->index = 0;
2301                 if (newcon->setup == NULL ||
2302                     newcon->setup(newcon, NULL) == 0) {
2303                         newcon->flags |= CON_ENABLED;
2304                         if (newcon->device) {
2305                                 newcon->flags |= CON_CONSDEV;
2306                                 preferred_console = 0;
2307                         }
2308                 }
2309         }
2310
2311         /*
2312          *      See if this console matches one we selected on
2313          *      the command line.
2314          */
2315         for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0];
2316                         i++) {
2317                 if (strcmp(console_cmdline[i].name, newcon->name) != 0)
2318                         continue;
2319                 if (newcon->index >= 0 &&
2320                     newcon->index != console_cmdline[i].index)
2321                         continue;
2322                 if (newcon->index < 0)
2323                         newcon->index = console_cmdline[i].index;
2324 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
2325                 if (console_cmdline[i].brl_options) {
2326                         newcon->flags |= CON_BRL;
2327                         braille_register_console(newcon,
2328                                         console_cmdline[i].index,
2329                                         console_cmdline[i].options,
2330                                         console_cmdline[i].brl_options);
2331                         return;
2332                 }
2333 #endif
2334                 if (newcon->setup &&
2335                     newcon->setup(newcon, console_cmdline[i].options) != 0)
2336                         break;
2337                 newcon->flags |= CON_ENABLED;
2338                 newcon->index = console_cmdline[i].index;
2339                 if (i == selected_console) {
2340                         newcon->flags |= CON_CONSDEV;
2341                         preferred_console = selected_console;
2342                 }
2343                 break;
2344         }
2345
2346         if (!(newcon->flags & CON_ENABLED))
2347                 return;
2348
2349         /*
2350          * If we have a bootconsole, and are switching to a real console,
2351          * don't print everything out again, since when the boot console, and
2352          * the real console are the same physical device, it's annoying to
2353          * see the beginning boot messages twice
2354          */
2355         if (bcon && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV))
2356                 newcon->flags &= ~CON_PRINTBUFFER;
2357
2358         /*
2359          *      Put this console in the list - keep the
2360          *      preferred driver at the head of the list.
2361          */
2362         console_lock();
2363         if ((newcon->flags & CON_CONSDEV) || console_drivers == NULL) {
2364                 newcon->next = console_drivers;
2365                 console_drivers = newcon;
2366                 if (newcon->next)
2367                         newcon->next->flags &= ~CON_CONSDEV;
2368         } else {
2369                 newcon->next = console_drivers->next;
2370                 console_drivers->next = newcon;
2371         }
2372         if (newcon->flags & CON_PRINTBUFFER) {
2373                 /*
2374                  * console_unlock(); will print out the buffered messages
2375                  * for us.
2376                  */
2377                 raw_spin_lock_irqsave(&logbuf_lock, flags);
2378                 console_seq = syslog_seq;
2379                 console_idx = syslog_idx;
2380                 console_prev = syslog_prev;
2381                 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2382                 /*
2383                  * We're about to replay the log buffer.  Only do this to the
2384                  * just-registered console to avoid excessive message spam to
2385                  * the already-registered consoles.
2386                  */
2387                 exclusive_console = newcon;
2388         }
2389         console_unlock();
2390         console_sysfs_notify();
2391
2392         /*
2393          * By unregistering the bootconsoles after we enable the real console
2394          * we get the "console xxx enabled" message on all the consoles -
2395          * boot consoles, real consoles, etc - this is to ensure that end
2396          * users know there might be something in the kernel's log buffer that
2397          * went to the bootconsole (that they do not see on the real console)
2398          */
2399         if (bcon &&
2400             ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV) &&
2401             !keep_bootcon) {
2402                 /* we need to iterate through twice, to make sure we print
2403                  * everything out, before we unregister the console(s)
2404                  */
2405                 printk(KERN_INFO "console [%s%d] enabled, bootconsole disabled\n",
2406                         newcon->name, newcon->index);
2407                 for_each_console(bcon)
2408                         if (bcon->flags & CON_BOOT)
2409                                 unregister_console(bcon);
2410         } else {
2411                 printk(KERN_INFO "%sconsole [%s%d] enabled\n",
2412                         (newcon->flags & CON_BOOT) ? "boot" : "" ,
2413                         newcon->name, newcon->index);
2414         }
2415 }
2416 EXPORT_SYMBOL(register_console);
2417
2418 int unregister_console(struct console *console)
2419 {
2420         struct console *a, *b;
2421         int res = 1;
2422
2423 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
2424         if (console->flags & CON_BRL)
2425                 return braille_unregister_console(console);
2426 #endif
2427
2428         console_lock();
2429         if (console_drivers == console) {
2430                 console_drivers=console->next;
2431                 res = 0;
2432         } else if (console_drivers) {
2433                 for (a=console_drivers->next, b=console_drivers ;
2434                      a; b=a, a=b->next) {
2435                         if (a == console) {
2436                                 b->next = a->next;
2437                                 res = 0;
2438                                 break;
2439                         }
2440                 }
2441         }
2442
2443         /*
2444          * If this isn't the last console and it has CON_CONSDEV set, we
2445          * need to set it on the next preferred console.
2446          */
2447         if (console_drivers != NULL && console->flags & CON_CONSDEV)
2448                 console_drivers->flags |= CON_CONSDEV;
2449
2450         console_unlock();
2451         console_sysfs_notify();
2452         return res;
2453 }
2454 EXPORT_SYMBOL(unregister_console);
2455
2456 static int __init printk_late_init(void)
2457 {
2458         struct console *con;
2459
2460         for_each_console(con) {
2461                 if (!keep_bootcon && con->flags & CON_BOOT) {
2462                         printk(KERN_INFO "turn off boot console %s%d\n",
2463                                 con->name, con->index);
2464                         unregister_console(con);
2465                 }
2466         }
2467         hotcpu_notifier(console_cpu_notify, 0);
2468         return 0;
2469 }
2470 late_initcall(printk_late_init);
2471
2472 #if defined CONFIG_PRINTK
2473 /*
2474  * Delayed printk version, for scheduler-internal messages:
2475  */
2476 #define PRINTK_BUF_SIZE         512
2477
2478 #define PRINTK_PENDING_WAKEUP   0x01
2479 #define PRINTK_PENDING_SCHED    0x02
2480
2481 static DEFINE_PER_CPU(int, printk_pending);
2482 static DEFINE_PER_CPU(char [PRINTK_BUF_SIZE], printk_sched_buf);
2483
2484 static void wake_up_klogd_work_func(struct irq_work *irq_work)
2485 {
2486         int pending = __this_cpu_xchg(printk_pending, 0);
2487
2488         if (pending & PRINTK_PENDING_SCHED) {
2489                 char *buf = __get_cpu_var(printk_sched_buf);
2490                 printk(KERN_WARNING "[sched_delayed] %s", buf);
2491         }
2492
2493         if (pending & PRINTK_PENDING_WAKEUP)
2494                 wake_up_interruptible(&log_wait);
2495 }
2496
2497 static DEFINE_PER_CPU(struct irq_work, wake_up_klogd_work) = {
2498         .func = wake_up_klogd_work_func,
2499         .flags = IRQ_WORK_LAZY,
2500 };
2501
2502 void wake_up_klogd(void)
2503 {
2504         preempt_disable();
2505         if (waitqueue_active(&log_wait)) {
2506                 this_cpu_or(printk_pending, PRINTK_PENDING_WAKEUP);
2507                 irq_work_queue(&__get_cpu_var(wake_up_klogd_work));
2508         }
2509         preempt_enable();
2510 }
2511
2512 int printk_sched(const char *fmt, ...)
2513 {
2514         unsigned long flags;
2515         va_list args;
2516         char *buf;
2517         int r;
2518
2519         local_irq_save(flags);
2520         buf = __get_cpu_var(printk_sched_buf);
2521
2522         va_start(args, fmt);
2523         r = vsnprintf(buf, PRINTK_BUF_SIZE, fmt, args);
2524         va_end(args);
2525
2526         __this_cpu_or(printk_pending, PRINTK_PENDING_SCHED);
2527         irq_work_queue(&__get_cpu_var(wake_up_klogd_work));
2528         local_irq_restore(flags);
2529
2530         return r;
2531 }
2532
2533 /*
2534  * printk rate limiting, lifted from the networking subsystem.
2535  *
2536  * This enforces a rate limit: not more than 10 kernel messages
2537  * every 5s to make a denial-of-service attack impossible.
2538  */
2539 DEFINE_RATELIMIT_STATE(printk_ratelimit_state, 5 * HZ, 10);
2540
2541 int __printk_ratelimit(const char *func)
2542 {
2543         return ___ratelimit(&printk_ratelimit_state, func);
2544 }
2545 EXPORT_SYMBOL(__printk_ratelimit);
2546
2547 /**
2548  * printk_timed_ratelimit - caller-controlled printk ratelimiting
2549  * @caller_jiffies: pointer to caller's state
2550  * @interval_msecs: minimum interval between prints
2551  *
2552  * printk_timed_ratelimit() returns true if more than @interval_msecs
2553  * milliseconds have elapsed since the last time printk_timed_ratelimit()
2554  * returned true.
2555  */
2556 bool printk_timed_ratelimit(unsigned long *caller_jiffies,
2557                         unsigned int interval_msecs)
2558 {
2559         if (*caller_jiffies == 0
2560                         || !time_in_range(jiffies, *caller_jiffies,
2561                                         *caller_jiffies
2562                                         + msecs_to_jiffies(interval_msecs))) {
2563                 *caller_jiffies = jiffies;
2564                 return true;
2565         }
2566         return false;
2567 }
2568 EXPORT_SYMBOL(printk_timed_ratelimit);
2569
2570 static DEFINE_SPINLOCK(dump_list_lock);
2571 static LIST_HEAD(dump_list);
2572
2573 /**
2574  * kmsg_dump_register - register a kernel log dumper.
2575  * @dumper: pointer to the kmsg_dumper structure
2576  *
2577  * Adds a kernel log dumper to the system. The dump callback in the
2578  * structure will be called when the kernel oopses or panics and must be
2579  * set. Returns zero on success and %-EINVAL or %-EBUSY otherwise.
2580  */
2581 int kmsg_dump_register(struct kmsg_dumper *dumper)
2582 {
2583         unsigned long flags;
2584         int err = -EBUSY;
2585
2586         /* The dump callback needs to be set */
2587         if (!dumper->dump)
2588                 return -EINVAL;
2589
2590         spin_lock_irqsave(&dump_list_lock, flags);
2591         /* Don't allow registering multiple times */
2592         if (!dumper->registered) {
2593                 dumper->registered = 1;
2594                 list_add_tail_rcu(&dumper->list, &dump_list);
2595                 err = 0;
2596         }
2597         spin_unlock_irqrestore(&dump_list_lock, flags);
2598
2599         return err;
2600 }
2601 EXPORT_SYMBOL_GPL(kmsg_dump_register);
2602
2603 /**
2604  * kmsg_dump_unregister - unregister a kmsg dumper.
2605  * @dumper: pointer to the kmsg_dumper structure
2606  *
2607  * Removes a dump device from the system. Returns zero on success and
2608  * %-EINVAL otherwise.
2609  */
2610 int kmsg_dump_unregister(struct kmsg_dumper *dumper)
2611 {
2612         unsigned long flags;
2613         int err = -EINVAL;
2614
2615         spin_lock_irqsave(&dump_list_lock, flags);
2616         if (dumper->registered) {
2617                 dumper->registered = 0;
2618                 list_del_rcu(&dumper->list);
2619                 err = 0;
2620         }
2621         spin_unlock_irqrestore(&dump_list_lock, flags);
2622         synchronize_rcu();
2623
2624         return err;
2625 }
2626 EXPORT_SYMBOL_GPL(kmsg_dump_unregister);
2627
2628 static bool always_kmsg_dump;
2629 module_param_named(always_kmsg_dump, always_kmsg_dump, bool, S_IRUGO | S_IWUSR);
2630
2631 /**
2632  * kmsg_dump - dump kernel log to kernel message dumpers.
2633  * @reason: the reason (oops, panic etc) for dumping
2634  *
2635  * Call each of the registered dumper's dump() callback, which can
2636  * retrieve the kmsg records with kmsg_dump_get_line() or
2637  * kmsg_dump_get_buffer().
2638  */
2639 void kmsg_dump(enum kmsg_dump_reason reason)
2640 {
2641         struct kmsg_dumper *dumper;
2642         unsigned long flags;
2643
2644         if ((reason > KMSG_DUMP_OOPS) && !always_kmsg_dump)
2645                 return;
2646
2647         rcu_read_lock();
2648         list_for_each_entry_rcu(dumper, &dump_list, list) {
2649                 if (dumper->max_reason && reason > dumper->max_reason)
2650                         continue;
2651
2652                 /* initialize iterator with data about the stored records */
2653                 dumper->active = true;
2654
2655                 raw_spin_lock_irqsave(&logbuf_lock, flags);
2656                 dumper->cur_seq = clear_seq;
2657                 dumper->cur_idx = clear_idx;
2658                 dumper->next_seq = log_next_seq;
2659                 dumper->next_idx = log_next_idx;
2660                 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2661
2662                 /* invoke dumper which will iterate over records */
2663                 dumper->dump(dumper, reason);
2664
2665                 /* reset iterator */
2666                 dumper->active = false;
2667         }
2668         rcu_read_unlock();
2669 }
2670
2671 /**
2672  * kmsg_dump_get_line_nolock - retrieve one kmsg log line (unlocked version)
2673  * @dumper: registered kmsg dumper
2674  * @syslog: include the "<4>" prefixes
2675  * @line: buffer to copy the line to
2676  * @size: maximum size of the buffer
2677  * @len: length of line placed into buffer
2678  *
2679  * Start at the beginning of the kmsg buffer, with the oldest kmsg
2680  * record, and copy one record into the provided buffer.
2681  *
2682  * Consecutive calls will return the next available record moving
2683  * towards the end of the buffer with the youngest messages.
2684  *
2685  * A return value of FALSE indicates that there are no more records to
2686  * read.
2687  *
2688  * The function is similar to kmsg_dump_get_line(), but grabs no locks.
2689  */
2690 bool kmsg_dump_get_line_nolock(struct kmsg_dumper *dumper, bool syslog,
2691                                char *line, size_t size, size_t *len)
2692 {
2693         struct log *msg;
2694         size_t l = 0;
2695         bool ret = false;
2696
2697         if (!dumper->active)
2698                 goto out;
2699
2700         if (dumper->cur_seq < log_first_seq) {
2701                 /* messages are gone, move to first available one */
2702                 dumper->cur_seq = log_first_seq;
2703                 dumper->cur_idx = log_first_idx;
2704         }
2705
2706         /* last entry */
2707         if (dumper->cur_seq >= log_next_seq)
2708                 goto out;
2709
2710         msg = log_from_idx(dumper->cur_idx);
2711         l = msg_print_text(msg, 0, syslog, line, size);
2712
2713         dumper->cur_idx = log_next(dumper->cur_idx);
2714         dumper->cur_seq++;
2715         ret = true;
2716 out:
2717         if (len)
2718                 *len = l;
2719         return ret;
2720 }
2721
2722 /**
2723  * kmsg_dump_get_line - retrieve one kmsg log line
2724  * @dumper: registered kmsg dumper
2725  * @syslog: include the "<4>" prefixes
2726  * @line: buffer to copy the line to
2727  * @size: maximum size of the buffer
2728  * @len: length of line placed into buffer
2729  *
2730  * Start at the beginning of the kmsg buffer, with the oldest kmsg
2731  * record, and copy one record into the provided buffer.
2732  *
2733  * Consecutive calls will return the next available record moving
2734  * towards the end of the buffer with the youngest messages.
2735  *
2736  * A return value of FALSE indicates that there are no more records to
2737  * read.
2738  */
2739 bool kmsg_dump_get_line(struct kmsg_dumper *dumper, bool syslog,
2740                         char *line, size_t size, size_t *len)
2741 {
2742         unsigned long flags;
2743         bool ret;
2744
2745         raw_spin_lock_irqsave(&logbuf_lock, flags);
2746         ret = kmsg_dump_get_line_nolock(dumper, syslog, line, size, len);
2747         raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2748
2749         return ret;
2750 }
2751 EXPORT_SYMBOL_GPL(kmsg_dump_get_line);
2752
2753 /**
2754  * kmsg_dump_get_buffer - copy kmsg log lines
2755  * @dumper: registered kmsg dumper
2756  * @syslog: include the "<4>" prefixes
2757  * @buf: buffer to copy the line to
2758  * @size: maximum size of the buffer
2759  * @len: length of line placed into buffer
2760  *
2761  * Start at the end of the kmsg buffer and fill the provided buffer
2762  * with as many of the the *youngest* kmsg records that fit into it.
2763  * If the buffer is large enough, all available kmsg records will be
2764  * copied with a single call.
2765  *
2766  * Consecutive calls will fill the buffer with the next block of
2767  * available older records, not including the earlier retrieved ones.
2768  *
2769  * A return value of FALSE indicates that there are no more records to
2770  * read.
2771  */
2772 bool kmsg_dump_get_buffer(struct kmsg_dumper *dumper, bool syslog,
2773                           char *buf, size_t size, size_t *len)
2774 {
2775         unsigned long flags;
2776         u64 seq;
2777         u32 idx;
2778         u64 next_seq;
2779         u32 next_idx;
2780         enum log_flags prev;
2781         size_t l = 0;
2782         bool ret = false;
2783
2784         if (!dumper->active)
2785                 goto out;
2786
2787         raw_spin_lock_irqsave(&logbuf_lock, flags);
2788         if (dumper->cur_seq < log_first_seq) {
2789                 /* messages are gone, move to first available one */
2790                 dumper->cur_seq = log_first_seq;
2791                 dumper->cur_idx = log_first_idx;
2792         }
2793
2794         /* last entry */
2795         if (dumper->cur_seq >= dumper->next_seq) {
2796                 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2797                 goto out;
2798         }
2799
2800         /* calculate length of entire buffer */
2801         seq = dumper->cur_seq;
2802         idx = dumper->cur_idx;
2803         prev = 0;
2804         while (seq < dumper->next_seq) {
2805                 struct log *msg = log_from_idx(idx);
2806
2807                 l += msg_print_text(msg, prev, true, NULL, 0);
2808                 idx = log_next(idx);
2809                 seq++;
2810                 prev = msg->flags;
2811         }
2812
2813         /* move first record forward until length fits into the buffer */
2814         seq = dumper->cur_seq;
2815         idx = dumper->cur_idx;
2816         prev = 0;
2817         while (l > size && seq < dumper->next_seq) {
2818                 struct log *msg = log_from_idx(idx);
2819
2820                 l -= msg_print_text(msg, prev, true, NULL, 0);
2821                 idx = log_next(idx);
2822                 seq++;
2823                 prev = msg->flags;
2824         }
2825
2826         /* last message in next interation */
2827         next_seq = seq;
2828         next_idx = idx;
2829
2830         l = 0;
2831         prev = 0;
2832         while (seq < dumper->next_seq) {
2833                 struct log *msg = log_from_idx(idx);
2834
2835                 l += msg_print_text(msg, prev, syslog, buf + l, size - l);
2836                 idx = log_next(idx);
2837                 seq++;
2838                 prev = msg->flags;
2839         }
2840
2841         dumper->next_seq = next_seq;
2842         dumper->next_idx = next_idx;
2843         ret = true;
2844         raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2845 out:
2846         if (len)
2847                 *len = l;
2848         return ret;
2849 }
2850 EXPORT_SYMBOL_GPL(kmsg_dump_get_buffer);
2851
2852 /**
2853  * kmsg_dump_rewind_nolock - reset the interator (unlocked version)
2854  * @dumper: registered kmsg dumper
2855  *
2856  * Reset the dumper's iterator so that kmsg_dump_get_line() and
2857  * kmsg_dump_get_buffer() can be called again and used multiple
2858  * times within the same dumper.dump() callback.
2859  *
2860  * The function is similar to kmsg_dump_rewind(), but grabs no locks.
2861  */
2862 void kmsg_dump_rewind_nolock(struct kmsg_dumper *dumper)
2863 {
2864         dumper->cur_seq = clear_seq;
2865         dumper->cur_idx = clear_idx;
2866         dumper->next_seq = log_next_seq;
2867         dumper->next_idx = log_next_idx;
2868 }
2869
2870 /**
2871  * kmsg_dump_rewind - reset the interator
2872  * @dumper: registered kmsg dumper
2873  *
2874  * Reset the dumper's iterator so that kmsg_dump_get_line() and
2875  * kmsg_dump_get_buffer() can be called again and used multiple
2876  * times within the same dumper.dump() callback.
2877  */
2878 void kmsg_dump_rewind(struct kmsg_dumper *dumper)
2879 {
2880         unsigned long flags;
2881
2882         raw_spin_lock_irqsave(&logbuf_lock, flags);
2883         kmsg_dump_rewind_nolock(dumper);
2884         raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2885 }
2886 EXPORT_SYMBOL_GPL(kmsg_dump_rewind);
2887
2888 static char dump_stack_arch_desc_str[128];
2889
2890 /**
2891  * dump_stack_set_arch_desc - set arch-specific str to show with task dumps
2892  * @fmt: printf-style format string
2893  * @...: arguments for the format string
2894  *
2895  * The configured string will be printed right after utsname during task
2896  * dumps.  Usually used to add arch-specific system identifiers.  If an
2897  * arch wants to make use of such an ID string, it should initialize this
2898  * as soon as possible during boot.
2899  */
2900 void __init dump_stack_set_arch_desc(const char *fmt, ...)
2901 {
2902         va_list args;
2903
2904         va_start(args, fmt);
2905         vsnprintf(dump_stack_arch_desc_str, sizeof(dump_stack_arch_desc_str),
2906                   fmt, args);
2907         va_end(args);
2908 }
2909
2910 /**
2911  * dump_stack_print_info - print generic debug info for dump_stack()
2912  * @log_lvl: log level
2913  *
2914  * Arch-specific dump_stack() implementations can use this function to
2915  * print out the same debug information as the generic dump_stack().
2916  */
2917 void dump_stack_print_info(const char *log_lvl)
2918 {
2919         printk("%sCPU: %d PID: %d Comm: %.20s %s %s %.*s\n",
2920                log_lvl, raw_smp_processor_id(), current->pid, current->comm,
2921                print_tainted(), init_utsname()->release,
2922                (int)strcspn(init_utsname()->version, " "),
2923                init_utsname()->version);
2924
2925         if (dump_stack_arch_desc_str[0] != '\0')
2926                 printk("%sHardware name: %s\n",
2927                        log_lvl, dump_stack_arch_desc_str);
2928
2929         print_worker_info(log_lvl, current);
2930 }
2931
2932 /**
2933  * show_regs_print_info - print generic debug info for show_regs()
2934  * @log_lvl: log level
2935  *
2936  * show_regs() implementations can use this function to print out generic
2937  * debug information.
2938  */
2939 void show_regs_print_info(const char *log_lvl)
2940 {
2941         dump_stack_print_info(log_lvl);
2942
2943         printk("%stask: %p ti: %p task.ti: %p\n",
2944                log_lvl, current, current_thread_info(),
2945                task_thread_info(current));
2946 }
2947
2948 #endif