blob: e6a85a085dec65b5a67e4cf5066918185cd30c4d [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/*
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#include <linux/kernel.h>
19#include <linux/mm.h>
20#include <linux/tty.h>
21#include <linux/tty_driver.h>
22#include <linux/console.h>
23#include <linux/init.h>
24#include <linux/jiffies.h>
25#include <linux/nmi.h>
26#include <linux/module.h>
27#include <linux/moduleparam.h>
28#include <linux/interrupt.h> /* For in_interrupt() */
29#include <linux/delay.h>
30#include <linux/smp.h>
31#include <linux/security.h>
32#include <linux/bootmem.h>
33#include <linux/memblock.h>
34#include <linux/syscalls.h>
35#include <linux/kexec.h>
36#include <linux/kdb.h>
37#include <linux/ratelimit.h>
38#include <linux/kmsg_dump.h>
39#include <linux/syslog.h>
40#include <linux/cpu.h>
41#include <linux/notifier.h>
42#include <linux/rculist.h>
43
44#include <asm/uaccess.h>
45
46#define CREATE_TRACE_POINTS
47#include <trace/events/printk.h>
48
49/* AP LOG BEGIN */
50#if 0
51#include "../drivers/staging/usbproxy/logcat_drv.h"
52//extern int KERNEL_START_END;
53extern T_RINGBUFFER* KERNEL_BUFF;
54extern int enable_kernellog;
55extern UINT32 WriteRingBuffer(T_RINGBUFFER *ringBuf, UINT8 *buf, UINT32 len);
56extern void readringbuf_for_printk();
57#endif
58/* AP LOG END */
59#define CONFIG_ZCAT_DEVICE
60#ifdef CONFIG_ZCAT_DEVICE
61#ifndef CONFIG_SYSTEM_RECOVERY
62extern ssize_t zCatAgt_Kernel_Write(const char *buf, unsigned long count);
63#endif
64#endif
65
66#define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT)
67
68
69#ifdef CONFIG_DEBUG_LL
70extern void printascii(char *);
71#endif
72
73/* printk's without a loglevel use this.. */
74#define DEFAULT_MESSAGE_LOGLEVEL CONFIG_DEFAULT_MESSAGE_LOGLEVEL
75
76/* We show everything that is MORE important than this.. */
77#define MINIMUM_CONSOLE_LOGLEVEL 1 /* Minimum loglevel we let people use */
78#define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */
79
80DECLARE_WAIT_QUEUE_HEAD(log_wait);
81
82int console_printk[4] = {
83 DEFAULT_CONSOLE_LOGLEVEL, /* console_loglevel */
84 DEFAULT_MESSAGE_LOGLEVEL, /* default_message_loglevel */
85 MINIMUM_CONSOLE_LOGLEVEL, /* minimum_console_loglevel */
86 DEFAULT_CONSOLE_LOGLEVEL, /* default_console_loglevel */
87};
88
89/*
90 * Low level drivers may need that to know if they can schedule in
91 * their unblank() callback or not. So let's export it.
92 */
93int oops_in_progress;
94EXPORT_SYMBOL(oops_in_progress);
95
96/*
97 * console_sem protects the console_drivers list, and also
98 * provides serialisation for access to the entire console
99 * driver system.
100 */
101static DEFINE_SEMAPHORE(console_sem);
102struct console *console_drivers;
103EXPORT_SYMBOL_GPL(console_drivers);
104
105/*
106 * This is used for debugging the mess that is the VT code by
107 * keeping track if we have the console semaphore held. It's
108 * definitely not the perfect debug tool (we don't know if _WE_
109 * hold it are racing, but it helps tracking those weird code
110 * path in the console code where we end up in places I want
111 * locked without the console sempahore held
112 */
113static int console_locked, console_suspended;
114
115/*
116 * logbuf_lock protects log_buf, log_start, log_end, con_start and logged_chars
117 * It is also used in interesting ways to provide interlocking in
118 * console_unlock();.
119 */
120static DEFINE_RAW_SPINLOCK(logbuf_lock);
121
122#define LOG_BUF_MASK (log_buf_len-1)
123#define LOG_BUF(idx) (log_buf[(idx) & LOG_BUF_MASK])
124
125/*
126 * The indices into log_buf are not constrained to log_buf_len - they
127 * must be masked before subscripting
128 */
129static unsigned log_start; /* Index into log_buf: next char to be read by syslog() */
130static unsigned con_start; /* Index into log_buf: next char to be sent to consoles */
131static unsigned log_end; /* Index into log_buf: most-recently-written-char + 1 */
132
133/*
134 * If exclusive_console is non-NULL then only this console is to be printed to.
135 */
136static struct console *exclusive_console;
137
138/*
139 * Array of consoles built from command line options (console=)
140 */
141struct console_cmdline
142{
143 char name[16]; /* Name of the driver */
144 int index; /* Minor dev. to use */
145 char *options; /* Options for the driver */
146#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
147 char *brl_options; /* Options for braille driver */
148#endif
149};
150
151#define MAX_CMDLINECONSOLES 8
152
153static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
154static int selected_console = -1;
155static int preferred_console = -1;
156int console_set_on_cmdline;
157EXPORT_SYMBOL(console_set_on_cmdline);
158
159/* Flag: console code may call schedule() */
160static int console_may_schedule;
161
162#ifdef CONFIG_PRINTK
163
164static char __log_buf[__LOG_BUF_LEN];
165static char *log_buf = __log_buf;
166static int log_buf_len = __LOG_BUF_LEN;
167static unsigned logged_chars; /* Number of chars produced since last read+clear operation */
168static int saved_console_loglevel = -1;
169
170#ifdef CONFIG_KEXEC
171/*
172 * This appends the listed symbols to /proc/vmcoreinfo
173 *
174 * /proc/vmcoreinfo is used by various utiilties, like crash and makedumpfile to
175 * obtain access to symbols that are otherwise very difficult to locate. These
176 * symbols are specifically used so that utilities can access and extract the
177 * dmesg log from a vmcore file after a crash.
178 */
179void log_buf_kexec_setup(void)
180{
181 VMCOREINFO_SYMBOL(log_buf);
182 VMCOREINFO_SYMBOL(log_end);
183 VMCOREINFO_SYMBOL(log_buf_len);
184 VMCOREINFO_SYMBOL(logged_chars);
185}
186#endif
187
188/* requested log_buf_len from kernel cmdline */
189static unsigned long __initdata new_log_buf_len;
190
191/* save requested log_buf_len since it's too early to process it */
192static int __init log_buf_len_setup(char *str)
193{
194 unsigned size = memparse(str, &str);
195
196 if (size)
197 size = roundup_pow_of_two(size);
198 if (size > log_buf_len)
199 new_log_buf_len = size;
200
201 return 0;
202}
203early_param("log_buf_len", log_buf_len_setup);
204
205void __init setup_log_buf(int early)
206{
207 unsigned long flags;
208 unsigned start, dest_idx, offset;
209 char *new_log_buf;
210 int free;
211
212 if (!new_log_buf_len)
213 return;
214
215 if (early) {
216 unsigned long mem;
217
218 mem = memblock_alloc(new_log_buf_len, PAGE_SIZE);
219 if (!mem)
220 return;
221 new_log_buf = __va(mem);
222 } else {
223 new_log_buf = alloc_bootmem_nopanic(new_log_buf_len);
224 }
225
226 if (unlikely(!new_log_buf)) {
227 pr_err("log_buf_len: %ld bytes not available\n",
228 new_log_buf_len);
229 return;
230 }
231
232 raw_spin_lock_irqsave(&logbuf_lock, flags);
233 log_buf_len = new_log_buf_len;
234 log_buf = new_log_buf;
235 new_log_buf_len = 0;
236 free = __LOG_BUF_LEN - log_end;
237
238 offset = start = min(con_start, log_start);
239 dest_idx = 0;
240 while (start != log_end) {
241 unsigned log_idx_mask = start & (__LOG_BUF_LEN - 1);
242
243 log_buf[dest_idx] = __log_buf[log_idx_mask];
244 start++;
245 dest_idx++;
246 }
247 log_start -= offset;
248 con_start -= offset;
249 log_end -= offset;
250 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
251
252 pr_info("log_buf_len: %d\n", log_buf_len);
253 pr_info("early log buf free: %d(%d%%)\n",
254 free, (free * 100) / __LOG_BUF_LEN);
255}
256
257#ifdef CONFIG_BOOT_PRINTK_DELAY
258
259static int boot_delay; /* msecs delay after each printk during bootup */
260static unsigned long long loops_per_msec; /* based on boot_delay */
261
262static int __init boot_delay_setup(char *str)
263{
264 unsigned long lpj;
265
266 lpj = preset_lpj ? preset_lpj : 1000000; /* some guess */
267 loops_per_msec = (unsigned long long)lpj / 1000 * HZ;
268
269 get_option(&str, &boot_delay);
270 if (boot_delay > 10 * 1000)
271 boot_delay = 0;
272
273 pr_debug("boot_delay: %u, preset_lpj: %ld, lpj: %lu, "
274 "HZ: %d, loops_per_msec: %llu\n",
275 boot_delay, preset_lpj, lpj, HZ, loops_per_msec);
276 return 1;
277}
278__setup("boot_delay=", boot_delay_setup);
279
280static void boot_delay_msec(void)
281{
282 unsigned long long k;
283 unsigned long timeout;
284
285 if (boot_delay == 0 || system_state != SYSTEM_BOOTING)
286 return;
287
288 k = (unsigned long long)loops_per_msec * boot_delay;
289
290 timeout = jiffies + msecs_to_jiffies(boot_delay);
291 while (k) {
292 k--;
293 cpu_relax();
294 /*
295 * use (volatile) jiffies to prevent
296 * compiler reduction; loop termination via jiffies
297 * is secondary and may or may not happen.
298 */
299 if (time_after(jiffies, timeout))
300 break;
301 touch_nmi_watchdog();
302 }
303}
304#else
305static inline void boot_delay_msec(void)
306{
307}
308#endif
309
310/*
311 * Return the number of unread characters in the log buffer.
312 */
313static int log_buf_get_len(void)
314{
315 return logged_chars;
316}
317
318/*
319 * Clears the ring-buffer
320 */
321void log_buf_clear(void)
322{
323 logged_chars = 0;
324}
325
326/*
327 * Copy a range of characters from the log buffer.
328 */
329int log_buf_copy(char *dest, int idx, int len)
330{
331 int ret, max;
332 bool took_lock = false;
333
334 if (!oops_in_progress) {
335 raw_spin_lock_irq(&logbuf_lock);
336 took_lock = true;
337 }
338
339 max = log_buf_get_len();
340 if (idx < 0 || idx >= max) {
341 ret = -1;
342 } else {
343 if (len > max - idx)
344 len = max - idx;
345 ret = len;
346 idx += (log_end - max);
347 while (len-- > 0)
348 dest[len] = LOG_BUF(idx + len);
349 }
350
351 if (took_lock)
352 raw_spin_unlock_irq(&logbuf_lock);
353
354 return ret;
355}
356
357#ifdef CONFIG_SECURITY_DMESG_RESTRICT
358int dmesg_restrict = 1;
359#else
360int dmesg_restrict;
361#endif
362
363static int syslog_action_restricted(int type)
364{
365 if (dmesg_restrict)
366 return 1;
367 /* Unless restricted, we allow "read all" and "get buffer size" for everybody */
368 return type != SYSLOG_ACTION_READ_ALL && type != SYSLOG_ACTION_SIZE_BUFFER;
369}
370
371static int check_syslog_permissions(int type, bool from_file)
372{
373 /*
374 * If this is from /proc/kmsg and we've already opened it, then we've
375 * already done the capabilities checks at open time.
376 */
377 if (from_file && type != SYSLOG_ACTION_OPEN)
378 return 0;
379
380 if (syslog_action_restricted(type)) {
381 if (capable(CAP_SYSLOG))
382 return 0;
383 /* For historical reasons, accept CAP_SYS_ADMIN too, with a warning */
384 if (capable(CAP_SYS_ADMIN)) {
385 printk_once(KERN_WARNING "%s (%d): "
386 "Attempt to access syslog with CAP_SYS_ADMIN "
387 "but no CAP_SYSLOG (deprecated).\n",
388 current->comm, task_pid_nr(current));
389 return 0;
390 }
391 return -EPERM;
392 }
393 return 0;
394}
395
396int do_syslog(int type, char __user *buf, int len, bool from_file)
397{
398 unsigned i, j, limit, count;
399 int do_clear = 0;
400 char c;
401 int error;
402
403 error = check_syslog_permissions(type, from_file);
404 if (error)
405 goto out;
406
407 error = security_syslog(type);
408 if (error)
409 return error;
410
411 switch (type) {
412 case SYSLOG_ACTION_CLOSE: /* Close log */
413 break;
414 case SYSLOG_ACTION_OPEN: /* Open log */
415 break;
416 case SYSLOG_ACTION_READ: /* Read from log */
417 error = -EINVAL;
418 if (!buf || len < 0)
419 goto out;
420 error = 0;
421 if (!len)
422 goto out;
423 if (!access_ok(VERIFY_WRITE, buf, len)) {
424 error = -EFAULT;
425 goto out;
426 }
427 error = wait_event_interruptible(log_wait,
428 (log_start - log_end));
429 if (error)
430 goto out;
431 i = 0;
432 raw_spin_lock_irq(&logbuf_lock);
433 while (!error && (log_start != log_end) && i < len) {
434 c = LOG_BUF(log_start);
435 log_start++;
436 raw_spin_unlock_irq(&logbuf_lock);
437 error = __put_user(c,buf);
438 buf++;
439 i++;
440 cond_resched();
441 raw_spin_lock_irq(&logbuf_lock);
442 }
443 raw_spin_unlock_irq(&logbuf_lock);
444 if (!error)
445 error = i;
446 break;
447 /* Read/clear last kernel messages */
448 case SYSLOG_ACTION_READ_CLEAR:
449 do_clear = 1;
450 /* FALL THRU */
451 /* Read last kernel messages */
452 case SYSLOG_ACTION_READ_ALL:
453 error = -EINVAL;
454 if (!buf || len < 0)
455 goto out;
456 error = 0;
457 if (!len)
458 goto out;
459 if (!access_ok(VERIFY_WRITE, buf, len)) {
460 error = -EFAULT;
461 goto out;
462 }
463 count = len;
464 if (count > log_buf_len)
465 count = log_buf_len;
466 raw_spin_lock_irq(&logbuf_lock);
467 if (count > logged_chars)
468 count = logged_chars;
469 if (do_clear)
470 logged_chars = 0;
471 limit = log_end;
472 /*
473 * __put_user() could sleep, and while we sleep
474 * printk() could overwrite the messages
475 * we try to copy to user space. Therefore
476 * the messages are copied in reverse. <manfreds>
477 */
478 for (i = 0; i < count && !error; i++) {
479 j = limit-1-i;
480 if (j + log_buf_len < log_end)
481 break;
482 c = LOG_BUF(j);
483 raw_spin_unlock_irq(&logbuf_lock);
484 error = __put_user(c,&buf[count-1-i]);
485 cond_resched();
486 raw_spin_lock_irq(&logbuf_lock);
487 }
488 raw_spin_unlock_irq(&logbuf_lock);
489 if (error)
490 break;
491 error = i;
492 if (i != count) {
493 int offset = count-error;
494 /* buffer overflow during copy, correct user buffer. */
495 for (i = 0; i < error; i++) {
496 if (__get_user(c,&buf[i+offset]) ||
497 __put_user(c,&buf[i])) {
498 error = -EFAULT;
499 break;
500 }
501 cond_resched();
502 }
503 }
504 break;
505 /* Clear ring buffer */
506 case SYSLOG_ACTION_CLEAR:
507 logged_chars = 0;
508 break;
509 /* Disable logging to console */
510 case SYSLOG_ACTION_CONSOLE_OFF:
511 if (saved_console_loglevel == -1)
512 saved_console_loglevel = console_loglevel;
513 console_loglevel = minimum_console_loglevel;
514 break;
515 /* Enable logging to console */
516 case SYSLOG_ACTION_CONSOLE_ON:
517 if (saved_console_loglevel != -1) {
518 console_loglevel = saved_console_loglevel;
519 saved_console_loglevel = -1;
520 }
521 break;
522 /* Set level of messages printed to console */
523 case SYSLOG_ACTION_CONSOLE_LEVEL:
524 error = -EINVAL;
525 if (len < 1 || len > 8)
526 goto out;
527 if (len < minimum_console_loglevel)
528 len = minimum_console_loglevel;
529 console_loglevel = len;
530 /* Implicitly re-enable logging to console */
531 saved_console_loglevel = -1;
532 error = 0;
533 break;
534 /* Number of chars in the log buffer */
535 case SYSLOG_ACTION_SIZE_UNREAD:
536 error = log_end - log_start;
537 break;
538 /* Size of the log buffer */
539 case SYSLOG_ACTION_SIZE_BUFFER:
540 error = log_buf_len;
541 break;
542 default:
543 error = -EINVAL;
544 break;
545 }
546out:
547 return error;
548}
549
550SYSCALL_DEFINE3(syslog, int, type, char __user *, buf, int, len)
551{
552 return do_syslog(type, buf, len, SYSLOG_FROM_CALL);
553}
554
555#ifdef CONFIG_KGDB_KDB
556/* kdb dmesg command needs access to the syslog buffer. do_syslog()
557 * uses locks so it cannot be used during debugging. Just tell kdb
558 * where the start and end of the physical and logical logs are. This
559 * is equivalent to do_syslog(3).
560 */
561void kdb_syslog_data(char *syslog_data[4])
562{
563 syslog_data[0] = log_buf;
564 syslog_data[1] = log_buf + log_buf_len;
565 syslog_data[2] = log_buf + log_end -
566 (logged_chars < log_buf_len ? logged_chars : log_buf_len);
567 syslog_data[3] = log_buf + log_end;
568}
569#endif /* CONFIG_KGDB_KDB */
570
571/*
572 * Call the console drivers on a range of log_buf
573 */
574static void __call_console_drivers(unsigned start, unsigned end)
575{
576 struct console *con;
577
578 migrate_disable();
579 for_each_console(con) {
580 if (exclusive_console && con != exclusive_console)
581 continue;
582 if ((con->flags & CON_ENABLED) && con->write &&
583 (cpu_online(smp_processor_id()) ||
584 (con->flags & CON_ANYTIME)))
585 con->write(con, &LOG_BUF(start), end - start);
586 }
587 migrate_enable();
588}
589
590#ifdef CONFIG_EARLY_PRINTK
591struct console *early_console;
592
593static void early_vprintk(const char *fmt, va_list ap)
594{
595 if (early_console) {
596 char buf[512];
597 int n = vscnprintf(buf, sizeof(buf), fmt, ap);
598
599 early_console->write(early_console, buf, n);
600 }
601}
602
603asmlinkage void early_printk(const char *fmt, ...)
604{
605 va_list ap;
606
607 va_start(ap, fmt);
608 early_vprintk(fmt, ap);
609 va_end(ap);
610}
611
612/*
613 * This is independent of any log levels - a global
614 * kill switch that turns off all of printk.
615 *
616 * Used by the NMI watchdog if early-printk is enabled.
617 */
618static bool __read_mostly printk_killswitch;
619
620static int __init force_early_printk_setup(char *str)
621{
622 printk_killswitch = true;
623 return 0;
624}
625early_param("force_early_printk", force_early_printk_setup);
626
627void printk_kill(void)
628{
629 printk_killswitch = true;
630}
631
632static int forced_early_printk(const char *fmt, va_list ap)
633{
634 if (!printk_killswitch)
635 return 0;
636 early_vprintk(fmt, ap);
637 return 1;
638}
639#else
640static inline int forced_early_printk(const char *fmt, va_list ap)
641{
642 return 0;
643}
644#endif
645
646static bool __read_mostly ignore_loglevel;
647
648static int __init ignore_loglevel_setup(char *str)
649{
650 ignore_loglevel = 1;
651 printk(KERN_INFO "debug: ignoring loglevel setting.\n");
652
653 return 0;
654}
655
656early_param("ignore_loglevel", ignore_loglevel_setup);
657module_param(ignore_loglevel, bool, S_IRUGO | S_IWUSR);
658MODULE_PARM_DESC(ignore_loglevel, "ignore loglevel setting, to"
659 "print all kernel messages to the console.");
660
661/*
662 * Write out chars from start to end - 1 inclusive
663 */
664static void _call_console_drivers(unsigned start,
665 unsigned end, int msg_log_level)
666{
667 trace_console(&LOG_BUF(0), start, end, log_buf_len);
668
669 if ((msg_log_level < console_loglevel || ignore_loglevel) &&
670 console_drivers && start != end) {
671 if ((start & LOG_BUF_MASK) > (end & LOG_BUF_MASK)) {
672 /* wrapped write */
673 __call_console_drivers(start & LOG_BUF_MASK,
674 log_buf_len);
675 __call_console_drivers(0, end & LOG_BUF_MASK);
676 } else {
677 __call_console_drivers(start, end);
678 }
679 }
680}
681
682/*
683 * Parse the syslog header <[0-9]*>. The decimal value represents 32bit, the
684 * lower 3 bit are the log level, the rest are the log facility. In case
685 * userspace passes usual userspace syslog messages to /dev/kmsg or
686 * /dev/ttyprintk, the log prefix might contain the facility. Printk needs
687 * to extract the correct log level for in-kernel processing, and not mangle
688 * the original value.
689 *
690 * If a prefix is found, the length of the prefix is returned. If 'level' is
691 * passed, it will be filled in with the log level without a possible facility
692 * value. If 'special' is passed, the special printk prefix chars are accepted
693 * and returned. If no valid header is found, 0 is returned and the passed
694 * variables are not touched.
695 */
696static size_t log_prefix(const char *p, unsigned int *level, char *special)
697{
698 unsigned int lev = 0;
699 char sp = '\0';
700 size_t len;
701
702 if (p[0] != '<' || !p[1])
703 return 0;
704 if (p[2] == '>') {
705 /* usual single digit level number or special char */
706 switch (p[1]) {
707 case '0' ... '7':
708 lev = p[1] - '0';
709 break;
710 case 'c': /* KERN_CONT */
711 case 'd': /* KERN_DEFAULT */
712 sp = p[1];
713 break;
714 default:
715 return 0;
716 }
717 len = 3;
718 } else {
719 /* multi digit including the level and facility number */
720 char *endp = NULL;
721
722 lev = (simple_strtoul(&p[1], &endp, 10) & 7);
723 if (endp == NULL || endp[0] != '>')
724 return 0;
725 len = (endp + 1) - p;
726 }
727
728 /* do not accept special char if not asked for */
729 if (sp && !special)
730 return 0;
731
732 if (special) {
733 *special = sp;
734 /* return special char, do not touch level */
735 if (sp)
736 return len;
737 }
738
739 if (level)
740 *level = lev;
741 return len;
742}
743
744/*
745 * Call the console drivers, asking them to write out
746 * log_buf[start] to log_buf[end - 1].
747 * The console_lock must be held.
748 */
749static void call_console_drivers(unsigned start, unsigned end)
750{
751 unsigned cur_index, start_print;
752 static int msg_level = -1;
753
754 BUG_ON(((int)(start - end)) > 0);
755
756 cur_index = start;
757 start_print = start;
758 while (cur_index != end) {
759 if (msg_level < 0 && ((end - cur_index) > 2)) {
760 /*
761 * prepare buf_prefix, as a contiguous array,
762 * to be processed by log_prefix function
763 */
764 char buf_prefix[SYSLOG_PRI_MAX_LENGTH+1];
765 unsigned i;
766 for (i = 0; i < ((end - cur_index)) && (i < SYSLOG_PRI_MAX_LENGTH); i++) {
767 buf_prefix[i] = LOG_BUF(cur_index + i);
768 }
769 buf_prefix[i] = '\0'; /* force '\0' as last string character */
770
771 /* strip log prefix */
772 cur_index += log_prefix((const char *)&buf_prefix, &msg_level, NULL);
773 start_print = cur_index;
774 }
775 while (cur_index != end) {
776 char c = LOG_BUF(cur_index);
777
778 cur_index++;
779 if (c == '\n') {
780 if (msg_level < 0) {
781 /*
782 * printk() has already given us loglevel tags in
783 * the buffer. This code is here in case the
784 * log buffer has wrapped right round and scribbled
785 * on those tags
786 */
787 msg_level = default_message_loglevel;
788 }
789 _call_console_drivers(start_print, cur_index, msg_level);
790 msg_level = -1;
791 start_print = cur_index;
792 break;
793 }
794 }
795 }
796 _call_console_drivers(start_print, end, msg_level);
797}
798
799static void emit_log_char(char c)
800{
801 LOG_BUF(log_end) = c;
802 log_end++;
803 if (log_end - log_start > log_buf_len)
804 log_start = log_end - log_buf_len;
805 if (log_end - con_start > log_buf_len)
806 con_start = log_end - log_buf_len;
807 if (logged_chars < log_buf_len)
808 logged_chars++;
809
810 /* AP LOG BEGIN */
811 //if (1 == KERNEL_START_END)
812 #if 0
813 if (KERNEL_BUFF!=NULL&&enable_kernellog ==1)
814 {
815 WriteRingBuffer(KERNEL_BUFF, &c, 1);
816 readringbuf_for_printk();
817 }
818 #endif
819 /* AP LOG END */
820
821#ifdef CONFIG_ZCAT_DEVICE
822#ifndef CONFIG_SYSTEM_RECOVERY
823 #ifdef USE_CPPS_KO
824 if(cpps_callbacks.zCatAgt_Kernel_Write)
825 cpps_callbacks.zCatAgt_Kernel_Write(&c, 1);
826 #else
827 zCatAgt_Kernel_Write(&c, 1);
828 #endif
829#endif
830#endif
831}
832
833/*
834 * Zap console related locks when oopsing. Only zap at most once
835 * every 10 seconds, to leave time for slow consoles to print a
836 * full oops.
837 */
838static void zap_locks(void)
839{
840 static unsigned long oops_timestamp;
841
842 if (time_after_eq(jiffies, oops_timestamp) &&
843 !time_after(jiffies, oops_timestamp + 30 * HZ))
844 return;
845
846 oops_timestamp = jiffies;
847
848 debug_locks_off();
849 /* If a crash is occurring, make sure we can't deadlock */
850 raw_spin_lock_init(&logbuf_lock);
851 /* And make sure that we print immediately */
852 sema_init(&console_sem, 1);
853}
854
855#if defined(CONFIG_PRINTK_TIME)
856static bool printk_time = 1;
857#else
858static bool printk_time = 0;
859#endif
860module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR);
861
862static bool always_kmsg_dump;
863module_param_named(always_kmsg_dump, always_kmsg_dump, bool, S_IRUGO | S_IWUSR);
864
865/* Check if we have any console registered that can be called early in boot. */
866static int have_callable_console(void)
867{
868 struct console *con;
869
870 for_each_console(con)
871 if (con->flags & CON_ANYTIME)
872 return 1;
873
874 return 0;
875}
876
877/**
878 * printk - print a kernel message
879 * @fmt: format string
880 *
881 * This is printk(). It can be called from any context. We want it to work.
882 *
883 * We try to grab the console_lock. If we succeed, it's easy - we log the output and
884 * call the console drivers. If we fail to get the semaphore we place the output
885 * into the log buffer and return. The current holder of the console_sem will
886 * notice the new output in console_unlock(); and will send it to the
887 * consoles before releasing the lock.
888 *
889 * One effect of this deferred printing is that code which calls printk() and
890 * then changes console_loglevel may break. This is because console_loglevel
891 * is inspected when the actual printing occurs.
892 *
893 * See also:
894 * printf(3)
895 *
896 * See the vsnprintf() documentation for format string extensions over C99.
897 */
898
899asmlinkage int printk(const char *fmt, ...)
900{
901 va_list args;
902 int r;
903
904#ifdef CONFIG_KGDB_KDB
905 if (unlikely(kdb_trap_printk)) {
906 va_start(args, fmt);
907 r = vkdb_printf(fmt, args);
908 va_end(args);
909 return r;
910 }
911#endif
912 va_start(args, fmt);
913 r = vprintk(fmt, args);
914 va_end(args);
915
916 return r;
917}
918
919/* cpu currently holding logbuf_lock */
920static volatile unsigned int printk_cpu = UINT_MAX;
921
922/*
923 * Can we actually use the console at this time on this cpu?
924 *
925 * Console drivers may assume that per-cpu resources have
926 * been allocated. So unless they're explicitly marked as
927 * being able to cope (CON_ANYTIME) don't call them until
928 * this CPU is officially up.
929 */
930static inline int can_use_console(unsigned int cpu)
931{
932 return cpu_online(cpu) || have_callable_console();
933}
934
935/*
936 * Try to get console ownership to actually show the kernel
937 * messages from a 'printk'. Return true (and with the
938 * console_lock held, and 'console_locked' set) if it
939 * is successful, false otherwise.
940 *
941 * This gets called with the 'logbuf_lock' spinlock held and
942 * interrupts disabled. It should return with 'lockbuf_lock'
943 * released but interrupts still disabled.
944 */
945static int console_trylock_for_printk(unsigned int cpu, unsigned long flags)
946 __releases(&logbuf_lock)
947{
948 int retval = 0, wake = 0;
949#ifdef CONFIG_PREEMPT_RT_FULL
950 /*Ïß³ÌÓÅÏȼ¶¸ßÓÚЭÒéÕ»ÖØÒªÏ̵߳ģ¨ÓëÅíÀÚ¼°¹úÆÂÈ·ÈÏ£¬Ôݶ¨62ÓÅÏȼ¶£©£¬²»ÔÙʵʱ´òÓ¡£¬EC 616000549169*/
951 int lock = !early_boot_irqs_disabled && !irqs_disabled_flags(flags) &&
952 (preempt_count() <= 1) && (current->prio > 62);
953#else
954 int lock = 1;
955#endif
956
957 if (lock && console_trylock()) {
958 retval = 1;
959
960 /*
961 * If we can't use the console, we need to release
962 * the console semaphore by hand to avoid flushing
963 * the buffer. We need to hold the console semaphore
964 * in order to do this test safely.
965 */
966 if (!can_use_console(cpu)) {
967 console_locked = 0;
968 wake = 1;
969 retval = 0;
970 }
971 }
972 printk_cpu = UINT_MAX;
973 raw_spin_unlock(&logbuf_lock);
974 if (wake)
975 up(&console_sem);
976 return retval;
977}
978static const char recursion_bug_msg [] =
979 KERN_CRIT "BUG: recent printk recursion!\n";
980static int recursion_bug;
981static int new_text_line = 1;
982static char printk_buf[1024];
983
984int printk_delay_msec __read_mostly;
985
986static inline void printk_delay(void)
987{
988 if (unlikely(printk_delay_msec)) {
989 int m = printk_delay_msec;
990
991 while (m--) {
992 mdelay(1);
993 touch_nmi_watchdog();
994 }
995 }
996}
997
998asmlinkage int vprintk(const char *fmt, va_list args)
999{
1000 int printed_len = 0;
1001 int current_log_level = default_message_loglevel;
1002 unsigned long flags;
1003 int this_cpu;
1004 char *p;
1005 size_t plen;
1006 char special;
1007
1008 /*
1009 * Fall back to early_printk if a debugging subsystem has
1010 * killed printk output
1011 */
1012 if (unlikely(forced_early_printk(fmt, args)))
1013 return 1;
1014
1015 boot_delay_msec();
1016 printk_delay();
1017
1018 /* This stops the holder of console_sem just where we want him */
1019 local_irq_save(flags);
1020 this_cpu = smp_processor_id();
1021
1022 /*
1023 * Ouch, printk recursed into itself!
1024 */
1025 if (unlikely(printk_cpu == this_cpu)) {
1026 /*
1027 * If a crash is occurring during printk() on this CPU,
1028 * then try to get the crash message out but make sure
1029 * we can't deadlock. Otherwise just return to avoid the
1030 * recursion and return - but flag the recursion so that
1031 * it can be printed at the next appropriate moment:
1032 */
1033 if (!oops_in_progress && !lockdep_recursing(current)) {
1034 recursion_bug = 1;
1035 goto out_restore_irqs;
1036 }
1037 zap_locks();
1038 }
1039
1040 lockdep_off();
1041 raw_spin_lock(&logbuf_lock);
1042 printk_cpu = this_cpu;
1043
1044 if (recursion_bug) {
1045 recursion_bug = 0;
1046 strcpy(printk_buf, recursion_bug_msg);
1047 printed_len = strlen(recursion_bug_msg);
1048 }
1049 /* Emit the output into the temporary buffer */
1050 printed_len += vscnprintf(printk_buf + printed_len,
1051 sizeof(printk_buf) - printed_len, fmt, args);
1052
1053#ifdef CONFIG_DEBUG_LL
1054 printascii(printk_buf);
1055#endif
1056
1057 p = printk_buf;
1058
1059 /* Read log level and handle special printk prefix */
1060 plen = log_prefix(p, &current_log_level, &special);
1061 if (plen) {
1062 p += plen;
1063
1064 switch (special) {
1065 case 'c': /* Strip <c> KERN_CONT, continue line */
1066 plen = 0;
1067 break;
1068 case 'd': /* Strip <d> KERN_DEFAULT, start new line */
1069 plen = 0;
1070 default:
1071 if (!new_text_line) {
1072 emit_log_char('\n');
1073 new_text_line = 1;
1074 }
1075 }
1076 }
1077
1078 /*
1079 * Copy the output into log_buf. If the caller didn't provide
1080 * the appropriate log prefix, we insert them here
1081 */
1082 for (; *p; p++) {
1083 if (new_text_line) {
1084 new_text_line = 0;
1085
1086 if (plen) {
1087 /* Copy original log prefix */
1088 int i;
1089
1090 for (i = 0; i < plen; i++)
1091 emit_log_char(printk_buf[i]);
1092 printed_len += plen;
1093 } else {
1094 /* Add log prefix */
1095 emit_log_char('<');
1096 emit_log_char(current_log_level + '0');
1097 emit_log_char('>');
1098 printed_len += 3;
1099 }
1100
1101 if (printk_time) {
1102 /* Add the current time stamp */
1103 char tbuf[50], *tp;
1104 unsigned tlen;
1105 unsigned long long t;
1106 unsigned long nanosec_rem;
1107
1108 t = cpu_clock(printk_cpu);
1109 nanosec_rem = do_div(t, 1000000000);
1110 tlen = sprintf(tbuf, "[%5lu.%06lu] ",
1111 (unsigned long) t,
1112 nanosec_rem / 1000);
1113
1114 for (tp = tbuf; tp < tbuf + tlen; tp++)
1115 emit_log_char(*tp);
1116 printed_len += tlen;
1117 }
1118
1119 if (!*p)
1120 break;
1121 }
1122
1123 emit_log_char(*p);
1124 if (*p == '\n')
1125 new_text_line = 1;
1126 }
1127
1128 /*
1129 * Try to acquire and then immediately release the
1130 * console semaphore. The release will do all the
1131 * actual magic (print out buffers, wake up klogd,
1132 * etc).
1133 *
1134 * The console_trylock_for_printk() function
1135 * will release 'logbuf_lock' regardless of whether it
1136 * actually gets the semaphore or not.
1137 */
1138 if (console_trylock_for_printk(this_cpu, flags)) {
1139#ifndef CONFIG_PREEMPT_RT_FULL
1140 console_unlock();
1141#else
1142 raw_local_irq_restore(flags);
1143 console_unlock();
1144 raw_local_irq_save(flags);
1145#endif
1146 }
1147
1148 lockdep_on();
1149out_restore_irqs:
1150 local_irq_restore(flags);
1151
1152 return printed_len;
1153}
1154EXPORT_SYMBOL(printk);
1155EXPORT_SYMBOL(vprintk);
1156
1157#else
1158
1159static void call_console_drivers(unsigned start, unsigned end)
1160{
1161}
1162
1163#endif
1164
1165static int __add_preferred_console(char *name, int idx, char *options,
1166 char *brl_options)
1167{
1168 struct console_cmdline *c;
1169 int i;
1170
1171 /*
1172 * See if this tty is not yet registered, and
1173 * if we have a slot free.
1174 */
1175 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
1176 if (strcmp(console_cmdline[i].name, name) == 0 &&
1177 console_cmdline[i].index == idx) {
1178 if (!brl_options)
1179 selected_console = i;
1180 return 0;
1181 }
1182 if (i == MAX_CMDLINECONSOLES)
1183 return -E2BIG;
1184 if (!brl_options)
1185 selected_console = i;
1186 c = &console_cmdline[i];
1187 strlcpy(c->name, name, sizeof(c->name));
1188 c->options = options;
1189#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1190 c->brl_options = brl_options;
1191#endif
1192 c->index = idx;
1193 return 0;
1194}
1195/*
1196 * Set up a list of consoles. Called from init/main.c
1197 */
1198static int __init console_setup(char *str)
1199{
1200 char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for index */
1201 char *s, *options, *brl_options = NULL;
1202 int idx;
1203
1204#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1205 if (!memcmp(str, "brl,", 4)) {
1206 brl_options = "";
1207 str += 4;
1208 } else if (!memcmp(str, "brl=", 4)) {
1209 brl_options = str + 4;
1210 str = strchr(brl_options, ',');
1211 if (!str) {
1212 printk(KERN_ERR "need port name after brl=\n");
1213 return 1;
1214 }
1215 *(str++) = 0;
1216 }
1217#endif
1218
1219 /*
1220 * Decode str into name, index, options.
1221 */
1222 if (str[0] >= '0' && str[0] <= '9') {
1223 strcpy(buf, "ttyS");
1224 strncpy(buf + 4, str, sizeof(buf) - 5);
1225 } else {
1226 strncpy(buf, str, sizeof(buf) - 1);
1227 }
1228 buf[sizeof(buf) - 1] = 0;
1229 if ((options = strchr(str, ',')) != NULL)
1230 *(options++) = 0;
1231#ifdef __sparc__
1232 if (!strcmp(str, "ttya"))
1233 strcpy(buf, "ttyS0");
1234 if (!strcmp(str, "ttyb"))
1235 strcpy(buf, "ttyS1");
1236#endif
1237 for (s = buf; *s; s++)
1238 if ((*s >= '0' && *s <= '9') || *s == ',')
1239 break;
1240 idx = simple_strtoul(s, NULL, 10);
1241 *s = 0;
1242
1243 __add_preferred_console(buf, idx, options, brl_options);
1244 console_set_on_cmdline = 1;
1245 return 1;
1246}
1247__setup("console=", console_setup);
1248
1249/**
1250 * add_preferred_console - add a device to the list of preferred consoles.
1251 * @name: device name
1252 * @idx: device index
1253 * @options: options for this console
1254 *
1255 * The last preferred console added will be used for kernel messages
1256 * and stdin/out/err for init. Normally this is used by console_setup
1257 * above to handle user-supplied console arguments; however it can also
1258 * be used by arch-specific code either to override the user or more
1259 * commonly to provide a default console (ie from PROM variables) when
1260 * the user has not supplied one.
1261 */
1262int add_preferred_console(char *name, int idx, char *options)
1263{
1264 return __add_preferred_console(name, idx, options, NULL);
1265}
1266
1267int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options)
1268{
1269 struct console_cmdline *c;
1270 int i;
1271
1272 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
1273 if (strcmp(console_cmdline[i].name, name) == 0 &&
1274 console_cmdline[i].index == idx) {
1275 c = &console_cmdline[i];
1276 strlcpy(c->name, name_new, sizeof(c->name));
1277 c->name[sizeof(c->name) - 1] = 0;
1278 c->options = options;
1279 c->index = idx_new;
1280 return i;
1281 }
1282 /* not found */
1283 return -1;
1284}
1285
1286bool console_suspend_enabled = 1;
1287EXPORT_SYMBOL(console_suspend_enabled);
1288
1289static int __init console_suspend_disable(char *str)
1290{
1291 console_suspend_enabled = 0;
1292 return 1;
1293}
1294__setup("no_console_suspend", console_suspend_disable);
1295module_param_named(console_suspend, console_suspend_enabled,
1296 bool, S_IRUGO | S_IWUSR);
1297MODULE_PARM_DESC(console_suspend, "suspend console during suspend"
1298 " and hibernate operations");
1299
1300/**
1301 * suspend_console - suspend the console subsystem
1302 *
1303 * This disables printk() while we go into suspend states
1304 */
1305void suspend_console(void)
1306{
1307 if (!console_suspend_enabled)
1308 return;
1309 printk("Suspending console(s) (use no_console_suspend to debug)\n");
1310 console_lock();
1311 console_suspended = 1;
1312 up(&console_sem);
1313}
1314
1315void resume_console(void)
1316{
1317 if (!console_suspend_enabled)
1318 return;
1319 down(&console_sem);
1320 console_suspended = 0;
1321 console_unlock();
1322}
1323
1324/**
1325 * console_cpu_notify - print deferred console messages after CPU hotplug
1326 * @self: notifier struct
1327 * @action: CPU hotplug event
1328 * @hcpu: unused
1329 *
1330 * If printk() is called from a CPU that is not online yet, the messages
1331 * will be spooled but will not show up on the console. This function is
1332 * called when a new CPU comes online (or fails to come up), and ensures
1333 * that any such output gets printed.
1334 */
1335static int __cpuinit console_cpu_notify(struct notifier_block *self,
1336 unsigned long action, void *hcpu)
1337{
1338 switch (action) {
1339 case CPU_ONLINE:
1340 case CPU_DEAD:
1341 case CPU_DOWN_FAILED:
1342 case CPU_UP_CANCELED:
1343 console_lock();
1344 console_unlock();
1345 }
1346 return NOTIFY_OK;
1347}
1348
1349/**
1350 * console_lock - lock the console system for exclusive use.
1351 *
1352 * Acquires a lock which guarantees that the caller has
1353 * exclusive access to the console system and the console_drivers list.
1354 *
1355 * Can sleep, returns nothing.
1356 */
1357void console_lock(void)
1358{
1359 BUG_ON(in_interrupt());
1360 down(&console_sem);
1361 if (console_suspended)
1362 return;
1363 console_locked = 1;
1364 console_may_schedule = 1;
1365}
1366EXPORT_SYMBOL(console_lock);
1367
1368/**
1369 * console_trylock - try to lock the console system for exclusive use.
1370 *
1371 * Tried to acquire a lock which guarantees that the caller has
1372 * exclusive access to the console system and the console_drivers list.
1373 *
1374 * returns 1 on success, and 0 on failure to acquire the lock.
1375 */
1376int console_trylock(void)
1377{
1378 if (down_trylock(&console_sem))
1379 return 0;
1380 if (console_suspended) {
1381 up(&console_sem);
1382 return 0;
1383 }
1384 console_locked = 1;
1385 console_may_schedule = 0;
1386 return 1;
1387}
1388EXPORT_SYMBOL(console_trylock);
1389
1390int is_console_locked(void)
1391{
1392 return console_locked;
1393}
1394
1395/*
1396 * Delayed printk facility, for scheduler-internal messages:
1397 */
1398#define PRINTK_BUF_SIZE 512
1399
1400#define PRINTK_PENDING_WAKEUP 0x01
1401#define PRINTK_PENDING_SCHED 0x02
1402
1403static DEFINE_PER_CPU(int, printk_pending);
1404static DEFINE_PER_CPU(char [PRINTK_BUF_SIZE], printk_sched_buf);
1405
1406void printk_tick(void)
1407{
1408 if (__this_cpu_read(printk_pending)) {
1409 int pending = __this_cpu_xchg(printk_pending, 0);
1410 if (pending & PRINTK_PENDING_SCHED) {
1411 char *buf = __get_cpu_var(printk_sched_buf);
1412 printk(KERN_WARNING "[sched_delayed] %s", buf);
1413 }
1414 if (pending & PRINTK_PENDING_WAKEUP)
1415 wake_up_interruptible(&log_wait);
1416 }
1417}
1418
1419int printk_needs_cpu(int cpu)
1420{
1421 if (unlikely(cpu_is_offline(cpu)))
1422 __this_cpu_write(printk_pending, 0);
1423 return __this_cpu_read(printk_pending);
1424}
1425
1426void wake_up_klogd(void)
1427{
1428 if (waitqueue_active(&log_wait))
1429 this_cpu_or(printk_pending, PRINTK_PENDING_WAKEUP);
1430}
1431
1432/**
1433 * console_unlock - unlock the console system
1434 *
1435 * Releases the console_lock which the caller holds on the console system
1436 * and the console driver list.
1437 *
1438 * While the console_lock was held, console output may have been buffered
1439 * by printk(). If this is the case, console_unlock(); emits
1440 * the output prior to releasing the lock.
1441 *
1442 * If there is output waiting for klogd, we wake it up.
1443 *
1444 * console_unlock(); may be called from any context.
1445 */
1446void console_unlock(void)
1447{
1448 unsigned long flags;
1449 unsigned _con_start, _log_end;
1450 unsigned wake_klogd = 0, retry = 0;
1451
1452 if (console_suspended) {
1453 up(&console_sem);
1454 return;
1455 }
1456
1457 console_may_schedule = 0;
1458
1459again:
1460 for ( ; ; ) {
1461 raw_spin_lock_irqsave(&logbuf_lock, flags);
1462 wake_klogd |= log_start - log_end;
1463 if (con_start == log_end)
1464 break; /* Nothing to print */
1465 _con_start = con_start;
1466 _log_end = log_end;
1467 con_start = log_end; /* Flush */
1468#ifndef CONFIG_PREEMPT_RT_FULL
1469 raw_spin_unlock(&logbuf_lock);
1470 stop_critical_timings(); /* don't trace print latency */
1471 call_console_drivers(_con_start, _log_end);
1472 start_critical_timings();
1473 local_irq_restore(flags);
1474#else
1475 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
1476 call_console_drivers(_con_start, _log_end);
1477#endif
1478 }
1479 console_locked = 0;
1480
1481 /* Release the exclusive_console once it is used */
1482 if (unlikely(exclusive_console))
1483 exclusive_console = NULL;
1484
1485 raw_spin_unlock(&logbuf_lock);
1486
1487 up(&console_sem);
1488
1489 /*
1490 * Someone could have filled up the buffer again, so re-check if there's
1491 * something to flush. In case we cannot trylock the console_sem again,
1492 * there's a new owner and the console_unlock() from them will do the
1493 * flush, no worries.
1494 */
1495 raw_spin_lock(&logbuf_lock);
1496 if (con_start != log_end)
1497 retry = 1;
1498 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
1499
1500 if (retry && console_trylock())
1501 goto again;
1502
1503 if (wake_klogd)
1504 wake_up_klogd();
1505}
1506EXPORT_SYMBOL(console_unlock);
1507
1508/**
1509 * console_conditional_schedule - yield the CPU if required
1510 *
1511 * If the console code is currently allowed to sleep, and
1512 * if this CPU should yield the CPU to another task, do
1513 * so here.
1514 *
1515 * Must be called within console_lock();.
1516 */
1517void __sched console_conditional_schedule(void)
1518{
1519 if (console_may_schedule)
1520 cond_resched();
1521}
1522EXPORT_SYMBOL(console_conditional_schedule);
1523
1524void console_unblank(void)
1525{
1526 struct console *c;
1527
1528 /*
1529 * console_unblank can no longer be called in interrupt context unless
1530 * oops_in_progress is set to 1..
1531 */
1532 if (oops_in_progress) {
1533 if (down_trylock(&console_sem) != 0)
1534 return;
1535 } else
1536 console_lock();
1537
1538 console_locked = 1;
1539 console_may_schedule = 0;
1540 for_each_console(c)
1541 if ((c->flags & CON_ENABLED) && c->unblank)
1542 c->unblank();
1543 console_unlock();
1544}
1545
1546/*
1547 * Return the console tty driver structure and its associated index
1548 */
1549struct tty_driver *console_device(int *index)
1550{
1551 struct console *c;
1552 struct tty_driver *driver = NULL;
1553
1554 console_lock();
1555 for_each_console(c) {
1556 if (!c->device)
1557 continue;
1558 driver = c->device(c, index);
1559 if (driver)
1560 break;
1561 }
1562 console_unlock();
1563 return driver;
1564}
1565
1566/*
1567 * Prevent further output on the passed console device so that (for example)
1568 * serial drivers can disable console output before suspending a port, and can
1569 * re-enable output afterwards.
1570 */
1571void console_stop(struct console *console)
1572{
1573 console_lock();
1574 console->flags &= ~CON_ENABLED;
1575 console_unlock();
1576}
1577EXPORT_SYMBOL(console_stop);
1578
1579void console_start(struct console *console)
1580{
1581 console_lock();
1582 console->flags |= CON_ENABLED;
1583 console_unlock();
1584}
1585EXPORT_SYMBOL(console_start);
1586
1587static int __read_mostly keep_bootcon;
1588
1589static int __init keep_bootcon_setup(char *str)
1590{
1591 keep_bootcon = 1;
1592 printk(KERN_INFO "debug: skip boot console de-registration.\n");
1593
1594 return 0;
1595}
1596
1597early_param("keep_bootcon", keep_bootcon_setup);
1598
1599/*
1600 * The console driver calls this routine during kernel initialization
1601 * to register the console printing procedure with printk() and to
1602 * print any messages that were printed by the kernel before the
1603 * console driver was initialized.
1604 *
1605 * This can happen pretty early during the boot process (because of
1606 * early_printk) - sometimes before setup_arch() completes - be careful
1607 * of what kernel features are used - they may not be initialised yet.
1608 *
1609 * There are two types of consoles - bootconsoles (early_printk) and
1610 * "real" consoles (everything which is not a bootconsole) which are
1611 * handled differently.
1612 * - Any number of bootconsoles can be registered at any time.
1613 * - As soon as a "real" console is registered, all bootconsoles
1614 * will be unregistered automatically.
1615 * - Once a "real" console is registered, any attempt to register a
1616 * bootconsoles will be rejected
1617 */
1618void register_console(struct console *newcon)
1619{
1620 int i;
1621 unsigned long flags;
1622 struct console *bcon = NULL;
1623
1624 /*
1625 * before we register a new CON_BOOT console, make sure we don't
1626 * already have a valid console
1627 */
1628 if (console_drivers && newcon->flags & CON_BOOT) {
1629 /* find the last or real console */
1630 for_each_console(bcon) {
1631 if (!(bcon->flags & CON_BOOT)) {
1632 printk(KERN_INFO "Too late to register bootconsole %s%d\n",
1633 newcon->name, newcon->index);
1634 return;
1635 }
1636 }
1637 }
1638
1639 if (console_drivers && console_drivers->flags & CON_BOOT)
1640 bcon = console_drivers;
1641
1642 if (preferred_console < 0 || bcon || !console_drivers)
1643 preferred_console = selected_console;
1644
1645 if (newcon->early_setup)
1646 newcon->early_setup();
1647
1648 /*
1649 * See if we want to use this console driver. If we
1650 * didn't select a console we take the first one
1651 * that registers here.
1652 */
1653 if (preferred_console < 0) {
1654 if (newcon->index < 0)
1655 newcon->index = 0;
1656 if (newcon->setup == NULL ||
1657 newcon->setup(newcon, NULL) == 0) {
1658 newcon->flags |= CON_ENABLED;
1659 if (newcon->device) {
1660 newcon->flags |= CON_CONSDEV;
1661 preferred_console = 0;
1662 }
1663 }
1664 }
1665
1666 /*
1667 * See if this console matches one we selected on
1668 * the command line.
1669 */
1670 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0];
1671 i++) {
1672 BUILD_BUG_ON(sizeof(console_cmdline[i].name) != sizeof(newcon->name));
1673 if (strcmp(console_cmdline[i].name, newcon->name) != 0)
1674 continue;
1675 if (newcon->index >= 0 &&
1676 newcon->index != console_cmdline[i].index)
1677 continue;
1678 if (newcon->index < 0)
1679 newcon->index = console_cmdline[i].index;
1680#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1681 if (console_cmdline[i].brl_options) {
1682 newcon->flags |= CON_BRL;
1683 braille_register_console(newcon,
1684 console_cmdline[i].index,
1685 console_cmdline[i].options,
1686 console_cmdline[i].brl_options);
1687 return;
1688 }
1689#endif
1690 if (newcon->setup &&
1691 newcon->setup(newcon, console_cmdline[i].options) != 0)
1692 break;
1693 newcon->flags |= CON_ENABLED;
1694 newcon->index = console_cmdline[i].index;
1695 if (i == selected_console) {
1696 newcon->flags |= CON_CONSDEV;
1697 preferred_console = selected_console;
1698 }
1699 break;
1700 }
1701
1702 if (!(newcon->flags & CON_ENABLED))
1703 return;
1704
1705 /*
1706 * If we have a bootconsole, and are switching to a real console,
1707 * don't print everything out again, since when the boot console, and
1708 * the real console are the same physical device, it's annoying to
1709 * see the beginning boot messages twice
1710 */
1711 if (bcon && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV))
1712 newcon->flags &= ~CON_PRINTBUFFER;
1713
1714 /*
1715 * Put this console in the list - keep the
1716 * preferred driver at the head of the list.
1717 */
1718 console_lock();
1719 if ((newcon->flags & CON_CONSDEV) || console_drivers == NULL) {
1720 newcon->next = console_drivers;
1721 console_drivers = newcon;
1722 if (newcon->next)
1723 newcon->next->flags &= ~CON_CONSDEV;
1724 } else {
1725 newcon->next = console_drivers->next;
1726 console_drivers->next = newcon;
1727 }
1728 if (newcon->flags & CON_PRINTBUFFER) {
1729 /*
1730 * console_unlock(); will print out the buffered messages
1731 * for us.
1732 */
1733 raw_spin_lock_irqsave(&logbuf_lock, flags);
1734 con_start = log_start;
1735 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
1736 /*
1737 * We're about to replay the log buffer. Only do this to the
1738 * just-registered console to avoid excessive message spam to
1739 * the already-registered consoles.
1740 */
1741 exclusive_console = newcon;
1742 }
1743 console_unlock();
1744 console_sysfs_notify();
1745
1746 /*
1747 * By unregistering the bootconsoles after we enable the real console
1748 * we get the "console xxx enabled" message on all the consoles -
1749 * boot consoles, real consoles, etc - this is to ensure that end
1750 * users know there might be something in the kernel's log buffer that
1751 * went to the bootconsole (that they do not see on the real console)
1752 */
1753 if (bcon &&
1754 ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV) &&
1755 !keep_bootcon) {
1756 /* we need to iterate through twice, to make sure we print
1757 * everything out, before we unregister the console(s)
1758 */
1759 printk(KERN_INFO "console [%s%d] enabled, bootconsole disabled\n",
1760 newcon->name, newcon->index);
1761 for_each_console(bcon)
1762 if (bcon->flags & CON_BOOT)
1763 unregister_console(bcon);
1764 } else {
1765 printk(KERN_INFO "%sconsole [%s%d] enabled\n",
1766 (newcon->flags & CON_BOOT) ? "boot" : "" ,
1767 newcon->name, newcon->index);
1768 }
1769}
1770EXPORT_SYMBOL(register_console);
1771
1772int unregister_console(struct console *console)
1773{
1774 struct console *a, *b;
1775 int res = 1;
1776
1777#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1778 if (console->flags & CON_BRL)
1779 return braille_unregister_console(console);
1780#endif
1781
1782 console_lock();
1783 if (console_drivers == console) {
1784 console_drivers=console->next;
1785 res = 0;
1786 } else if (console_drivers) {
1787 for (a=console_drivers->next, b=console_drivers ;
1788 a; b=a, a=b->next) {
1789 if (a == console) {
1790 b->next = a->next;
1791 res = 0;
1792 break;
1793 }
1794 }
1795 }
1796
1797 /*
1798 * If this isn't the last console and it has CON_CONSDEV set, we
1799 * need to set it on the next preferred console.
1800 */
1801 if (console_drivers != NULL && console->flags & CON_CONSDEV)
1802 console_drivers->flags |= CON_CONSDEV;
1803
1804 console_unlock();
1805 console_sysfs_notify();
1806 return res;
1807}
1808EXPORT_SYMBOL(unregister_console);
1809
1810static int __init printk_late_init(void)
1811{
1812 struct console *con;
1813
1814 for_each_console(con) {
1815 if (!keep_bootcon && con->flags & CON_BOOT) {
1816 printk(KERN_INFO "turn off boot console %s%d\n",
1817 con->name, con->index);
1818 unregister_console(con);
1819 }
1820 }
1821 hotcpu_notifier(console_cpu_notify, 0);
1822 return 0;
1823}
1824late_initcall(printk_late_init);
1825
1826#if defined CONFIG_PRINTK
1827
1828int printk_deferred(const char *fmt, ...)
1829{
1830 unsigned long flags;
1831 va_list args;
1832 char *buf;
1833 int r;
1834
1835 local_irq_save(flags);
1836 buf = __get_cpu_var(printk_sched_buf);
1837
1838 va_start(args, fmt);
1839 r = vsnprintf(buf, PRINTK_BUF_SIZE, fmt, args);
1840 va_end(args);
1841
1842 __this_cpu_or(printk_pending, PRINTK_PENDING_SCHED);
1843 local_irq_restore(flags);
1844
1845 return r;
1846}
1847
1848/*
1849 * printk rate limiting, lifted from the networking subsystem.
1850 *
1851 * This enforces a rate limit: not more than 10 kernel messages
1852 * every 5s to make a denial-of-service attack impossible.
1853 */
1854DEFINE_RATELIMIT_STATE(printk_ratelimit_state, 5 * HZ, 10);
1855
1856int __printk_ratelimit(const char *func)
1857{
1858 return ___ratelimit(&printk_ratelimit_state, func);
1859}
1860EXPORT_SYMBOL(__printk_ratelimit);
1861
1862/**
1863 * printk_timed_ratelimit - caller-controlled printk ratelimiting
1864 * @caller_jiffies: pointer to caller's state
1865 * @interval_msecs: minimum interval between prints
1866 *
1867 * printk_timed_ratelimit() returns true if more than @interval_msecs
1868 * milliseconds have elapsed since the last time printk_timed_ratelimit()
1869 * returned true.
1870 */
1871bool printk_timed_ratelimit(unsigned long *caller_jiffies,
1872 unsigned int interval_msecs)
1873{
1874 if (*caller_jiffies == 0
1875 || !time_in_range(jiffies, *caller_jiffies,
1876 *caller_jiffies
1877 + msecs_to_jiffies(interval_msecs))) {
1878 *caller_jiffies = jiffies;
1879 return true;
1880 }
1881 return false;
1882}
1883EXPORT_SYMBOL(printk_timed_ratelimit);
1884
1885static DEFINE_SPINLOCK(dump_list_lock);
1886static LIST_HEAD(dump_list);
1887
1888/**
1889 * kmsg_dump_register - register a kernel log dumper.
1890 * @dumper: pointer to the kmsg_dumper structure
1891 *
1892 * Adds a kernel log dumper to the system. The dump callback in the
1893 * structure will be called when the kernel oopses or panics and must be
1894 * set. Returns zero on success and %-EINVAL or %-EBUSY otherwise.
1895 */
1896int kmsg_dump_register(struct kmsg_dumper *dumper)
1897{
1898 unsigned long flags;
1899 int err = -EBUSY;
1900
1901 /* The dump callback needs to be set */
1902 if (!dumper->dump)
1903 return -EINVAL;
1904
1905 spin_lock_irqsave(&dump_list_lock, flags);
1906 /* Don't allow registering multiple times */
1907 if (!dumper->registered) {
1908 dumper->registered = 1;
1909 list_add_tail_rcu(&dumper->list, &dump_list);
1910 err = 0;
1911 }
1912 spin_unlock_irqrestore(&dump_list_lock, flags);
1913
1914 return err;
1915}
1916EXPORT_SYMBOL_GPL(kmsg_dump_register);
1917
1918/**
1919 * kmsg_dump_unregister - unregister a kmsg dumper.
1920 * @dumper: pointer to the kmsg_dumper structure
1921 *
1922 * Removes a dump device from the system. Returns zero on success and
1923 * %-EINVAL otherwise.
1924 */
1925int kmsg_dump_unregister(struct kmsg_dumper *dumper)
1926{
1927 unsigned long flags;
1928 int err = -EINVAL;
1929
1930 spin_lock_irqsave(&dump_list_lock, flags);
1931 if (dumper->registered) {
1932 dumper->registered = 0;
1933 list_del_rcu(&dumper->list);
1934 err = 0;
1935 }
1936 spin_unlock_irqrestore(&dump_list_lock, flags);
1937 synchronize_rcu();
1938
1939 return err;
1940}
1941EXPORT_SYMBOL_GPL(kmsg_dump_unregister);
1942
1943/**
1944 * kmsg_dump - dump kernel log to kernel message dumpers.
1945 * @reason: the reason (oops, panic etc) for dumping
1946 *
1947 * Iterate through each of the dump devices and call the oops/panic
1948 * callbacks with the log buffer.
1949 */
1950void kmsg_dump(enum kmsg_dump_reason reason)
1951{
1952 unsigned long end;
1953 unsigned chars;
1954 struct kmsg_dumper *dumper;
1955 const char *s1, *s2;
1956 unsigned long l1, l2;
1957 unsigned long flags;
1958
1959 if ((reason > KMSG_DUMP_OOPS) && !always_kmsg_dump)
1960 return;
1961
1962 /* Theoretically, the log could move on after we do this, but
1963 there's not a lot we can do about that. The new messages
1964 will overwrite the start of what we dump. */
1965 raw_spin_lock_irqsave(&logbuf_lock, flags);
1966 end = log_end & LOG_BUF_MASK;
1967 chars = logged_chars;
1968 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
1969
1970 if (chars > end) {
1971 s1 = log_buf + log_buf_len - chars + end;
1972 l1 = chars - end;
1973
1974 s2 = log_buf;
1975 l2 = end;
1976 } else {
1977 s1 = "";
1978 l1 = 0;
1979
1980 s2 = log_buf + end - chars;
1981 l2 = chars;
1982 }
1983
1984 rcu_read_lock();
1985 list_for_each_entry_rcu(dumper, &dump_list, list)
1986 dumper->dump(dumper, reason, s1, l1, s2, l2);
1987 rcu_read_unlock();
1988}
1989
1990void get_logbuf_info(unsigned long *addr, unsigned long *size)
1991{
1992 *addr = __log_buf;
1993 *size = __LOG_BUF_LEN;
1994}
1995EXPORT_SYMBOL(get_logbuf_info);
1996
1997#endif