blob: 0723cde7dfb37397fa4a5091957d8a462f688f4f [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -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
862#define CORE_PREFIX_STR "[AP-3.4] "
863#define CORE_PREFIX_LEN strlen(CORE_PREFIX_STR)
864
865static bool always_kmsg_dump;
866module_param_named(always_kmsg_dump, always_kmsg_dump, bool, S_IRUGO | S_IWUSR);
867
868/* Check if we have any console registered that can be called early in boot. */
869static int have_callable_console(void)
870{
871 struct console *con;
872
873 for_each_console(con)
874 if (con->flags & CON_ANYTIME)
875 return 1;
876
877 return 0;
878}
879
880/**
881 * printk - print a kernel message
882 * @fmt: format string
883 *
884 * This is printk(). It can be called from any context. We want it to work.
885 *
886 * We try to grab the console_lock. If we succeed, it's easy - we log the output and
887 * call the console drivers. If we fail to get the semaphore we place the output
888 * into the log buffer and return. The current holder of the console_sem will
889 * notice the new output in console_unlock(); and will send it to the
890 * consoles before releasing the lock.
891 *
892 * One effect of this deferred printing is that code which calls printk() and
893 * then changes console_loglevel may break. This is because console_loglevel
894 * is inspected when the actual printing occurs.
895 *
896 * See also:
897 * printf(3)
898 *
899 * See the vsnprintf() documentation for format string extensions over C99.
900 */
901
902asmlinkage int printk(const char *fmt, ...)
903{
904 va_list args;
905 int r;
906
907#ifdef CONFIG_KGDB_KDB
908 if (unlikely(kdb_trap_printk)) {
909 va_start(args, fmt);
910 r = vkdb_printf(fmt, args);
911 va_end(args);
912 return r;
913 }
914#endif
915 va_start(args, fmt);
916 r = vprintk(fmt, args);
917 va_end(args);
918
919 return r;
920}
921
922/* cpu currently holding logbuf_lock */
923static volatile unsigned int printk_cpu = UINT_MAX;
924
925/*
926 * Can we actually use the console at this time on this cpu?
927 *
928 * Console drivers may assume that per-cpu resources have
929 * been allocated. So unless they're explicitly marked as
930 * being able to cope (CON_ANYTIME) don't call them until
931 * this CPU is officially up.
932 */
933static inline int can_use_console(unsigned int cpu)
934{
935 return cpu_online(cpu) || have_callable_console();
936}
937
938/*
939 * Try to get console ownership to actually show the kernel
940 * messages from a 'printk'. Return true (and with the
941 * console_lock held, and 'console_locked' set) if it
942 * is successful, false otherwise.
943 *
944 * This gets called with the 'logbuf_lock' spinlock held and
945 * interrupts disabled. It should return with 'lockbuf_lock'
946 * released but interrupts still disabled.
947 */
948static int console_trylock_for_printk(unsigned int cpu, unsigned long flags)
949 __releases(&logbuf_lock)
950{
951 int retval = 0, wake = 0;
952#ifdef CONFIG_PREEMPT_RT_FULL
953 /*Ïß³ÌÓÅÏȼ¶¸ßÓÚЭÒéÕ»ÖØÒªÏ̵߳ģ¨ÓëÅíÀÚ¼°¹úÆÂÈ·ÈÏ£¬Ôݶ¨62ÓÅÏȼ¶£©£¬²»ÔÙʵʱ´òÓ¡£¬EC 616000549169*/
954 int lock = !early_boot_irqs_disabled && !irqs_disabled_flags(flags) &&
955 (preempt_count() <= 1) && (current->prio > 62);
956#else
957 int lock = 1;
958#endif
959
960 if (lock && console_trylock()) {
961 retval = 1;
962
963 /*
964 * If we can't use the console, we need to release
965 * the console semaphore by hand to avoid flushing
966 * the buffer. We need to hold the console semaphore
967 * in order to do this test safely.
968 */
969 if (!can_use_console(cpu)) {
970 console_locked = 0;
971 wake = 1;
972 retval = 0;
973 }
974 }
975 printk_cpu = UINT_MAX;
976 raw_spin_unlock(&logbuf_lock);
977 if (wake)
978 up(&console_sem);
979 return retval;
980}
981static const char recursion_bug_msg [] =
982 KERN_CRIT "BUG: recent printk recursion!\n";
983static int recursion_bug;
984static int new_text_line = 1;
985static char printk_buf[1024];
986
987int printk_delay_msec __read_mostly;
988
989static inline void printk_delay(void)
990{
991 if (unlikely(printk_delay_msec)) {
992 int m = printk_delay_msec;
993
994 while (m--) {
995 mdelay(1);
996 touch_nmi_watchdog();
997 }
998 }
999}
1000
1001asmlinkage int vprintk(const char *fmt, va_list args)
1002{
1003 int printed_len = 0;
1004 int current_log_level = default_message_loglevel;
1005 unsigned long flags;
1006 int this_cpu;
1007 char *p;
1008 size_t plen;
1009 char special;
1010
1011 /*
1012 * Fall back to early_printk if a debugging subsystem has
1013 * killed printk output
1014 */
1015 if (unlikely(forced_early_printk(fmt, args)))
1016 return 1;
1017
1018 boot_delay_msec();
1019 printk_delay();
1020
1021 /* This stops the holder of console_sem just where we want him */
1022 local_irq_save(flags);
1023 this_cpu = smp_processor_id();
1024
1025 /*
1026 * Ouch, printk recursed into itself!
1027 */
1028 if (unlikely(printk_cpu == this_cpu)) {
1029 /*
1030 * If a crash is occurring during printk() on this CPU,
1031 * then try to get the crash message out but make sure
1032 * we can't deadlock. Otherwise just return to avoid the
1033 * recursion and return - but flag the recursion so that
1034 * it can be printed at the next appropriate moment:
1035 */
1036 if (!oops_in_progress && !lockdep_recursing(current)) {
1037 recursion_bug = 1;
1038 goto out_restore_irqs;
1039 }
1040 zap_locks();
1041 }
1042
1043 lockdep_off();
1044 raw_spin_lock(&logbuf_lock);
1045 printk_cpu = this_cpu;
1046
1047 if (recursion_bug) {
1048 recursion_bug = 0;
1049 strcpy(printk_buf, recursion_bug_msg);
1050 printed_len = strlen(recursion_bug_msg);
1051 }
1052 /* Emit the output into the temporary buffer */
1053 printed_len += vscnprintf(printk_buf + printed_len,
1054 sizeof(printk_buf) - printed_len, fmt, args);
1055
1056#ifdef CONFIG_DEBUG_LL
1057 printascii(printk_buf);
1058#endif
1059
1060 p = printk_buf;
1061
1062 /* Read log level and handle special printk prefix */
1063 plen = log_prefix(p, &current_log_level, &special);
1064 if (plen) {
1065 p += plen;
1066
1067 switch (special) {
1068 case 'c': /* Strip <c> KERN_CONT, continue line */
1069 plen = 0;
1070 break;
1071 case 'd': /* Strip <d> KERN_DEFAULT, start new line */
1072 plen = 0;
1073 default:
1074 if (!new_text_line) {
1075 emit_log_char('\n');
1076 new_text_line = 1;
1077 }
1078 }
1079 }
1080
1081 /*
1082 * Copy the output into log_buf. If the caller didn't provide
1083 * the appropriate log prefix, we insert them here
1084 */
1085 for (; *p; p++) {
1086 if (new_text_line) {
1087 new_text_line = 0;
1088
1089 if (plen) {
1090 /* Copy original log prefix */
1091 int i;
1092
1093 for (i = 0; i < plen; i++)
1094 emit_log_char(printk_buf[i]);
1095 printed_len += plen;
1096 } else {
1097 /* Add log prefix */
1098 emit_log_char('<');
1099 emit_log_char(current_log_level + '0');
1100 emit_log_char('>');
1101 printed_len += 3;
1102 }
1103
1104#ifdef _USE_VEHICLE_DC
1105 /* Add the current core type */
1106 char cbuf[] = CORE_PREFIX_STR, *cp;
1107 unsigned clen = CORE_PREFIX_LEN;
1108
1109 for (cp = cbuf; cp < cbuf + clen; cp++)
1110 emit_log_char(*cp);
1111 printed_len += clen;
1112#endif
1113
1114 if (printk_time) {
1115 /* Add the current time stamp */
1116 char tbuf[50], *tp;
1117 unsigned tlen;
1118 unsigned long long t;
1119 unsigned long nanosec_rem;
1120
1121 t = cpu_clock(printk_cpu);
1122 nanosec_rem = do_div(t, 1000000000);
1123 tlen = sprintf(tbuf, "[%5lu.%06lu] ",
1124 (unsigned long) t,
1125 nanosec_rem / 1000);
1126
1127 for (tp = tbuf; tp < tbuf + tlen; tp++)
1128 emit_log_char(*tp);
1129 printed_len += tlen;
1130 }
1131
1132 if (!*p)
1133 break;
1134 }
1135
1136 emit_log_char(*p);
1137 if (*p == '\n')
1138 new_text_line = 1;
1139 }
1140
1141 /*
1142 * Try to acquire and then immediately release the
1143 * console semaphore. The release will do all the
1144 * actual magic (print out buffers, wake up klogd,
1145 * etc).
1146 *
1147 * The console_trylock_for_printk() function
1148 * will release 'logbuf_lock' regardless of whether it
1149 * actually gets the semaphore or not.
1150 */
1151 if (console_trylock_for_printk(this_cpu, flags)) {
1152#ifndef CONFIG_PREEMPT_RT_FULL
1153 console_unlock();
1154#else
1155 raw_local_irq_restore(flags);
1156 console_unlock();
1157 raw_local_irq_save(flags);
1158#endif
1159 }
1160
1161 lockdep_on();
1162out_restore_irqs:
1163 local_irq_restore(flags);
1164
1165 return printed_len;
1166}
1167EXPORT_SYMBOL(printk);
1168EXPORT_SYMBOL(vprintk);
1169
1170#else
1171
1172static void call_console_drivers(unsigned start, unsigned end)
1173{
1174}
1175
1176#endif
1177
1178static int __add_preferred_console(char *name, int idx, char *options,
1179 char *brl_options)
1180{
1181 struct console_cmdline *c;
1182 int i;
1183
1184 /*
1185 * See if this tty is not yet registered, and
1186 * if we have a slot free.
1187 */
1188 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
1189 if (strcmp(console_cmdline[i].name, name) == 0 &&
1190 console_cmdline[i].index == idx) {
1191 if (!brl_options)
1192 selected_console = i;
1193 return 0;
1194 }
1195 if (i == MAX_CMDLINECONSOLES)
1196 return -E2BIG;
1197 if (!brl_options)
1198 selected_console = i;
1199 c = &console_cmdline[i];
1200 strlcpy(c->name, name, sizeof(c->name));
1201 c->options = options;
1202#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1203 c->brl_options = brl_options;
1204#endif
1205 c->index = idx;
1206 return 0;
1207}
1208/*
1209 * Set up a list of consoles. Called from init/main.c
1210 */
1211static int __init console_setup(char *str)
1212{
1213 char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for index */
1214 char *s, *options, *brl_options = NULL;
1215 int idx;
1216
1217#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1218 if (!memcmp(str, "brl,", 4)) {
1219 brl_options = "";
1220 str += 4;
1221 } else if (!memcmp(str, "brl=", 4)) {
1222 brl_options = str + 4;
1223 str = strchr(brl_options, ',');
1224 if (!str) {
1225 printk(KERN_ERR "need port name after brl=\n");
1226 return 1;
1227 }
1228 *(str++) = 0;
1229 }
1230#endif
1231
1232 /*
1233 * Decode str into name, index, options.
1234 */
1235 if (str[0] >= '0' && str[0] <= '9') {
1236 strcpy(buf, "ttyS");
1237 strncpy(buf + 4, str, sizeof(buf) - 5);
1238 } else {
1239 strncpy(buf, str, sizeof(buf) - 1);
1240 }
1241 buf[sizeof(buf) - 1] = 0;
1242 if ((options = strchr(str, ',')) != NULL)
1243 *(options++) = 0;
1244#ifdef __sparc__
1245 if (!strcmp(str, "ttya"))
1246 strcpy(buf, "ttyS0");
1247 if (!strcmp(str, "ttyb"))
1248 strcpy(buf, "ttyS1");
1249#endif
1250 for (s = buf; *s; s++)
1251 if ((*s >= '0' && *s <= '9') || *s == ',')
1252 break;
1253 idx = simple_strtoul(s, NULL, 10);
1254 *s = 0;
1255
1256 __add_preferred_console(buf, idx, options, brl_options);
1257 console_set_on_cmdline = 1;
1258 return 1;
1259}
1260__setup("console=", console_setup);
1261
1262/**
1263 * add_preferred_console - add a device to the list of preferred consoles.
1264 * @name: device name
1265 * @idx: device index
1266 * @options: options for this console
1267 *
1268 * The last preferred console added will be used for kernel messages
1269 * and stdin/out/err for init. Normally this is used by console_setup
1270 * above to handle user-supplied console arguments; however it can also
1271 * be used by arch-specific code either to override the user or more
1272 * commonly to provide a default console (ie from PROM variables) when
1273 * the user has not supplied one.
1274 */
1275int add_preferred_console(char *name, int idx, char *options)
1276{
1277 return __add_preferred_console(name, idx, options, NULL);
1278}
1279
1280int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options)
1281{
1282 struct console_cmdline *c;
1283 int i;
1284
1285 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
1286 if (strcmp(console_cmdline[i].name, name) == 0 &&
1287 console_cmdline[i].index == idx) {
1288 c = &console_cmdline[i];
1289 strlcpy(c->name, name_new, sizeof(c->name));
1290 c->name[sizeof(c->name) - 1] = 0;
1291 c->options = options;
1292 c->index = idx_new;
1293 return i;
1294 }
1295 /* not found */
1296 return -1;
1297}
1298
1299bool console_suspend_enabled = 1;
1300EXPORT_SYMBOL(console_suspend_enabled);
1301
1302static int __init console_suspend_disable(char *str)
1303{
1304 console_suspend_enabled = 0;
1305 return 1;
1306}
1307__setup("no_console_suspend", console_suspend_disable);
1308module_param_named(console_suspend, console_suspend_enabled,
1309 bool, S_IRUGO | S_IWUSR);
1310MODULE_PARM_DESC(console_suspend, "suspend console during suspend"
1311 " and hibernate operations");
1312
1313/**
1314 * suspend_console - suspend the console subsystem
1315 *
1316 * This disables printk() while we go into suspend states
1317 */
1318void suspend_console(void)
1319{
1320 if (!console_suspend_enabled)
1321 return;
1322 printk("Suspending console(s) (use no_console_suspend to debug)\n");
1323 console_lock();
1324 console_suspended = 1;
1325 up(&console_sem);
1326}
1327
1328void resume_console(void)
1329{
1330 if (!console_suspend_enabled)
1331 return;
1332 down(&console_sem);
1333 console_suspended = 0;
1334 console_unlock();
1335}
1336
1337/**
1338 * console_cpu_notify - print deferred console messages after CPU hotplug
1339 * @self: notifier struct
1340 * @action: CPU hotplug event
1341 * @hcpu: unused
1342 *
1343 * If printk() is called from a CPU that is not online yet, the messages
1344 * will be spooled but will not show up on the console. This function is
1345 * called when a new CPU comes online (or fails to come up), and ensures
1346 * that any such output gets printed.
1347 */
1348static int __cpuinit console_cpu_notify(struct notifier_block *self,
1349 unsigned long action, void *hcpu)
1350{
1351 switch (action) {
1352 case CPU_ONLINE:
1353 case CPU_DEAD:
1354 case CPU_DOWN_FAILED:
1355 case CPU_UP_CANCELED:
1356 console_lock();
1357 console_unlock();
1358 }
1359 return NOTIFY_OK;
1360}
1361
1362/**
1363 * console_lock - lock the console system for exclusive use.
1364 *
1365 * Acquires a lock which guarantees that the caller has
1366 * exclusive access to the console system and the console_drivers list.
1367 *
1368 * Can sleep, returns nothing.
1369 */
1370void console_lock(void)
1371{
1372 BUG_ON(in_interrupt());
1373 down(&console_sem);
1374 if (console_suspended)
1375 return;
1376 console_locked = 1;
1377 console_may_schedule = 1;
1378}
1379EXPORT_SYMBOL(console_lock);
1380
1381/**
1382 * console_trylock - try to lock the console system for exclusive use.
1383 *
1384 * Tried to acquire a lock which guarantees that the caller has
1385 * exclusive access to the console system and the console_drivers list.
1386 *
1387 * returns 1 on success, and 0 on failure to acquire the lock.
1388 */
1389int console_trylock(void)
1390{
1391 if (down_trylock(&console_sem))
1392 return 0;
1393 if (console_suspended) {
1394 up(&console_sem);
1395 return 0;
1396 }
1397 console_locked = 1;
1398 console_may_schedule = 0;
1399 return 1;
1400}
1401EXPORT_SYMBOL(console_trylock);
1402
1403int is_console_locked(void)
1404{
1405 return console_locked;
1406}
1407
1408/*
1409 * Delayed printk facility, for scheduler-internal messages:
1410 */
1411#define PRINTK_BUF_SIZE 512
1412
1413#define PRINTK_PENDING_WAKEUP 0x01
1414#define PRINTK_PENDING_SCHED 0x02
1415
1416static DEFINE_PER_CPU(int, printk_pending);
1417static DEFINE_PER_CPU(char [PRINTK_BUF_SIZE], printk_sched_buf);
1418
1419void printk_tick(void)
1420{
1421 if (__this_cpu_read(printk_pending)) {
1422 int pending = __this_cpu_xchg(printk_pending, 0);
1423 if (pending & PRINTK_PENDING_SCHED) {
1424 char *buf = __get_cpu_var(printk_sched_buf);
1425 printk(KERN_WARNING "[sched_delayed] %s", buf);
1426 }
1427 if (pending & PRINTK_PENDING_WAKEUP)
1428 wake_up_interruptible(&log_wait);
1429 }
1430}
1431
1432int printk_needs_cpu(int cpu)
1433{
1434 if (unlikely(cpu_is_offline(cpu)))
1435 __this_cpu_write(printk_pending, 0);
1436 return __this_cpu_read(printk_pending);
1437}
1438
1439void wake_up_klogd(void)
1440{
1441 if (waitqueue_active(&log_wait))
1442 this_cpu_or(printk_pending, PRINTK_PENDING_WAKEUP);
1443}
1444
1445/**
1446 * console_unlock - unlock the console system
1447 *
1448 * Releases the console_lock which the caller holds on the console system
1449 * and the console driver list.
1450 *
1451 * While the console_lock was held, console output may have been buffered
1452 * by printk(). If this is the case, console_unlock(); emits
1453 * the output prior to releasing the lock.
1454 *
1455 * If there is output waiting for klogd, we wake it up.
1456 *
1457 * console_unlock(); may be called from any context.
1458 */
1459void console_unlock(void)
1460{
1461 unsigned long flags;
1462 unsigned _con_start, _log_end;
1463 unsigned wake_klogd = 0, retry = 0;
1464
1465 if (console_suspended) {
1466 up(&console_sem);
1467 return;
1468 }
1469
1470 console_may_schedule = 0;
1471
1472again:
1473 for ( ; ; ) {
1474 raw_spin_lock_irqsave(&logbuf_lock, flags);
1475 wake_klogd |= log_start - log_end;
1476 if (con_start == log_end)
1477 break; /* Nothing to print */
1478 _con_start = con_start;
1479 _log_end = log_end;
1480 con_start = log_end; /* Flush */
1481#ifndef CONFIG_PREEMPT_RT_FULL
1482 raw_spin_unlock(&logbuf_lock);
1483 stop_critical_timings(); /* don't trace print latency */
1484 call_console_drivers(_con_start, _log_end);
1485 start_critical_timings();
1486 local_irq_restore(flags);
1487#else
1488 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
1489 call_console_drivers(_con_start, _log_end);
1490#endif
1491 }
1492 console_locked = 0;
1493
1494 /* Release the exclusive_console once it is used */
1495 if (unlikely(exclusive_console))
1496 exclusive_console = NULL;
1497
1498 raw_spin_unlock(&logbuf_lock);
1499
1500 up(&console_sem);
1501
1502 /*
1503 * Someone could have filled up the buffer again, so re-check if there's
1504 * something to flush. In case we cannot trylock the console_sem again,
1505 * there's a new owner and the console_unlock() from them will do the
1506 * flush, no worries.
1507 */
1508 raw_spin_lock(&logbuf_lock);
1509 if (con_start != log_end)
1510 retry = 1;
1511 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
1512
1513 if (retry && console_trylock())
1514 goto again;
1515
1516 if (wake_klogd)
1517 wake_up_klogd();
1518}
1519EXPORT_SYMBOL(console_unlock);
1520
1521/**
1522 * console_conditional_schedule - yield the CPU if required
1523 *
1524 * If the console code is currently allowed to sleep, and
1525 * if this CPU should yield the CPU to another task, do
1526 * so here.
1527 *
1528 * Must be called within console_lock();.
1529 */
1530void __sched console_conditional_schedule(void)
1531{
1532 if (console_may_schedule)
1533 cond_resched();
1534}
1535EXPORT_SYMBOL(console_conditional_schedule);
1536
1537void console_unblank(void)
1538{
1539 struct console *c;
1540
1541 /*
1542 * console_unblank can no longer be called in interrupt context unless
1543 * oops_in_progress is set to 1..
1544 */
1545 if (oops_in_progress) {
1546 if (down_trylock(&console_sem) != 0)
1547 return;
1548 } else
1549 console_lock();
1550
1551 console_locked = 1;
1552 console_may_schedule = 0;
1553 for_each_console(c)
1554 if ((c->flags & CON_ENABLED) && c->unblank)
1555 c->unblank();
1556 console_unlock();
1557}
1558
1559/*
1560 * Return the console tty driver structure and its associated index
1561 */
1562struct tty_driver *console_device(int *index)
1563{
1564 struct console *c;
1565 struct tty_driver *driver = NULL;
1566
1567 console_lock();
1568 for_each_console(c) {
1569 if (!c->device)
1570 continue;
1571 driver = c->device(c, index);
1572 if (driver)
1573 break;
1574 }
1575 console_unlock();
1576 return driver;
1577}
1578
1579/*
1580 * Prevent further output on the passed console device so that (for example)
1581 * serial drivers can disable console output before suspending a port, and can
1582 * re-enable output afterwards.
1583 */
1584void console_stop(struct console *console)
1585{
1586 console_lock();
1587 console->flags &= ~CON_ENABLED;
1588 console_unlock();
1589}
1590EXPORT_SYMBOL(console_stop);
1591
1592void console_start(struct console *console)
1593{
1594 console_lock();
1595 console->flags |= CON_ENABLED;
1596 console_unlock();
1597}
1598EXPORT_SYMBOL(console_start);
1599
1600static int __read_mostly keep_bootcon;
1601
1602static int __init keep_bootcon_setup(char *str)
1603{
1604 keep_bootcon = 1;
1605 printk(KERN_INFO "debug: skip boot console de-registration.\n");
1606
1607 return 0;
1608}
1609
1610early_param("keep_bootcon", keep_bootcon_setup);
1611
1612/*
1613 * The console driver calls this routine during kernel initialization
1614 * to register the console printing procedure with printk() and to
1615 * print any messages that were printed by the kernel before the
1616 * console driver was initialized.
1617 *
1618 * This can happen pretty early during the boot process (because of
1619 * early_printk) - sometimes before setup_arch() completes - be careful
1620 * of what kernel features are used - they may not be initialised yet.
1621 *
1622 * There are two types of consoles - bootconsoles (early_printk) and
1623 * "real" consoles (everything which is not a bootconsole) which are
1624 * handled differently.
1625 * - Any number of bootconsoles can be registered at any time.
1626 * - As soon as a "real" console is registered, all bootconsoles
1627 * will be unregistered automatically.
1628 * - Once a "real" console is registered, any attempt to register a
1629 * bootconsoles will be rejected
1630 */
1631void register_console(struct console *newcon)
1632{
1633 int i;
1634 unsigned long flags;
1635 struct console *bcon = NULL;
1636
1637 /*
1638 * before we register a new CON_BOOT console, make sure we don't
1639 * already have a valid console
1640 */
1641 if (console_drivers && newcon->flags & CON_BOOT) {
1642 /* find the last or real console */
1643 for_each_console(bcon) {
1644 if (!(bcon->flags & CON_BOOT)) {
1645 printk(KERN_INFO "Too late to register bootconsole %s%d\n",
1646 newcon->name, newcon->index);
1647 return;
1648 }
1649 }
1650 }
1651
1652 if (console_drivers && console_drivers->flags & CON_BOOT)
1653 bcon = console_drivers;
1654
1655 if (preferred_console < 0 || bcon || !console_drivers)
1656 preferred_console = selected_console;
1657
1658 if (newcon->early_setup)
1659 newcon->early_setup();
1660
1661 /*
1662 * See if we want to use this console driver. If we
1663 * didn't select a console we take the first one
1664 * that registers here.
1665 */
1666 if (preferred_console < 0) {
1667 if (newcon->index < 0)
1668 newcon->index = 0;
1669 if (newcon->setup == NULL ||
1670 newcon->setup(newcon, NULL) == 0) {
1671 newcon->flags |= CON_ENABLED;
1672 if (newcon->device) {
1673 newcon->flags |= CON_CONSDEV;
1674 preferred_console = 0;
1675 }
1676 }
1677 }
1678
1679 /*
1680 * See if this console matches one we selected on
1681 * the command line.
1682 */
1683 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0];
1684 i++) {
1685 BUILD_BUG_ON(sizeof(console_cmdline[i].name) != sizeof(newcon->name));
1686 if (strcmp(console_cmdline[i].name, newcon->name) != 0)
1687 continue;
1688 if (newcon->index >= 0 &&
1689 newcon->index != console_cmdline[i].index)
1690 continue;
1691 if (newcon->index < 0)
1692 newcon->index = console_cmdline[i].index;
1693#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1694 if (console_cmdline[i].brl_options) {
1695 newcon->flags |= CON_BRL;
1696 braille_register_console(newcon,
1697 console_cmdline[i].index,
1698 console_cmdline[i].options,
1699 console_cmdline[i].brl_options);
1700 return;
1701 }
1702#endif
1703 if (newcon->setup &&
1704 newcon->setup(newcon, console_cmdline[i].options) != 0)
1705 break;
1706 newcon->flags |= CON_ENABLED;
1707 newcon->index = console_cmdline[i].index;
1708 if (i == selected_console) {
1709 newcon->flags |= CON_CONSDEV;
1710 preferred_console = selected_console;
1711 }
1712 break;
1713 }
1714
1715 if (!(newcon->flags & CON_ENABLED))
1716 return;
1717
1718 /*
1719 * If we have a bootconsole, and are switching to a real console,
1720 * don't print everything out again, since when the boot console, and
1721 * the real console are the same physical device, it's annoying to
1722 * see the beginning boot messages twice
1723 */
1724 if (bcon && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV))
1725 newcon->flags &= ~CON_PRINTBUFFER;
1726
1727 /*
1728 * Put this console in the list - keep the
1729 * preferred driver at the head of the list.
1730 */
1731 console_lock();
1732 if ((newcon->flags & CON_CONSDEV) || console_drivers == NULL) {
1733 newcon->next = console_drivers;
1734 console_drivers = newcon;
1735 if (newcon->next)
1736 newcon->next->flags &= ~CON_CONSDEV;
1737 } else {
1738 newcon->next = console_drivers->next;
1739 console_drivers->next = newcon;
1740 }
1741 if (newcon->flags & CON_PRINTBUFFER) {
1742 /*
1743 * console_unlock(); will print out the buffered messages
1744 * for us.
1745 */
1746 raw_spin_lock_irqsave(&logbuf_lock, flags);
1747 con_start = log_start;
1748 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
1749 /*
1750 * We're about to replay the log buffer. Only do this to the
1751 * just-registered console to avoid excessive message spam to
1752 * the already-registered consoles.
1753 */
1754 exclusive_console = newcon;
1755 }
1756 console_unlock();
1757 console_sysfs_notify();
1758
1759 /*
1760 * By unregistering the bootconsoles after we enable the real console
1761 * we get the "console xxx enabled" message on all the consoles -
1762 * boot consoles, real consoles, etc - this is to ensure that end
1763 * users know there might be something in the kernel's log buffer that
1764 * went to the bootconsole (that they do not see on the real console)
1765 */
1766 if (bcon &&
1767 ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV) &&
1768 !keep_bootcon) {
1769 /* we need to iterate through twice, to make sure we print
1770 * everything out, before we unregister the console(s)
1771 */
1772 printk(KERN_INFO "console [%s%d] enabled, bootconsole disabled\n",
1773 newcon->name, newcon->index);
1774 for_each_console(bcon)
1775 if (bcon->flags & CON_BOOT)
1776 unregister_console(bcon);
1777 } else {
1778 printk(KERN_INFO "%sconsole [%s%d] enabled\n",
1779 (newcon->flags & CON_BOOT) ? "boot" : "" ,
1780 newcon->name, newcon->index);
1781 }
1782}
1783EXPORT_SYMBOL(register_console);
1784
1785int unregister_console(struct console *console)
1786{
1787 struct console *a, *b;
1788 int res = 1;
1789
1790#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1791 if (console->flags & CON_BRL)
1792 return braille_unregister_console(console);
1793#endif
1794
1795 console_lock();
1796 if (console_drivers == console) {
1797 console_drivers=console->next;
1798 res = 0;
1799 } else if (console_drivers) {
1800 for (a=console_drivers->next, b=console_drivers ;
1801 a; b=a, a=b->next) {
1802 if (a == console) {
1803 b->next = a->next;
1804 res = 0;
1805 break;
1806 }
1807 }
1808 }
1809
1810 /*
1811 * If this isn't the last console and it has CON_CONSDEV set, we
1812 * need to set it on the next preferred console.
1813 */
1814 if (console_drivers != NULL && console->flags & CON_CONSDEV)
1815 console_drivers->flags |= CON_CONSDEV;
1816
1817 console_unlock();
1818 console_sysfs_notify();
1819 return res;
1820}
1821EXPORT_SYMBOL(unregister_console);
1822
1823static int __init printk_late_init(void)
1824{
1825 struct console *con;
1826
1827 for_each_console(con) {
1828 if (!keep_bootcon && con->flags & CON_BOOT) {
1829 printk(KERN_INFO "turn off boot console %s%d\n",
1830 con->name, con->index);
1831 unregister_console(con);
1832 }
1833 }
1834 hotcpu_notifier(console_cpu_notify, 0);
1835 return 0;
1836}
1837late_initcall(printk_late_init);
1838
1839#if defined CONFIG_PRINTK
1840
1841int printk_deferred(const char *fmt, ...)
1842{
1843 unsigned long flags;
1844 va_list args;
1845 char *buf;
1846 int r;
1847
1848 local_irq_save(flags);
1849 buf = __get_cpu_var(printk_sched_buf);
1850
1851 va_start(args, fmt);
1852 r = vsnprintf(buf, PRINTK_BUF_SIZE, fmt, args);
1853 va_end(args);
1854
1855 __this_cpu_or(printk_pending, PRINTK_PENDING_SCHED);
1856 local_irq_restore(flags);
1857
1858 return r;
1859}
1860
1861/*
1862 * printk rate limiting, lifted from the networking subsystem.
1863 *
1864 * This enforces a rate limit: not more than 10 kernel messages
1865 * every 5s to make a denial-of-service attack impossible.
1866 */
1867DEFINE_RATELIMIT_STATE(printk_ratelimit_state, 5 * HZ, 10);
1868
1869int __printk_ratelimit(const char *func)
1870{
1871 return ___ratelimit(&printk_ratelimit_state, func);
1872}
1873EXPORT_SYMBOL(__printk_ratelimit);
1874
1875/**
1876 * printk_timed_ratelimit - caller-controlled printk ratelimiting
1877 * @caller_jiffies: pointer to caller's state
1878 * @interval_msecs: minimum interval between prints
1879 *
1880 * printk_timed_ratelimit() returns true if more than @interval_msecs
1881 * milliseconds have elapsed since the last time printk_timed_ratelimit()
1882 * returned true.
1883 */
1884bool printk_timed_ratelimit(unsigned long *caller_jiffies,
1885 unsigned int interval_msecs)
1886{
1887 if (*caller_jiffies == 0
1888 || !time_in_range(jiffies, *caller_jiffies,
1889 *caller_jiffies
1890 + msecs_to_jiffies(interval_msecs))) {
1891 *caller_jiffies = jiffies;
1892 return true;
1893 }
1894 return false;
1895}
1896EXPORT_SYMBOL(printk_timed_ratelimit);
1897
1898static DEFINE_SPINLOCK(dump_list_lock);
1899static LIST_HEAD(dump_list);
1900
1901/**
1902 * kmsg_dump_register - register a kernel log dumper.
1903 * @dumper: pointer to the kmsg_dumper structure
1904 *
1905 * Adds a kernel log dumper to the system. The dump callback in the
1906 * structure will be called when the kernel oopses or panics and must be
1907 * set. Returns zero on success and %-EINVAL or %-EBUSY otherwise.
1908 */
1909int kmsg_dump_register(struct kmsg_dumper *dumper)
1910{
1911 unsigned long flags;
1912 int err = -EBUSY;
1913
1914 /* The dump callback needs to be set */
1915 if (!dumper->dump)
1916 return -EINVAL;
1917
1918 spin_lock_irqsave(&dump_list_lock, flags);
1919 /* Don't allow registering multiple times */
1920 if (!dumper->registered) {
1921 dumper->registered = 1;
1922 list_add_tail_rcu(&dumper->list, &dump_list);
1923 err = 0;
1924 }
1925 spin_unlock_irqrestore(&dump_list_lock, flags);
1926
1927 return err;
1928}
1929EXPORT_SYMBOL_GPL(kmsg_dump_register);
1930
1931/**
1932 * kmsg_dump_unregister - unregister a kmsg dumper.
1933 * @dumper: pointer to the kmsg_dumper structure
1934 *
1935 * Removes a dump device from the system. Returns zero on success and
1936 * %-EINVAL otherwise.
1937 */
1938int kmsg_dump_unregister(struct kmsg_dumper *dumper)
1939{
1940 unsigned long flags;
1941 int err = -EINVAL;
1942
1943 spin_lock_irqsave(&dump_list_lock, flags);
1944 if (dumper->registered) {
1945 dumper->registered = 0;
1946 list_del_rcu(&dumper->list);
1947 err = 0;
1948 }
1949 spin_unlock_irqrestore(&dump_list_lock, flags);
1950 synchronize_rcu();
1951
1952 return err;
1953}
1954EXPORT_SYMBOL_GPL(kmsg_dump_unregister);
1955
1956/**
1957 * kmsg_dump - dump kernel log to kernel message dumpers.
1958 * @reason: the reason (oops, panic etc) for dumping
1959 *
1960 * Iterate through each of the dump devices and call the oops/panic
1961 * callbacks with the log buffer.
1962 */
1963void kmsg_dump(enum kmsg_dump_reason reason)
1964{
1965 unsigned long end;
1966 unsigned chars;
1967 struct kmsg_dumper *dumper;
1968 const char *s1, *s2;
1969 unsigned long l1, l2;
1970 unsigned long flags;
1971
1972 if ((reason > KMSG_DUMP_OOPS) && !always_kmsg_dump)
1973 return;
1974
1975 /* Theoretically, the log could move on after we do this, but
1976 there's not a lot we can do about that. The new messages
1977 will overwrite the start of what we dump. */
1978 raw_spin_lock_irqsave(&logbuf_lock, flags);
1979 end = log_end & LOG_BUF_MASK;
1980 chars = logged_chars;
1981 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
1982
1983 if (chars > end) {
1984 s1 = log_buf + log_buf_len - chars + end;
1985 l1 = chars - end;
1986
1987 s2 = log_buf;
1988 l2 = end;
1989 } else {
1990 s1 = "";
1991 l1 = 0;
1992
1993 s2 = log_buf + end - chars;
1994 l2 = chars;
1995 }
1996
1997 rcu_read_lock();
1998 list_for_each_entry_rcu(dumper, &dump_list, list)
1999 dumper->dump(dumper, reason, s1, l1, s2, l2);
2000 rcu_read_unlock();
2001}
2002
2003void get_logbuf_info(unsigned long *addr, unsigned long *size)
2004{
2005 *addr = __log_buf;
2006 *size = __LOG_BUF_LEN;
2007}
2008EXPORT_SYMBOL(get_logbuf_info);
2009
2010#endif