blob: c451533c10d1e7bb48716686bb524fafe0997613 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -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.
Jesper Juhl40dc5652005-10-30 15:02:46 -080013 * Fixed SMP synchronization, 08/08/99, Manfred Spraul
Christian Kujau624dffc2006-01-15 02:43:54 +010014 * manfred@colorfullife.com
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * Rewrote bits to get rid of console_lock
Francois Camie1f8e872008-10-15 22:01:59 -070016 * 01Mar01 Andrew Morton
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
18
19#include <linux/kernel.h>
20#include <linux/mm.h>
21#include <linux/tty.h>
22#include <linux/tty_driver.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/console.h>
24#include <linux/init.h>
Randy Dunlapbfe8df32007-10-16 01:23:46 -070025#include <linux/jiffies.h>
26#include <linux/nmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/module.h>
Jan Engelhardt3b9c0412006-06-25 05:48:15 -070028#include <linux/moduleparam.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/interrupt.h> /* For in_interrupt() */
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/delay.h>
31#include <linux/smp.h>
32#include <linux/security.h>
33#include <linux/bootmem.h>
Mike Travis162a7e72011-05-24 17:13:20 -070034#include <linux/memblock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/syscalls.h>
Neil Horman04d491a2009-04-02 16:58:57 -070036#include <linux/kexec.h>
Jason Wesseld37d39a2010-05-20 21:04:27 -050037#include <linux/kdb.h>
Ingo Molnar3fff4c42009-09-22 16:18:09 +020038#include <linux/ratelimit.h>
Simon Kagstrom456b5652009-10-16 14:09:18 +020039#include <linux/kmsg_dump.h>
Kees Cook00234592010-02-03 15:36:43 -080040#include <linux/syslog.h>
Kevin Cernekee034260d2010-06-03 22:11:25 -070041#include <linux/cpu.h>
42#include <linux/notifier.h>
Huang Yingfb842b02011-01-12 16:59:43 -080043#include <linux/rculist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45#include <asm/uaccess.h>
46
Ingo Molnar076f9772008-01-30 13:33:06 +010047/*
48 * Architectures can override it:
49 */
Jiri Slabye17ba732008-05-12 15:44:40 +020050void asmlinkage __attribute__((weak)) early_printk(const char *fmt, ...)
Ingo Molnar076f9772008-01-30 13:33:06 +010051{
52}
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT)
55
Tony Lindgren76b39902005-05-09 14:10:26 -070056#ifdef CONFIG_DEBUG_LL
57extern void printascii(char *);
58#endif
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060/* printk's without a loglevel use this.. */
Mandeep Singh Baines5af5bcb2011-03-22 16:34:23 -070061#define DEFAULT_MESSAGE_LOGLEVEL CONFIG_DEFAULT_MESSAGE_LOGLEVEL
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070076/*
Patrick Pletscher0bbfb7c2007-02-17 20:10:16 +010077 * Low level drivers may need that to know if they can schedule in
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 * 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 */
Thomas Gleixner5b8c4f22010-09-07 14:33:43 +000088static DEFINE_SEMAPHORE(console_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089struct console *console_drivers;
Ingo Molnara29d1cf2008-06-02 13:19:08 +020090EXPORT_SYMBOL_GPL(console_drivers);
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/*
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 */
Linus Torvalds557240b2006-06-19 18:16:01 -0700100static int console_locked, console_suspended;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
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
Torben Hohnac751ef2011-01-25 15:07:35 -0800105 * console_unlock();.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 */
107static DEFINE_SPINLOCK(logbuf_lock);
108
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800109#define LOG_BUF_MASK (log_buf_len-1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110#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 */
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800116static 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120/*
Feng Tangfe3d8ad2011-03-22 16:34:21 -0700121 * If exclusive_console is non-NULL then only this console is to be printed to.
122 */
123static struct console *exclusive_console;
124
125/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 * Array of consoles built from command line options (console=)
127 */
128struct console_cmdline
129{
130 char name[8]; /* Name of the driver */
131 int index; /* Minor dev. to use */
132 char *options; /* Options for the driver */
Samuel Thibaultf7511d52008-04-30 00:54:51 -0700133#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
134 char *brl_options; /* Options for braille driver */
135#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136};
137
138#define MAX_CMDLINECONSOLES 8
139
140static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
141static int selected_console = -1;
142static int preferred_console = -1;
Markus Armbruster9e124fe2008-05-26 23:31:07 +0100143int console_set_on_cmdline;
144EXPORT_SYMBOL(console_set_on_cmdline);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146/* Flag: console code may call schedule() */
147static int console_may_schedule;
148
Matt Mackalld59745c2005-05-01 08:59:02 -0700149#ifdef CONFIG_PRINTK
150
151static char __log_buf[__LOG_BUF_LEN];
152static char *log_buf = __log_buf;
153static int log_buf_len = __LOG_BUF_LEN;
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800154static unsigned logged_chars; /* Number of chars produced since last read+clear operation */
Gustavo F. Padovancea83882010-03-05 13:42:58 -0800155static int saved_console_loglevel = -1;
Matt Mackalld59745c2005-05-01 08:59:02 -0700156
Neil Horman04d491a2009-04-02 16:58:57 -0700157#ifdef CONFIG_KEXEC
158/*
159 * This appends the listed symbols to /proc/vmcoreinfo
160 *
161 * /proc/vmcoreinfo is used by various utiilties, like crash and makedumpfile to
162 * obtain access to symbols that are otherwise very difficult to locate. These
163 * symbols are specifically used so that utilities can access and extract the
164 * dmesg log from a vmcore file after a crash.
165 */
166void log_buf_kexec_setup(void)
167{
168 VMCOREINFO_SYMBOL(log_buf);
169 VMCOREINFO_SYMBOL(log_end);
170 VMCOREINFO_SYMBOL(log_buf_len);
171 VMCOREINFO_SYMBOL(logged_chars);
172}
173#endif
174
Mike Travis162a7e72011-05-24 17:13:20 -0700175/* requested log_buf_len from kernel cmdline */
176static unsigned long __initdata new_log_buf_len;
177
178/* save requested log_buf_len since it's too early to process it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179static int __init log_buf_len_setup(char *str)
180{
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800181 unsigned size = memparse(str, &str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 if (size)
184 size = roundup_pow_of_two(size);
Mike Travis162a7e72011-05-24 17:13:20 -0700185 if (size > log_buf_len)
186 new_log_buf_len = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Mike Travis162a7e72011-05-24 17:13:20 -0700188 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
Mike Travis162a7e72011-05-24 17:13:20 -0700190early_param("log_buf_len", log_buf_len_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Mike Travis162a7e72011-05-24 17:13:20 -0700192void __init setup_log_buf(int early)
193{
194 unsigned long flags;
195 unsigned start, dest_idx, offset;
196 char *new_log_buf;
197 int free;
198
199 if (!new_log_buf_len)
200 return;
201
202 if (early) {
203 unsigned long mem;
204
205 mem = memblock_alloc(new_log_buf_len, PAGE_SIZE);
206 if (mem == MEMBLOCK_ERROR)
207 return;
208 new_log_buf = __va(mem);
209 } else {
210 new_log_buf = alloc_bootmem_nopanic(new_log_buf_len);
211 }
212
213 if (unlikely(!new_log_buf)) {
214 pr_err("log_buf_len: %ld bytes not available\n",
215 new_log_buf_len);
216 return;
217 }
218
219 spin_lock_irqsave(&logbuf_lock, flags);
220 log_buf_len = new_log_buf_len;
221 log_buf = new_log_buf;
222 new_log_buf_len = 0;
223 free = __LOG_BUF_LEN - log_end;
224
225 offset = start = min(con_start, log_start);
226 dest_idx = 0;
227 while (start != log_end) {
228 unsigned log_idx_mask = start & (__LOG_BUF_LEN - 1);
229
230 log_buf[dest_idx] = __log_buf[log_idx_mask];
231 start++;
232 dest_idx++;
233 }
234 log_start -= offset;
235 con_start -= offset;
236 log_end -= offset;
237 spin_unlock_irqrestore(&logbuf_lock, flags);
238
239 pr_info("log_buf_len: %d\n", log_buf_len);
240 pr_info("early log buf free: %d(%d%%)\n",
241 free, (free * 100) / __LOG_BUF_LEN);
242}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700244#ifdef CONFIG_BOOT_PRINTK_DELAY
245
Namhyung Kim674dff62010-10-26 14:22:48 -0700246static int boot_delay; /* msecs delay after each printk during bootup */
Dave Young3a3b6ed2009-09-22 16:43:31 -0700247static unsigned long long loops_per_msec; /* based on boot_delay */
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700248
249static int __init boot_delay_setup(char *str)
250{
251 unsigned long lpj;
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700252
253 lpj = preset_lpj ? preset_lpj : 1000000; /* some guess */
254 loops_per_msec = (unsigned long long)lpj / 1000 * HZ;
255
256 get_option(&str, &boot_delay);
257 if (boot_delay > 10 * 1000)
258 boot_delay = 0;
259
Dave Young3a3b6ed2009-09-22 16:43:31 -0700260 pr_debug("boot_delay: %u, preset_lpj: %ld, lpj: %lu, "
261 "HZ: %d, loops_per_msec: %llu\n",
262 boot_delay, preset_lpj, lpj, HZ, loops_per_msec);
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700263 return 1;
264}
265__setup("boot_delay=", boot_delay_setup);
266
267static void boot_delay_msec(void)
268{
269 unsigned long long k;
270 unsigned long timeout;
271
272 if (boot_delay == 0 || system_state != SYSTEM_BOOTING)
273 return;
274
Dave Young3a3b6ed2009-09-22 16:43:31 -0700275 k = (unsigned long long)loops_per_msec * boot_delay;
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700276
277 timeout = jiffies + msecs_to_jiffies(boot_delay);
278 while (k) {
279 k--;
280 cpu_relax();
281 /*
282 * use (volatile) jiffies to prevent
283 * compiler reduction; loop termination via jiffies
284 * is secondary and may or may not happen.
285 */
286 if (time_after(jiffies, timeout))
287 break;
288 touch_nmi_watchdog();
289 }
290}
291#else
292static inline void boot_delay_msec(void)
293{
294}
295#endif
296
Dan Rosenbergeaf06b22010-11-11 14:05:18 -0800297#ifdef CONFIG_SECURITY_DMESG_RESTRICT
298int dmesg_restrict = 1;
299#else
300int dmesg_restrict;
301#endif
302
Linus Torvaldsee24aeb2011-02-10 17:53:55 -0800303static int syslog_action_restricted(int type)
304{
305 if (dmesg_restrict)
306 return 1;
307 /* Unless restricted, we allow "read all" and "get buffer size" for everybody */
308 return type != SYSLOG_ACTION_READ_ALL && type != SYSLOG_ACTION_SIZE_BUFFER;
309}
310
311static int check_syslog_permissions(int type, bool from_file)
312{
313 /*
314 * If this is from /proc/kmsg and we've already opened it, then we've
315 * already done the capabilities checks at open time.
316 */
317 if (from_file && type != SYSLOG_ACTION_OPEN)
318 return 0;
319
320 if (syslog_action_restricted(type)) {
321 if (capable(CAP_SYSLOG))
322 return 0;
323 /* For historical reasons, accept CAP_SYS_ADMIN too, with a warning */
324 if (capable(CAP_SYS_ADMIN)) {
325 WARN_ONCE(1, "Attempt to access syslog with CAP_SYS_ADMIN "
326 "but no CAP_SYSLOG (deprecated).\n");
327 return 0;
328 }
329 return -EPERM;
330 }
331 return 0;
332}
333
Kees Cook00234592010-02-03 15:36:43 -0800334int do_syslog(int type, char __user *buf, int len, bool from_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800336 unsigned i, j, limit, count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 int do_clear = 0;
338 char c;
Linus Torvaldsee24aeb2011-02-10 17:53:55 -0800339 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Linus Torvaldsee24aeb2011-02-10 17:53:55 -0800341 error = check_syslog_permissions(type, from_file);
342 if (error)
343 goto out;
Eric Paris12b30522010-11-15 18:36:29 -0500344
345 error = security_syslog(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 if (error)
347 return error;
348
349 switch (type) {
Kees Cookd78ca3c2010-02-03 15:37:13 -0800350 case SYSLOG_ACTION_CLOSE: /* Close log */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -0800352 case SYSLOG_ACTION_OPEN: /* Open log */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -0800354 case SYSLOG_ACTION_READ: /* Read from log */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 error = -EINVAL;
356 if (!buf || len < 0)
357 goto out;
358 error = 0;
359 if (!len)
360 goto out;
361 if (!access_ok(VERIFY_WRITE, buf, len)) {
362 error = -EFAULT;
363 goto out;
364 }
Jesper Juhl40dc5652005-10-30 15:02:46 -0800365 error = wait_event_interruptible(log_wait,
366 (log_start - log_end));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 if (error)
368 goto out;
369 i = 0;
370 spin_lock_irq(&logbuf_lock);
371 while (!error && (log_start != log_end) && i < len) {
372 c = LOG_BUF(log_start);
373 log_start++;
374 spin_unlock_irq(&logbuf_lock);
375 error = __put_user(c,buf);
376 buf++;
377 i++;
378 cond_resched();
379 spin_lock_irq(&logbuf_lock);
380 }
381 spin_unlock_irq(&logbuf_lock);
382 if (!error)
383 error = i;
384 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -0800385 /* Read/clear last kernel messages */
386 case SYSLOG_ACTION_READ_CLEAR:
Jesper Juhl40dc5652005-10-30 15:02:46 -0800387 do_clear = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 /* FALL THRU */
Kees Cookd78ca3c2010-02-03 15:37:13 -0800389 /* Read last kernel messages */
390 case SYSLOG_ACTION_READ_ALL:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 error = -EINVAL;
392 if (!buf || len < 0)
393 goto out;
394 error = 0;
395 if (!len)
396 goto out;
397 if (!access_ok(VERIFY_WRITE, buf, len)) {
398 error = -EFAULT;
399 goto out;
400 }
401 count = len;
402 if (count > log_buf_len)
403 count = log_buf_len;
404 spin_lock_irq(&logbuf_lock);
405 if (count > logged_chars)
406 count = logged_chars;
407 if (do_clear)
408 logged_chars = 0;
409 limit = log_end;
410 /*
411 * __put_user() could sleep, and while we sleep
Jesper Juhl40dc5652005-10-30 15:02:46 -0800412 * printk() could overwrite the messages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 * we try to copy to user space. Therefore
414 * the messages are copied in reverse. <manfreds>
415 */
Jesper Juhl40dc5652005-10-30 15:02:46 -0800416 for (i = 0; i < count && !error; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 j = limit-1-i;
418 if (j + log_buf_len < log_end)
419 break;
420 c = LOG_BUF(j);
421 spin_unlock_irq(&logbuf_lock);
422 error = __put_user(c,&buf[count-1-i]);
423 cond_resched();
424 spin_lock_irq(&logbuf_lock);
425 }
426 spin_unlock_irq(&logbuf_lock);
427 if (error)
428 break;
429 error = i;
Jesper Juhl40dc5652005-10-30 15:02:46 -0800430 if (i != count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 int offset = count-error;
432 /* buffer overflow during copy, correct user buffer. */
Jesper Juhl40dc5652005-10-30 15:02:46 -0800433 for (i = 0; i < error; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 if (__get_user(c,&buf[i+offset]) ||
435 __put_user(c,&buf[i])) {
436 error = -EFAULT;
437 break;
438 }
439 cond_resched();
440 }
441 }
442 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -0800443 /* Clear ring buffer */
444 case SYSLOG_ACTION_CLEAR:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 logged_chars = 0;
446 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -0800447 /* Disable logging to console */
448 case SYSLOG_ACTION_CONSOLE_OFF:
Frans Pop1aaad492009-07-06 13:31:48 +0200449 if (saved_console_loglevel == -1)
450 saved_console_loglevel = console_loglevel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 console_loglevel = minimum_console_loglevel;
452 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -0800453 /* Enable logging to console */
454 case SYSLOG_ACTION_CONSOLE_ON:
Frans Pop1aaad492009-07-06 13:31:48 +0200455 if (saved_console_loglevel != -1) {
456 console_loglevel = saved_console_loglevel;
457 saved_console_loglevel = -1;
458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -0800460 /* Set level of messages printed to console */
461 case SYSLOG_ACTION_CONSOLE_LEVEL:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 error = -EINVAL;
463 if (len < 1 || len > 8)
464 goto out;
465 if (len < minimum_console_loglevel)
466 len = minimum_console_loglevel;
467 console_loglevel = len;
Frans Pop1aaad492009-07-06 13:31:48 +0200468 /* Implicitly re-enable logging to console */
469 saved_console_loglevel = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 error = 0;
471 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -0800472 /* Number of chars in the log buffer */
473 case SYSLOG_ACTION_SIZE_UNREAD:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 error = log_end - log_start;
475 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -0800476 /* Size of the log buffer */
477 case SYSLOG_ACTION_SIZE_BUFFER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 error = log_buf_len;
479 break;
480 default:
481 error = -EINVAL;
482 break;
483 }
484out:
485 return error;
486}
487
Heiko Carstens1e7bfb22009-01-14 14:14:29 +0100488SYSCALL_DEFINE3(syslog, int, type, char __user *, buf, int, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
Kees Cook00234592010-02-03 15:36:43 -0800490 return do_syslog(type, buf, len, SYSLOG_FROM_CALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491}
492
Jason Wessel67fc4e02010-05-20 21:04:21 -0500493#ifdef CONFIG_KGDB_KDB
494/* kdb dmesg command needs access to the syslog buffer. do_syslog()
495 * uses locks so it cannot be used during debugging. Just tell kdb
496 * where the start and end of the physical and logical logs are. This
497 * is equivalent to do_syslog(3).
498 */
499void kdb_syslog_data(char *syslog_data[4])
500{
501 syslog_data[0] = log_buf;
502 syslog_data[1] = log_buf + log_buf_len;
503 syslog_data[2] = log_buf + log_end -
504 (logged_chars < log_buf_len ? logged_chars : log_buf_len);
505 syslog_data[3] = log_buf + log_end;
506}
507#endif /* CONFIG_KGDB_KDB */
508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509/*
510 * Call the console drivers on a range of log_buf
511 */
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800512static void __call_console_drivers(unsigned start, unsigned end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
514 struct console *con;
515
Robin Getz4d091612009-07-01 21:08:37 -0400516 for_each_console(con) {
Feng Tangfe3d8ad2011-03-22 16:34:21 -0700517 if (exclusive_console && con != exclusive_console)
518 continue;
Michael Ellerman76a8ad22006-06-25 05:47:40 -0700519 if ((con->flags & CON_ENABLED) && con->write &&
520 (cpu_online(smp_processor_id()) ||
521 (con->flags & CON_ANYTIME)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 con->write(con, &LOG_BUF(start), end - start);
523 }
524}
525
Ingo Molnar792908222006-12-06 20:40:51 -0800526static int __read_mostly ignore_loglevel;
527
Adrian Bunk99eea6a2006-12-22 01:07:00 -0800528static int __init ignore_loglevel_setup(char *str)
Ingo Molnar792908222006-12-06 20:40:51 -0800529{
530 ignore_loglevel = 1;
531 printk(KERN_INFO "debug: ignoring loglevel setting.\n");
532
Ingo Molnarc4772d92008-01-31 22:45:23 +0100533 return 0;
Ingo Molnar792908222006-12-06 20:40:51 -0800534}
535
Ingo Molnarc4772d92008-01-31 22:45:23 +0100536early_param("ignore_loglevel", ignore_loglevel_setup);
Ingo Molnar792908222006-12-06 20:40:51 -0800537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538/*
539 * Write out chars from start to end - 1 inclusive
540 */
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800541static void _call_console_drivers(unsigned start,
542 unsigned end, int msg_log_level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
Ingo Molnar792908222006-12-06 20:40:51 -0800544 if ((msg_log_level < console_loglevel || ignore_loglevel) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 console_drivers && start != end) {
546 if ((start & LOG_BUF_MASK) > (end & LOG_BUF_MASK)) {
547 /* wrapped write */
548 __call_console_drivers(start & LOG_BUF_MASK,
549 log_buf_len);
550 __call_console_drivers(0, end & LOG_BUF_MASK);
551 } else {
552 __call_console_drivers(start, end);
553 }
554 }
555}
556
557/*
Kay Sievers9d90c8d2011-03-13 03:19:51 +0100558 * Parse the syslog header <[0-9]*>. The decimal value represents 32bit, the
559 * lower 3 bit are the log level, the rest are the log facility. In case
560 * userspace passes usual userspace syslog messages to /dev/kmsg or
561 * /dev/ttyprintk, the log prefix might contain the facility. Printk needs
562 * to extract the correct log level for in-kernel processing, and not mangle
563 * the original value.
564 *
565 * If a prefix is found, the length of the prefix is returned. If 'level' is
566 * passed, it will be filled in with the log level without a possible facility
567 * value. If 'special' is passed, the special printk prefix chars are accepted
568 * and returned. If no valid header is found, 0 is returned and the passed
569 * variables are not touched.
570 */
571static size_t log_prefix(const char *p, unsigned int *level, char *special)
572{
573 unsigned int lev = 0;
574 char sp = '\0';
575 size_t len;
576
577 if (p[0] != '<' || !p[1])
578 return 0;
579 if (p[2] == '>') {
580 /* usual single digit level number or special char */
581 switch (p[1]) {
582 case '0' ... '7':
583 lev = p[1] - '0';
584 break;
585 case 'c': /* KERN_CONT */
586 case 'd': /* KERN_DEFAULT */
587 sp = p[1];
588 break;
589 default:
590 return 0;
591 }
592 len = 3;
593 } else {
594 /* multi digit including the level and facility number */
595 char *endp = NULL;
596
597 if (p[1] < '0' && p[1] > '9')
598 return 0;
599
600 lev = (simple_strtoul(&p[1], &endp, 10) & 7);
601 if (endp == NULL || endp[0] != '>')
602 return 0;
603 len = (endp + 1) - p;
604 }
605
606 /* do not accept special char if not asked for */
607 if (sp && !special)
608 return 0;
609
610 if (special) {
611 *special = sp;
612 /* return special char, do not touch level */
613 if (sp)
614 return len;
615 }
616
617 if (level)
618 *level = lev;
619 return len;
620}
621
622/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 * Call the console drivers, asking them to write out
624 * log_buf[start] to log_buf[end - 1].
Torben Hohnac751ef2011-01-25 15:07:35 -0800625 * The console_lock must be held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 */
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800627static void call_console_drivers(unsigned start, unsigned end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800629 unsigned cur_index, start_print;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 static int msg_level = -1;
631
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800632 BUG_ON(((int)(start - end)) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 cur_index = start;
635 start_print = start;
636 while (cur_index != end) {
Kay Sievers9d90c8d2011-03-13 03:19:51 +0100637 if (msg_level < 0 && ((end - cur_index) > 2)) {
638 /* strip log prefix */
639 cur_index += log_prefix(&LOG_BUF(cur_index), &msg_level, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 start_print = cur_index;
641 }
642 while (cur_index != end) {
643 char c = LOG_BUF(cur_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Jesper Juhl40dc5652005-10-30 15:02:46 -0800645 cur_index++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 if (c == '\n') {
647 if (msg_level < 0) {
648 /*
649 * printk() has already given us loglevel tags in
650 * the buffer. This code is here in case the
651 * log buffer has wrapped right round and scribbled
652 * on those tags
653 */
654 msg_level = default_message_loglevel;
655 }
656 _call_console_drivers(start_print, cur_index, msg_level);
657 msg_level = -1;
658 start_print = cur_index;
659 break;
660 }
661 }
662 }
663 _call_console_drivers(start_print, end, msg_level);
664}
665
666static void emit_log_char(char c)
667{
668 LOG_BUF(log_end) = c;
669 log_end++;
670 if (log_end - log_start > log_buf_len)
671 log_start = log_end - log_buf_len;
672 if (log_end - con_start > log_buf_len)
673 con_start = log_end - log_buf_len;
674 if (logged_chars < log_buf_len)
675 logged_chars++;
676}
677
678/*
679 * Zap console related locks when oopsing. Only zap at most once
680 * every 10 seconds, to leave time for slow consoles to print a
681 * full oops.
682 */
683static void zap_locks(void)
684{
685 static unsigned long oops_timestamp;
686
687 if (time_after_eq(jiffies, oops_timestamp) &&
Jesper Juhl40dc5652005-10-30 15:02:46 -0800688 !time_after(jiffies, oops_timestamp + 30 * HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 return;
690
691 oops_timestamp = jiffies;
692
693 /* If a crash is occurring, make sure we can't deadlock */
694 spin_lock_init(&logbuf_lock);
695 /* And make sure that we print immediately */
Thomas Gleixner5b8c4f22010-09-07 14:33:43 +0000696 sema_init(&console_sem, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697}
698
699#if defined(CONFIG_PRINTK_TIME)
700static int printk_time = 1;
701#else
702static int printk_time = 0;
703#endif
Randy Dunlape84845c2007-07-15 23:40:25 -0700704module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Michael Ellerman76a8ad22006-06-25 05:47:40 -0700706/* Check if we have any console registered that can be called early in boot. */
707static int have_callable_console(void)
708{
709 struct console *con;
710
Robin Getz4d091612009-07-01 21:08:37 -0400711 for_each_console(con)
Michael Ellerman76a8ad22006-06-25 05:47:40 -0700712 if (con->flags & CON_ANYTIME)
713 return 1;
714
715 return 0;
716}
717
Martin Waitzddad86c2005-11-13 16:08:14 -0800718/**
719 * printk - print a kernel message
720 * @fmt: format string
721 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800722 * This is printk(). It can be called from any context. We want it to work.
Jesper Juhl40dc5652005-10-30 15:02:46 -0800723 *
Torben Hohnac751ef2011-01-25 15:07:35 -0800724 * We try to grab the console_lock. If we succeed, it's easy - we log the output and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 * call the console drivers. If we fail to get the semaphore we place the output
726 * into the log buffer and return. The current holder of the console_sem will
Torben Hohnac751ef2011-01-25 15:07:35 -0800727 * notice the new output in console_unlock(); and will send it to the
728 * consoles before releasing the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 *
730 * One effect of this deferred printing is that code which calls printk() and
731 * then changes console_loglevel may break. This is because console_loglevel
732 * is inspected when the actual printing occurs.
Martin Waitzddad86c2005-11-13 16:08:14 -0800733 *
734 * See also:
735 * printf(3)
Andi Kleen20036fd2008-10-15 22:02:02 -0700736 *
737 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 */
Matt Mackalld59745c2005-05-01 08:59:02 -0700739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740asmlinkage int printk(const char *fmt, ...)
741{
742 va_list args;
743 int r;
744
Jason Wesseld37d39a2010-05-20 21:04:27 -0500745#ifdef CONFIG_KGDB_KDB
746 if (unlikely(kdb_trap_printk)) {
747 va_start(args, fmt);
748 r = vkdb_printf(fmt, args);
749 va_end(args);
750 return r;
751 }
752#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 va_start(args, fmt);
754 r = vprintk(fmt, args);
755 va_end(args);
756
757 return r;
758}
759
David Howellsfe217732005-09-06 15:16:34 -0700760/* cpu currently holding logbuf_lock */
761static volatile unsigned int printk_cpu = UINT_MAX;
762
Linus Torvalds266c2e02008-03-24 19:25:08 -0700763/*
764 * Can we actually use the console at this time on this cpu?
765 *
766 * Console drivers may assume that per-cpu resources have
767 * been allocated. So unless they're explicitly marked as
768 * being able to cope (CON_ANYTIME) don't call them until
769 * this CPU is officially up.
770 */
771static inline int can_use_console(unsigned int cpu)
772{
773 return cpu_online(cpu) || have_callable_console();
774}
775
776/*
777 * Try to get console ownership to actually show the kernel
778 * messages from a 'printk'. Return true (and with the
Torben Hohnac751ef2011-01-25 15:07:35 -0800779 * console_lock held, and 'console_locked' set) if it
Linus Torvalds266c2e02008-03-24 19:25:08 -0700780 * is successful, false otherwise.
781 *
782 * This gets called with the 'logbuf_lock' spinlock held and
783 * interrupts disabled. It should return with 'lockbuf_lock'
784 * released but interrupts still disabled.
785 */
Torben Hohnac751ef2011-01-25 15:07:35 -0800786static int console_trylock_for_printk(unsigned int cpu)
Namhyung Kim8155c022010-10-26 14:22:47 -0700787 __releases(&logbuf_lock)
Linus Torvalds266c2e02008-03-24 19:25:08 -0700788{
789 int retval = 0;
790
Torben Hohnac751ef2011-01-25 15:07:35 -0800791 if (console_trylock()) {
Linus Torvalds093a07e2008-04-15 13:09:54 -0700792 retval = 1;
793
794 /*
795 * If we can't use the console, we need to release
796 * the console semaphore by hand to avoid flushing
797 * the buffer. We need to hold the console semaphore
798 * in order to do this test safely.
799 */
800 if (!can_use_console(cpu)) {
801 console_locked = 0;
802 up(&console_sem);
803 retval = 0;
804 }
805 }
Linus Torvalds266c2e02008-03-24 19:25:08 -0700806 printk_cpu = UINT_MAX;
807 spin_unlock(&logbuf_lock);
808 return retval;
809}
Tejun Heo3b8945e2008-05-12 21:21:04 +0200810static const char recursion_bug_msg [] =
811 KERN_CRIT "BUG: recent printk recursion!\n";
812static int recursion_bug;
Jiri Kosinaedb123e2008-12-04 12:39:49 +0100813static int new_text_line = 1;
Tejun Heo3b8945e2008-05-12 21:21:04 +0200814static char printk_buf[1024];
Ingo Molnar32a76002008-01-25 21:07:58 +0100815
Dave Youngaf913222009-09-22 16:43:33 -0700816int printk_delay_msec __read_mostly;
817
818static inline void printk_delay(void)
819{
820 if (unlikely(printk_delay_msec)) {
821 int m = printk_delay_msec;
822
823 while (m--) {
824 mdelay(1);
825 touch_nmi_watchdog();
826 }
827 }
828}
829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830asmlinkage int vprintk(const char *fmt, va_list args)
831{
Ingo Molnar32a76002008-01-25 21:07:58 +0100832 int printed_len = 0;
Nick Andrew09159302008-05-12 21:21:04 +0200833 int current_log_level = default_message_loglevel;
Nick Andrewac60ad72008-05-12 21:21:04 +0200834 unsigned long flags;
Ingo Molnar32a76002008-01-25 21:07:58 +0100835 int this_cpu;
836 char *p;
Kay Sievers9d90c8d2011-03-13 03:19:51 +0100837 size_t plen;
838 char special;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700840 boot_delay_msec();
Dave Youngaf913222009-09-22 16:43:33 -0700841 printk_delay();
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700842
David Howellsfe217732005-09-06 15:16:34 -0700843 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 /* This stops the holder of console_sem just where we want him */
Mathieu Desnoyers1efc5da2007-02-10 01:46:29 -0800845 raw_local_irq_save(flags);
Ingo Molnar32a76002008-01-25 21:07:58 +0100846 this_cpu = smp_processor_id();
847
848 /*
849 * Ouch, printk recursed into itself!
850 */
851 if (unlikely(printk_cpu == this_cpu)) {
852 /*
853 * If a crash is occurring during printk() on this CPU,
854 * then try to get the crash message out but make sure
855 * we can't deadlock. Otherwise just return to avoid the
856 * recursion and return - but flag the recursion so that
857 * it can be printed at the next appropriate moment:
858 */
859 if (!oops_in_progress) {
Tejun Heo3b8945e2008-05-12 21:21:04 +0200860 recursion_bug = 1;
Ingo Molnar32a76002008-01-25 21:07:58 +0100861 goto out_restore_irqs;
862 }
863 zap_locks();
864 }
865
Ingo Molnara0f1ccf2006-07-03 00:24:58 -0700866 lockdep_off();
867 spin_lock(&logbuf_lock);
Ingo Molnar32a76002008-01-25 21:07:58 +0100868 printk_cpu = this_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Tejun Heo3b8945e2008-05-12 21:21:04 +0200870 if (recursion_bug) {
871 recursion_bug = 0;
872 strcpy(printk_buf, recursion_bug_msg);
Hiroshi Shimamoto26cc2712008-12-19 10:23:03 -0800873 printed_len = strlen(recursion_bug_msg);
Ingo Molnar32a76002008-01-25 21:07:58 +0100874 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 /* Emit the output into the temporary buffer */
Ingo Molnar32a76002008-01-25 21:07:58 +0100876 printed_len += vscnprintf(printk_buf + printed_len,
Tejun Heocf3680b2008-02-14 10:32:07 +0900877 sizeof(printk_buf) - printed_len, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Tony Lindgren76b39902005-05-09 14:10:26 -0700879#ifdef CONFIG_DEBUG_LL
880 printascii(printk_buf);
881#endif
882
Linus Torvalds5fd29d62009-06-16 10:57:02 -0700883 p = printk_buf;
884
Kay Sievers9d90c8d2011-03-13 03:19:51 +0100885 /* Read log level and handle special printk prefix */
886 plen = log_prefix(p, &current_log_level, &special);
887 if (plen) {
888 p += plen;
889
890 switch (special) {
891 case 'c': /* Strip <c> KERN_CONT, continue line */
892 plen = 0;
893 break;
894 case 'd': /* Strip <d> KERN_DEFAULT, start new line */
895 plen = 0;
896 default:
897 if (!new_text_line) {
898 emit_log_char('\n');
899 new_text_line = 1;
Linus Torvalds5fd29d62009-06-16 10:57:02 -0700900 }
901 }
902 }
903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 /*
Kay Sievers9d90c8d2011-03-13 03:19:51 +0100905 * Copy the output into log_buf. If the caller didn't provide
906 * the appropriate log prefix, we insert them here
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 */
Kay Sievers9d90c8d2011-03-13 03:19:51 +0100908 for (; *p; p++) {
Nick Andrewac60ad72008-05-12 21:21:04 +0200909 if (new_text_line) {
Nick Andrewac60ad72008-05-12 21:21:04 +0200910 new_text_line = 0;
911
Kay Sievers9d90c8d2011-03-13 03:19:51 +0100912 if (plen) {
913 /* Copy original log prefix */
914 int i;
915
916 for (i = 0; i < plen; i++)
917 emit_log_char(printk_buf[i]);
918 printed_len += plen;
919 } else {
920 /* Add log prefix */
921 emit_log_char('<');
922 emit_log_char(current_log_level + '0');
923 emit_log_char('>');
924 printed_len += 3;
925 }
926
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 if (printk_time) {
Kay Sievers9d90c8d2011-03-13 03:19:51 +0100928 /* Add the current time stamp */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 char tbuf[50], *tp;
930 unsigned tlen;
931 unsigned long long t;
932 unsigned long nanosec_rem;
933
Ingo Molnar326e96b2008-01-27 08:03:54 +0100934 t = cpu_clock(printk_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 nanosec_rem = do_div(t, 1000000000);
Nick Andrewac60ad72008-05-12 21:21:04 +0200936 tlen = sprintf(tbuf, "[%5lu.%06lu] ",
937 (unsigned long) t,
938 nanosec_rem / 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
940 for (tp = tbuf; tp < tbuf + tlen; tp++)
941 emit_log_char(*tp);
Guillaume Chazarain025510c2006-01-08 01:02:41 -0800942 printed_len += tlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 }
Nick Andrewac60ad72008-05-12 21:21:04 +0200944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 if (!*p)
946 break;
947 }
Nick Andrewac60ad72008-05-12 21:21:04 +0200948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 emit_log_char(*p);
950 if (*p == '\n')
Nick Andrewac60ad72008-05-12 21:21:04 +0200951 new_text_line = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 }
953
Linus Torvalds266c2e02008-03-24 19:25:08 -0700954 /*
955 * Try to acquire and then immediately release the
956 * console semaphore. The release will do all the
957 * actual magic (print out buffers, wake up klogd,
958 * etc).
959 *
Torben Hohnac751ef2011-01-25 15:07:35 -0800960 * The console_trylock_for_printk() function
Linus Torvalds266c2e02008-03-24 19:25:08 -0700961 * will release 'logbuf_lock' regardless of whether it
962 * actually gets the semaphore or not.
963 */
Torben Hohnac751ef2011-01-25 15:07:35 -0800964 if (console_trylock_for_printk(this_cpu))
965 console_unlock();
Michael Ellerman76a8ad22006-06-25 05:47:40 -0700966
Linus Torvalds266c2e02008-03-24 19:25:08 -0700967 lockdep_on();
Ingo Molnar32a76002008-01-25 21:07:58 +0100968out_restore_irqs:
Linus Torvalds266c2e02008-03-24 19:25:08 -0700969 raw_local_irq_restore(flags);
Michael Ellerman76a8ad22006-06-25 05:47:40 -0700970
David Howellsfe217732005-09-06 15:16:34 -0700971 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 return printed_len;
973}
974EXPORT_SYMBOL(printk);
975EXPORT_SYMBOL(vprintk);
976
Matt Mackalld59745c2005-05-01 08:59:02 -0700977#else
978
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800979static void call_console_drivers(unsigned start, unsigned end)
Jesper Juhl40dc5652005-10-30 15:02:46 -0800980{
981}
Matt Mackalld59745c2005-05-01 08:59:02 -0700982
983#endif
984
Samuel Thibaultf7511d52008-04-30 00:54:51 -0700985static int __add_preferred_console(char *name, int idx, char *options,
986 char *brl_options)
987{
988 struct console_cmdline *c;
989 int i;
990
991 /*
992 * See if this tty is not yet registered, and
993 * if we have a slot free.
994 */
995 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
996 if (strcmp(console_cmdline[i].name, name) == 0 &&
997 console_cmdline[i].index == idx) {
998 if (!brl_options)
999 selected_console = i;
1000 return 0;
1001 }
1002 if (i == MAX_CMDLINECONSOLES)
1003 return -E2BIG;
1004 if (!brl_options)
1005 selected_console = i;
1006 c = &console_cmdline[i];
1007 strlcpy(c->name, name, sizeof(c->name));
1008 c->options = options;
1009#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1010 c->brl_options = brl_options;
1011#endif
1012 c->index = idx;
1013 return 0;
1014}
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001015/*
1016 * Set up a list of consoles. Called from init/main.c
1017 */
1018static int __init console_setup(char *str)
1019{
Yinghai Lueaa944a2007-07-15 23:37:27 -07001020 char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for index */
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001021 char *s, *options, *brl_options = NULL;
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001022 int idx;
1023
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001024#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1025 if (!memcmp(str, "brl,", 4)) {
1026 brl_options = "";
1027 str += 4;
1028 } else if (!memcmp(str, "brl=", 4)) {
1029 brl_options = str + 4;
1030 str = strchr(brl_options, ',');
1031 if (!str) {
1032 printk(KERN_ERR "need port name after brl=\n");
1033 return 1;
1034 }
1035 *(str++) = 0;
1036 }
1037#endif
1038
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001039 /*
1040 * Decode str into name, index, options.
1041 */
1042 if (str[0] >= '0' && str[0] <= '9') {
Yinghai Lueaa944a2007-07-15 23:37:27 -07001043 strcpy(buf, "ttyS");
1044 strncpy(buf + 4, str, sizeof(buf) - 5);
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001045 } else {
Yinghai Lueaa944a2007-07-15 23:37:27 -07001046 strncpy(buf, str, sizeof(buf) - 1);
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001047 }
Yinghai Lueaa944a2007-07-15 23:37:27 -07001048 buf[sizeof(buf) - 1] = 0;
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001049 if ((options = strchr(str, ',')) != NULL)
1050 *(options++) = 0;
1051#ifdef __sparc__
1052 if (!strcmp(str, "ttya"))
Yinghai Lueaa944a2007-07-15 23:37:27 -07001053 strcpy(buf, "ttyS0");
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001054 if (!strcmp(str, "ttyb"))
Yinghai Lueaa944a2007-07-15 23:37:27 -07001055 strcpy(buf, "ttyS1");
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001056#endif
Yinghai Lueaa944a2007-07-15 23:37:27 -07001057 for (s = buf; *s; s++)
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001058 if ((*s >= '0' && *s <= '9') || *s == ',')
1059 break;
1060 idx = simple_strtoul(s, NULL, 10);
1061 *s = 0;
1062
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001063 __add_preferred_console(buf, idx, options, brl_options);
Markus Armbruster9e124fe2008-05-26 23:31:07 +01001064 console_set_on_cmdline = 1;
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001065 return 1;
1066}
1067__setup("console=", console_setup);
1068
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069/**
Matt Mackall3c0547b2005-05-16 21:53:47 -07001070 * add_preferred_console - add a device to the list of preferred consoles.
Martin Waitzddad86c2005-11-13 16:08:14 -08001071 * @name: device name
1072 * @idx: device index
1073 * @options: options for this console
Matt Mackall3c0547b2005-05-16 21:53:47 -07001074 *
1075 * The last preferred console added will be used for kernel messages
1076 * and stdin/out/err for init. Normally this is used by console_setup
1077 * above to handle user-supplied console arguments; however it can also
1078 * be used by arch-specific code either to override the user or more
1079 * commonly to provide a default console (ie from PROM variables) when
1080 * the user has not supplied one.
1081 */
David S. Millerfb445ee2007-12-29 01:19:49 -08001082int add_preferred_console(char *name, int idx, char *options)
Matt Mackall3c0547b2005-05-16 21:53:47 -07001083{
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001084 return __add_preferred_console(name, idx, options, NULL);
Matt Mackall3c0547b2005-05-16 21:53:47 -07001085}
1086
Daniel Ritzb6b1d872007-08-03 16:07:43 +02001087int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options)
Yinghai Lu18a8bd92007-07-15 23:37:59 -07001088{
1089 struct console_cmdline *c;
1090 int i;
1091
1092 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
1093 if (strcmp(console_cmdline[i].name, name) == 0 &&
1094 console_cmdline[i].index == idx) {
1095 c = &console_cmdline[i];
Markus Armbrusterf7352952008-04-30 00:54:52 -07001096 strlcpy(c->name, name_new, sizeof(c->name));
Yinghai Lu18a8bd92007-07-15 23:37:59 -07001097 c->name[sizeof(c->name) - 1] = 0;
1098 c->options = options;
1099 c->index = idx_new;
1100 return i;
1101 }
1102 /* not found */
1103 return -1;
1104}
1105
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07001106int console_suspend_enabled = 1;
1107EXPORT_SYMBOL(console_suspend_enabled);
1108
1109static int __init console_suspend_disable(char *str)
1110{
1111 console_suspend_enabled = 0;
1112 return 1;
1113}
1114__setup("no_console_suspend", console_suspend_disable);
1115
Matt Mackall3c0547b2005-05-16 21:53:47 -07001116/**
Linus Torvalds557240b2006-06-19 18:16:01 -07001117 * suspend_console - suspend the console subsystem
1118 *
1119 * This disables printk() while we go into suspend states
1120 */
1121void suspend_console(void)
1122{
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07001123 if (!console_suspend_enabled)
1124 return;
Pavel Machek0d630812008-07-23 21:28:32 -07001125 printk("Suspending console(s) (use no_console_suspend to debug)\n");
Torben Hohnac751ef2011-01-25 15:07:35 -08001126 console_lock();
Linus Torvalds557240b2006-06-19 18:16:01 -07001127 console_suspended = 1;
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01001128 up(&console_sem);
Linus Torvalds557240b2006-06-19 18:16:01 -07001129}
1130
1131void resume_console(void)
1132{
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07001133 if (!console_suspend_enabled)
1134 return;
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01001135 down(&console_sem);
Linus Torvalds557240b2006-06-19 18:16:01 -07001136 console_suspended = 0;
Torben Hohnac751ef2011-01-25 15:07:35 -08001137 console_unlock();
Linus Torvalds557240b2006-06-19 18:16:01 -07001138}
1139
1140/**
Kevin Cernekee034260d2010-06-03 22:11:25 -07001141 * console_cpu_notify - print deferred console messages after CPU hotplug
1142 * @self: notifier struct
1143 * @action: CPU hotplug event
1144 * @hcpu: unused
1145 *
1146 * If printk() is called from a CPU that is not online yet, the messages
1147 * will be spooled but will not show up on the console. This function is
1148 * called when a new CPU comes online (or fails to come up), and ensures
1149 * that any such output gets printed.
1150 */
1151static int __cpuinit console_cpu_notify(struct notifier_block *self,
1152 unsigned long action, void *hcpu)
1153{
1154 switch (action) {
1155 case CPU_ONLINE:
1156 case CPU_DEAD:
1157 case CPU_DYING:
1158 case CPU_DOWN_FAILED:
1159 case CPU_UP_CANCELED:
Torben Hohnac751ef2011-01-25 15:07:35 -08001160 console_lock();
1161 console_unlock();
Kevin Cernekee034260d2010-06-03 22:11:25 -07001162 }
1163 return NOTIFY_OK;
1164}
1165
1166/**
Torben Hohnac751ef2011-01-25 15:07:35 -08001167 * console_lock - lock the console system for exclusive use.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 *
Torben Hohnac751ef2011-01-25 15:07:35 -08001169 * Acquires a lock which guarantees that the caller has
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 * exclusive access to the console system and the console_drivers list.
1171 *
1172 * Can sleep, returns nothing.
1173 */
Torben Hohnac751ef2011-01-25 15:07:35 -08001174void console_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175{
Eric Sesterhenn8abd8e22006-04-01 01:21:17 +02001176 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 down(&console_sem);
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01001178 if (console_suspended)
1179 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 console_locked = 1;
1181 console_may_schedule = 1;
1182}
Torben Hohnac751ef2011-01-25 15:07:35 -08001183EXPORT_SYMBOL(console_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
Torben Hohnac751ef2011-01-25 15:07:35 -08001185/**
1186 * console_trylock - try to lock the console system for exclusive use.
1187 *
1188 * Tried to acquire a lock which guarantees that the caller has
1189 * exclusive access to the console system and the console_drivers list.
1190 *
1191 * returns 1 on success, and 0 on failure to acquire the lock.
1192 */
1193int console_trylock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194{
1195 if (down_trylock(&console_sem))
Torben Hohnac751ef2011-01-25 15:07:35 -08001196 return 0;
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01001197 if (console_suspended) {
1198 up(&console_sem);
Torben Hohnac751ef2011-01-25 15:07:35 -08001199 return 0;
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01001200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 console_locked = 1;
1202 console_may_schedule = 0;
Torben Hohnac751ef2011-01-25 15:07:35 -08001203 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204}
Torben Hohnac751ef2011-01-25 15:07:35 -08001205EXPORT_SYMBOL(console_trylock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
1207int is_console_locked(void)
1208{
1209 return console_locked;
1210}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Peter Zijlstrab845b512008-08-08 21:47:09 +02001212static DEFINE_PER_CPU(int, printk_pending);
1213
1214void printk_tick(void)
1215{
Eric Dumazet40dc11f2010-11-26 17:22:16 +01001216 if (__this_cpu_read(printk_pending)) {
1217 __this_cpu_write(printk_pending, 0);
Peter Zijlstrab845b512008-08-08 21:47:09 +02001218 wake_up_interruptible(&log_wait);
1219 }
1220}
1221
1222int printk_needs_cpu(int cpu)
1223{
Eric Dumazet40dc11f2010-11-26 17:22:16 +01001224 if (cpu_is_offline(cpu))
Heiko Carstens61ab2542010-11-26 13:00:59 +01001225 printk_tick();
Eric Dumazet40dc11f2010-11-26 17:22:16 +01001226 return __this_cpu_read(printk_pending);
Peter Zijlstrab845b512008-08-08 21:47:09 +02001227}
1228
Kirill Korotaeve3e8a752007-02-10 01:46:19 -08001229void wake_up_klogd(void)
1230{
Peter Zijlstrab845b512008-08-08 21:47:09 +02001231 if (waitqueue_active(&log_wait))
Heiko Carstens49f41382010-11-26 13:42:47 +01001232 this_cpu_write(printk_pending, 1);
Kirill Korotaeve3e8a752007-02-10 01:46:19 -08001233}
1234
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235/**
Torben Hohnac751ef2011-01-25 15:07:35 -08001236 * console_unlock - unlock the console system
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 *
Torben Hohnac751ef2011-01-25 15:07:35 -08001238 * Releases the console_lock which the caller holds on the console system
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 * and the console driver list.
1240 *
Torben Hohnac751ef2011-01-25 15:07:35 -08001241 * While the console_lock was held, console output may have been buffered
1242 * by printk(). If this is the case, console_unlock(); emits
1243 * the output prior to releasing the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 *
1245 * If there is output waiting for klogd, we wake it up.
1246 *
Torben Hohnac751ef2011-01-25 15:07:35 -08001247 * console_unlock(); may be called from any context.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 */
Torben Hohnac751ef2011-01-25 15:07:35 -08001249void console_unlock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
1251 unsigned long flags;
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -08001252 unsigned _con_start, _log_end;
1253 unsigned wake_klogd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Linus Torvalds557240b2006-06-19 18:16:01 -07001255 if (console_suspended) {
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01001256 up(&console_sem);
Linus Torvalds557240b2006-06-19 18:16:01 -07001257 return;
1258 }
Antonino A. Daplas78944e52006-08-05 12:14:16 -07001259
1260 console_may_schedule = 0;
1261
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 for ( ; ; ) {
1263 spin_lock_irqsave(&logbuf_lock, flags);
1264 wake_klogd |= log_start - log_end;
1265 if (con_start == log_end)
1266 break; /* Nothing to print */
1267 _con_start = con_start;
1268 _log_end = log_end;
1269 con_start = log_end; /* Flush */
1270 spin_unlock(&logbuf_lock);
Steven Rostedt81d68a92008-05-12 21:20:42 +02001271 stop_critical_timings(); /* don't trace print latency */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 call_console_drivers(_con_start, _log_end);
Steven Rostedt81d68a92008-05-12 21:20:42 +02001273 start_critical_timings();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 local_irq_restore(flags);
1275 }
1276 console_locked = 0;
Feng Tangfe3d8ad2011-03-22 16:34:21 -07001277
1278 /* Release the exclusive_console once it is used */
1279 if (unlikely(exclusive_console))
1280 exclusive_console = NULL;
1281
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 up(&console_sem);
1283 spin_unlock_irqrestore(&logbuf_lock, flags);
Kirill Korotaeve3e8a752007-02-10 01:46:19 -08001284 if (wake_klogd)
1285 wake_up_klogd();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286}
Torben Hohnac751ef2011-01-25 15:07:35 -08001287EXPORT_SYMBOL(console_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
Martin Waitzddad86c2005-11-13 16:08:14 -08001289/**
1290 * console_conditional_schedule - yield the CPU if required
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 *
1292 * If the console code is currently allowed to sleep, and
1293 * if this CPU should yield the CPU to another task, do
1294 * so here.
1295 *
Torben Hohnac751ef2011-01-25 15:07:35 -08001296 * Must be called within console_lock();.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 */
1298void __sched console_conditional_schedule(void)
1299{
1300 if (console_may_schedule)
1301 cond_resched();
1302}
1303EXPORT_SYMBOL(console_conditional_schedule);
1304
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305void console_unblank(void)
1306{
1307 struct console *c;
1308
1309 /*
1310 * console_unblank can no longer be called in interrupt context unless
1311 * oops_in_progress is set to 1..
1312 */
1313 if (oops_in_progress) {
1314 if (down_trylock(&console_sem) != 0)
1315 return;
1316 } else
Torben Hohnac751ef2011-01-25 15:07:35 -08001317 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
1319 console_locked = 1;
1320 console_may_schedule = 0;
Robin Getz4d091612009-07-01 21:08:37 -04001321 for_each_console(c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 if ((c->flags & CON_ENABLED) && c->unblank)
1323 c->unblank();
Torben Hohnac751ef2011-01-25 15:07:35 -08001324 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
1327/*
1328 * Return the console tty driver structure and its associated index
1329 */
1330struct tty_driver *console_device(int *index)
1331{
1332 struct console *c;
1333 struct tty_driver *driver = NULL;
1334
Torben Hohnac751ef2011-01-25 15:07:35 -08001335 console_lock();
Robin Getz4d091612009-07-01 21:08:37 -04001336 for_each_console(c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 if (!c->device)
1338 continue;
1339 driver = c->device(c, index);
1340 if (driver)
1341 break;
1342 }
Torben Hohnac751ef2011-01-25 15:07:35 -08001343 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 return driver;
1345}
1346
1347/*
1348 * Prevent further output on the passed console device so that (for example)
1349 * serial drivers can disable console output before suspending a port, and can
1350 * re-enable output afterwards.
1351 */
1352void console_stop(struct console *console)
1353{
Torben Hohnac751ef2011-01-25 15:07:35 -08001354 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 console->flags &= ~CON_ENABLED;
Torben Hohnac751ef2011-01-25 15:07:35 -08001356 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357}
1358EXPORT_SYMBOL(console_stop);
1359
1360void console_start(struct console *console)
1361{
Torben Hohnac751ef2011-01-25 15:07:35 -08001362 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 console->flags |= CON_ENABLED;
Torben Hohnac751ef2011-01-25 15:07:35 -08001364 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365}
1366EXPORT_SYMBOL(console_start);
1367
Fabio M. Di Nitto7bf69392011-03-22 16:34:20 -07001368static int __read_mostly keep_bootcon;
1369
1370static int __init keep_bootcon_setup(char *str)
1371{
1372 keep_bootcon = 1;
1373 printk(KERN_INFO "debug: skip boot console de-registration.\n");
1374
1375 return 0;
1376}
1377
1378early_param("keep_bootcon", keep_bootcon_setup);
1379
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380/*
1381 * The console driver calls this routine during kernel initialization
1382 * to register the console printing procedure with printk() and to
1383 * print any messages that were printed by the kernel before the
1384 * console driver was initialized.
Robin Getz4d091612009-07-01 21:08:37 -04001385 *
1386 * This can happen pretty early during the boot process (because of
1387 * early_printk) - sometimes before setup_arch() completes - be careful
1388 * of what kernel features are used - they may not be initialised yet.
1389 *
1390 * There are two types of consoles - bootconsoles (early_printk) and
1391 * "real" consoles (everything which is not a bootconsole) which are
1392 * handled differently.
1393 * - Any number of bootconsoles can be registered at any time.
1394 * - As soon as a "real" console is registered, all bootconsoles
1395 * will be unregistered automatically.
1396 * - Once a "real" console is registered, any attempt to register a
1397 * bootconsoles will be rejected
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 */
Robin Getz4d091612009-07-01 21:08:37 -04001399void register_console(struct console *newcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400{
Jesper Juhl40dc5652005-10-30 15:02:46 -08001401 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 unsigned long flags;
Robin Getz4d091612009-07-01 21:08:37 -04001403 struct console *bcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Robin Getz4d091612009-07-01 21:08:37 -04001405 /*
1406 * before we register a new CON_BOOT console, make sure we don't
1407 * already have a valid console
1408 */
1409 if (console_drivers && newcon->flags & CON_BOOT) {
1410 /* find the last or real console */
1411 for_each_console(bcon) {
1412 if (!(bcon->flags & CON_BOOT)) {
1413 printk(KERN_INFO "Too late to register bootconsole %s%d\n",
1414 newcon->name, newcon->index);
1415 return;
1416 }
1417 }
Gerd Hoffmann69331af2007-05-08 00:26:49 -07001418 }
1419
Robin Getz4d091612009-07-01 21:08:37 -04001420 if (console_drivers && console_drivers->flags & CON_BOOT)
1421 bcon = console_drivers;
1422
1423 if (preferred_console < 0 || bcon || !console_drivers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 preferred_console = selected_console;
1425
Robin Getz4d091612009-07-01 21:08:37 -04001426 if (newcon->early_setup)
1427 newcon->early_setup();
Yinghai Lu18a8bd92007-07-15 23:37:59 -07001428
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 /*
1430 * See if we want to use this console driver. If we
1431 * didn't select a console we take the first one
1432 * that registers here.
1433 */
1434 if (preferred_console < 0) {
Robin Getz4d091612009-07-01 21:08:37 -04001435 if (newcon->index < 0)
1436 newcon->index = 0;
1437 if (newcon->setup == NULL ||
1438 newcon->setup(newcon, NULL) == 0) {
1439 newcon->flags |= CON_ENABLED;
1440 if (newcon->device) {
1441 newcon->flags |= CON_CONSDEV;
Jan Kiszkacd3a1b82008-05-12 21:21:04 +02001442 preferred_console = 0;
1443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 }
1445 }
1446
1447 /*
1448 * See if this console matches one we selected on
1449 * the command line.
1450 */
Jesper Juhl40dc5652005-10-30 15:02:46 -08001451 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0];
1452 i++) {
Robin Getz4d091612009-07-01 21:08:37 -04001453 if (strcmp(console_cmdline[i].name, newcon->name) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 continue;
Robin Getz4d091612009-07-01 21:08:37 -04001455 if (newcon->index >= 0 &&
1456 newcon->index != console_cmdline[i].index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 continue;
Robin Getz4d091612009-07-01 21:08:37 -04001458 if (newcon->index < 0)
1459 newcon->index = console_cmdline[i].index;
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001460#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1461 if (console_cmdline[i].brl_options) {
Robin Getz4d091612009-07-01 21:08:37 -04001462 newcon->flags |= CON_BRL;
1463 braille_register_console(newcon,
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001464 console_cmdline[i].index,
1465 console_cmdline[i].options,
1466 console_cmdline[i].brl_options);
1467 return;
1468 }
1469#endif
Robin Getz4d091612009-07-01 21:08:37 -04001470 if (newcon->setup &&
1471 newcon->setup(newcon, console_cmdline[i].options) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 break;
Robin Getz4d091612009-07-01 21:08:37 -04001473 newcon->flags |= CON_ENABLED;
1474 newcon->index = console_cmdline[i].index;
Greg Edwardsab4af032005-06-23 00:09:05 -07001475 if (i == selected_console) {
Robin Getz4d091612009-07-01 21:08:37 -04001476 newcon->flags |= CON_CONSDEV;
Greg Edwardsab4af032005-06-23 00:09:05 -07001477 preferred_console = selected_console;
1478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 break;
1480 }
1481
Robin Getz4d091612009-07-01 21:08:37 -04001482 if (!(newcon->flags & CON_ENABLED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 return;
1484
Robin Getz8259cf42009-07-09 13:08:37 -04001485 /*
1486 * If we have a bootconsole, and are switching to a real console,
1487 * don't print everything out again, since when the boot console, and
1488 * the real console are the same physical device, it's annoying to
1489 * see the beginning boot messages twice
1490 */
1491 if (bcon && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV))
Robin Getz4d091612009-07-01 21:08:37 -04001492 newcon->flags &= ~CON_PRINTBUFFER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
1494 /*
1495 * Put this console in the list - keep the
1496 * preferred driver at the head of the list.
1497 */
Torben Hohnac751ef2011-01-25 15:07:35 -08001498 console_lock();
Robin Getz4d091612009-07-01 21:08:37 -04001499 if ((newcon->flags & CON_CONSDEV) || console_drivers == NULL) {
1500 newcon->next = console_drivers;
1501 console_drivers = newcon;
1502 if (newcon->next)
1503 newcon->next->flags &= ~CON_CONSDEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 } else {
Robin Getz4d091612009-07-01 21:08:37 -04001505 newcon->next = console_drivers->next;
1506 console_drivers->next = newcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 }
Robin Getz4d091612009-07-01 21:08:37 -04001508 if (newcon->flags & CON_PRINTBUFFER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 /*
Torben Hohnac751ef2011-01-25 15:07:35 -08001510 * console_unlock(); will print out the buffered messages
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 * for us.
1512 */
1513 spin_lock_irqsave(&logbuf_lock, flags);
1514 con_start = log_start;
1515 spin_unlock_irqrestore(&logbuf_lock, flags);
Feng Tangfe3d8ad2011-03-22 16:34:21 -07001516 /*
1517 * We're about to replay the log buffer. Only do this to the
1518 * just-registered console to avoid excessive message spam to
1519 * the already-registered consoles.
1520 */
1521 exclusive_console = newcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 }
Torben Hohnac751ef2011-01-25 15:07:35 -08001523 console_unlock();
Kay Sieversfbc92a32010-12-01 18:51:05 +01001524 console_sysfs_notify();
Robin Getz8259cf42009-07-09 13:08:37 -04001525
1526 /*
1527 * By unregistering the bootconsoles after we enable the real console
1528 * we get the "console xxx enabled" message on all the consoles -
1529 * boot consoles, real consoles, etc - this is to ensure that end
1530 * users know there might be something in the kernel's log buffer that
1531 * went to the bootconsole (that they do not see on the real console)
1532 */
Fabio M. Di Nitto7bf69392011-03-22 16:34:20 -07001533 if (bcon &&
1534 ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV) &&
1535 !keep_bootcon) {
Robin Getz8259cf42009-07-09 13:08:37 -04001536 /* we need to iterate through twice, to make sure we print
1537 * everything out, before we unregister the console(s)
1538 */
1539 printk(KERN_INFO "console [%s%d] enabled, bootconsole disabled\n",
1540 newcon->name, newcon->index);
1541 for_each_console(bcon)
1542 if (bcon->flags & CON_BOOT)
1543 unregister_console(bcon);
1544 } else {
1545 printk(KERN_INFO "%sconsole [%s%d] enabled\n",
1546 (newcon->flags & CON_BOOT) ? "boot" : "" ,
1547 newcon->name, newcon->index);
1548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549}
1550EXPORT_SYMBOL(register_console);
1551
Jesper Juhl40dc5652005-10-30 15:02:46 -08001552int unregister_console(struct console *console)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553{
Jesper Juhl40dc5652005-10-30 15:02:46 -08001554 struct console *a, *b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 int res = 1;
1556
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001557#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1558 if (console->flags & CON_BRL)
1559 return braille_unregister_console(console);
1560#endif
1561
Torben Hohnac751ef2011-01-25 15:07:35 -08001562 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 if (console_drivers == console) {
1564 console_drivers=console->next;
1565 res = 0;
Benjamin Herrenschmidte9b15b52005-11-23 13:37:44 -08001566 } else if (console_drivers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 for (a=console_drivers->next, b=console_drivers ;
1568 a; b=a, a=b->next) {
1569 if (a == console) {
1570 b->next = a->next;
1571 res = 0;
1572 break;
Jesper Juhl40dc5652005-10-30 15:02:46 -08001573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 }
1575 }
Jesper Juhl40dc5652005-10-30 15:02:46 -08001576
Gerd Hoffmann69331af2007-05-08 00:26:49 -07001577 /*
Greg Edwardsab4af032005-06-23 00:09:05 -07001578 * If this isn't the last console and it has CON_CONSDEV set, we
1579 * need to set it on the next preferred console.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 */
Gerd Hoffmann69331af2007-05-08 00:26:49 -07001581 if (console_drivers != NULL && console->flags & CON_CONSDEV)
Greg Edwardsab4af032005-06-23 00:09:05 -07001582 console_drivers->flags |= CON_CONSDEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
Torben Hohnac751ef2011-01-25 15:07:35 -08001584 console_unlock();
Kay Sieversfbc92a32010-12-01 18:51:05 +01001585 console_sysfs_notify();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 return res;
1587}
1588EXPORT_SYMBOL(unregister_console);
Matt Mackalld59745c2005-05-01 08:59:02 -07001589
Kevin Cernekee034260d2010-06-03 22:11:25 -07001590static int __init printk_late_init(void)
Robin Getz0c5564b2007-08-20 15:22:47 -04001591{
Robin Getz4d091612009-07-01 21:08:37 -04001592 struct console *con;
1593
1594 for_each_console(con) {
1595 if (con->flags & CON_BOOT) {
Robin Getzcb00e992007-08-21 23:14:58 -04001596 printk(KERN_INFO "turn off boot console %s%d\n",
Robin Getz4d091612009-07-01 21:08:37 -04001597 con->name, con->index);
Sonic Zhang42c2c8c2009-08-06 15:58:11 -07001598 unregister_console(con);
Robin Getzcb00e992007-08-21 23:14:58 -04001599 }
Robin Getz0c5564b2007-08-20 15:22:47 -04001600 }
Kevin Cernekee034260d2010-06-03 22:11:25 -07001601 hotcpu_notifier(console_cpu_notify, 0);
Robin Getz0c5564b2007-08-20 15:22:47 -04001602 return 0;
1603}
Kevin Cernekee034260d2010-06-03 22:11:25 -07001604late_initcall(printk_late_init);
Robin Getz0c5564b2007-08-20 15:22:47 -04001605
Joe Perches7ef3d2f2008-02-08 04:21:25 -08001606#if defined CONFIG_PRINTK
Dave Young717115e2008-07-25 01:45:58 -07001607
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608/*
1609 * printk rate limiting, lifted from the networking subsystem.
1610 *
Uwe Kleine-König641de9d2008-07-29 22:33:38 -07001611 * This enforces a rate limit: not more than 10 kernel messages
1612 * every 5s to make a denial-of-service attack impossible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 */
Uwe Kleine-König641de9d2008-07-29 22:33:38 -07001614DEFINE_RATELIMIT_STATE(printk_ratelimit_state, 5 * HZ, 10);
1615
Christian Borntraeger5c828712009-10-23 14:58:11 +02001616int __printk_ratelimit(const char *func)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617{
Christian Borntraeger5c828712009-10-23 14:58:11 +02001618 return ___ratelimit(&printk_ratelimit_state, func);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619}
Christian Borntraeger5c828712009-10-23 14:58:11 +02001620EXPORT_SYMBOL(__printk_ratelimit);
Andrew Mortonf46c4832006-11-02 22:07:16 -08001621
1622/**
1623 * printk_timed_ratelimit - caller-controlled printk ratelimiting
1624 * @caller_jiffies: pointer to caller's state
1625 * @interval_msecs: minimum interval between prints
1626 *
1627 * printk_timed_ratelimit() returns true if more than @interval_msecs
1628 * milliseconds have elapsed since the last time printk_timed_ratelimit()
1629 * returned true.
1630 */
1631bool printk_timed_ratelimit(unsigned long *caller_jiffies,
1632 unsigned int interval_msecs)
1633{
Guillaume Knispelf2d28a22009-03-17 16:18:42 +01001634 if (*caller_jiffies == 0
1635 || !time_in_range(jiffies, *caller_jiffies,
1636 *caller_jiffies
1637 + msecs_to_jiffies(interval_msecs))) {
1638 *caller_jiffies = jiffies;
Andrew Mortonf46c4832006-11-02 22:07:16 -08001639 return true;
1640 }
1641 return false;
1642}
1643EXPORT_SYMBOL(printk_timed_ratelimit);
Simon Kagstrom456b5652009-10-16 14:09:18 +02001644
1645static DEFINE_SPINLOCK(dump_list_lock);
1646static LIST_HEAD(dump_list);
1647
1648/**
1649 * kmsg_dump_register - register a kernel log dumper.
Randy Dunlap64855362009-12-17 15:27:27 -08001650 * @dumper: pointer to the kmsg_dumper structure
Simon Kagstrom456b5652009-10-16 14:09:18 +02001651 *
1652 * Adds a kernel log dumper to the system. The dump callback in the
1653 * structure will be called when the kernel oopses or panics and must be
1654 * set. Returns zero on success and %-EINVAL or %-EBUSY otherwise.
1655 */
1656int kmsg_dump_register(struct kmsg_dumper *dumper)
1657{
1658 unsigned long flags;
1659 int err = -EBUSY;
1660
1661 /* The dump callback needs to be set */
1662 if (!dumper->dump)
1663 return -EINVAL;
1664
1665 spin_lock_irqsave(&dump_list_lock, flags);
1666 /* Don't allow registering multiple times */
1667 if (!dumper->registered) {
1668 dumper->registered = 1;
Huang Yingfb842b02011-01-12 16:59:43 -08001669 list_add_tail_rcu(&dumper->list, &dump_list);
Simon Kagstrom456b5652009-10-16 14:09:18 +02001670 err = 0;
1671 }
1672 spin_unlock_irqrestore(&dump_list_lock, flags);
1673
1674 return err;
1675}
1676EXPORT_SYMBOL_GPL(kmsg_dump_register);
1677
1678/**
1679 * kmsg_dump_unregister - unregister a kmsg dumper.
Randy Dunlap64855362009-12-17 15:27:27 -08001680 * @dumper: pointer to the kmsg_dumper structure
Simon Kagstrom456b5652009-10-16 14:09:18 +02001681 *
1682 * Removes a dump device from the system. Returns zero on success and
1683 * %-EINVAL otherwise.
1684 */
1685int kmsg_dump_unregister(struct kmsg_dumper *dumper)
1686{
1687 unsigned long flags;
1688 int err = -EINVAL;
1689
1690 spin_lock_irqsave(&dump_list_lock, flags);
1691 if (dumper->registered) {
1692 dumper->registered = 0;
Huang Yingfb842b02011-01-12 16:59:43 -08001693 list_del_rcu(&dumper->list);
Simon Kagstrom456b5652009-10-16 14:09:18 +02001694 err = 0;
1695 }
1696 spin_unlock_irqrestore(&dump_list_lock, flags);
Huang Yingfb842b02011-01-12 16:59:43 -08001697 synchronize_rcu();
Simon Kagstrom456b5652009-10-16 14:09:18 +02001698
1699 return err;
1700}
1701EXPORT_SYMBOL_GPL(kmsg_dump_unregister);
1702
Simon Kagstrom456b5652009-10-16 14:09:18 +02001703/**
1704 * kmsg_dump - dump kernel log to kernel message dumpers.
1705 * @reason: the reason (oops, panic etc) for dumping
1706 *
1707 * Iterate through each of the dump devices and call the oops/panic
1708 * callbacks with the log buffer.
1709 */
1710void kmsg_dump(enum kmsg_dump_reason reason)
1711{
1712 unsigned long end;
1713 unsigned chars;
1714 struct kmsg_dumper *dumper;
1715 const char *s1, *s2;
1716 unsigned long l1, l2;
1717 unsigned long flags;
1718
1719 /* Theoretically, the log could move on after we do this, but
1720 there's not a lot we can do about that. The new messages
1721 will overwrite the start of what we dump. */
1722 spin_lock_irqsave(&logbuf_lock, flags);
1723 end = log_end & LOG_BUF_MASK;
1724 chars = logged_chars;
1725 spin_unlock_irqrestore(&logbuf_lock, flags);
1726
Andi Kleen8c4af382010-08-09 17:20:36 -07001727 if (chars > end) {
1728 s1 = log_buf + log_buf_len - chars + end;
1729 l1 = chars - end;
Simon Kagstrom456b5652009-10-16 14:09:18 +02001730
1731 s2 = log_buf;
1732 l2 = end;
1733 } else {
1734 s1 = "";
1735 l1 = 0;
1736
Andi Kleen8c4af382010-08-09 17:20:36 -07001737 s2 = log_buf + end - chars;
1738 l2 = chars;
Simon Kagstrom456b5652009-10-16 14:09:18 +02001739 }
1740
Huang Yingfb842b02011-01-12 16:59:43 -08001741 rcu_read_lock();
1742 list_for_each_entry_rcu(dumper, &dump_list, list)
Simon Kagstrom456b5652009-10-16 14:09:18 +02001743 dumper->dump(dumper, reason, s1, l1, s2, l2);
Huang Yingfb842b02011-01-12 16:59:43 -08001744 rcu_read_unlock();
Simon Kagstrom456b5652009-10-16 14:09:18 +02001745}
Joe Perches7ef3d2f2008-02-08 04:21:25 -08001746#endif