drivers/macintosh/adb: Do not claim that the semaphore is a mutex
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / kernel / printk.c
CommitLineData
1da177e4
LT
1/*
2 * linux/kernel/printk.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * Modified to make sys_syslog() more flexible: added commands to
7 * return the last 4k of kernel messages, regardless of whether
8 * they've been read or not. Added option to suppress kernel printk's
9 * to the console. Added hook for sending the console messages
10 * elsewhere, in preparation for a serial line console (someday).
11 * Ted Ts'o, 2/11/93.
12 * Modified for sysctl support, 1/8/97, Chris Horn.
40dc5651 13 * Fixed SMP synchronization, 08/08/99, Manfred Spraul
624dffcb 14 * manfred@colorfullife.com
1da177e4 15 * Rewrote bits to get rid of console_lock
e1f8e874 16 * 01Mar01 Andrew Morton
1da177e4
LT
17 */
18
19#include <linux/kernel.h>
20#include <linux/mm.h>
21#include <linux/tty.h>
22#include <linux/tty_driver.h>
1da177e4
LT
23#include <linux/console.h>
24#include <linux/init.h>
bfe8df3d
RD
25#include <linux/jiffies.h>
26#include <linux/nmi.h>
1da177e4 27#include <linux/module.h>
3b9c0410 28#include <linux/moduleparam.h>
1da177e4 29#include <linux/interrupt.h> /* For in_interrupt() */
1da177e4
LT
30#include <linux/delay.h>
31#include <linux/smp.h>
32#include <linux/security.h>
33#include <linux/bootmem.h>
34#include <linux/syscalls.h>
04d491ab 35#include <linux/kexec.h>
d37d39ae 36#include <linux/kdb.h>
3fff4c42 37#include <linux/ratelimit.h>
456b565c 38#include <linux/kmsg_dump.h>
00234592 39#include <linux/syslog.h>
034260d6
KC
40#include <linux/cpu.h>
41#include <linux/notifier.h>
1da177e4
LT
42
43#include <asm/uaccess.h>
44
4d091611
RG
45/*
46 * for_each_console() allows you to iterate on each console
47 */
48#define for_each_console(con) \
49 for (con = console_drivers; con != NULL; con = con->next)
50
076f9776
IM
51/*
52 * Architectures can override it:
53 */
e17ba73b 54void asmlinkage __attribute__((weak)) early_printk(const char *fmt, ...)
076f9776
IM
55{
56}
57
1da177e4
LT
58#define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT)
59
60/* printk's without a loglevel use this.. */
61#define DEFAULT_MESSAGE_LOGLEVEL 4 /* KERN_WARNING */
62
63/* We show everything that is MORE important than this.. */
64#define MINIMUM_CONSOLE_LOGLEVEL 1 /* Minimum loglevel we let people use */
65#define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */
66
67DECLARE_WAIT_QUEUE_HEAD(log_wait);
68
69int console_printk[4] = {
70 DEFAULT_CONSOLE_LOGLEVEL, /* console_loglevel */
71 DEFAULT_MESSAGE_LOGLEVEL, /* default_message_loglevel */
72 MINIMUM_CONSOLE_LOGLEVEL, /* minimum_console_loglevel */
73 DEFAULT_CONSOLE_LOGLEVEL, /* default_console_loglevel */
74};
75
1da177e4 76/*
0bbfb7c2 77 * Low level drivers may need that to know if they can schedule in
1da177e4
LT
78 * their unblank() callback or not. So let's export it.
79 */
80int oops_in_progress;
81EXPORT_SYMBOL(oops_in_progress);
82
83/*
84 * console_sem protects the console_drivers list, and also
85 * provides serialisation for access to the entire console
86 * driver system.
87 */
88static DECLARE_MUTEX(console_sem);
89struct console *console_drivers;
a29d1cfe
IM
90EXPORT_SYMBOL_GPL(console_drivers);
91
1da177e4
LT
92/*
93 * This is used for debugging the mess that is the VT code by
94 * keeping track if we have the console semaphore held. It's
95 * definitely not the perfect debug tool (we don't know if _WE_
96 * hold it are racing, but it helps tracking those weird code
97 * path in the console code where we end up in places I want
98 * locked without the console sempahore held
99 */
557240b4 100static int console_locked, console_suspended;
1da177e4
LT
101
102/*
103 * logbuf_lock protects log_buf, log_start, log_end, con_start and logged_chars
104 * It is also used in interesting ways to provide interlocking in
105 * release_console_sem().
106 */
107static DEFINE_SPINLOCK(logbuf_lock);
108
eed4a2ab 109#define LOG_BUF_MASK (log_buf_len-1)
1da177e4
LT
110#define LOG_BUF(idx) (log_buf[(idx) & LOG_BUF_MASK])
111
112/*
113 * The indices into log_buf are not constrained to log_buf_len - they
114 * must be masked before subscripting
115 */
eed4a2ab
DV
116static unsigned log_start; /* Index into log_buf: next char to be read by syslog() */
117static unsigned con_start; /* Index into log_buf: next char to be sent to consoles */
118static unsigned log_end; /* Index into log_buf: most-recently-written-char + 1 */
1da177e4
LT
119
120/*
121 * Array of consoles built from command line options (console=)
122 */
123struct console_cmdline
124{
125 char name[8]; /* Name of the driver */
126 int index; /* Minor dev. to use */
127 char *options; /* Options for the driver */
f7511d5f
ST
128#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
129 char *brl_options; /* Options for braille driver */
130#endif
1da177e4
LT
131};
132
133#define MAX_CMDLINECONSOLES 8
134
135static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
136static int selected_console = -1;
137static int preferred_console = -1;
9e124fe1
MA
138int console_set_on_cmdline;
139EXPORT_SYMBOL(console_set_on_cmdline);
1da177e4
LT
140
141/* Flag: console code may call schedule() */
142static int console_may_schedule;
143
d59745ce
MM
144#ifdef CONFIG_PRINTK
145
146static char __log_buf[__LOG_BUF_LEN];
147static char *log_buf = __log_buf;
148static int log_buf_len = __LOG_BUF_LEN;
eed4a2ab 149static unsigned logged_chars; /* Number of chars produced since last read+clear operation */
cea83886 150static int saved_console_loglevel = -1;
d59745ce 151
04d491ab
NH
152#ifdef CONFIG_KEXEC
153/*
154 * This appends the listed symbols to /proc/vmcoreinfo
155 *
156 * /proc/vmcoreinfo is used by various utiilties, like crash and makedumpfile to
157 * obtain access to symbols that are otherwise very difficult to locate. These
158 * symbols are specifically used so that utilities can access and extract the
159 * dmesg log from a vmcore file after a crash.
160 */
161void log_buf_kexec_setup(void)
162{
163 VMCOREINFO_SYMBOL(log_buf);
164 VMCOREINFO_SYMBOL(log_end);
165 VMCOREINFO_SYMBOL(log_buf_len);
166 VMCOREINFO_SYMBOL(logged_chars);
167}
168#endif
169
1da177e4
LT
170static int __init log_buf_len_setup(char *str)
171{
eed4a2ab 172 unsigned size = memparse(str, &str);
1da177e4
LT
173 unsigned long flags;
174
175 if (size)
176 size = roundup_pow_of_two(size);
177 if (size > log_buf_len) {
eed4a2ab 178 unsigned start, dest_idx, offset;
40dc5651 179 char *new_log_buf;
1da177e4
LT
180
181 new_log_buf = alloc_bootmem(size);
182 if (!new_log_buf) {
40dc5651 183 printk(KERN_WARNING "log_buf_len: allocation failed\n");
1da177e4
LT
184 goto out;
185 }
186
187 spin_lock_irqsave(&logbuf_lock, flags);
188 log_buf_len = size;
189 log_buf = new_log_buf;
190
191 offset = start = min(con_start, log_start);
192 dest_idx = 0;
193 while (start != log_end) {
194 log_buf[dest_idx] = __log_buf[start & (__LOG_BUF_LEN - 1)];
195 start++;
196 dest_idx++;
197 }
198 log_start -= offset;
199 con_start -= offset;
200 log_end -= offset;
201 spin_unlock_irqrestore(&logbuf_lock, flags);
202
40dc5651 203 printk(KERN_NOTICE "log_buf_len: %d\n", log_buf_len);
1da177e4
LT
204 }
205out:
1da177e4
LT
206 return 1;
207}
208
209__setup("log_buf_len=", log_buf_len_setup);
210
bfe8df3d
RD
211#ifdef CONFIG_BOOT_PRINTK_DELAY
212
213static unsigned int boot_delay; /* msecs delay after each printk during bootup */
3a3b6ed2 214static unsigned long long loops_per_msec; /* based on boot_delay */
bfe8df3d
RD
215
216static int __init boot_delay_setup(char *str)
217{
218 unsigned long lpj;
bfe8df3d
RD
219
220 lpj = preset_lpj ? preset_lpj : 1000000; /* some guess */
221 loops_per_msec = (unsigned long long)lpj / 1000 * HZ;
222
223 get_option(&str, &boot_delay);
224 if (boot_delay > 10 * 1000)
225 boot_delay = 0;
226
3a3b6ed2
DY
227 pr_debug("boot_delay: %u, preset_lpj: %ld, lpj: %lu, "
228 "HZ: %d, loops_per_msec: %llu\n",
229 boot_delay, preset_lpj, lpj, HZ, loops_per_msec);
bfe8df3d
RD
230 return 1;
231}
232__setup("boot_delay=", boot_delay_setup);
233
234static void boot_delay_msec(void)
235{
236 unsigned long long k;
237 unsigned long timeout;
238
239 if (boot_delay == 0 || system_state != SYSTEM_BOOTING)
240 return;
241
3a3b6ed2 242 k = (unsigned long long)loops_per_msec * boot_delay;
bfe8df3d
RD
243
244 timeout = jiffies + msecs_to_jiffies(boot_delay);
245 while (k) {
246 k--;
247 cpu_relax();
248 /*
249 * use (volatile) jiffies to prevent
250 * compiler reduction; loop termination via jiffies
251 * is secondary and may or may not happen.
252 */
253 if (time_after(jiffies, timeout))
254 break;
255 touch_nmi_watchdog();
256 }
257}
258#else
259static inline void boot_delay_msec(void)
260{
261}
262#endif
263
00234592 264int do_syslog(int type, char __user *buf, int len, bool from_file)
1da177e4 265{
eed4a2ab 266 unsigned i, j, limit, count;
1da177e4
LT
267 int do_clear = 0;
268 char c;
269 int error = 0;
270
00234592 271 error = security_syslog(type, from_file);
1da177e4
LT
272 if (error)
273 return error;
274
275 switch (type) {
d78ca3cd 276 case SYSLOG_ACTION_CLOSE: /* Close log */
1da177e4 277 break;
d78ca3cd 278 case SYSLOG_ACTION_OPEN: /* Open log */
1da177e4 279 break;
d78ca3cd 280 case SYSLOG_ACTION_READ: /* Read from log */
1da177e4
LT
281 error = -EINVAL;
282 if (!buf || len < 0)
283 goto out;
284 error = 0;
285 if (!len)
286 goto out;
287 if (!access_ok(VERIFY_WRITE, buf, len)) {
288 error = -EFAULT;
289 goto out;
290 }
40dc5651
JJ
291 error = wait_event_interruptible(log_wait,
292 (log_start - log_end));
1da177e4
LT
293 if (error)
294 goto out;
295 i = 0;
296 spin_lock_irq(&logbuf_lock);
297 while (!error && (log_start != log_end) && i < len) {
298 c = LOG_BUF(log_start);
299 log_start++;
300 spin_unlock_irq(&logbuf_lock);
301 error = __put_user(c,buf);
302 buf++;
303 i++;
304 cond_resched();
305 spin_lock_irq(&logbuf_lock);
306 }
307 spin_unlock_irq(&logbuf_lock);
308 if (!error)
309 error = i;
310 break;
d78ca3cd
KC
311 /* Read/clear last kernel messages */
312 case SYSLOG_ACTION_READ_CLEAR:
40dc5651 313 do_clear = 1;
1da177e4 314 /* FALL THRU */
d78ca3cd
KC
315 /* Read last kernel messages */
316 case SYSLOG_ACTION_READ_ALL:
1da177e4
LT
317 error = -EINVAL;
318 if (!buf || len < 0)
319 goto out;
320 error = 0;
321 if (!len)
322 goto out;
323 if (!access_ok(VERIFY_WRITE, buf, len)) {
324 error = -EFAULT;
325 goto out;
326 }
327 count = len;
328 if (count > log_buf_len)
329 count = log_buf_len;
330 spin_lock_irq(&logbuf_lock);
331 if (count > logged_chars)
332 count = logged_chars;
333 if (do_clear)
334 logged_chars = 0;
335 limit = log_end;
336 /*
337 * __put_user() could sleep, and while we sleep
40dc5651 338 * printk() could overwrite the messages
1da177e4
LT
339 * we try to copy to user space. Therefore
340 * the messages are copied in reverse. <manfreds>
341 */
40dc5651 342 for (i = 0; i < count && !error; i++) {
1da177e4
LT
343 j = limit-1-i;
344 if (j + log_buf_len < log_end)
345 break;
346 c = LOG_BUF(j);
347 spin_unlock_irq(&logbuf_lock);
348 error = __put_user(c,&buf[count-1-i]);
349 cond_resched();
350 spin_lock_irq(&logbuf_lock);
351 }
352 spin_unlock_irq(&logbuf_lock);
353 if (error)
354 break;
355 error = i;
40dc5651 356 if (i != count) {
1da177e4
LT
357 int offset = count-error;
358 /* buffer overflow during copy, correct user buffer. */
40dc5651 359 for (i = 0; i < error; i++) {
1da177e4
LT
360 if (__get_user(c,&buf[i+offset]) ||
361 __put_user(c,&buf[i])) {
362 error = -EFAULT;
363 break;
364 }
365 cond_resched();
366 }
367 }
368 break;
d78ca3cd
KC
369 /* Clear ring buffer */
370 case SYSLOG_ACTION_CLEAR:
1da177e4
LT
371 logged_chars = 0;
372 break;
d78ca3cd
KC
373 /* Disable logging to console */
374 case SYSLOG_ACTION_CONSOLE_OFF:
1aaad49e
FP
375 if (saved_console_loglevel == -1)
376 saved_console_loglevel = console_loglevel;
1da177e4
LT
377 console_loglevel = minimum_console_loglevel;
378 break;
d78ca3cd
KC
379 /* Enable logging to console */
380 case SYSLOG_ACTION_CONSOLE_ON:
1aaad49e
FP
381 if (saved_console_loglevel != -1) {
382 console_loglevel = saved_console_loglevel;
383 saved_console_loglevel = -1;
384 }
1da177e4 385 break;
d78ca3cd
KC
386 /* Set level of messages printed to console */
387 case SYSLOG_ACTION_CONSOLE_LEVEL:
1da177e4
LT
388 error = -EINVAL;
389 if (len < 1 || len > 8)
390 goto out;
391 if (len < minimum_console_loglevel)
392 len = minimum_console_loglevel;
393 console_loglevel = len;
1aaad49e
FP
394 /* Implicitly re-enable logging to console */
395 saved_console_loglevel = -1;
1da177e4
LT
396 error = 0;
397 break;
d78ca3cd
KC
398 /* Number of chars in the log buffer */
399 case SYSLOG_ACTION_SIZE_UNREAD:
1da177e4
LT
400 error = log_end - log_start;
401 break;
d78ca3cd
KC
402 /* Size of the log buffer */
403 case SYSLOG_ACTION_SIZE_BUFFER:
1da177e4
LT
404 error = log_buf_len;
405 break;
406 default:
407 error = -EINVAL;
408 break;
409 }
410out:
411 return error;
412}
413
1e7bfb21 414SYSCALL_DEFINE3(syslog, int, type, char __user *, buf, int, len)
1da177e4 415{
00234592 416 return do_syslog(type, buf, len, SYSLOG_FROM_CALL);
1da177e4
LT
417}
418
67fc4e0c
JW
419#ifdef CONFIG_KGDB_KDB
420/* kdb dmesg command needs access to the syslog buffer. do_syslog()
421 * uses locks so it cannot be used during debugging. Just tell kdb
422 * where the start and end of the physical and logical logs are. This
423 * is equivalent to do_syslog(3).
424 */
425void kdb_syslog_data(char *syslog_data[4])
426{
427 syslog_data[0] = log_buf;
428 syslog_data[1] = log_buf + log_buf_len;
429 syslog_data[2] = log_buf + log_end -
430 (logged_chars < log_buf_len ? logged_chars : log_buf_len);
431 syslog_data[3] = log_buf + log_end;
432}
433#endif /* CONFIG_KGDB_KDB */
434
1da177e4
LT
435/*
436 * Call the console drivers on a range of log_buf
437 */
eed4a2ab 438static void __call_console_drivers(unsigned start, unsigned end)
1da177e4
LT
439{
440 struct console *con;
441
4d091611 442 for_each_console(con) {
76a8ad29
ME
443 if ((con->flags & CON_ENABLED) && con->write &&
444 (cpu_online(smp_processor_id()) ||
445 (con->flags & CON_ANYTIME)))
1da177e4
LT
446 con->write(con, &LOG_BUF(start), end - start);
447 }
448}
449
79290822
IM
450static int __read_mostly ignore_loglevel;
451
99eea6a1 452static int __init ignore_loglevel_setup(char *str)
79290822
IM
453{
454 ignore_loglevel = 1;
455 printk(KERN_INFO "debug: ignoring loglevel setting.\n");
456
c4772d99 457 return 0;
79290822
IM
458}
459
c4772d99 460early_param("ignore_loglevel", ignore_loglevel_setup);
79290822 461
1da177e4
LT
462/*
463 * Write out chars from start to end - 1 inclusive
464 */
eed4a2ab
DV
465static void _call_console_drivers(unsigned start,
466 unsigned end, int msg_log_level)
1da177e4 467{
79290822 468 if ((msg_log_level < console_loglevel || ignore_loglevel) &&
1da177e4
LT
469 console_drivers && start != end) {
470 if ((start & LOG_BUF_MASK) > (end & LOG_BUF_MASK)) {
471 /* wrapped write */
472 __call_console_drivers(start & LOG_BUF_MASK,
473 log_buf_len);
474 __call_console_drivers(0, end & LOG_BUF_MASK);
475 } else {
476 __call_console_drivers(start, end);
477 }
478 }
479}
480
481/*
482 * Call the console drivers, asking them to write out
483 * log_buf[start] to log_buf[end - 1].
484 * The console_sem must be held.
485 */
eed4a2ab 486static void call_console_drivers(unsigned start, unsigned end)
1da177e4 487{
eed4a2ab 488 unsigned cur_index, start_print;
1da177e4
LT
489 static int msg_level = -1;
490
eed4a2ab 491 BUG_ON(((int)(start - end)) > 0);
1da177e4
LT
492
493 cur_index = start;
494 start_print = start;
495 while (cur_index != end) {
40dc5651
JJ
496 if (msg_level < 0 && ((end - cur_index) > 2) &&
497 LOG_BUF(cur_index + 0) == '<' &&
498 LOG_BUF(cur_index + 1) >= '0' &&
499 LOG_BUF(cur_index + 1) <= '7' &&
500 LOG_BUF(cur_index + 2) == '>') {
1da177e4
LT
501 msg_level = LOG_BUF(cur_index + 1) - '0';
502 cur_index += 3;
503 start_print = cur_index;
504 }
505 while (cur_index != end) {
506 char c = LOG_BUF(cur_index);
1da177e4 507
40dc5651 508 cur_index++;
1da177e4
LT
509 if (c == '\n') {
510 if (msg_level < 0) {
511 /*
512 * printk() has already given us loglevel tags in
513 * the buffer. This code is here in case the
514 * log buffer has wrapped right round and scribbled
515 * on those tags
516 */
517 msg_level = default_message_loglevel;
518 }
519 _call_console_drivers(start_print, cur_index, msg_level);
520 msg_level = -1;
521 start_print = cur_index;
522 break;
523 }
524 }
525 }
526 _call_console_drivers(start_print, end, msg_level);
527}
528
529static void emit_log_char(char c)
530{
531 LOG_BUF(log_end) = c;
532 log_end++;
533 if (log_end - log_start > log_buf_len)
534 log_start = log_end - log_buf_len;
535 if (log_end - con_start > log_buf_len)
536 con_start = log_end - log_buf_len;
537 if (logged_chars < log_buf_len)
538 logged_chars++;
539}
540
541/*
542 * Zap console related locks when oopsing. Only zap at most once
543 * every 10 seconds, to leave time for slow consoles to print a
544 * full oops.
545 */
546static void zap_locks(void)
547{
548 static unsigned long oops_timestamp;
549
550 if (time_after_eq(jiffies, oops_timestamp) &&
40dc5651 551 !time_after(jiffies, oops_timestamp + 30 * HZ))
1da177e4
LT
552 return;
553
554 oops_timestamp = jiffies;
555
556 /* If a crash is occurring, make sure we can't deadlock */
557 spin_lock_init(&logbuf_lock);
558 /* And make sure that we print immediately */
559 init_MUTEX(&console_sem);
560}
561
562#if defined(CONFIG_PRINTK_TIME)
563static int printk_time = 1;
564#else
565static int printk_time = 0;
566#endif
e84845c4 567module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR);
1da177e4 568
76a8ad29
ME
569/* Check if we have any console registered that can be called early in boot. */
570static int have_callable_console(void)
571{
572 struct console *con;
573
4d091611 574 for_each_console(con)
76a8ad29
ME
575 if (con->flags & CON_ANYTIME)
576 return 1;
577
578 return 0;
579}
580
ddad86c2
MW
581/**
582 * printk - print a kernel message
583 * @fmt: format string
584 *
72fd4a35 585 * This is printk(). It can be called from any context. We want it to work.
40dc5651 586 *
1da177e4
LT
587 * We try to grab the console_sem. If we succeed, it's easy - we log the output and
588 * call the console drivers. If we fail to get the semaphore we place the output
589 * into the log buffer and return. The current holder of the console_sem will
590 * notice the new output in release_console_sem() and will send it to the
591 * consoles before releasing the semaphore.
592 *
593 * One effect of this deferred printing is that code which calls printk() and
594 * then changes console_loglevel may break. This is because console_loglevel
595 * is inspected when the actual printing occurs.
ddad86c2
MW
596 *
597 * See also:
598 * printf(3)
20036fdc
AK
599 *
600 * See the vsnprintf() documentation for format string extensions over C99.
1da177e4 601 */
d59745ce 602
1da177e4
LT
603asmlinkage int printk(const char *fmt, ...)
604{
605 va_list args;
606 int r;
607
d37d39ae
JW
608#ifdef CONFIG_KGDB_KDB
609 if (unlikely(kdb_trap_printk)) {
610 va_start(args, fmt);
611 r = vkdb_printf(fmt, args);
612 va_end(args);
613 return r;
614 }
615#endif
1da177e4
LT
616 va_start(args, fmt);
617 r = vprintk(fmt, args);
618 va_end(args);
619
620 return r;
621}
622
fe21773d
DH
623/* cpu currently holding logbuf_lock */
624static volatile unsigned int printk_cpu = UINT_MAX;
625
266c2e0a
LT
626/*
627 * Can we actually use the console at this time on this cpu?
628 *
629 * Console drivers may assume that per-cpu resources have
630 * been allocated. So unless they're explicitly marked as
631 * being able to cope (CON_ANYTIME) don't call them until
632 * this CPU is officially up.
633 */
634static inline int can_use_console(unsigned int cpu)
635{
636 return cpu_online(cpu) || have_callable_console();
637}
638
639/*
640 * Try to get console ownership to actually show the kernel
641 * messages from a 'printk'. Return true (and with the
642 * console_semaphore held, and 'console_locked' set) if it
643 * is successful, false otherwise.
644 *
645 * This gets called with the 'logbuf_lock' spinlock held and
646 * interrupts disabled. It should return with 'lockbuf_lock'
647 * released but interrupts still disabled.
648 */
649static int acquire_console_semaphore_for_printk(unsigned int cpu)
650{
651 int retval = 0;
652
093a07e2
LT
653 if (!try_acquire_console_sem()) {
654 retval = 1;
655
656 /*
657 * If we can't use the console, we need to release
658 * the console semaphore by hand to avoid flushing
659 * the buffer. We need to hold the console semaphore
660 * in order to do this test safely.
661 */
662 if (!can_use_console(cpu)) {
663 console_locked = 0;
664 up(&console_sem);
665 retval = 0;
666 }
667 }
266c2e0a
LT
668 printk_cpu = UINT_MAX;
669 spin_unlock(&logbuf_lock);
670 return retval;
671}
3b8945e8
TH
672static const char recursion_bug_msg [] =
673 KERN_CRIT "BUG: recent printk recursion!\n";
674static int recursion_bug;
edb123e1 675static int new_text_line = 1;
3b8945e8 676static char printk_buf[1024];
32a76006 677
af91322e
DY
678int printk_delay_msec __read_mostly;
679
680static inline void printk_delay(void)
681{
682 if (unlikely(printk_delay_msec)) {
683 int m = printk_delay_msec;
684
685 while (m--) {
686 mdelay(1);
687 touch_nmi_watchdog();
688 }
689 }
690}
691
1da177e4
LT
692asmlinkage int vprintk(const char *fmt, va_list args)
693{
32a76006 694 int printed_len = 0;
09159308 695 int current_log_level = default_message_loglevel;
ac60ad74 696 unsigned long flags;
32a76006 697 int this_cpu;
1da177e4 698 char *p;
1da177e4 699
bfe8df3d 700 boot_delay_msec();
af91322e 701 printk_delay();
bfe8df3d 702
fe21773d 703 preempt_disable();
1da177e4 704 /* This stops the holder of console_sem just where we want him */
1efc5da3 705 raw_local_irq_save(flags);
32a76006
IM
706 this_cpu = smp_processor_id();
707
708 /*
709 * Ouch, printk recursed into itself!
710 */
711 if (unlikely(printk_cpu == this_cpu)) {
712 /*
713 * If a crash is occurring during printk() on this CPU,
714 * then try to get the crash message out but make sure
715 * we can't deadlock. Otherwise just return to avoid the
716 * recursion and return - but flag the recursion so that
717 * it can be printed at the next appropriate moment:
718 */
719 if (!oops_in_progress) {
3b8945e8 720 recursion_bug = 1;
32a76006
IM
721 goto out_restore_irqs;
722 }
723 zap_locks();
724 }
725
a0f1ccfd
IM
726 lockdep_off();
727 spin_lock(&logbuf_lock);
32a76006 728 printk_cpu = this_cpu;
1da177e4 729
3b8945e8
TH
730 if (recursion_bug) {
731 recursion_bug = 0;
732 strcpy(printk_buf, recursion_bug_msg);
26cc271d 733 printed_len = strlen(recursion_bug_msg);
32a76006 734 }
1da177e4 735 /* Emit the output into the temporary buffer */
32a76006 736 printed_len += vscnprintf(printk_buf + printed_len,
cf3680b9 737 sizeof(printk_buf) - printed_len, fmt, args);
1da177e4 738
ac60ad74 739
5fd29d6c
LT
740 p = printk_buf;
741
742 /* Do we have a loglevel in the string? */
743 if (p[0] == '<') {
744 unsigned char c = p[1];
745 if (c && p[2] == '>') {
746 switch (c) {
747 case '0' ... '7': /* loglevel */
748 current_log_level = c - '0';
e28d7137
LT
749 /* Fallthrough - make sure we're on a new line */
750 case 'd': /* KERN_DEFAULT */
5fd29d6c
LT
751 if (!new_text_line) {
752 emit_log_char('\n');
753 new_text_line = 1;
754 }
755 /* Fallthrough - skip the loglevel */
756 case 'c': /* KERN_CONT */
757 p += 3;
758 break;
759 }
760 }
761 }
762
1da177e4
LT
763 /*
764 * Copy the output into log_buf. If the caller didn't provide
765 * appropriate log level tags, we insert them here
766 */
5fd29d6c 767 for ( ; *p; p++) {
ac60ad74 768 if (new_text_line) {
ac60ad74
NA
769 /* Always output the token */
770 emit_log_char('<');
771 emit_log_char(current_log_level + '0');
772 emit_log_char('>');
773 printed_len += 3;
774 new_text_line = 0;
775
1da177e4 776 if (printk_time) {
ac60ad74 777 /* Follow the token with the time */
1da177e4
LT
778 char tbuf[50], *tp;
779 unsigned tlen;
780 unsigned long long t;
781 unsigned long nanosec_rem;
782
326e96b9 783 t = cpu_clock(printk_cpu);
1da177e4 784 nanosec_rem = do_div(t, 1000000000);
ac60ad74
NA
785 tlen = sprintf(tbuf, "[%5lu.%06lu] ",
786 (unsigned long) t,
787 nanosec_rem / 1000);
1da177e4
LT
788
789 for (tp = tbuf; tp < tbuf + tlen; tp++)
790 emit_log_char(*tp);
025510cd 791 printed_len += tlen;
1da177e4 792 }
ac60ad74 793
1da177e4
LT
794 if (!*p)
795 break;
796 }
ac60ad74 797
1da177e4
LT
798 emit_log_char(*p);
799 if (*p == '\n')
ac60ad74 800 new_text_line = 1;
1da177e4
LT
801 }
802
266c2e0a
LT
803 /*
804 * Try to acquire and then immediately release the
805 * console semaphore. The release will do all the
806 * actual magic (print out buffers, wake up klogd,
807 * etc).
808 *
809 * The acquire_console_semaphore_for_printk() function
810 * will release 'logbuf_lock' regardless of whether it
811 * actually gets the semaphore or not.
812 */
813 if (acquire_console_semaphore_for_printk(this_cpu))
814 release_console_sem();
76a8ad29 815
266c2e0a 816 lockdep_on();
32a76006 817out_restore_irqs:
266c2e0a 818 raw_local_irq_restore(flags);
76a8ad29 819
fe21773d 820 preempt_enable();
1da177e4
LT
821 return printed_len;
822}
823EXPORT_SYMBOL(printk);
824EXPORT_SYMBOL(vprintk);
825
d59745ce
MM
826#else
827
eed4a2ab 828static void call_console_drivers(unsigned start, unsigned end)
40dc5651
JJ
829{
830}
d59745ce
MM
831
832#endif
833
f7511d5f
ST
834static int __add_preferred_console(char *name, int idx, char *options,
835 char *brl_options)
836{
837 struct console_cmdline *c;
838 int i;
839
840 /*
841 * See if this tty is not yet registered, and
842 * if we have a slot free.
843 */
844 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
845 if (strcmp(console_cmdline[i].name, name) == 0 &&
846 console_cmdline[i].index == idx) {
847 if (!brl_options)
848 selected_console = i;
849 return 0;
850 }
851 if (i == MAX_CMDLINECONSOLES)
852 return -E2BIG;
853 if (!brl_options)
854 selected_console = i;
855 c = &console_cmdline[i];
856 strlcpy(c->name, name, sizeof(c->name));
857 c->options = options;
858#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
859 c->brl_options = brl_options;
860#endif
861 c->index = idx;
862 return 0;
863}
2ea1c539
JB
864/*
865 * Set up a list of consoles. Called from init/main.c
866 */
867static int __init console_setup(char *str)
868{
eaa944af 869 char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for index */
f7511d5f 870 char *s, *options, *brl_options = NULL;
2ea1c539
JB
871 int idx;
872
f7511d5f
ST
873#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
874 if (!memcmp(str, "brl,", 4)) {
875 brl_options = "";
876 str += 4;
877 } else if (!memcmp(str, "brl=", 4)) {
878 brl_options = str + 4;
879 str = strchr(brl_options, ',');
880 if (!str) {
881 printk(KERN_ERR "need port name after brl=\n");
882 return 1;
883 }
884 *(str++) = 0;
885 }
886#endif
887
2ea1c539
JB
888 /*
889 * Decode str into name, index, options.
890 */
891 if (str[0] >= '0' && str[0] <= '9') {
eaa944af
YL
892 strcpy(buf, "ttyS");
893 strncpy(buf + 4, str, sizeof(buf) - 5);
2ea1c539 894 } else {
eaa944af 895 strncpy(buf, str, sizeof(buf) - 1);
2ea1c539 896 }
eaa944af 897 buf[sizeof(buf) - 1] = 0;
2ea1c539
JB
898 if ((options = strchr(str, ',')) != NULL)
899 *(options++) = 0;
900#ifdef __sparc__
901 if (!strcmp(str, "ttya"))
eaa944af 902 strcpy(buf, "ttyS0");
2ea1c539 903 if (!strcmp(str, "ttyb"))
eaa944af 904 strcpy(buf, "ttyS1");
2ea1c539 905#endif
eaa944af 906 for (s = buf; *s; s++)
2ea1c539
JB
907 if ((*s >= '0' && *s <= '9') || *s == ',')
908 break;
909 idx = simple_strtoul(s, NULL, 10);
910 *s = 0;
911
f7511d5f 912 __add_preferred_console(buf, idx, options, brl_options);
9e124fe1 913 console_set_on_cmdline = 1;
2ea1c539
JB
914 return 1;
915}
916__setup("console=", console_setup);
917
3c0547ba
MM
918/**
919 * add_preferred_console - add a device to the list of preferred consoles.
ddad86c2
MW
920 * @name: device name
921 * @idx: device index
922 * @options: options for this console
3c0547ba
MM
923 *
924 * The last preferred console added will be used for kernel messages
925 * and stdin/out/err for init. Normally this is used by console_setup
926 * above to handle user-supplied console arguments; however it can also
927 * be used by arch-specific code either to override the user or more
928 * commonly to provide a default console (ie from PROM variables) when
929 * the user has not supplied one.
930 */
fb445ee5 931int add_preferred_console(char *name, int idx, char *options)
3c0547ba 932{
f7511d5f 933 return __add_preferred_console(name, idx, options, NULL);
3c0547ba
MM
934}
935
b6b1d877 936int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options)
18a8bd94
YL
937{
938 struct console_cmdline *c;
939 int i;
940
941 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
942 if (strcmp(console_cmdline[i].name, name) == 0 &&
943 console_cmdline[i].index == idx) {
944 c = &console_cmdline[i];
f735295b 945 strlcpy(c->name, name_new, sizeof(c->name));
18a8bd94
YL
946 c->name[sizeof(c->name) - 1] = 0;
947 c->options = options;
948 c->index = idx_new;
949 return i;
950 }
951 /* not found */
952 return -1;
953}
954
8f4ce8c3
AS
955int console_suspend_enabled = 1;
956EXPORT_SYMBOL(console_suspend_enabled);
957
958static int __init console_suspend_disable(char *str)
959{
960 console_suspend_enabled = 0;
961 return 1;
962}
963__setup("no_console_suspend", console_suspend_disable);
964
557240b4
LT
965/**
966 * suspend_console - suspend the console subsystem
967 *
968 * This disables printk() while we go into suspend states
969 */
970void suspend_console(void)
971{
8f4ce8c3
AS
972 if (!console_suspend_enabled)
973 return;
0d63081d 974 printk("Suspending console(s) (use no_console_suspend to debug)\n");
557240b4
LT
975 acquire_console_sem();
976 console_suspended = 1;
403f3075 977 up(&console_sem);
557240b4
LT
978}
979
980void resume_console(void)
981{
8f4ce8c3
AS
982 if (!console_suspend_enabled)
983 return;
403f3075 984 down(&console_sem);
557240b4
LT
985 console_suspended = 0;
986 release_console_sem();
987}
988
034260d6
KC
989/**
990 * console_cpu_notify - print deferred console messages after CPU hotplug
991 * @self: notifier struct
992 * @action: CPU hotplug event
993 * @hcpu: unused
994 *
995 * If printk() is called from a CPU that is not online yet, the messages
996 * will be spooled but will not show up on the console. This function is
997 * called when a new CPU comes online (or fails to come up), and ensures
998 * that any such output gets printed.
999 */
1000static int __cpuinit console_cpu_notify(struct notifier_block *self,
1001 unsigned long action, void *hcpu)
1002{
1003 switch (action) {
1004 case CPU_ONLINE:
1005 case CPU_DEAD:
1006 case CPU_DYING:
1007 case CPU_DOWN_FAILED:
1008 case CPU_UP_CANCELED:
1009 acquire_console_sem();
1010 release_console_sem();
1011 }
1012 return NOTIFY_OK;
1013}
1014
1da177e4
LT
1015/**
1016 * acquire_console_sem - lock the console system for exclusive use.
1017 *
1018 * Acquires a semaphore which guarantees that the caller has
1019 * exclusive access to the console system and the console_drivers list.
1020 *
1021 * Can sleep, returns nothing.
1022 */
1023void acquire_console_sem(void)
1024{
8abd8e29 1025 BUG_ON(in_interrupt());
1da177e4 1026 down(&console_sem);
403f3075
AH
1027 if (console_suspended)
1028 return;
1da177e4
LT
1029 console_locked = 1;
1030 console_may_schedule = 1;
1031}
1032EXPORT_SYMBOL(acquire_console_sem);
1033
1034int try_acquire_console_sem(void)
1035{
1036 if (down_trylock(&console_sem))
1037 return -1;
403f3075
AH
1038 if (console_suspended) {
1039 up(&console_sem);
1040 return -1;
1041 }
1da177e4
LT
1042 console_locked = 1;
1043 console_may_schedule = 0;
1044 return 0;
1045}
1046EXPORT_SYMBOL(try_acquire_console_sem);
1047
1048int is_console_locked(void)
1049{
1050 return console_locked;
1051}
1da177e4 1052
b845b517
PZ
1053static DEFINE_PER_CPU(int, printk_pending);
1054
1055void printk_tick(void)
e3e8a75d 1056{
b845b517
PZ
1057 if (__get_cpu_var(printk_pending)) {
1058 __get_cpu_var(printk_pending) = 0;
e3e8a75d 1059 wake_up_interruptible(&log_wait);
b845b517
PZ
1060 }
1061}
1062
1063int printk_needs_cpu(int cpu)
1064{
1065 return per_cpu(printk_pending, cpu);
1066}
1067
1068void wake_up_klogd(void)
1069{
1070 if (waitqueue_active(&log_wait))
fa33507a 1071 __raw_get_cpu_var(printk_pending) = 1;
e3e8a75d
KK
1072}
1073
1da177e4
LT
1074/**
1075 * release_console_sem - unlock the console system
1076 *
1077 * Releases the semaphore which the caller holds on the console system
1078 * and the console driver list.
1079 *
1080 * While the semaphore was held, console output may have been buffered
1081 * by printk(). If this is the case, release_console_sem() emits
1082 * the output prior to releasing the semaphore.
1083 *
1084 * If there is output waiting for klogd, we wake it up.
1085 *
1086 * release_console_sem() may be called from any context.
1087 */
1088void release_console_sem(void)
1089{
1090 unsigned long flags;
eed4a2ab
DV
1091 unsigned _con_start, _log_end;
1092 unsigned wake_klogd = 0;
1da177e4 1093
557240b4 1094 if (console_suspended) {
403f3075 1095 up(&console_sem);
557240b4
LT
1096 return;
1097 }
78944e54
AD
1098
1099 console_may_schedule = 0;
1100
1da177e4
LT
1101 for ( ; ; ) {
1102 spin_lock_irqsave(&logbuf_lock, flags);
1103 wake_klogd |= log_start - log_end;
1104 if (con_start == log_end)
1105 break; /* Nothing to print */
1106 _con_start = con_start;
1107 _log_end = log_end;
1108 con_start = log_end; /* Flush */
1109 spin_unlock(&logbuf_lock);
81d68a96 1110 stop_critical_timings(); /* don't trace print latency */
1da177e4 1111 call_console_drivers(_con_start, _log_end);
81d68a96 1112 start_critical_timings();
1da177e4
LT
1113 local_irq_restore(flags);
1114 }
1115 console_locked = 0;
1da177e4
LT
1116 up(&console_sem);
1117 spin_unlock_irqrestore(&logbuf_lock, flags);
e3e8a75d
KK
1118 if (wake_klogd)
1119 wake_up_klogd();
1da177e4
LT
1120}
1121EXPORT_SYMBOL(release_console_sem);
1122
ddad86c2
MW
1123/**
1124 * console_conditional_schedule - yield the CPU if required
1da177e4
LT
1125 *
1126 * If the console code is currently allowed to sleep, and
1127 * if this CPU should yield the CPU to another task, do
1128 * so here.
1129 *
1130 * Must be called within acquire_console_sem().
1131 */
1132void __sched console_conditional_schedule(void)
1133{
1134 if (console_may_schedule)
1135 cond_resched();
1136}
1137EXPORT_SYMBOL(console_conditional_schedule);
1138
1da177e4
LT
1139void console_unblank(void)
1140{
1141 struct console *c;
1142
1143 /*
1144 * console_unblank can no longer be called in interrupt context unless
1145 * oops_in_progress is set to 1..
1146 */
1147 if (oops_in_progress) {
1148 if (down_trylock(&console_sem) != 0)
1149 return;
1150 } else
1151 acquire_console_sem();
1152
1153 console_locked = 1;
1154 console_may_schedule = 0;
4d091611 1155 for_each_console(c)
1da177e4
LT
1156 if ((c->flags & CON_ENABLED) && c->unblank)
1157 c->unblank();
1158 release_console_sem();
1159}
1da177e4
LT
1160
1161/*
1162 * Return the console tty driver structure and its associated index
1163 */
1164struct tty_driver *console_device(int *index)
1165{
1166 struct console *c;
1167 struct tty_driver *driver = NULL;
1168
1169 acquire_console_sem();
4d091611 1170 for_each_console(c) {
1da177e4
LT
1171 if (!c->device)
1172 continue;
1173 driver = c->device(c, index);
1174 if (driver)
1175 break;
1176 }
1177 release_console_sem();
1178 return driver;
1179}
1180
1181/*
1182 * Prevent further output on the passed console device so that (for example)
1183 * serial drivers can disable console output before suspending a port, and can
1184 * re-enable output afterwards.
1185 */
1186void console_stop(struct console *console)
1187{
1188 acquire_console_sem();
1189 console->flags &= ~CON_ENABLED;
1190 release_console_sem();
1191}
1192EXPORT_SYMBOL(console_stop);
1193
1194void console_start(struct console *console)
1195{
1196 acquire_console_sem();
1197 console->flags |= CON_ENABLED;
1198 release_console_sem();
1199}
1200EXPORT_SYMBOL(console_start);
1201
1202/*
1203 * The console driver calls this routine during kernel initialization
1204 * to register the console printing procedure with printk() and to
1205 * print any messages that were printed by the kernel before the
1206 * console driver was initialized.
4d091611
RG
1207 *
1208 * This can happen pretty early during the boot process (because of
1209 * early_printk) - sometimes before setup_arch() completes - be careful
1210 * of what kernel features are used - they may not be initialised yet.
1211 *
1212 * There are two types of consoles - bootconsoles (early_printk) and
1213 * "real" consoles (everything which is not a bootconsole) which are
1214 * handled differently.
1215 * - Any number of bootconsoles can be registered at any time.
1216 * - As soon as a "real" console is registered, all bootconsoles
1217 * will be unregistered automatically.
1218 * - Once a "real" console is registered, any attempt to register a
1219 * bootconsoles will be rejected
1da177e4 1220 */
4d091611 1221void register_console(struct console *newcon)
1da177e4 1222{
40dc5651 1223 int i;
1da177e4 1224 unsigned long flags;
4d091611 1225 struct console *bcon = NULL;
1da177e4 1226
4d091611
RG
1227 /*
1228 * before we register a new CON_BOOT console, make sure we don't
1229 * already have a valid console
1230 */
1231 if (console_drivers && newcon->flags & CON_BOOT) {
1232 /* find the last or real console */
1233 for_each_console(bcon) {
1234 if (!(bcon->flags & CON_BOOT)) {
1235 printk(KERN_INFO "Too late to register bootconsole %s%d\n",
1236 newcon->name, newcon->index);
1237 return;
1238 }
1239 }
69331af7
GH
1240 }
1241
4d091611
RG
1242 if (console_drivers && console_drivers->flags & CON_BOOT)
1243 bcon = console_drivers;
1244
1245 if (preferred_console < 0 || bcon || !console_drivers)
1da177e4
LT
1246 preferred_console = selected_console;
1247
4d091611
RG
1248 if (newcon->early_setup)
1249 newcon->early_setup();
18a8bd94 1250
1da177e4
LT
1251 /*
1252 * See if we want to use this console driver. If we
1253 * didn't select a console we take the first one
1254 * that registers here.
1255 */
1256 if (preferred_console < 0) {
4d091611
RG
1257 if (newcon->index < 0)
1258 newcon->index = 0;
1259 if (newcon->setup == NULL ||
1260 newcon->setup(newcon, NULL) == 0) {
1261 newcon->flags |= CON_ENABLED;
1262 if (newcon->device) {
1263 newcon->flags |= CON_CONSDEV;
cd3a1b85
JK
1264 preferred_console = 0;
1265 }
1da177e4
LT
1266 }
1267 }
1268
1269 /*
1270 * See if this console matches one we selected on
1271 * the command line.
1272 */
40dc5651
JJ
1273 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0];
1274 i++) {
4d091611 1275 if (strcmp(console_cmdline[i].name, newcon->name) != 0)
1da177e4 1276 continue;
4d091611
RG
1277 if (newcon->index >= 0 &&
1278 newcon->index != console_cmdline[i].index)
1da177e4 1279 continue;
4d091611
RG
1280 if (newcon->index < 0)
1281 newcon->index = console_cmdline[i].index;
f7511d5f
ST
1282#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1283 if (console_cmdline[i].brl_options) {
4d091611
RG
1284 newcon->flags |= CON_BRL;
1285 braille_register_console(newcon,
f7511d5f
ST
1286 console_cmdline[i].index,
1287 console_cmdline[i].options,
1288 console_cmdline[i].brl_options);
1289 return;
1290 }
1291#endif
4d091611
RG
1292 if (newcon->setup &&
1293 newcon->setup(newcon, console_cmdline[i].options) != 0)
1da177e4 1294 break;
4d091611
RG
1295 newcon->flags |= CON_ENABLED;
1296 newcon->index = console_cmdline[i].index;
ab4af03a 1297 if (i == selected_console) {
4d091611 1298 newcon->flags |= CON_CONSDEV;
ab4af03a
GE
1299 preferred_console = selected_console;
1300 }
1da177e4
LT
1301 break;
1302 }
1303
4d091611 1304 if (!(newcon->flags & CON_ENABLED))
1da177e4
LT
1305 return;
1306
8259cf43
RG
1307 /*
1308 * If we have a bootconsole, and are switching to a real console,
1309 * don't print everything out again, since when the boot console, and
1310 * the real console are the same physical device, it's annoying to
1311 * see the beginning boot messages twice
1312 */
1313 if (bcon && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV))
4d091611 1314 newcon->flags &= ~CON_PRINTBUFFER;
1da177e4
LT
1315
1316 /*
1317 * Put this console in the list - keep the
1318 * preferred driver at the head of the list.
1319 */
1320 acquire_console_sem();
4d091611
RG
1321 if ((newcon->flags & CON_CONSDEV) || console_drivers == NULL) {
1322 newcon->next = console_drivers;
1323 console_drivers = newcon;
1324 if (newcon->next)
1325 newcon->next->flags &= ~CON_CONSDEV;
1da177e4 1326 } else {
4d091611
RG
1327 newcon->next = console_drivers->next;
1328 console_drivers->next = newcon;
1da177e4 1329 }
4d091611 1330 if (newcon->flags & CON_PRINTBUFFER) {
1da177e4
LT
1331 /*
1332 * release_console_sem() will print out the buffered messages
1333 * for us.
1334 */
1335 spin_lock_irqsave(&logbuf_lock, flags);
1336 con_start = log_start;
1337 spin_unlock_irqrestore(&logbuf_lock, flags);
1338 }
1339 release_console_sem();
8259cf43
RG
1340
1341 /*
1342 * By unregistering the bootconsoles after we enable the real console
1343 * we get the "console xxx enabled" message on all the consoles -
1344 * boot consoles, real consoles, etc - this is to ensure that end
1345 * users know there might be something in the kernel's log buffer that
1346 * went to the bootconsole (that they do not see on the real console)
1347 */
1348 if (bcon && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV)) {
1349 /* we need to iterate through twice, to make sure we print
1350 * everything out, before we unregister the console(s)
1351 */
1352 printk(KERN_INFO "console [%s%d] enabled, bootconsole disabled\n",
1353 newcon->name, newcon->index);
1354 for_each_console(bcon)
1355 if (bcon->flags & CON_BOOT)
1356 unregister_console(bcon);
1357 } else {
1358 printk(KERN_INFO "%sconsole [%s%d] enabled\n",
1359 (newcon->flags & CON_BOOT) ? "boot" : "" ,
1360 newcon->name, newcon->index);
1361 }
1da177e4
LT
1362}
1363EXPORT_SYMBOL(register_console);
1364
40dc5651 1365int unregister_console(struct console *console)
1da177e4 1366{
40dc5651 1367 struct console *a, *b;
1da177e4
LT
1368 int res = 1;
1369
f7511d5f
ST
1370#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1371 if (console->flags & CON_BRL)
1372 return braille_unregister_console(console);
1373#endif
1374
1da177e4
LT
1375 acquire_console_sem();
1376 if (console_drivers == console) {
1377 console_drivers=console->next;
1378 res = 0;
e9b15b54 1379 } else if (console_drivers) {
1da177e4
LT
1380 for (a=console_drivers->next, b=console_drivers ;
1381 a; b=a, a=b->next) {
1382 if (a == console) {
1383 b->next = a->next;
1384 res = 0;
1385 break;
40dc5651 1386 }
1da177e4
LT
1387 }
1388 }
40dc5651 1389
69331af7 1390 /*
ab4af03a
GE
1391 * If this isn't the last console and it has CON_CONSDEV set, we
1392 * need to set it on the next preferred console.
1da177e4 1393 */
69331af7 1394 if (console_drivers != NULL && console->flags & CON_CONSDEV)
ab4af03a 1395 console_drivers->flags |= CON_CONSDEV;
1da177e4
LT
1396
1397 release_console_sem();
1398 return res;
1399}
1400EXPORT_SYMBOL(unregister_console);
d59745ce 1401
034260d6 1402static int __init printk_late_init(void)
0c5564bd 1403{
4d091611
RG
1404 struct console *con;
1405
1406 for_each_console(con) {
1407 if (con->flags & CON_BOOT) {
cb00e99c 1408 printk(KERN_INFO "turn off boot console %s%d\n",
4d091611 1409 con->name, con->index);
42c2c8c8 1410 unregister_console(con);
cb00e99c 1411 }
0c5564bd 1412 }
034260d6 1413 hotcpu_notifier(console_cpu_notify, 0);
0c5564bd
RG
1414 return 0;
1415}
034260d6 1416late_initcall(printk_late_init);
0c5564bd 1417
7ef3d2fd 1418#if defined CONFIG_PRINTK
717115e1 1419
1da177e4
LT
1420/*
1421 * printk rate limiting, lifted from the networking subsystem.
1422 *
641de9d8
UKK
1423 * This enforces a rate limit: not more than 10 kernel messages
1424 * every 5s to make a denial-of-service attack impossible.
1da177e4 1425 */
641de9d8
UKK
1426DEFINE_RATELIMIT_STATE(printk_ratelimit_state, 5 * HZ, 10);
1427
5c828713 1428int __printk_ratelimit(const char *func)
1da177e4 1429{
5c828713 1430 return ___ratelimit(&printk_ratelimit_state, func);
1da177e4 1431}
5c828713 1432EXPORT_SYMBOL(__printk_ratelimit);
f46c4833
AM
1433
1434/**
1435 * printk_timed_ratelimit - caller-controlled printk ratelimiting
1436 * @caller_jiffies: pointer to caller's state
1437 * @interval_msecs: minimum interval between prints
1438 *
1439 * printk_timed_ratelimit() returns true if more than @interval_msecs
1440 * milliseconds have elapsed since the last time printk_timed_ratelimit()
1441 * returned true.
1442 */
1443bool printk_timed_ratelimit(unsigned long *caller_jiffies,
1444 unsigned int interval_msecs)
1445{
f2d28a2e
GK
1446 if (*caller_jiffies == 0
1447 || !time_in_range(jiffies, *caller_jiffies,
1448 *caller_jiffies
1449 + msecs_to_jiffies(interval_msecs))) {
1450 *caller_jiffies = jiffies;
f46c4833
AM
1451 return true;
1452 }
1453 return false;
1454}
1455EXPORT_SYMBOL(printk_timed_ratelimit);
456b565c
SK
1456
1457static DEFINE_SPINLOCK(dump_list_lock);
1458static LIST_HEAD(dump_list);
1459
1460/**
1461 * kmsg_dump_register - register a kernel log dumper.
6485536b 1462 * @dumper: pointer to the kmsg_dumper structure
456b565c
SK
1463 *
1464 * Adds a kernel log dumper to the system. The dump callback in the
1465 * structure will be called when the kernel oopses or panics and must be
1466 * set. Returns zero on success and %-EINVAL or %-EBUSY otherwise.
1467 */
1468int kmsg_dump_register(struct kmsg_dumper *dumper)
1469{
1470 unsigned long flags;
1471 int err = -EBUSY;
1472
1473 /* The dump callback needs to be set */
1474 if (!dumper->dump)
1475 return -EINVAL;
1476
1477 spin_lock_irqsave(&dump_list_lock, flags);
1478 /* Don't allow registering multiple times */
1479 if (!dumper->registered) {
1480 dumper->registered = 1;
1481 list_add_tail(&dumper->list, &dump_list);
1482 err = 0;
1483 }
1484 spin_unlock_irqrestore(&dump_list_lock, flags);
1485
1486 return err;
1487}
1488EXPORT_SYMBOL_GPL(kmsg_dump_register);
1489
1490/**
1491 * kmsg_dump_unregister - unregister a kmsg dumper.
6485536b 1492 * @dumper: pointer to the kmsg_dumper structure
456b565c
SK
1493 *
1494 * Removes a dump device from the system. Returns zero on success and
1495 * %-EINVAL otherwise.
1496 */
1497int kmsg_dump_unregister(struct kmsg_dumper *dumper)
1498{
1499 unsigned long flags;
1500 int err = -EINVAL;
1501
1502 spin_lock_irqsave(&dump_list_lock, flags);
1503 if (dumper->registered) {
1504 dumper->registered = 0;
1505 list_del(&dumper->list);
1506 err = 0;
1507 }
1508 spin_unlock_irqrestore(&dump_list_lock, flags);
1509
1510 return err;
1511}
1512EXPORT_SYMBOL_GPL(kmsg_dump_unregister);
1513
1514static const char const *kmsg_reasons[] = {
1515 [KMSG_DUMP_OOPS] = "oops",
1516 [KMSG_DUMP_PANIC] = "panic",
0f4bd46e 1517 [KMSG_DUMP_KEXEC] = "kexec",
456b565c
SK
1518};
1519
1520static const char *kmsg_to_str(enum kmsg_dump_reason reason)
1521{
1522 if (reason >= ARRAY_SIZE(kmsg_reasons) || reason < 0)
1523 return "unknown";
1524
1525 return kmsg_reasons[reason];
1526}
1527
1528/**
1529 * kmsg_dump - dump kernel log to kernel message dumpers.
1530 * @reason: the reason (oops, panic etc) for dumping
1531 *
1532 * Iterate through each of the dump devices and call the oops/panic
1533 * callbacks with the log buffer.
1534 */
1535void kmsg_dump(enum kmsg_dump_reason reason)
1536{
1537 unsigned long end;
1538 unsigned chars;
1539 struct kmsg_dumper *dumper;
1540 const char *s1, *s2;
1541 unsigned long l1, l2;
1542 unsigned long flags;
1543
1544 /* Theoretically, the log could move on after we do this, but
1545 there's not a lot we can do about that. The new messages
1546 will overwrite the start of what we dump. */
1547 spin_lock_irqsave(&logbuf_lock, flags);
1548 end = log_end & LOG_BUF_MASK;
1549 chars = logged_chars;
1550 spin_unlock_irqrestore(&logbuf_lock, flags);
1551
8c4af38e
AK
1552 if (chars > end) {
1553 s1 = log_buf + log_buf_len - chars + end;
1554 l1 = chars - end;
456b565c
SK
1555
1556 s2 = log_buf;
1557 l2 = end;
1558 } else {
1559 s1 = "";
1560 l1 = 0;
1561
8c4af38e
AK
1562 s2 = log_buf + end - chars;
1563 l2 = chars;
456b565c
SK
1564 }
1565
1566 if (!spin_trylock_irqsave(&dump_list_lock, flags)) {
1567 printk(KERN_ERR "dump_kmsg: dump list lock is held during %s, skipping dump\n",
1568 kmsg_to_str(reason));
1569 return;
1570 }
1571 list_for_each_entry(dumper, &dump_list, list)
1572 dumper->dump(dumper, reason, s1, l1, s2, l2);
1573 spin_unlock_irqrestore(&dump_list_lock, flags);
1574}
7ef3d2fd 1575#endif