blob: 6ff26151f4ffb16e56b517e2990aec1e38ba7491 [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>
34#include <linux/syscalls.h>
Neil Horman04d491a2009-04-02 16:58:57 -070035#include <linux/kexec.h>
Jason Wesseld37d39a2010-05-20 21:04:27 -050036#include <linux/kdb.h>
Ingo Molnar3fff4c42009-09-22 16:18:09 +020037#include <linux/ratelimit.h>
Simon Kagstrom456b5652009-10-16 14:09:18 +020038#include <linux/kmsg_dump.h>
Kees Cook00234592010-02-03 15:36:43 -080039#include <linux/syslog.h>
Kevin Cernekee034260d2010-06-03 22:11:25 -070040#include <linux/cpu.h>
41#include <linux/notifier.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include <asm/uaccess.h>
44
Ingo Molnar076f9772008-01-30 13:33:06 +010045/*
Robin Getz4d091612009-07-01 21:08:37 -040046 * 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
51/*
Ingo Molnar076f9772008-01-30 13:33:06 +010052 * Architectures can override it:
53 */
Jiri Slabye17ba732008-05-12 15:44:40 +020054void asmlinkage __attribute__((weak)) early_printk(const char *fmt, ...)
Ingo Molnar076f9772008-01-30 13:33:06 +010055{
56}
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#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
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
105 * release_console_sem().
106 */
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/*
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 */
Samuel Thibaultf7511d52008-04-30 00:54:51 -0700128#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
129 char *brl_options; /* Options for braille driver */
130#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131};
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;
Markus Armbruster9e124fe2008-05-26 23:31:07 +0100138int console_set_on_cmdline;
139EXPORT_SYMBOL(console_set_on_cmdline);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141/* Flag: console code may call schedule() */
142static int console_may_schedule;
143
Matt Mackalld59745c2005-05-01 08:59:02 -0700144#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;
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800149static unsigned logged_chars; /* Number of chars produced since last read+clear operation */
Gustavo F. Padovancea83882010-03-05 13:42:58 -0800150static int saved_console_loglevel = -1;
Matt Mackalld59745c2005-05-01 08:59:02 -0700151
Neil Horman04d491a2009-04-02 16:58:57 -0700152#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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170static int __init log_buf_len_setup(char *str)
171{
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800172 unsigned size = memparse(str, &str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 unsigned long flags;
174
175 if (size)
176 size = roundup_pow_of_two(size);
177 if (size > log_buf_len) {
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800178 unsigned start, dest_idx, offset;
Jesper Juhl40dc5652005-10-30 15:02:46 -0800179 char *new_log_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 new_log_buf = alloc_bootmem(size);
182 if (!new_log_buf) {
Jesper Juhl40dc5652005-10-30 15:02:46 -0800183 printk(KERN_WARNING "log_buf_len: allocation failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 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
Jesper Juhl40dc5652005-10-30 15:02:46 -0800203 printk(KERN_NOTICE "log_buf_len: %d\n", log_buf_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 }
205out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 return 1;
207}
208
209__setup("log_buf_len=", log_buf_len_setup);
210
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700211#ifdef CONFIG_BOOT_PRINTK_DELAY
212
213static unsigned int boot_delay; /* msecs delay after each printk during bootup */
Dave Young3a3b6ed2009-09-22 16:43:31 -0700214static unsigned long long loops_per_msec; /* based on boot_delay */
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700215
216static int __init boot_delay_setup(char *str)
217{
218 unsigned long lpj;
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700219
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
Dave Young3a3b6ed2009-09-22 16:43:31 -0700227 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);
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700230 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
Dave Young3a3b6ed2009-09-22 16:43:31 -0700242 k = (unsigned long long)loops_per_msec * boot_delay;
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700243
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
Kees Cook00234592010-02-03 15:36:43 -0800264int do_syslog(int type, char __user *buf, int len, bool from_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800266 unsigned i, j, limit, count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 int do_clear = 0;
268 char c;
269 int error = 0;
270
Kees Cook00234592010-02-03 15:36:43 -0800271 error = security_syslog(type, from_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 if (error)
273 return error;
274
275 switch (type) {
Kees Cookd78ca3c2010-02-03 15:37:13 -0800276 case SYSLOG_ACTION_CLOSE: /* Close log */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -0800278 case SYSLOG_ACTION_OPEN: /* Open log */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -0800280 case SYSLOG_ACTION_READ: /* Read from log */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 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 }
Jesper Juhl40dc5652005-10-30 15:02:46 -0800291 error = wait_event_interruptible(log_wait,
292 (log_start - log_end));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 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;
Kees Cookd78ca3c2010-02-03 15:37:13 -0800311 /* Read/clear last kernel messages */
312 case SYSLOG_ACTION_READ_CLEAR:
Jesper Juhl40dc5652005-10-30 15:02:46 -0800313 do_clear = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 /* FALL THRU */
Kees Cookd78ca3c2010-02-03 15:37:13 -0800315 /* Read last kernel messages */
316 case SYSLOG_ACTION_READ_ALL:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 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
Jesper Juhl40dc5652005-10-30 15:02:46 -0800338 * printk() could overwrite the messages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 * we try to copy to user space. Therefore
340 * the messages are copied in reverse. <manfreds>
341 */
Jesper Juhl40dc5652005-10-30 15:02:46 -0800342 for (i = 0; i < count && !error; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 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;
Jesper Juhl40dc5652005-10-30 15:02:46 -0800356 if (i != count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 int offset = count-error;
358 /* buffer overflow during copy, correct user buffer. */
Jesper Juhl40dc5652005-10-30 15:02:46 -0800359 for (i = 0; i < error; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 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;
Kees Cookd78ca3c2010-02-03 15:37:13 -0800369 /* Clear ring buffer */
370 case SYSLOG_ACTION_CLEAR:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 logged_chars = 0;
372 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -0800373 /* Disable logging to console */
374 case SYSLOG_ACTION_CONSOLE_OFF:
Frans Pop1aaad492009-07-06 13:31:48 +0200375 if (saved_console_loglevel == -1)
376 saved_console_loglevel = console_loglevel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 console_loglevel = minimum_console_loglevel;
378 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -0800379 /* Enable logging to console */
380 case SYSLOG_ACTION_CONSOLE_ON:
Frans Pop1aaad492009-07-06 13:31:48 +0200381 if (saved_console_loglevel != -1) {
382 console_loglevel = saved_console_loglevel;
383 saved_console_loglevel = -1;
384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -0800386 /* Set level of messages printed to console */
387 case SYSLOG_ACTION_CONSOLE_LEVEL:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 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;
Frans Pop1aaad492009-07-06 13:31:48 +0200394 /* Implicitly re-enable logging to console */
395 saved_console_loglevel = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 error = 0;
397 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -0800398 /* Number of chars in the log buffer */
399 case SYSLOG_ACTION_SIZE_UNREAD:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 error = log_end - log_start;
401 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -0800402 /* Size of the log buffer */
403 case SYSLOG_ACTION_SIZE_BUFFER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 error = log_buf_len;
405 break;
406 default:
407 error = -EINVAL;
408 break;
409 }
410out:
411 return error;
412}
413
Heiko Carstens1e7bfb22009-01-14 14:14:29 +0100414SYSCALL_DEFINE3(syslog, int, type, char __user *, buf, int, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
Kees Cook00234592010-02-03 15:36:43 -0800416 return do_syslog(type, buf, len, SYSLOG_FROM_CALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417}
418
Jason Wessel67fc4e02010-05-20 21:04:21 -0500419#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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435/*
436 * Call the console drivers on a range of log_buf
437 */
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800438static void __call_console_drivers(unsigned start, unsigned end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
440 struct console *con;
441
Robin Getz4d091612009-07-01 21:08:37 -0400442 for_each_console(con) {
Michael Ellerman76a8ad22006-06-25 05:47:40 -0700443 if ((con->flags & CON_ENABLED) && con->write &&
444 (cpu_online(smp_processor_id()) ||
445 (con->flags & CON_ANYTIME)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 con->write(con, &LOG_BUF(start), end - start);
447 }
448}
449
Ingo Molnar792908222006-12-06 20:40:51 -0800450static int __read_mostly ignore_loglevel;
451
Adrian Bunk99eea6a2006-12-22 01:07:00 -0800452static int __init ignore_loglevel_setup(char *str)
Ingo Molnar792908222006-12-06 20:40:51 -0800453{
454 ignore_loglevel = 1;
455 printk(KERN_INFO "debug: ignoring loglevel setting.\n");
456
Ingo Molnarc4772d92008-01-31 22:45:23 +0100457 return 0;
Ingo Molnar792908222006-12-06 20:40:51 -0800458}
459
Ingo Molnarc4772d92008-01-31 22:45:23 +0100460early_param("ignore_loglevel", ignore_loglevel_setup);
Ingo Molnar792908222006-12-06 20:40:51 -0800461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462/*
463 * Write out chars from start to end - 1 inclusive
464 */
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800465static void _call_console_drivers(unsigned start,
466 unsigned end, int msg_log_level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
Ingo Molnar792908222006-12-06 20:40:51 -0800468 if ((msg_log_level < console_loglevel || ignore_loglevel) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 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 */
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800486static void call_console_drivers(unsigned start, unsigned end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800488 unsigned cur_index, start_print;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 static int msg_level = -1;
490
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800491 BUG_ON(((int)(start - end)) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 cur_index = start;
494 start_print = start;
495 while (cur_index != end) {
Jesper Juhl40dc5652005-10-30 15:02:46 -0800496 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) == '>') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Jesper Juhl40dc5652005-10-30 15:02:46 -0800508 cur_index++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 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) &&
Jesper Juhl40dc5652005-10-30 15:02:46 -0800551 !time_after(jiffies, oops_timestamp + 30 * HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 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 */
Thomas Gleixner5b8c4f22010-09-07 14:33:43 +0000559 sema_init(&console_sem, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560}
561
562#if defined(CONFIG_PRINTK_TIME)
563static int printk_time = 1;
564#else
565static int printk_time = 0;
566#endif
Randy Dunlape84845c2007-07-15 23:40:25 -0700567module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Michael Ellerman76a8ad22006-06-25 05:47:40 -0700569/* 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
Robin Getz4d091612009-07-01 21:08:37 -0400574 for_each_console(con)
Michael Ellerman76a8ad22006-06-25 05:47:40 -0700575 if (con->flags & CON_ANYTIME)
576 return 1;
577
578 return 0;
579}
580
Martin Waitzddad86c2005-11-13 16:08:14 -0800581/**
582 * printk - print a kernel message
583 * @fmt: format string
584 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800585 * This is printk(). It can be called from any context. We want it to work.
Jesper Juhl40dc5652005-10-30 15:02:46 -0800586 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 * 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.
Martin Waitzddad86c2005-11-13 16:08:14 -0800596 *
597 * See also:
598 * printf(3)
Andi Kleen20036fd2008-10-15 22:02:02 -0700599 *
600 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 */
Matt Mackalld59745c2005-05-01 08:59:02 -0700602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603asmlinkage int printk(const char *fmt, ...)
604{
605 va_list args;
606 int r;
607
Jason Wesseld37d39a2010-05-20 21:04:27 -0500608#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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 va_start(args, fmt);
617 r = vprintk(fmt, args);
618 va_end(args);
619
620 return r;
621}
622
David Howellsfe217732005-09-06 15:16:34 -0700623/* cpu currently holding logbuf_lock */
624static volatile unsigned int printk_cpu = UINT_MAX;
625
Linus Torvalds266c2e02008-03-24 19:25:08 -0700626/*
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)
Namhyung Kim8155c022010-10-26 14:22:47 -0700650 __releases(&logbuf_lock)
Linus Torvalds266c2e02008-03-24 19:25:08 -0700651{
652 int retval = 0;
653
Linus Torvalds093a07e2008-04-15 13:09:54 -0700654 if (!try_acquire_console_sem()) {
655 retval = 1;
656
657 /*
658 * If we can't use the console, we need to release
659 * the console semaphore by hand to avoid flushing
660 * the buffer. We need to hold the console semaphore
661 * in order to do this test safely.
662 */
663 if (!can_use_console(cpu)) {
664 console_locked = 0;
665 up(&console_sem);
666 retval = 0;
667 }
668 }
Linus Torvalds266c2e02008-03-24 19:25:08 -0700669 printk_cpu = UINT_MAX;
670 spin_unlock(&logbuf_lock);
671 return retval;
672}
Tejun Heo3b8945e2008-05-12 21:21:04 +0200673static const char recursion_bug_msg [] =
674 KERN_CRIT "BUG: recent printk recursion!\n";
675static int recursion_bug;
Jiri Kosinaedb123e2008-12-04 12:39:49 +0100676static int new_text_line = 1;
Tejun Heo3b8945e2008-05-12 21:21:04 +0200677static char printk_buf[1024];
Ingo Molnar32a76002008-01-25 21:07:58 +0100678
Dave Youngaf913222009-09-22 16:43:33 -0700679int printk_delay_msec __read_mostly;
680
681static inline void printk_delay(void)
682{
683 if (unlikely(printk_delay_msec)) {
684 int m = printk_delay_msec;
685
686 while (m--) {
687 mdelay(1);
688 touch_nmi_watchdog();
689 }
690 }
691}
692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693asmlinkage int vprintk(const char *fmt, va_list args)
694{
Ingo Molnar32a76002008-01-25 21:07:58 +0100695 int printed_len = 0;
Nick Andrew09159302008-05-12 21:21:04 +0200696 int current_log_level = default_message_loglevel;
Nick Andrewac60ad72008-05-12 21:21:04 +0200697 unsigned long flags;
Ingo Molnar32a76002008-01-25 21:07:58 +0100698 int this_cpu;
699 char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700701 boot_delay_msec();
Dave Youngaf913222009-09-22 16:43:33 -0700702 printk_delay();
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700703
David Howellsfe217732005-09-06 15:16:34 -0700704 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 /* This stops the holder of console_sem just where we want him */
Mathieu Desnoyers1efc5da2007-02-10 01:46:29 -0800706 raw_local_irq_save(flags);
Ingo Molnar32a76002008-01-25 21:07:58 +0100707 this_cpu = smp_processor_id();
708
709 /*
710 * Ouch, printk recursed into itself!
711 */
712 if (unlikely(printk_cpu == this_cpu)) {
713 /*
714 * If a crash is occurring during printk() on this CPU,
715 * then try to get the crash message out but make sure
716 * we can't deadlock. Otherwise just return to avoid the
717 * recursion and return - but flag the recursion so that
718 * it can be printed at the next appropriate moment:
719 */
720 if (!oops_in_progress) {
Tejun Heo3b8945e2008-05-12 21:21:04 +0200721 recursion_bug = 1;
Ingo Molnar32a76002008-01-25 21:07:58 +0100722 goto out_restore_irqs;
723 }
724 zap_locks();
725 }
726
Ingo Molnara0f1ccf2006-07-03 00:24:58 -0700727 lockdep_off();
728 spin_lock(&logbuf_lock);
Ingo Molnar32a76002008-01-25 21:07:58 +0100729 printk_cpu = this_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Tejun Heo3b8945e2008-05-12 21:21:04 +0200731 if (recursion_bug) {
732 recursion_bug = 0;
733 strcpy(printk_buf, recursion_bug_msg);
Hiroshi Shimamoto26cc2712008-12-19 10:23:03 -0800734 printed_len = strlen(recursion_bug_msg);
Ingo Molnar32a76002008-01-25 21:07:58 +0100735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 /* Emit the output into the temporary buffer */
Ingo Molnar32a76002008-01-25 21:07:58 +0100737 printed_len += vscnprintf(printk_buf + printed_len,
Tejun Heocf3680b2008-02-14 10:32:07 +0900738 sizeof(printk_buf) - printed_len, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Nick Andrewac60ad72008-05-12 21:21:04 +0200740
Linus Torvalds5fd29d62009-06-16 10:57:02 -0700741 p = printk_buf;
742
743 /* Do we have a loglevel in the string? */
744 if (p[0] == '<') {
745 unsigned char c = p[1];
746 if (c && p[2] == '>') {
747 switch (c) {
748 case '0' ... '7': /* loglevel */
749 current_log_level = c - '0';
Linus Torvaldse28d7132009-06-16 11:02:28 -0700750 /* Fallthrough - make sure we're on a new line */
751 case 'd': /* KERN_DEFAULT */
Linus Torvalds5fd29d62009-06-16 10:57:02 -0700752 if (!new_text_line) {
753 emit_log_char('\n');
754 new_text_line = 1;
755 }
756 /* Fallthrough - skip the loglevel */
757 case 'c': /* KERN_CONT */
758 p += 3;
759 break;
760 }
761 }
762 }
763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 /*
765 * Copy the output into log_buf. If the caller didn't provide
766 * appropriate log level tags, we insert them here
767 */
Linus Torvalds5fd29d62009-06-16 10:57:02 -0700768 for ( ; *p; p++) {
Nick Andrewac60ad72008-05-12 21:21:04 +0200769 if (new_text_line) {
Nick Andrewac60ad72008-05-12 21:21:04 +0200770 /* Always output the token */
771 emit_log_char('<');
772 emit_log_char(current_log_level + '0');
773 emit_log_char('>');
774 printed_len += 3;
775 new_text_line = 0;
776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 if (printk_time) {
Nick Andrewac60ad72008-05-12 21:21:04 +0200778 /* Follow the token with the time */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 char tbuf[50], *tp;
780 unsigned tlen;
781 unsigned long long t;
782 unsigned long nanosec_rem;
783
Ingo Molnar326e96b2008-01-27 08:03:54 +0100784 t = cpu_clock(printk_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 nanosec_rem = do_div(t, 1000000000);
Nick Andrewac60ad72008-05-12 21:21:04 +0200786 tlen = sprintf(tbuf, "[%5lu.%06lu] ",
787 (unsigned long) t,
788 nanosec_rem / 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 for (tp = tbuf; tp < tbuf + tlen; tp++)
791 emit_log_char(*tp);
Guillaume Chazarain025510c2006-01-08 01:02:41 -0800792 printed_len += tlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 }
Nick Andrewac60ad72008-05-12 21:21:04 +0200794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 if (!*p)
796 break;
797 }
Nick Andrewac60ad72008-05-12 21:21:04 +0200798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 emit_log_char(*p);
800 if (*p == '\n')
Nick Andrewac60ad72008-05-12 21:21:04 +0200801 new_text_line = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 }
803
Linus Torvalds266c2e02008-03-24 19:25:08 -0700804 /*
805 * Try to acquire and then immediately release the
806 * console semaphore. The release will do all the
807 * actual magic (print out buffers, wake up klogd,
808 * etc).
809 *
810 * The acquire_console_semaphore_for_printk() function
811 * will release 'logbuf_lock' regardless of whether it
812 * actually gets the semaphore or not.
813 */
814 if (acquire_console_semaphore_for_printk(this_cpu))
815 release_console_sem();
Michael Ellerman76a8ad22006-06-25 05:47:40 -0700816
Linus Torvalds266c2e02008-03-24 19:25:08 -0700817 lockdep_on();
Ingo Molnar32a76002008-01-25 21:07:58 +0100818out_restore_irqs:
Linus Torvalds266c2e02008-03-24 19:25:08 -0700819 raw_local_irq_restore(flags);
Michael Ellerman76a8ad22006-06-25 05:47:40 -0700820
David Howellsfe217732005-09-06 15:16:34 -0700821 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 return printed_len;
823}
824EXPORT_SYMBOL(printk);
825EXPORT_SYMBOL(vprintk);
826
Matt Mackalld59745c2005-05-01 08:59:02 -0700827#else
828
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800829static void call_console_drivers(unsigned start, unsigned end)
Jesper Juhl40dc5652005-10-30 15:02:46 -0800830{
831}
Matt Mackalld59745c2005-05-01 08:59:02 -0700832
833#endif
834
Samuel Thibaultf7511d52008-04-30 00:54:51 -0700835static int __add_preferred_console(char *name, int idx, char *options,
836 char *brl_options)
837{
838 struct console_cmdline *c;
839 int i;
840
841 /*
842 * See if this tty is not yet registered, and
843 * if we have a slot free.
844 */
845 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
846 if (strcmp(console_cmdline[i].name, name) == 0 &&
847 console_cmdline[i].index == idx) {
848 if (!brl_options)
849 selected_console = i;
850 return 0;
851 }
852 if (i == MAX_CMDLINECONSOLES)
853 return -E2BIG;
854 if (!brl_options)
855 selected_console = i;
856 c = &console_cmdline[i];
857 strlcpy(c->name, name, sizeof(c->name));
858 c->options = options;
859#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
860 c->brl_options = brl_options;
861#endif
862 c->index = idx;
863 return 0;
864}
John Z. Bohach2ea1c532006-03-24 03:18:19 -0800865/*
866 * Set up a list of consoles. Called from init/main.c
867 */
868static int __init console_setup(char *str)
869{
Yinghai Lueaa944a2007-07-15 23:37:27 -0700870 char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for index */
Samuel Thibaultf7511d52008-04-30 00:54:51 -0700871 char *s, *options, *brl_options = NULL;
John Z. Bohach2ea1c532006-03-24 03:18:19 -0800872 int idx;
873
Samuel Thibaultf7511d52008-04-30 00:54:51 -0700874#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
875 if (!memcmp(str, "brl,", 4)) {
876 brl_options = "";
877 str += 4;
878 } else if (!memcmp(str, "brl=", 4)) {
879 brl_options = str + 4;
880 str = strchr(brl_options, ',');
881 if (!str) {
882 printk(KERN_ERR "need port name after brl=\n");
883 return 1;
884 }
885 *(str++) = 0;
886 }
887#endif
888
John Z. Bohach2ea1c532006-03-24 03:18:19 -0800889 /*
890 * Decode str into name, index, options.
891 */
892 if (str[0] >= '0' && str[0] <= '9') {
Yinghai Lueaa944a2007-07-15 23:37:27 -0700893 strcpy(buf, "ttyS");
894 strncpy(buf + 4, str, sizeof(buf) - 5);
John Z. Bohach2ea1c532006-03-24 03:18:19 -0800895 } else {
Yinghai Lueaa944a2007-07-15 23:37:27 -0700896 strncpy(buf, str, sizeof(buf) - 1);
John Z. Bohach2ea1c532006-03-24 03:18:19 -0800897 }
Yinghai Lueaa944a2007-07-15 23:37:27 -0700898 buf[sizeof(buf) - 1] = 0;
John Z. Bohach2ea1c532006-03-24 03:18:19 -0800899 if ((options = strchr(str, ',')) != NULL)
900 *(options++) = 0;
901#ifdef __sparc__
902 if (!strcmp(str, "ttya"))
Yinghai Lueaa944a2007-07-15 23:37:27 -0700903 strcpy(buf, "ttyS0");
John Z. Bohach2ea1c532006-03-24 03:18:19 -0800904 if (!strcmp(str, "ttyb"))
Yinghai Lueaa944a2007-07-15 23:37:27 -0700905 strcpy(buf, "ttyS1");
John Z. Bohach2ea1c532006-03-24 03:18:19 -0800906#endif
Yinghai Lueaa944a2007-07-15 23:37:27 -0700907 for (s = buf; *s; s++)
John Z. Bohach2ea1c532006-03-24 03:18:19 -0800908 if ((*s >= '0' && *s <= '9') || *s == ',')
909 break;
910 idx = simple_strtoul(s, NULL, 10);
911 *s = 0;
912
Samuel Thibaultf7511d52008-04-30 00:54:51 -0700913 __add_preferred_console(buf, idx, options, brl_options);
Markus Armbruster9e124fe2008-05-26 23:31:07 +0100914 console_set_on_cmdline = 1;
John Z. Bohach2ea1c532006-03-24 03:18:19 -0800915 return 1;
916}
917__setup("console=", console_setup);
918
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919/**
Matt Mackall3c0547b2005-05-16 21:53:47 -0700920 * add_preferred_console - add a device to the list of preferred consoles.
Martin Waitzddad86c2005-11-13 16:08:14 -0800921 * @name: device name
922 * @idx: device index
923 * @options: options for this console
Matt Mackall3c0547b2005-05-16 21:53:47 -0700924 *
925 * The last preferred console added will be used for kernel messages
926 * and stdin/out/err for init. Normally this is used by console_setup
927 * above to handle user-supplied console arguments; however it can also
928 * be used by arch-specific code either to override the user or more
929 * commonly to provide a default console (ie from PROM variables) when
930 * the user has not supplied one.
931 */
David S. Millerfb445ee2007-12-29 01:19:49 -0800932int add_preferred_console(char *name, int idx, char *options)
Matt Mackall3c0547b2005-05-16 21:53:47 -0700933{
Samuel Thibaultf7511d52008-04-30 00:54:51 -0700934 return __add_preferred_console(name, idx, options, NULL);
Matt Mackall3c0547b2005-05-16 21:53:47 -0700935}
936
Daniel Ritzb6b1d872007-08-03 16:07:43 +0200937int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options)
Yinghai Lu18a8bd92007-07-15 23:37:59 -0700938{
939 struct console_cmdline *c;
940 int i;
941
942 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
943 if (strcmp(console_cmdline[i].name, name) == 0 &&
944 console_cmdline[i].index == idx) {
945 c = &console_cmdline[i];
Markus Armbrusterf7352952008-04-30 00:54:52 -0700946 strlcpy(c->name, name_new, sizeof(c->name));
Yinghai Lu18a8bd92007-07-15 23:37:59 -0700947 c->name[sizeof(c->name) - 1] = 0;
948 c->options = options;
949 c->index = idx_new;
950 return i;
951 }
952 /* not found */
953 return -1;
954}
955
Andres Salomon8f4ce8c2007-10-18 03:04:50 -0700956int console_suspend_enabled = 1;
957EXPORT_SYMBOL(console_suspend_enabled);
958
959static int __init console_suspend_disable(char *str)
960{
961 console_suspend_enabled = 0;
962 return 1;
963}
964__setup("no_console_suspend", console_suspend_disable);
965
Matt Mackall3c0547b2005-05-16 21:53:47 -0700966/**
Linus Torvalds557240b2006-06-19 18:16:01 -0700967 * suspend_console - suspend the console subsystem
968 *
969 * This disables printk() while we go into suspend states
970 */
971void suspend_console(void)
972{
Andres Salomon8f4ce8c2007-10-18 03:04:50 -0700973 if (!console_suspend_enabled)
974 return;
Pavel Machek0d630812008-07-23 21:28:32 -0700975 printk("Suspending console(s) (use no_console_suspend to debug)\n");
Linus Torvalds557240b2006-06-19 18:16:01 -0700976 acquire_console_sem();
977 console_suspended = 1;
Arve Hjønnevåg403f3072009-02-14 02:07:24 +0100978 up(&console_sem);
Linus Torvalds557240b2006-06-19 18:16:01 -0700979}
980
981void resume_console(void)
982{
Andres Salomon8f4ce8c2007-10-18 03:04:50 -0700983 if (!console_suspend_enabled)
984 return;
Arve Hjønnevåg403f3072009-02-14 02:07:24 +0100985 down(&console_sem);
Linus Torvalds557240b2006-06-19 18:16:01 -0700986 console_suspended = 0;
987 release_console_sem();
988}
989
990/**
Kevin Cernekee034260d2010-06-03 22:11:25 -0700991 * console_cpu_notify - print deferred console messages after CPU hotplug
992 * @self: notifier struct
993 * @action: CPU hotplug event
994 * @hcpu: unused
995 *
996 * If printk() is called from a CPU that is not online yet, the messages
997 * will be spooled but will not show up on the console. This function is
998 * called when a new CPU comes online (or fails to come up), and ensures
999 * that any such output gets printed.
1000 */
1001static int __cpuinit console_cpu_notify(struct notifier_block *self,
1002 unsigned long action, void *hcpu)
1003{
1004 switch (action) {
1005 case CPU_ONLINE:
1006 case CPU_DEAD:
1007 case CPU_DYING:
1008 case CPU_DOWN_FAILED:
1009 case CPU_UP_CANCELED:
1010 acquire_console_sem();
1011 release_console_sem();
1012 }
1013 return NOTIFY_OK;
1014}
1015
1016/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 * acquire_console_sem - lock the console system for exclusive use.
1018 *
1019 * Acquires a semaphore which guarantees that the caller has
1020 * exclusive access to the console system and the console_drivers list.
1021 *
1022 * Can sleep, returns nothing.
1023 */
1024void acquire_console_sem(void)
1025{
Eric Sesterhenn8abd8e22006-04-01 01:21:17 +02001026 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 down(&console_sem);
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01001028 if (console_suspended)
1029 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 console_locked = 1;
1031 console_may_schedule = 1;
1032}
1033EXPORT_SYMBOL(acquire_console_sem);
1034
1035int try_acquire_console_sem(void)
1036{
1037 if (down_trylock(&console_sem))
1038 return -1;
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01001039 if (console_suspended) {
1040 up(&console_sem);
1041 return -1;
1042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 console_locked = 1;
1044 console_may_schedule = 0;
1045 return 0;
1046}
1047EXPORT_SYMBOL(try_acquire_console_sem);
1048
1049int is_console_locked(void)
1050{
1051 return console_locked;
1052}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Peter Zijlstrab845b512008-08-08 21:47:09 +02001054static DEFINE_PER_CPU(int, printk_pending);
1055
1056void printk_tick(void)
1057{
1058 if (__get_cpu_var(printk_pending)) {
1059 __get_cpu_var(printk_pending) = 0;
1060 wake_up_interruptible(&log_wait);
1061 }
1062}
1063
1064int printk_needs_cpu(int cpu)
1065{
1066 return per_cpu(printk_pending, cpu);
1067}
1068
Kirill Korotaeve3e8a752007-02-10 01:46:19 -08001069void wake_up_klogd(void)
1070{
Peter Zijlstrab845b512008-08-08 21:47:09 +02001071 if (waitqueue_active(&log_wait))
Peter Zijlstrafa335072008-08-20 09:31:26 +02001072 __raw_get_cpu_var(printk_pending) = 1;
Kirill Korotaeve3e8a752007-02-10 01:46:19 -08001073}
1074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075/**
1076 * release_console_sem - unlock the console system
1077 *
1078 * Releases the semaphore which the caller holds on the console system
1079 * and the console driver list.
1080 *
1081 * While the semaphore was held, console output may have been buffered
1082 * by printk(). If this is the case, release_console_sem() emits
1083 * the output prior to releasing the semaphore.
1084 *
1085 * If there is output waiting for klogd, we wake it up.
1086 *
1087 * release_console_sem() may be called from any context.
1088 */
1089void release_console_sem(void)
1090{
1091 unsigned long flags;
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -08001092 unsigned _con_start, _log_end;
1093 unsigned wake_klogd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Linus Torvalds557240b2006-06-19 18:16:01 -07001095 if (console_suspended) {
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01001096 up(&console_sem);
Linus Torvalds557240b2006-06-19 18:16:01 -07001097 return;
1098 }
Antonino A. Daplas78944e52006-08-05 12:14:16 -07001099
1100 console_may_schedule = 0;
1101
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 for ( ; ; ) {
1103 spin_lock_irqsave(&logbuf_lock, flags);
1104 wake_klogd |= log_start - log_end;
1105 if (con_start == log_end)
1106 break; /* Nothing to print */
1107 _con_start = con_start;
1108 _log_end = log_end;
1109 con_start = log_end; /* Flush */
1110 spin_unlock(&logbuf_lock);
Steven Rostedt81d68a92008-05-12 21:20:42 +02001111 stop_critical_timings(); /* don't trace print latency */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 call_console_drivers(_con_start, _log_end);
Steven Rostedt81d68a92008-05-12 21:20:42 +02001113 start_critical_timings();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 local_irq_restore(flags);
1115 }
1116 console_locked = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 up(&console_sem);
1118 spin_unlock_irqrestore(&logbuf_lock, flags);
Kirill Korotaeve3e8a752007-02-10 01:46:19 -08001119 if (wake_klogd)
1120 wake_up_klogd();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121}
1122EXPORT_SYMBOL(release_console_sem);
1123
Martin Waitzddad86c2005-11-13 16:08:14 -08001124/**
1125 * console_conditional_schedule - yield the CPU if required
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 *
1127 * If the console code is currently allowed to sleep, and
1128 * if this CPU should yield the CPU to another task, do
1129 * so here.
1130 *
1131 * Must be called within acquire_console_sem().
1132 */
1133void __sched console_conditional_schedule(void)
1134{
1135 if (console_may_schedule)
1136 cond_resched();
1137}
1138EXPORT_SYMBOL(console_conditional_schedule);
1139
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140void console_unblank(void)
1141{
1142 struct console *c;
1143
1144 /*
1145 * console_unblank can no longer be called in interrupt context unless
1146 * oops_in_progress is set to 1..
1147 */
1148 if (oops_in_progress) {
1149 if (down_trylock(&console_sem) != 0)
1150 return;
1151 } else
1152 acquire_console_sem();
1153
1154 console_locked = 1;
1155 console_may_schedule = 0;
Robin Getz4d091612009-07-01 21:08:37 -04001156 for_each_console(c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 if ((c->flags & CON_ENABLED) && c->unblank)
1158 c->unblank();
1159 release_console_sem();
1160}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
1162/*
1163 * Return the console tty driver structure and its associated index
1164 */
1165struct tty_driver *console_device(int *index)
1166{
1167 struct console *c;
1168 struct tty_driver *driver = NULL;
1169
1170 acquire_console_sem();
Robin Getz4d091612009-07-01 21:08:37 -04001171 for_each_console(c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 if (!c->device)
1173 continue;
1174 driver = c->device(c, index);
1175 if (driver)
1176 break;
1177 }
1178 release_console_sem();
1179 return driver;
1180}
1181
1182/*
1183 * Prevent further output on the passed console device so that (for example)
1184 * serial drivers can disable console output before suspending a port, and can
1185 * re-enable output afterwards.
1186 */
1187void console_stop(struct console *console)
1188{
1189 acquire_console_sem();
1190 console->flags &= ~CON_ENABLED;
1191 release_console_sem();
1192}
1193EXPORT_SYMBOL(console_stop);
1194
1195void console_start(struct console *console)
1196{
1197 acquire_console_sem();
1198 console->flags |= CON_ENABLED;
1199 release_console_sem();
1200}
1201EXPORT_SYMBOL(console_start);
1202
1203/*
1204 * The console driver calls this routine during kernel initialization
1205 * to register the console printing procedure with printk() and to
1206 * print any messages that were printed by the kernel before the
1207 * console driver was initialized.
Robin Getz4d091612009-07-01 21:08:37 -04001208 *
1209 * This can happen pretty early during the boot process (because of
1210 * early_printk) - sometimes before setup_arch() completes - be careful
1211 * of what kernel features are used - they may not be initialised yet.
1212 *
1213 * There are two types of consoles - bootconsoles (early_printk) and
1214 * "real" consoles (everything which is not a bootconsole) which are
1215 * handled differently.
1216 * - Any number of bootconsoles can be registered at any time.
1217 * - As soon as a "real" console is registered, all bootconsoles
1218 * will be unregistered automatically.
1219 * - Once a "real" console is registered, any attempt to register a
1220 * bootconsoles will be rejected
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 */
Robin Getz4d091612009-07-01 21:08:37 -04001222void register_console(struct console *newcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
Jesper Juhl40dc5652005-10-30 15:02:46 -08001224 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 unsigned long flags;
Robin Getz4d091612009-07-01 21:08:37 -04001226 struct console *bcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Robin Getz4d091612009-07-01 21:08:37 -04001228 /*
1229 * before we register a new CON_BOOT console, make sure we don't
1230 * already have a valid console
1231 */
1232 if (console_drivers && newcon->flags & CON_BOOT) {
1233 /* find the last or real console */
1234 for_each_console(bcon) {
1235 if (!(bcon->flags & CON_BOOT)) {
1236 printk(KERN_INFO "Too late to register bootconsole %s%d\n",
1237 newcon->name, newcon->index);
1238 return;
1239 }
1240 }
Gerd Hoffmann69331af2007-05-08 00:26:49 -07001241 }
1242
Robin Getz4d091612009-07-01 21:08:37 -04001243 if (console_drivers && console_drivers->flags & CON_BOOT)
1244 bcon = console_drivers;
1245
1246 if (preferred_console < 0 || bcon || !console_drivers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 preferred_console = selected_console;
1248
Robin Getz4d091612009-07-01 21:08:37 -04001249 if (newcon->early_setup)
1250 newcon->early_setup();
Yinghai Lu18a8bd92007-07-15 23:37:59 -07001251
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 /*
1253 * See if we want to use this console driver. If we
1254 * didn't select a console we take the first one
1255 * that registers here.
1256 */
1257 if (preferred_console < 0) {
Robin Getz4d091612009-07-01 21:08:37 -04001258 if (newcon->index < 0)
1259 newcon->index = 0;
1260 if (newcon->setup == NULL ||
1261 newcon->setup(newcon, NULL) == 0) {
1262 newcon->flags |= CON_ENABLED;
1263 if (newcon->device) {
1264 newcon->flags |= CON_CONSDEV;
Jan Kiszkacd3a1b82008-05-12 21:21:04 +02001265 preferred_console = 0;
1266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 }
1268 }
1269
1270 /*
1271 * See if this console matches one we selected on
1272 * the command line.
1273 */
Jesper Juhl40dc5652005-10-30 15:02:46 -08001274 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0];
1275 i++) {
Robin Getz4d091612009-07-01 21:08:37 -04001276 if (strcmp(console_cmdline[i].name, newcon->name) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 continue;
Robin Getz4d091612009-07-01 21:08:37 -04001278 if (newcon->index >= 0 &&
1279 newcon->index != console_cmdline[i].index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 continue;
Robin Getz4d091612009-07-01 21:08:37 -04001281 if (newcon->index < 0)
1282 newcon->index = console_cmdline[i].index;
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001283#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1284 if (console_cmdline[i].brl_options) {
Robin Getz4d091612009-07-01 21:08:37 -04001285 newcon->flags |= CON_BRL;
1286 braille_register_console(newcon,
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001287 console_cmdline[i].index,
1288 console_cmdline[i].options,
1289 console_cmdline[i].brl_options);
1290 return;
1291 }
1292#endif
Robin Getz4d091612009-07-01 21:08:37 -04001293 if (newcon->setup &&
1294 newcon->setup(newcon, console_cmdline[i].options) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 break;
Robin Getz4d091612009-07-01 21:08:37 -04001296 newcon->flags |= CON_ENABLED;
1297 newcon->index = console_cmdline[i].index;
Greg Edwardsab4af032005-06-23 00:09:05 -07001298 if (i == selected_console) {
Robin Getz4d091612009-07-01 21:08:37 -04001299 newcon->flags |= CON_CONSDEV;
Greg Edwardsab4af032005-06-23 00:09:05 -07001300 preferred_console = selected_console;
1301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 break;
1303 }
1304
Robin Getz4d091612009-07-01 21:08:37 -04001305 if (!(newcon->flags & CON_ENABLED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 return;
1307
Robin Getz8259cf42009-07-09 13:08:37 -04001308 /*
1309 * If we have a bootconsole, and are switching to a real console,
1310 * don't print everything out again, since when the boot console, and
1311 * the real console are the same physical device, it's annoying to
1312 * see the beginning boot messages twice
1313 */
1314 if (bcon && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV))
Robin Getz4d091612009-07-01 21:08:37 -04001315 newcon->flags &= ~CON_PRINTBUFFER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
1317 /*
1318 * Put this console in the list - keep the
1319 * preferred driver at the head of the list.
1320 */
1321 acquire_console_sem();
Robin Getz4d091612009-07-01 21:08:37 -04001322 if ((newcon->flags & CON_CONSDEV) || console_drivers == NULL) {
1323 newcon->next = console_drivers;
1324 console_drivers = newcon;
1325 if (newcon->next)
1326 newcon->next->flags &= ~CON_CONSDEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 } else {
Robin Getz4d091612009-07-01 21:08:37 -04001328 newcon->next = console_drivers->next;
1329 console_drivers->next = newcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 }
Robin Getz4d091612009-07-01 21:08:37 -04001331 if (newcon->flags & CON_PRINTBUFFER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 /*
1333 * release_console_sem() will print out the buffered messages
1334 * for us.
1335 */
1336 spin_lock_irqsave(&logbuf_lock, flags);
1337 con_start = log_start;
1338 spin_unlock_irqrestore(&logbuf_lock, flags);
1339 }
1340 release_console_sem();
Robin Getz8259cf42009-07-09 13:08:37 -04001341
1342 /*
1343 * By unregistering the bootconsoles after we enable the real console
1344 * we get the "console xxx enabled" message on all the consoles -
1345 * boot consoles, real consoles, etc - this is to ensure that end
1346 * users know there might be something in the kernel's log buffer that
1347 * went to the bootconsole (that they do not see on the real console)
1348 */
1349 if (bcon && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV)) {
1350 /* we need to iterate through twice, to make sure we print
1351 * everything out, before we unregister the console(s)
1352 */
1353 printk(KERN_INFO "console [%s%d] enabled, bootconsole disabled\n",
1354 newcon->name, newcon->index);
1355 for_each_console(bcon)
1356 if (bcon->flags & CON_BOOT)
1357 unregister_console(bcon);
1358 } else {
1359 printk(KERN_INFO "%sconsole [%s%d] enabled\n",
1360 (newcon->flags & CON_BOOT) ? "boot" : "" ,
1361 newcon->name, newcon->index);
1362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363}
1364EXPORT_SYMBOL(register_console);
1365
Jesper Juhl40dc5652005-10-30 15:02:46 -08001366int unregister_console(struct console *console)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367{
Jesper Juhl40dc5652005-10-30 15:02:46 -08001368 struct console *a, *b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 int res = 1;
1370
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001371#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1372 if (console->flags & CON_BRL)
1373 return braille_unregister_console(console);
1374#endif
1375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 acquire_console_sem();
1377 if (console_drivers == console) {
1378 console_drivers=console->next;
1379 res = 0;
Benjamin Herrenschmidte9b15b52005-11-23 13:37:44 -08001380 } else if (console_drivers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 for (a=console_drivers->next, b=console_drivers ;
1382 a; b=a, a=b->next) {
1383 if (a == console) {
1384 b->next = a->next;
1385 res = 0;
1386 break;
Jesper Juhl40dc5652005-10-30 15:02:46 -08001387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 }
1389 }
Jesper Juhl40dc5652005-10-30 15:02:46 -08001390
Gerd Hoffmann69331af2007-05-08 00:26:49 -07001391 /*
Greg Edwardsab4af032005-06-23 00:09:05 -07001392 * If this isn't the last console and it has CON_CONSDEV set, we
1393 * need to set it on the next preferred console.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 */
Gerd Hoffmann69331af2007-05-08 00:26:49 -07001395 if (console_drivers != NULL && console->flags & CON_CONSDEV)
Greg Edwardsab4af032005-06-23 00:09:05 -07001396 console_drivers->flags |= CON_CONSDEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
1398 release_console_sem();
1399 return res;
1400}
1401EXPORT_SYMBOL(unregister_console);
Matt Mackalld59745c2005-05-01 08:59:02 -07001402
Kevin Cernekee034260d2010-06-03 22:11:25 -07001403static int __init printk_late_init(void)
Robin Getz0c5564b2007-08-20 15:22:47 -04001404{
Robin Getz4d091612009-07-01 21:08:37 -04001405 struct console *con;
1406
1407 for_each_console(con) {
1408 if (con->flags & CON_BOOT) {
Robin Getzcb00e992007-08-21 23:14:58 -04001409 printk(KERN_INFO "turn off boot console %s%d\n",
Robin Getz4d091612009-07-01 21:08:37 -04001410 con->name, con->index);
Sonic Zhang42c2c8c2009-08-06 15:58:11 -07001411 unregister_console(con);
Robin Getzcb00e992007-08-21 23:14:58 -04001412 }
Robin Getz0c5564b2007-08-20 15:22:47 -04001413 }
Kevin Cernekee034260d2010-06-03 22:11:25 -07001414 hotcpu_notifier(console_cpu_notify, 0);
Robin Getz0c5564b2007-08-20 15:22:47 -04001415 return 0;
1416}
Kevin Cernekee034260d2010-06-03 22:11:25 -07001417late_initcall(printk_late_init);
Robin Getz0c5564b2007-08-20 15:22:47 -04001418
Joe Perches7ef3d2f2008-02-08 04:21:25 -08001419#if defined CONFIG_PRINTK
Dave Young717115e2008-07-25 01:45:58 -07001420
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421/*
1422 * printk rate limiting, lifted from the networking subsystem.
1423 *
Uwe Kleine-König641de9d2008-07-29 22:33:38 -07001424 * This enforces a rate limit: not more than 10 kernel messages
1425 * every 5s to make a denial-of-service attack impossible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 */
Uwe Kleine-König641de9d2008-07-29 22:33:38 -07001427DEFINE_RATELIMIT_STATE(printk_ratelimit_state, 5 * HZ, 10);
1428
Christian Borntraeger5c828712009-10-23 14:58:11 +02001429int __printk_ratelimit(const char *func)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430{
Christian Borntraeger5c828712009-10-23 14:58:11 +02001431 return ___ratelimit(&printk_ratelimit_state, func);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432}
Christian Borntraeger5c828712009-10-23 14:58:11 +02001433EXPORT_SYMBOL(__printk_ratelimit);
Andrew Mortonf46c4832006-11-02 22:07:16 -08001434
1435/**
1436 * printk_timed_ratelimit - caller-controlled printk ratelimiting
1437 * @caller_jiffies: pointer to caller's state
1438 * @interval_msecs: minimum interval between prints
1439 *
1440 * printk_timed_ratelimit() returns true if more than @interval_msecs
1441 * milliseconds have elapsed since the last time printk_timed_ratelimit()
1442 * returned true.
1443 */
1444bool printk_timed_ratelimit(unsigned long *caller_jiffies,
1445 unsigned int interval_msecs)
1446{
Guillaume Knispelf2d28a22009-03-17 16:18:42 +01001447 if (*caller_jiffies == 0
1448 || !time_in_range(jiffies, *caller_jiffies,
1449 *caller_jiffies
1450 + msecs_to_jiffies(interval_msecs))) {
1451 *caller_jiffies = jiffies;
Andrew Mortonf46c4832006-11-02 22:07:16 -08001452 return true;
1453 }
1454 return false;
1455}
1456EXPORT_SYMBOL(printk_timed_ratelimit);
Simon Kagstrom456b5652009-10-16 14:09:18 +02001457
1458static DEFINE_SPINLOCK(dump_list_lock);
1459static LIST_HEAD(dump_list);
1460
1461/**
1462 * kmsg_dump_register - register a kernel log dumper.
Randy Dunlap64855362009-12-17 15:27:27 -08001463 * @dumper: pointer to the kmsg_dumper structure
Simon Kagstrom456b5652009-10-16 14:09:18 +02001464 *
1465 * Adds a kernel log dumper to the system. The dump callback in the
1466 * structure will be called when the kernel oopses or panics and must be
1467 * set. Returns zero on success and %-EINVAL or %-EBUSY otherwise.
1468 */
1469int kmsg_dump_register(struct kmsg_dumper *dumper)
1470{
1471 unsigned long flags;
1472 int err = -EBUSY;
1473
1474 /* The dump callback needs to be set */
1475 if (!dumper->dump)
1476 return -EINVAL;
1477
1478 spin_lock_irqsave(&dump_list_lock, flags);
1479 /* Don't allow registering multiple times */
1480 if (!dumper->registered) {
1481 dumper->registered = 1;
1482 list_add_tail(&dumper->list, &dump_list);
1483 err = 0;
1484 }
1485 spin_unlock_irqrestore(&dump_list_lock, flags);
1486
1487 return err;
1488}
1489EXPORT_SYMBOL_GPL(kmsg_dump_register);
1490
1491/**
1492 * kmsg_dump_unregister - unregister a kmsg dumper.
Randy Dunlap64855362009-12-17 15:27:27 -08001493 * @dumper: pointer to the kmsg_dumper structure
Simon Kagstrom456b5652009-10-16 14:09:18 +02001494 *
1495 * Removes a dump device from the system. Returns zero on success and
1496 * %-EINVAL otherwise.
1497 */
1498int kmsg_dump_unregister(struct kmsg_dumper *dumper)
1499{
1500 unsigned long flags;
1501 int err = -EINVAL;
1502
1503 spin_lock_irqsave(&dump_list_lock, flags);
1504 if (dumper->registered) {
1505 dumper->registered = 0;
1506 list_del(&dumper->list);
1507 err = 0;
1508 }
1509 spin_unlock_irqrestore(&dump_list_lock, flags);
1510
1511 return err;
1512}
1513EXPORT_SYMBOL_GPL(kmsg_dump_unregister);
1514
Namhyung Kim6c095ef2010-10-26 14:22:47 -07001515static const char * const kmsg_reasons[] = {
Simon Kagstrom456b5652009-10-16 14:09:18 +02001516 [KMSG_DUMP_OOPS] = "oops",
1517 [KMSG_DUMP_PANIC] = "panic",
KOSAKI Motohiro0f4bd462009-12-22 03:15:43 +00001518 [KMSG_DUMP_KEXEC] = "kexec",
Simon Kagstrom456b5652009-10-16 14:09:18 +02001519};
1520
1521static const char *kmsg_to_str(enum kmsg_dump_reason reason)
1522{
1523 if (reason >= ARRAY_SIZE(kmsg_reasons) || reason < 0)
1524 return "unknown";
1525
1526 return kmsg_reasons[reason];
1527}
1528
1529/**
1530 * kmsg_dump - dump kernel log to kernel message dumpers.
1531 * @reason: the reason (oops, panic etc) for dumping
1532 *
1533 * Iterate through each of the dump devices and call the oops/panic
1534 * callbacks with the log buffer.
1535 */
1536void kmsg_dump(enum kmsg_dump_reason reason)
1537{
1538 unsigned long end;
1539 unsigned chars;
1540 struct kmsg_dumper *dumper;
1541 const char *s1, *s2;
1542 unsigned long l1, l2;
1543 unsigned long flags;
1544
1545 /* Theoretically, the log could move on after we do this, but
1546 there's not a lot we can do about that. The new messages
1547 will overwrite the start of what we dump. */
1548 spin_lock_irqsave(&logbuf_lock, flags);
1549 end = log_end & LOG_BUF_MASK;
1550 chars = logged_chars;
1551 spin_unlock_irqrestore(&logbuf_lock, flags);
1552
Andi Kleen8c4af382010-08-09 17:20:36 -07001553 if (chars > end) {
1554 s1 = log_buf + log_buf_len - chars + end;
1555 l1 = chars - end;
Simon Kagstrom456b5652009-10-16 14:09:18 +02001556
1557 s2 = log_buf;
1558 l2 = end;
1559 } else {
1560 s1 = "";
1561 l1 = 0;
1562
Andi Kleen8c4af382010-08-09 17:20:36 -07001563 s2 = log_buf + end - chars;
1564 l2 = chars;
Simon Kagstrom456b5652009-10-16 14:09:18 +02001565 }
1566
1567 if (!spin_trylock_irqsave(&dump_list_lock, flags)) {
1568 printk(KERN_ERR "dump_kmsg: dump list lock is held during %s, skipping dump\n",
1569 kmsg_to_str(reason));
1570 return;
1571 }
1572 list_for_each_entry(dumper, &dump_list, list)
1573 dumper->dump(dumper, reason, s1, l1, s2, l2);
1574 spin_unlock_irqrestore(&dump_list_lock, flags);
1575}
Joe Perches7ef3d2f2008-02-08 04:21:25 -08001576#endif