blob: b5ac4d99c667c96ba55542f69963baf7b151f838 [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>
Ingo Molnar3fff4c42009-09-22 16:18:09 +020036#include <linux/ratelimit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38#include <asm/uaccess.h>
39
Ingo Molnar076f9772008-01-30 13:33:06 +010040/*
Robin Getz4d091612009-07-01 21:08:37 -040041 * for_each_console() allows you to iterate on each console
42 */
43#define for_each_console(con) \
44 for (con = console_drivers; con != NULL; con = con->next)
45
46/*
Ingo Molnar076f9772008-01-30 13:33:06 +010047 * Architectures can override it:
48 */
Jiri Slabye17ba732008-05-12 15:44:40 +020049void asmlinkage __attribute__((weak)) early_printk(const char *fmt, ...)
Ingo Molnar076f9772008-01-30 13:33:06 +010050{
51}
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT)
54
55/* printk's without a loglevel use this.. */
56#define DEFAULT_MESSAGE_LOGLEVEL 4 /* KERN_WARNING */
57
58/* We show everything that is MORE important than this.. */
59#define MINIMUM_CONSOLE_LOGLEVEL 1 /* Minimum loglevel we let people use */
60#define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */
61
62DECLARE_WAIT_QUEUE_HEAD(log_wait);
63
64int console_printk[4] = {
65 DEFAULT_CONSOLE_LOGLEVEL, /* console_loglevel */
66 DEFAULT_MESSAGE_LOGLEVEL, /* default_message_loglevel */
67 MINIMUM_CONSOLE_LOGLEVEL, /* minimum_console_loglevel */
68 DEFAULT_CONSOLE_LOGLEVEL, /* default_console_loglevel */
69};
70
Frans Pop1aaad492009-07-06 13:31:48 +020071static int saved_console_loglevel = -1;
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073/*
Patrick Pletscher0bbfb7c2007-02-17 20:10:16 +010074 * Low level drivers may need that to know if they can schedule in
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 * their unblank() callback or not. So let's export it.
76 */
77int oops_in_progress;
78EXPORT_SYMBOL(oops_in_progress);
79
80/*
81 * console_sem protects the console_drivers list, and also
82 * provides serialisation for access to the entire console
83 * driver system.
84 */
85static DECLARE_MUTEX(console_sem);
86struct console *console_drivers;
Ingo Molnara29d1cf2008-06-02 13:19:08 +020087EXPORT_SYMBOL_GPL(console_drivers);
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089/*
90 * This is used for debugging the mess that is the VT code by
91 * keeping track if we have the console semaphore held. It's
92 * definitely not the perfect debug tool (we don't know if _WE_
93 * hold it are racing, but it helps tracking those weird code
94 * path in the console code where we end up in places I want
95 * locked without the console sempahore held
96 */
Linus Torvalds557240b2006-06-19 18:16:01 -070097static int console_locked, console_suspended;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99/*
100 * logbuf_lock protects log_buf, log_start, log_end, con_start and logged_chars
101 * It is also used in interesting ways to provide interlocking in
102 * release_console_sem().
103 */
104static DEFINE_SPINLOCK(logbuf_lock);
105
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800106#define LOG_BUF_MASK (log_buf_len-1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107#define LOG_BUF(idx) (log_buf[(idx) & LOG_BUF_MASK])
108
109/*
110 * The indices into log_buf are not constrained to log_buf_len - they
111 * must be masked before subscripting
112 */
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800113static unsigned log_start; /* Index into log_buf: next char to be read by syslog() */
114static unsigned con_start; /* Index into log_buf: next char to be sent to consoles */
115static unsigned log_end; /* Index into log_buf: most-recently-written-char + 1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117/*
118 * Array of consoles built from command line options (console=)
119 */
120struct console_cmdline
121{
122 char name[8]; /* Name of the driver */
123 int index; /* Minor dev. to use */
124 char *options; /* Options for the driver */
Samuel Thibaultf7511d52008-04-30 00:54:51 -0700125#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
126 char *brl_options; /* Options for braille driver */
127#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128};
129
130#define MAX_CMDLINECONSOLES 8
131
132static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
133static int selected_console = -1;
134static int preferred_console = -1;
Markus Armbruster9e124fe2008-05-26 23:31:07 +0100135int console_set_on_cmdline;
136EXPORT_SYMBOL(console_set_on_cmdline);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138/* Flag: console code may call schedule() */
139static int console_may_schedule;
140
Matt Mackalld59745c2005-05-01 08:59:02 -0700141#ifdef CONFIG_PRINTK
142
143static char __log_buf[__LOG_BUF_LEN];
144static char *log_buf = __log_buf;
145static int log_buf_len = __LOG_BUF_LEN;
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800146static unsigned logged_chars; /* Number of chars produced since last read+clear operation */
Matt Mackalld59745c2005-05-01 08:59:02 -0700147
Neil Horman04d491a2009-04-02 16:58:57 -0700148#ifdef CONFIG_KEXEC
149/*
150 * This appends the listed symbols to /proc/vmcoreinfo
151 *
152 * /proc/vmcoreinfo is used by various utiilties, like crash and makedumpfile to
153 * obtain access to symbols that are otherwise very difficult to locate. These
154 * symbols are specifically used so that utilities can access and extract the
155 * dmesg log from a vmcore file after a crash.
156 */
157void log_buf_kexec_setup(void)
158{
159 VMCOREINFO_SYMBOL(log_buf);
160 VMCOREINFO_SYMBOL(log_end);
161 VMCOREINFO_SYMBOL(log_buf_len);
162 VMCOREINFO_SYMBOL(logged_chars);
163}
164#endif
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166static int __init log_buf_len_setup(char *str)
167{
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800168 unsigned size = memparse(str, &str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 unsigned long flags;
170
171 if (size)
172 size = roundup_pow_of_two(size);
173 if (size > log_buf_len) {
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800174 unsigned start, dest_idx, offset;
Jesper Juhl40dc5652005-10-30 15:02:46 -0800175 char *new_log_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177 new_log_buf = alloc_bootmem(size);
178 if (!new_log_buf) {
Jesper Juhl40dc5652005-10-30 15:02:46 -0800179 printk(KERN_WARNING "log_buf_len: allocation failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 goto out;
181 }
182
183 spin_lock_irqsave(&logbuf_lock, flags);
184 log_buf_len = size;
185 log_buf = new_log_buf;
186
187 offset = start = min(con_start, log_start);
188 dest_idx = 0;
189 while (start != log_end) {
190 log_buf[dest_idx] = __log_buf[start & (__LOG_BUF_LEN - 1)];
191 start++;
192 dest_idx++;
193 }
194 log_start -= offset;
195 con_start -= offset;
196 log_end -= offset;
197 spin_unlock_irqrestore(&logbuf_lock, flags);
198
Jesper Juhl40dc5652005-10-30 15:02:46 -0800199 printk(KERN_NOTICE "log_buf_len: %d\n", log_buf_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 }
201out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 return 1;
203}
204
205__setup("log_buf_len=", log_buf_len_setup);
206
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700207#ifdef CONFIG_BOOT_PRINTK_DELAY
208
209static unsigned int boot_delay; /* msecs delay after each printk during bootup */
Dave Young3a3b6ed2009-09-22 16:43:31 -0700210static unsigned long long loops_per_msec; /* based on boot_delay */
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700211
212static int __init boot_delay_setup(char *str)
213{
214 unsigned long lpj;
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700215
216 lpj = preset_lpj ? preset_lpj : 1000000; /* some guess */
217 loops_per_msec = (unsigned long long)lpj / 1000 * HZ;
218
219 get_option(&str, &boot_delay);
220 if (boot_delay > 10 * 1000)
221 boot_delay = 0;
222
Dave Young3a3b6ed2009-09-22 16:43:31 -0700223 pr_debug("boot_delay: %u, preset_lpj: %ld, lpj: %lu, "
224 "HZ: %d, loops_per_msec: %llu\n",
225 boot_delay, preset_lpj, lpj, HZ, loops_per_msec);
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700226 return 1;
227}
228__setup("boot_delay=", boot_delay_setup);
229
230static void boot_delay_msec(void)
231{
232 unsigned long long k;
233 unsigned long timeout;
234
235 if (boot_delay == 0 || system_state != SYSTEM_BOOTING)
236 return;
237
Dave Young3a3b6ed2009-09-22 16:43:31 -0700238 k = (unsigned long long)loops_per_msec * boot_delay;
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700239
240 timeout = jiffies + msecs_to_jiffies(boot_delay);
241 while (k) {
242 k--;
243 cpu_relax();
244 /*
245 * use (volatile) jiffies to prevent
246 * compiler reduction; loop termination via jiffies
247 * is secondary and may or may not happen.
248 */
249 if (time_after(jiffies, timeout))
250 break;
251 touch_nmi_watchdog();
252 }
253}
254#else
255static inline void boot_delay_msec(void)
256{
257}
258#endif
259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260/*
261 * Commands to do_syslog:
262 *
263 * 0 -- Close the log. Currently a NOP.
264 * 1 -- Open the log. Currently a NOP.
265 * 2 -- Read from the log.
266 * 3 -- Read all messages remaining in the ring buffer.
267 * 4 -- Read and clear all messages remaining in the ring buffer
268 * 5 -- Clear ring buffer.
269 * 6 -- Disable printk's to console
270 * 7 -- Enable printk's to console
271 * 8 -- Set level of messages printed to console
272 * 9 -- Return number of unread characters in the log buffer
273 * 10 -- Return size of the log buffer
274 */
Jesper Juhl40dc5652005-10-30 15:02:46 -0800275int do_syslog(int type, char __user *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800277 unsigned i, j, limit, count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 int do_clear = 0;
279 char c;
280 int error = 0;
281
282 error = security_syslog(type);
283 if (error)
284 return error;
285
286 switch (type) {
287 case 0: /* Close log */
288 break;
289 case 1: /* Open log */
290 break;
291 case 2: /* Read from log */
292 error = -EINVAL;
293 if (!buf || len < 0)
294 goto out;
295 error = 0;
296 if (!len)
297 goto out;
298 if (!access_ok(VERIFY_WRITE, buf, len)) {
299 error = -EFAULT;
300 goto out;
301 }
Jesper Juhl40dc5652005-10-30 15:02:46 -0800302 error = wait_event_interruptible(log_wait,
303 (log_start - log_end));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 if (error)
305 goto out;
306 i = 0;
307 spin_lock_irq(&logbuf_lock);
308 while (!error && (log_start != log_end) && i < len) {
309 c = LOG_BUF(log_start);
310 log_start++;
311 spin_unlock_irq(&logbuf_lock);
312 error = __put_user(c,buf);
313 buf++;
314 i++;
315 cond_resched();
316 spin_lock_irq(&logbuf_lock);
317 }
318 spin_unlock_irq(&logbuf_lock);
319 if (!error)
320 error = i;
321 break;
322 case 4: /* Read/clear last kernel messages */
Jesper Juhl40dc5652005-10-30 15:02:46 -0800323 do_clear = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 /* FALL THRU */
325 case 3: /* Read last kernel messages */
326 error = -EINVAL;
327 if (!buf || len < 0)
328 goto out;
329 error = 0;
330 if (!len)
331 goto out;
332 if (!access_ok(VERIFY_WRITE, buf, len)) {
333 error = -EFAULT;
334 goto out;
335 }
336 count = len;
337 if (count > log_buf_len)
338 count = log_buf_len;
339 spin_lock_irq(&logbuf_lock);
340 if (count > logged_chars)
341 count = logged_chars;
342 if (do_clear)
343 logged_chars = 0;
344 limit = log_end;
345 /*
346 * __put_user() could sleep, and while we sleep
Jesper Juhl40dc5652005-10-30 15:02:46 -0800347 * printk() could overwrite the messages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 * we try to copy to user space. Therefore
349 * the messages are copied in reverse. <manfreds>
350 */
Jesper Juhl40dc5652005-10-30 15:02:46 -0800351 for (i = 0; i < count && !error; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 j = limit-1-i;
353 if (j + log_buf_len < log_end)
354 break;
355 c = LOG_BUF(j);
356 spin_unlock_irq(&logbuf_lock);
357 error = __put_user(c,&buf[count-1-i]);
358 cond_resched();
359 spin_lock_irq(&logbuf_lock);
360 }
361 spin_unlock_irq(&logbuf_lock);
362 if (error)
363 break;
364 error = i;
Jesper Juhl40dc5652005-10-30 15:02:46 -0800365 if (i != count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 int offset = count-error;
367 /* buffer overflow during copy, correct user buffer. */
Jesper Juhl40dc5652005-10-30 15:02:46 -0800368 for (i = 0; i < error; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 if (__get_user(c,&buf[i+offset]) ||
370 __put_user(c,&buf[i])) {
371 error = -EFAULT;
372 break;
373 }
374 cond_resched();
375 }
376 }
377 break;
378 case 5: /* Clear ring buffer */
379 logged_chars = 0;
380 break;
381 case 6: /* Disable logging to console */
Frans Pop1aaad492009-07-06 13:31:48 +0200382 if (saved_console_loglevel == -1)
383 saved_console_loglevel = console_loglevel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 console_loglevel = minimum_console_loglevel;
385 break;
386 case 7: /* Enable logging to console */
Frans Pop1aaad492009-07-06 13:31:48 +0200387 if (saved_console_loglevel != -1) {
388 console_loglevel = saved_console_loglevel;
389 saved_console_loglevel = -1;
390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 break;
392 case 8: /* Set level of messages printed to console */
393 error = -EINVAL;
394 if (len < 1 || len > 8)
395 goto out;
396 if (len < minimum_console_loglevel)
397 len = minimum_console_loglevel;
398 console_loglevel = len;
Frans Pop1aaad492009-07-06 13:31:48 +0200399 /* Implicitly re-enable logging to console */
400 saved_console_loglevel = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 error = 0;
402 break;
403 case 9: /* Number of chars in the log buffer */
404 error = log_end - log_start;
405 break;
406 case 10: /* Size of the log buffer */
407 error = log_buf_len;
408 break;
409 default:
410 error = -EINVAL;
411 break;
412 }
413out:
414 return error;
415}
416
Heiko Carstens1e7bfb22009-01-14 14:14:29 +0100417SYSCALL_DEFINE3(syslog, int, type, char __user *, buf, int, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
419 return do_syslog(type, buf, len);
420}
421
422/*
423 * Call the console drivers on a range of log_buf
424 */
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800425static void __call_console_drivers(unsigned start, unsigned end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
427 struct console *con;
428
Robin Getz4d091612009-07-01 21:08:37 -0400429 for_each_console(con) {
Michael Ellerman76a8ad22006-06-25 05:47:40 -0700430 if ((con->flags & CON_ENABLED) && con->write &&
431 (cpu_online(smp_processor_id()) ||
432 (con->flags & CON_ANYTIME)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 con->write(con, &LOG_BUF(start), end - start);
434 }
435}
436
Ingo Molnar792908222006-12-06 20:40:51 -0800437static int __read_mostly ignore_loglevel;
438
Adrian Bunk99eea6a2006-12-22 01:07:00 -0800439static int __init ignore_loglevel_setup(char *str)
Ingo Molnar792908222006-12-06 20:40:51 -0800440{
441 ignore_loglevel = 1;
442 printk(KERN_INFO "debug: ignoring loglevel setting.\n");
443
Ingo Molnarc4772d92008-01-31 22:45:23 +0100444 return 0;
Ingo Molnar792908222006-12-06 20:40:51 -0800445}
446
Ingo Molnarc4772d92008-01-31 22:45:23 +0100447early_param("ignore_loglevel", ignore_loglevel_setup);
Ingo Molnar792908222006-12-06 20:40:51 -0800448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449/*
450 * Write out chars from start to end - 1 inclusive
451 */
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800452static void _call_console_drivers(unsigned start,
453 unsigned end, int msg_log_level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
Ingo Molnar792908222006-12-06 20:40:51 -0800455 if ((msg_log_level < console_loglevel || ignore_loglevel) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 console_drivers && start != end) {
457 if ((start & LOG_BUF_MASK) > (end & LOG_BUF_MASK)) {
458 /* wrapped write */
459 __call_console_drivers(start & LOG_BUF_MASK,
460 log_buf_len);
461 __call_console_drivers(0, end & LOG_BUF_MASK);
462 } else {
463 __call_console_drivers(start, end);
464 }
465 }
466}
467
468/*
469 * Call the console drivers, asking them to write out
470 * log_buf[start] to log_buf[end - 1].
471 * The console_sem must be held.
472 */
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800473static void call_console_drivers(unsigned start, unsigned end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800475 unsigned cur_index, start_print;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 static int msg_level = -1;
477
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800478 BUG_ON(((int)(start - end)) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480 cur_index = start;
481 start_print = start;
482 while (cur_index != end) {
Jesper Juhl40dc5652005-10-30 15:02:46 -0800483 if (msg_level < 0 && ((end - cur_index) > 2) &&
484 LOG_BUF(cur_index + 0) == '<' &&
485 LOG_BUF(cur_index + 1) >= '0' &&
486 LOG_BUF(cur_index + 1) <= '7' &&
487 LOG_BUF(cur_index + 2) == '>') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 msg_level = LOG_BUF(cur_index + 1) - '0';
489 cur_index += 3;
490 start_print = cur_index;
491 }
492 while (cur_index != end) {
493 char c = LOG_BUF(cur_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Jesper Juhl40dc5652005-10-30 15:02:46 -0800495 cur_index++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 if (c == '\n') {
497 if (msg_level < 0) {
498 /*
499 * printk() has already given us loglevel tags in
500 * the buffer. This code is here in case the
501 * log buffer has wrapped right round and scribbled
502 * on those tags
503 */
504 msg_level = default_message_loglevel;
505 }
506 _call_console_drivers(start_print, cur_index, msg_level);
507 msg_level = -1;
508 start_print = cur_index;
509 break;
510 }
511 }
512 }
513 _call_console_drivers(start_print, end, msg_level);
514}
515
516static void emit_log_char(char c)
517{
518 LOG_BUF(log_end) = c;
519 log_end++;
520 if (log_end - log_start > log_buf_len)
521 log_start = log_end - log_buf_len;
522 if (log_end - con_start > log_buf_len)
523 con_start = log_end - log_buf_len;
524 if (logged_chars < log_buf_len)
525 logged_chars++;
526}
527
528/*
529 * Zap console related locks when oopsing. Only zap at most once
530 * every 10 seconds, to leave time for slow consoles to print a
531 * full oops.
532 */
533static void zap_locks(void)
534{
535 static unsigned long oops_timestamp;
536
537 if (time_after_eq(jiffies, oops_timestamp) &&
Jesper Juhl40dc5652005-10-30 15:02:46 -0800538 !time_after(jiffies, oops_timestamp + 30 * HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 return;
540
541 oops_timestamp = jiffies;
542
543 /* If a crash is occurring, make sure we can't deadlock */
544 spin_lock_init(&logbuf_lock);
545 /* And make sure that we print immediately */
546 init_MUTEX(&console_sem);
547}
548
549#if defined(CONFIG_PRINTK_TIME)
550static int printk_time = 1;
551#else
552static int printk_time = 0;
553#endif
Randy Dunlape84845c2007-07-15 23:40:25 -0700554module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Michael Ellerman76a8ad22006-06-25 05:47:40 -0700556/* Check if we have any console registered that can be called early in boot. */
557static int have_callable_console(void)
558{
559 struct console *con;
560
Robin Getz4d091612009-07-01 21:08:37 -0400561 for_each_console(con)
Michael Ellerman76a8ad22006-06-25 05:47:40 -0700562 if (con->flags & CON_ANYTIME)
563 return 1;
564
565 return 0;
566}
567
Martin Waitzddad86c2005-11-13 16:08:14 -0800568/**
569 * printk - print a kernel message
570 * @fmt: format string
571 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800572 * This is printk(). It can be called from any context. We want it to work.
Jesper Juhl40dc5652005-10-30 15:02:46 -0800573 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 * We try to grab the console_sem. If we succeed, it's easy - we log the output and
575 * call the console drivers. If we fail to get the semaphore we place the output
576 * into the log buffer and return. The current holder of the console_sem will
577 * notice the new output in release_console_sem() and will send it to the
578 * consoles before releasing the semaphore.
579 *
580 * One effect of this deferred printing is that code which calls printk() and
581 * then changes console_loglevel may break. This is because console_loglevel
582 * is inspected when the actual printing occurs.
Martin Waitzddad86c2005-11-13 16:08:14 -0800583 *
584 * See also:
585 * printf(3)
Andi Kleen20036fd2008-10-15 22:02:02 -0700586 *
587 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 */
Matt Mackalld59745c2005-05-01 08:59:02 -0700589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590asmlinkage int printk(const char *fmt, ...)
591{
592 va_list args;
593 int r;
594
595 va_start(args, fmt);
596 r = vprintk(fmt, args);
597 va_end(args);
598
599 return r;
600}
601
David Howellsfe217732005-09-06 15:16:34 -0700602/* cpu currently holding logbuf_lock */
603static volatile unsigned int printk_cpu = UINT_MAX;
604
Linus Torvalds266c2e02008-03-24 19:25:08 -0700605/*
606 * Can we actually use the console at this time on this cpu?
607 *
608 * Console drivers may assume that per-cpu resources have
609 * been allocated. So unless they're explicitly marked as
610 * being able to cope (CON_ANYTIME) don't call them until
611 * this CPU is officially up.
612 */
613static inline int can_use_console(unsigned int cpu)
614{
615 return cpu_online(cpu) || have_callable_console();
616}
617
618/*
619 * Try to get console ownership to actually show the kernel
620 * messages from a 'printk'. Return true (and with the
621 * console_semaphore held, and 'console_locked' set) if it
622 * is successful, false otherwise.
623 *
624 * This gets called with the 'logbuf_lock' spinlock held and
625 * interrupts disabled. It should return with 'lockbuf_lock'
626 * released but interrupts still disabled.
627 */
628static int acquire_console_semaphore_for_printk(unsigned int cpu)
629{
630 int retval = 0;
631
Linus Torvalds093a07e2008-04-15 13:09:54 -0700632 if (!try_acquire_console_sem()) {
633 retval = 1;
634
635 /*
636 * If we can't use the console, we need to release
637 * the console semaphore by hand to avoid flushing
638 * the buffer. We need to hold the console semaphore
639 * in order to do this test safely.
640 */
641 if (!can_use_console(cpu)) {
642 console_locked = 0;
643 up(&console_sem);
644 retval = 0;
645 }
646 }
Linus Torvalds266c2e02008-03-24 19:25:08 -0700647 printk_cpu = UINT_MAX;
648 spin_unlock(&logbuf_lock);
649 return retval;
650}
Tejun Heo3b8945e2008-05-12 21:21:04 +0200651static const char recursion_bug_msg [] =
652 KERN_CRIT "BUG: recent printk recursion!\n";
653static int recursion_bug;
Jiri Kosinaedb123e2008-12-04 12:39:49 +0100654static int new_text_line = 1;
Tejun Heo3b8945e2008-05-12 21:21:04 +0200655static char printk_buf[1024];
Ingo Molnar32a76002008-01-25 21:07:58 +0100656
Dave Youngaf913222009-09-22 16:43:33 -0700657int printk_delay_msec __read_mostly;
658
659static inline void printk_delay(void)
660{
661 if (unlikely(printk_delay_msec)) {
662 int m = printk_delay_msec;
663
664 while (m--) {
665 mdelay(1);
666 touch_nmi_watchdog();
667 }
668 }
669}
670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671asmlinkage int vprintk(const char *fmt, va_list args)
672{
Ingo Molnar32a76002008-01-25 21:07:58 +0100673 int printed_len = 0;
Nick Andrew09159302008-05-12 21:21:04 +0200674 int current_log_level = default_message_loglevel;
Nick Andrewac60ad72008-05-12 21:21:04 +0200675 unsigned long flags;
Ingo Molnar32a76002008-01-25 21:07:58 +0100676 int this_cpu;
677 char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700679 boot_delay_msec();
Dave Youngaf913222009-09-22 16:43:33 -0700680 printk_delay();
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700681
David Howellsfe217732005-09-06 15:16:34 -0700682 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 /* This stops the holder of console_sem just where we want him */
Mathieu Desnoyers1efc5da2007-02-10 01:46:29 -0800684 raw_local_irq_save(flags);
Ingo Molnar32a76002008-01-25 21:07:58 +0100685 this_cpu = smp_processor_id();
686
687 /*
688 * Ouch, printk recursed into itself!
689 */
690 if (unlikely(printk_cpu == this_cpu)) {
691 /*
692 * If a crash is occurring during printk() on this CPU,
693 * then try to get the crash message out but make sure
694 * we can't deadlock. Otherwise just return to avoid the
695 * recursion and return - but flag the recursion so that
696 * it can be printed at the next appropriate moment:
697 */
698 if (!oops_in_progress) {
Tejun Heo3b8945e2008-05-12 21:21:04 +0200699 recursion_bug = 1;
Ingo Molnar32a76002008-01-25 21:07:58 +0100700 goto out_restore_irqs;
701 }
702 zap_locks();
703 }
704
Ingo Molnara0f1ccf2006-07-03 00:24:58 -0700705 lockdep_off();
706 spin_lock(&logbuf_lock);
Ingo Molnar32a76002008-01-25 21:07:58 +0100707 printk_cpu = this_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Tejun Heo3b8945e2008-05-12 21:21:04 +0200709 if (recursion_bug) {
710 recursion_bug = 0;
711 strcpy(printk_buf, recursion_bug_msg);
Hiroshi Shimamoto26cc2712008-12-19 10:23:03 -0800712 printed_len = strlen(recursion_bug_msg);
Ingo Molnar32a76002008-01-25 21:07:58 +0100713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 /* Emit the output into the temporary buffer */
Ingo Molnar32a76002008-01-25 21:07:58 +0100715 printed_len += vscnprintf(printk_buf + printed_len,
Tejun Heocf3680b2008-02-14 10:32:07 +0900716 sizeof(printk_buf) - printed_len, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Nick Andrewac60ad72008-05-12 21:21:04 +0200718
Linus Torvalds5fd29d62009-06-16 10:57:02 -0700719 p = printk_buf;
720
721 /* Do we have a loglevel in the string? */
722 if (p[0] == '<') {
723 unsigned char c = p[1];
724 if (c && p[2] == '>') {
725 switch (c) {
726 case '0' ... '7': /* loglevel */
727 current_log_level = c - '0';
Linus Torvaldse28d7132009-06-16 11:02:28 -0700728 /* Fallthrough - make sure we're on a new line */
729 case 'd': /* KERN_DEFAULT */
Linus Torvalds5fd29d62009-06-16 10:57:02 -0700730 if (!new_text_line) {
731 emit_log_char('\n');
732 new_text_line = 1;
733 }
734 /* Fallthrough - skip the loglevel */
735 case 'c': /* KERN_CONT */
736 p += 3;
737 break;
738 }
739 }
740 }
741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 /*
743 * Copy the output into log_buf. If the caller didn't provide
744 * appropriate log level tags, we insert them here
745 */
Linus Torvalds5fd29d62009-06-16 10:57:02 -0700746 for ( ; *p; p++) {
Nick Andrewac60ad72008-05-12 21:21:04 +0200747 if (new_text_line) {
Nick Andrewac60ad72008-05-12 21:21:04 +0200748 /* Always output the token */
749 emit_log_char('<');
750 emit_log_char(current_log_level + '0');
751 emit_log_char('>');
752 printed_len += 3;
753 new_text_line = 0;
754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 if (printk_time) {
Nick Andrewac60ad72008-05-12 21:21:04 +0200756 /* Follow the token with the time */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 char tbuf[50], *tp;
758 unsigned tlen;
759 unsigned long long t;
760 unsigned long nanosec_rem;
761
Ingo Molnar326e96b2008-01-27 08:03:54 +0100762 t = cpu_clock(printk_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 nanosec_rem = do_div(t, 1000000000);
Nick Andrewac60ad72008-05-12 21:21:04 +0200764 tlen = sprintf(tbuf, "[%5lu.%06lu] ",
765 (unsigned long) t,
766 nanosec_rem / 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768 for (tp = tbuf; tp < tbuf + tlen; tp++)
769 emit_log_char(*tp);
Guillaume Chazarain025510c2006-01-08 01:02:41 -0800770 printed_len += tlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
Nick Andrewac60ad72008-05-12 21:21:04 +0200772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 if (!*p)
774 break;
775 }
Nick Andrewac60ad72008-05-12 21:21:04 +0200776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 emit_log_char(*p);
778 if (*p == '\n')
Nick Andrewac60ad72008-05-12 21:21:04 +0200779 new_text_line = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 }
781
Linus Torvalds266c2e02008-03-24 19:25:08 -0700782 /*
783 * Try to acquire and then immediately release the
784 * console semaphore. The release will do all the
785 * actual magic (print out buffers, wake up klogd,
786 * etc).
787 *
788 * The acquire_console_semaphore_for_printk() function
789 * will release 'logbuf_lock' regardless of whether it
790 * actually gets the semaphore or not.
791 */
792 if (acquire_console_semaphore_for_printk(this_cpu))
793 release_console_sem();
Michael Ellerman76a8ad22006-06-25 05:47:40 -0700794
Linus Torvalds266c2e02008-03-24 19:25:08 -0700795 lockdep_on();
Ingo Molnar32a76002008-01-25 21:07:58 +0100796out_restore_irqs:
Linus Torvalds266c2e02008-03-24 19:25:08 -0700797 raw_local_irq_restore(flags);
Michael Ellerman76a8ad22006-06-25 05:47:40 -0700798
David Howellsfe217732005-09-06 15:16:34 -0700799 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 return printed_len;
801}
802EXPORT_SYMBOL(printk);
803EXPORT_SYMBOL(vprintk);
804
Matt Mackalld59745c2005-05-01 08:59:02 -0700805#else
806
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800807static void call_console_drivers(unsigned start, unsigned end)
Jesper Juhl40dc5652005-10-30 15:02:46 -0800808{
809}
Matt Mackalld59745c2005-05-01 08:59:02 -0700810
811#endif
812
Samuel Thibaultf7511d52008-04-30 00:54:51 -0700813static int __add_preferred_console(char *name, int idx, char *options,
814 char *brl_options)
815{
816 struct console_cmdline *c;
817 int i;
818
819 /*
820 * See if this tty is not yet registered, and
821 * if we have a slot free.
822 */
823 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
824 if (strcmp(console_cmdline[i].name, name) == 0 &&
825 console_cmdline[i].index == idx) {
826 if (!brl_options)
827 selected_console = i;
828 return 0;
829 }
830 if (i == MAX_CMDLINECONSOLES)
831 return -E2BIG;
832 if (!brl_options)
833 selected_console = i;
834 c = &console_cmdline[i];
835 strlcpy(c->name, name, sizeof(c->name));
836 c->options = options;
837#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
838 c->brl_options = brl_options;
839#endif
840 c->index = idx;
841 return 0;
842}
John Z. Bohach2ea1c532006-03-24 03:18:19 -0800843/*
844 * Set up a list of consoles. Called from init/main.c
845 */
846static int __init console_setup(char *str)
847{
Yinghai Lueaa944a2007-07-15 23:37:27 -0700848 char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for index */
Samuel Thibaultf7511d52008-04-30 00:54:51 -0700849 char *s, *options, *brl_options = NULL;
John Z. Bohach2ea1c532006-03-24 03:18:19 -0800850 int idx;
851
Samuel Thibaultf7511d52008-04-30 00:54:51 -0700852#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
853 if (!memcmp(str, "brl,", 4)) {
854 brl_options = "";
855 str += 4;
856 } else if (!memcmp(str, "brl=", 4)) {
857 brl_options = str + 4;
858 str = strchr(brl_options, ',');
859 if (!str) {
860 printk(KERN_ERR "need port name after brl=\n");
861 return 1;
862 }
863 *(str++) = 0;
864 }
865#endif
866
John Z. Bohach2ea1c532006-03-24 03:18:19 -0800867 /*
868 * Decode str into name, index, options.
869 */
870 if (str[0] >= '0' && str[0] <= '9') {
Yinghai Lueaa944a2007-07-15 23:37:27 -0700871 strcpy(buf, "ttyS");
872 strncpy(buf + 4, str, sizeof(buf) - 5);
John Z. Bohach2ea1c532006-03-24 03:18:19 -0800873 } else {
Yinghai Lueaa944a2007-07-15 23:37:27 -0700874 strncpy(buf, str, sizeof(buf) - 1);
John Z. Bohach2ea1c532006-03-24 03:18:19 -0800875 }
Yinghai Lueaa944a2007-07-15 23:37:27 -0700876 buf[sizeof(buf) - 1] = 0;
John Z. Bohach2ea1c532006-03-24 03:18:19 -0800877 if ((options = strchr(str, ',')) != NULL)
878 *(options++) = 0;
879#ifdef __sparc__
880 if (!strcmp(str, "ttya"))
Yinghai Lueaa944a2007-07-15 23:37:27 -0700881 strcpy(buf, "ttyS0");
John Z. Bohach2ea1c532006-03-24 03:18:19 -0800882 if (!strcmp(str, "ttyb"))
Yinghai Lueaa944a2007-07-15 23:37:27 -0700883 strcpy(buf, "ttyS1");
John Z. Bohach2ea1c532006-03-24 03:18:19 -0800884#endif
Yinghai Lueaa944a2007-07-15 23:37:27 -0700885 for (s = buf; *s; s++)
John Z. Bohach2ea1c532006-03-24 03:18:19 -0800886 if ((*s >= '0' && *s <= '9') || *s == ',')
887 break;
888 idx = simple_strtoul(s, NULL, 10);
889 *s = 0;
890
Samuel Thibaultf7511d52008-04-30 00:54:51 -0700891 __add_preferred_console(buf, idx, options, brl_options);
Markus Armbruster9e124fe2008-05-26 23:31:07 +0100892 console_set_on_cmdline = 1;
John Z. Bohach2ea1c532006-03-24 03:18:19 -0800893 return 1;
894}
895__setup("console=", console_setup);
896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897/**
Matt Mackall3c0547b2005-05-16 21:53:47 -0700898 * add_preferred_console - add a device to the list of preferred consoles.
Martin Waitzddad86c2005-11-13 16:08:14 -0800899 * @name: device name
900 * @idx: device index
901 * @options: options for this console
Matt Mackall3c0547b2005-05-16 21:53:47 -0700902 *
903 * The last preferred console added will be used for kernel messages
904 * and stdin/out/err for init. Normally this is used by console_setup
905 * above to handle user-supplied console arguments; however it can also
906 * be used by arch-specific code either to override the user or more
907 * commonly to provide a default console (ie from PROM variables) when
908 * the user has not supplied one.
909 */
David S. Millerfb445ee2007-12-29 01:19:49 -0800910int add_preferred_console(char *name, int idx, char *options)
Matt Mackall3c0547b2005-05-16 21:53:47 -0700911{
Samuel Thibaultf7511d52008-04-30 00:54:51 -0700912 return __add_preferred_console(name, idx, options, NULL);
Matt Mackall3c0547b2005-05-16 21:53:47 -0700913}
914
Daniel Ritzb6b1d872007-08-03 16:07:43 +0200915int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options)
Yinghai Lu18a8bd92007-07-15 23:37:59 -0700916{
917 struct console_cmdline *c;
918 int i;
919
920 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
921 if (strcmp(console_cmdline[i].name, name) == 0 &&
922 console_cmdline[i].index == idx) {
923 c = &console_cmdline[i];
Markus Armbrusterf7352952008-04-30 00:54:52 -0700924 strlcpy(c->name, name_new, sizeof(c->name));
Yinghai Lu18a8bd92007-07-15 23:37:59 -0700925 c->name[sizeof(c->name) - 1] = 0;
926 c->options = options;
927 c->index = idx_new;
928 return i;
929 }
930 /* not found */
931 return -1;
932}
933
Andres Salomon8f4ce8c2007-10-18 03:04:50 -0700934int console_suspend_enabled = 1;
935EXPORT_SYMBOL(console_suspend_enabled);
936
937static int __init console_suspend_disable(char *str)
938{
939 console_suspend_enabled = 0;
940 return 1;
941}
942__setup("no_console_suspend", console_suspend_disable);
943
Matt Mackall3c0547b2005-05-16 21:53:47 -0700944/**
Linus Torvalds557240b2006-06-19 18:16:01 -0700945 * suspend_console - suspend the console subsystem
946 *
947 * This disables printk() while we go into suspend states
948 */
949void suspend_console(void)
950{
Andres Salomon8f4ce8c2007-10-18 03:04:50 -0700951 if (!console_suspend_enabled)
952 return;
Pavel Machek0d630812008-07-23 21:28:32 -0700953 printk("Suspending console(s) (use no_console_suspend to debug)\n");
Linus Torvalds557240b2006-06-19 18:16:01 -0700954 acquire_console_sem();
955 console_suspended = 1;
Arve Hjønnevåg403f3072009-02-14 02:07:24 +0100956 up(&console_sem);
Linus Torvalds557240b2006-06-19 18:16:01 -0700957}
958
959void resume_console(void)
960{
Andres Salomon8f4ce8c2007-10-18 03:04:50 -0700961 if (!console_suspend_enabled)
962 return;
Arve Hjønnevåg403f3072009-02-14 02:07:24 +0100963 down(&console_sem);
Linus Torvalds557240b2006-06-19 18:16:01 -0700964 console_suspended = 0;
965 release_console_sem();
966}
967
968/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 * acquire_console_sem - lock the console system for exclusive use.
970 *
971 * Acquires a semaphore which guarantees that the caller has
972 * exclusive access to the console system and the console_drivers list.
973 *
974 * Can sleep, returns nothing.
975 */
976void acquire_console_sem(void)
977{
Eric Sesterhenn8abd8e22006-04-01 01:21:17 +0200978 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 down(&console_sem);
Arve Hjønnevåg403f3072009-02-14 02:07:24 +0100980 if (console_suspended)
981 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 console_locked = 1;
983 console_may_schedule = 1;
984}
985EXPORT_SYMBOL(acquire_console_sem);
986
987int try_acquire_console_sem(void)
988{
989 if (down_trylock(&console_sem))
990 return -1;
Arve Hjønnevåg403f3072009-02-14 02:07:24 +0100991 if (console_suspended) {
992 up(&console_sem);
993 return -1;
994 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 console_locked = 1;
996 console_may_schedule = 0;
997 return 0;
998}
999EXPORT_SYMBOL(try_acquire_console_sem);
1000
1001int is_console_locked(void)
1002{
1003 return console_locked;
1004}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Peter Zijlstrab845b512008-08-08 21:47:09 +02001006static DEFINE_PER_CPU(int, printk_pending);
1007
1008void printk_tick(void)
1009{
1010 if (__get_cpu_var(printk_pending)) {
1011 __get_cpu_var(printk_pending) = 0;
1012 wake_up_interruptible(&log_wait);
1013 }
1014}
1015
1016int printk_needs_cpu(int cpu)
1017{
1018 return per_cpu(printk_pending, cpu);
1019}
1020
Kirill Korotaeve3e8a752007-02-10 01:46:19 -08001021void wake_up_klogd(void)
1022{
Peter Zijlstrab845b512008-08-08 21:47:09 +02001023 if (waitqueue_active(&log_wait))
Peter Zijlstrafa335072008-08-20 09:31:26 +02001024 __raw_get_cpu_var(printk_pending) = 1;
Kirill Korotaeve3e8a752007-02-10 01:46:19 -08001025}
1026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027/**
1028 * release_console_sem - unlock the console system
1029 *
1030 * Releases the semaphore which the caller holds on the console system
1031 * and the console driver list.
1032 *
1033 * While the semaphore was held, console output may have been buffered
1034 * by printk(). If this is the case, release_console_sem() emits
1035 * the output prior to releasing the semaphore.
1036 *
1037 * If there is output waiting for klogd, we wake it up.
1038 *
1039 * release_console_sem() may be called from any context.
1040 */
1041void release_console_sem(void)
1042{
1043 unsigned long flags;
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -08001044 unsigned _con_start, _log_end;
1045 unsigned wake_klogd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Linus Torvalds557240b2006-06-19 18:16:01 -07001047 if (console_suspended) {
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01001048 up(&console_sem);
Linus Torvalds557240b2006-06-19 18:16:01 -07001049 return;
1050 }
Antonino A. Daplas78944e52006-08-05 12:14:16 -07001051
1052 console_may_schedule = 0;
1053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 for ( ; ; ) {
1055 spin_lock_irqsave(&logbuf_lock, flags);
1056 wake_klogd |= log_start - log_end;
1057 if (con_start == log_end)
1058 break; /* Nothing to print */
1059 _con_start = con_start;
1060 _log_end = log_end;
1061 con_start = log_end; /* Flush */
1062 spin_unlock(&logbuf_lock);
Steven Rostedt81d68a92008-05-12 21:20:42 +02001063 stop_critical_timings(); /* don't trace print latency */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 call_console_drivers(_con_start, _log_end);
Steven Rostedt81d68a92008-05-12 21:20:42 +02001065 start_critical_timings();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 local_irq_restore(flags);
1067 }
1068 console_locked = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 up(&console_sem);
1070 spin_unlock_irqrestore(&logbuf_lock, flags);
Kirill Korotaeve3e8a752007-02-10 01:46:19 -08001071 if (wake_klogd)
1072 wake_up_klogd();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073}
1074EXPORT_SYMBOL(release_console_sem);
1075
Martin Waitzddad86c2005-11-13 16:08:14 -08001076/**
1077 * console_conditional_schedule - yield the CPU if required
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 *
1079 * If the console code is currently allowed to sleep, and
1080 * if this CPU should yield the CPU to another task, do
1081 * so here.
1082 *
1083 * Must be called within acquire_console_sem().
1084 */
1085void __sched console_conditional_schedule(void)
1086{
1087 if (console_may_schedule)
1088 cond_resched();
1089}
1090EXPORT_SYMBOL(console_conditional_schedule);
1091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092void console_unblank(void)
1093{
1094 struct console *c;
1095
1096 /*
1097 * console_unblank can no longer be called in interrupt context unless
1098 * oops_in_progress is set to 1..
1099 */
1100 if (oops_in_progress) {
1101 if (down_trylock(&console_sem) != 0)
1102 return;
1103 } else
1104 acquire_console_sem();
1105
1106 console_locked = 1;
1107 console_may_schedule = 0;
Robin Getz4d091612009-07-01 21:08:37 -04001108 for_each_console(c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 if ((c->flags & CON_ENABLED) && c->unblank)
1110 c->unblank();
1111 release_console_sem();
1112}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
1114/*
1115 * Return the console tty driver structure and its associated index
1116 */
1117struct tty_driver *console_device(int *index)
1118{
1119 struct console *c;
1120 struct tty_driver *driver = NULL;
1121
1122 acquire_console_sem();
Robin Getz4d091612009-07-01 21:08:37 -04001123 for_each_console(c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 if (!c->device)
1125 continue;
1126 driver = c->device(c, index);
1127 if (driver)
1128 break;
1129 }
1130 release_console_sem();
1131 return driver;
1132}
1133
1134/*
1135 * Prevent further output on the passed console device so that (for example)
1136 * serial drivers can disable console output before suspending a port, and can
1137 * re-enable output afterwards.
1138 */
1139void console_stop(struct console *console)
1140{
1141 acquire_console_sem();
1142 console->flags &= ~CON_ENABLED;
1143 release_console_sem();
1144}
1145EXPORT_SYMBOL(console_stop);
1146
1147void console_start(struct console *console)
1148{
1149 acquire_console_sem();
1150 console->flags |= CON_ENABLED;
1151 release_console_sem();
1152}
1153EXPORT_SYMBOL(console_start);
1154
1155/*
1156 * The console driver calls this routine during kernel initialization
1157 * to register the console printing procedure with printk() and to
1158 * print any messages that were printed by the kernel before the
1159 * console driver was initialized.
Robin Getz4d091612009-07-01 21:08:37 -04001160 *
1161 * This can happen pretty early during the boot process (because of
1162 * early_printk) - sometimes before setup_arch() completes - be careful
1163 * of what kernel features are used - they may not be initialised yet.
1164 *
1165 * There are two types of consoles - bootconsoles (early_printk) and
1166 * "real" consoles (everything which is not a bootconsole) which are
1167 * handled differently.
1168 * - Any number of bootconsoles can be registered at any time.
1169 * - As soon as a "real" console is registered, all bootconsoles
1170 * will be unregistered automatically.
1171 * - Once a "real" console is registered, any attempt to register a
1172 * bootconsoles will be rejected
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 */
Robin Getz4d091612009-07-01 21:08:37 -04001174void register_console(struct console *newcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175{
Jesper Juhl40dc5652005-10-30 15:02:46 -08001176 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 unsigned long flags;
Robin Getz4d091612009-07-01 21:08:37 -04001178 struct console *bcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Robin Getz4d091612009-07-01 21:08:37 -04001180 /*
1181 * before we register a new CON_BOOT console, make sure we don't
1182 * already have a valid console
1183 */
1184 if (console_drivers && newcon->flags & CON_BOOT) {
1185 /* find the last or real console */
1186 for_each_console(bcon) {
1187 if (!(bcon->flags & CON_BOOT)) {
1188 printk(KERN_INFO "Too late to register bootconsole %s%d\n",
1189 newcon->name, newcon->index);
1190 return;
1191 }
1192 }
Gerd Hoffmann69331af2007-05-08 00:26:49 -07001193 }
1194
Robin Getz4d091612009-07-01 21:08:37 -04001195 if (console_drivers && console_drivers->flags & CON_BOOT)
1196 bcon = console_drivers;
1197
1198 if (preferred_console < 0 || bcon || !console_drivers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 preferred_console = selected_console;
1200
Robin Getz4d091612009-07-01 21:08:37 -04001201 if (newcon->early_setup)
1202 newcon->early_setup();
Yinghai Lu18a8bd92007-07-15 23:37:59 -07001203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 /*
1205 * See if we want to use this console driver. If we
1206 * didn't select a console we take the first one
1207 * that registers here.
1208 */
1209 if (preferred_console < 0) {
Robin Getz4d091612009-07-01 21:08:37 -04001210 if (newcon->index < 0)
1211 newcon->index = 0;
1212 if (newcon->setup == NULL ||
1213 newcon->setup(newcon, NULL) == 0) {
1214 newcon->flags |= CON_ENABLED;
1215 if (newcon->device) {
1216 newcon->flags |= CON_CONSDEV;
Jan Kiszkacd3a1b82008-05-12 21:21:04 +02001217 preferred_console = 0;
1218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 }
1220 }
1221
1222 /*
1223 * See if this console matches one we selected on
1224 * the command line.
1225 */
Jesper Juhl40dc5652005-10-30 15:02:46 -08001226 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0];
1227 i++) {
Robin Getz4d091612009-07-01 21:08:37 -04001228 if (strcmp(console_cmdline[i].name, newcon->name) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 continue;
Robin Getz4d091612009-07-01 21:08:37 -04001230 if (newcon->index >= 0 &&
1231 newcon->index != console_cmdline[i].index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 continue;
Robin Getz4d091612009-07-01 21:08:37 -04001233 if (newcon->index < 0)
1234 newcon->index = console_cmdline[i].index;
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001235#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1236 if (console_cmdline[i].brl_options) {
Robin Getz4d091612009-07-01 21:08:37 -04001237 newcon->flags |= CON_BRL;
1238 braille_register_console(newcon,
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001239 console_cmdline[i].index,
1240 console_cmdline[i].options,
1241 console_cmdline[i].brl_options);
1242 return;
1243 }
1244#endif
Robin Getz4d091612009-07-01 21:08:37 -04001245 if (newcon->setup &&
1246 newcon->setup(newcon, console_cmdline[i].options) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 break;
Robin Getz4d091612009-07-01 21:08:37 -04001248 newcon->flags |= CON_ENABLED;
1249 newcon->index = console_cmdline[i].index;
Greg Edwardsab4af032005-06-23 00:09:05 -07001250 if (i == selected_console) {
Robin Getz4d091612009-07-01 21:08:37 -04001251 newcon->flags |= CON_CONSDEV;
Greg Edwardsab4af032005-06-23 00:09:05 -07001252 preferred_console = selected_console;
1253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 break;
1255 }
1256
Robin Getz4d091612009-07-01 21:08:37 -04001257 if (!(newcon->flags & CON_ENABLED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 return;
1259
Robin Getz8259cf42009-07-09 13:08:37 -04001260 /*
1261 * If we have a bootconsole, and are switching to a real console,
1262 * don't print everything out again, since when the boot console, and
1263 * the real console are the same physical device, it's annoying to
1264 * see the beginning boot messages twice
1265 */
1266 if (bcon && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV))
Robin Getz4d091612009-07-01 21:08:37 -04001267 newcon->flags &= ~CON_PRINTBUFFER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
1269 /*
1270 * Put this console in the list - keep the
1271 * preferred driver at the head of the list.
1272 */
1273 acquire_console_sem();
Robin Getz4d091612009-07-01 21:08:37 -04001274 if ((newcon->flags & CON_CONSDEV) || console_drivers == NULL) {
1275 newcon->next = console_drivers;
1276 console_drivers = newcon;
1277 if (newcon->next)
1278 newcon->next->flags &= ~CON_CONSDEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 } else {
Robin Getz4d091612009-07-01 21:08:37 -04001280 newcon->next = console_drivers->next;
1281 console_drivers->next = newcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 }
Robin Getz4d091612009-07-01 21:08:37 -04001283 if (newcon->flags & CON_PRINTBUFFER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 /*
1285 * release_console_sem() will print out the buffered messages
1286 * for us.
1287 */
1288 spin_lock_irqsave(&logbuf_lock, flags);
1289 con_start = log_start;
1290 spin_unlock_irqrestore(&logbuf_lock, flags);
1291 }
1292 release_console_sem();
Robin Getz8259cf42009-07-09 13:08:37 -04001293
1294 /*
1295 * By unregistering the bootconsoles after we enable the real console
1296 * we get the "console xxx enabled" message on all the consoles -
1297 * boot consoles, real consoles, etc - this is to ensure that end
1298 * users know there might be something in the kernel's log buffer that
1299 * went to the bootconsole (that they do not see on the real console)
1300 */
1301 if (bcon && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV)) {
1302 /* we need to iterate through twice, to make sure we print
1303 * everything out, before we unregister the console(s)
1304 */
1305 printk(KERN_INFO "console [%s%d] enabled, bootconsole disabled\n",
1306 newcon->name, newcon->index);
1307 for_each_console(bcon)
1308 if (bcon->flags & CON_BOOT)
1309 unregister_console(bcon);
1310 } else {
1311 printk(KERN_INFO "%sconsole [%s%d] enabled\n",
1312 (newcon->flags & CON_BOOT) ? "boot" : "" ,
1313 newcon->name, newcon->index);
1314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315}
1316EXPORT_SYMBOL(register_console);
1317
Jesper Juhl40dc5652005-10-30 15:02:46 -08001318int unregister_console(struct console *console)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
Jesper Juhl40dc5652005-10-30 15:02:46 -08001320 struct console *a, *b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 int res = 1;
1322
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001323#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1324 if (console->flags & CON_BRL)
1325 return braille_unregister_console(console);
1326#endif
1327
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 acquire_console_sem();
1329 if (console_drivers == console) {
1330 console_drivers=console->next;
1331 res = 0;
Benjamin Herrenschmidte9b15b52005-11-23 13:37:44 -08001332 } else if (console_drivers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 for (a=console_drivers->next, b=console_drivers ;
1334 a; b=a, a=b->next) {
1335 if (a == console) {
1336 b->next = a->next;
1337 res = 0;
1338 break;
Jesper Juhl40dc5652005-10-30 15:02:46 -08001339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 }
1341 }
Jesper Juhl40dc5652005-10-30 15:02:46 -08001342
Gerd Hoffmann69331af2007-05-08 00:26:49 -07001343 /*
Greg Edwardsab4af032005-06-23 00:09:05 -07001344 * If this isn't the last console and it has CON_CONSDEV set, we
1345 * need to set it on the next preferred console.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 */
Gerd Hoffmann69331af2007-05-08 00:26:49 -07001347 if (console_drivers != NULL && console->flags & CON_CONSDEV)
Greg Edwardsab4af032005-06-23 00:09:05 -07001348 console_drivers->flags |= CON_CONSDEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
1350 release_console_sem();
1351 return res;
1352}
1353EXPORT_SYMBOL(unregister_console);
Matt Mackalld59745c2005-05-01 08:59:02 -07001354
Robin Getz0c5564b2007-08-20 15:22:47 -04001355static int __init disable_boot_consoles(void)
1356{
Robin Getz4d091612009-07-01 21:08:37 -04001357 struct console *con;
1358
1359 for_each_console(con) {
1360 if (con->flags & CON_BOOT) {
Robin Getzcb00e992007-08-21 23:14:58 -04001361 printk(KERN_INFO "turn off boot console %s%d\n",
Robin Getz4d091612009-07-01 21:08:37 -04001362 con->name, con->index);
Sonic Zhang42c2c8c2009-08-06 15:58:11 -07001363 unregister_console(con);
Robin Getzcb00e992007-08-21 23:14:58 -04001364 }
Robin Getz0c5564b2007-08-20 15:22:47 -04001365 }
1366 return 0;
1367}
1368late_initcall(disable_boot_consoles);
1369
Joe Perches7ef3d2f2008-02-08 04:21:25 -08001370#if defined CONFIG_PRINTK
Dave Young717115e2008-07-25 01:45:58 -07001371
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372/*
1373 * printk rate limiting, lifted from the networking subsystem.
1374 *
Uwe Kleine-König641de9d2008-07-29 22:33:38 -07001375 * This enforces a rate limit: not more than 10 kernel messages
1376 * every 5s to make a denial-of-service attack impossible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 */
Uwe Kleine-König641de9d2008-07-29 22:33:38 -07001378DEFINE_RATELIMIT_STATE(printk_ratelimit_state, 5 * HZ, 10);
1379
Christian Borntraeger5c828712009-10-23 14:58:11 +02001380int __printk_ratelimit(const char *func)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381{
Christian Borntraeger5c828712009-10-23 14:58:11 +02001382 return ___ratelimit(&printk_ratelimit_state, func);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383}
Christian Borntraeger5c828712009-10-23 14:58:11 +02001384EXPORT_SYMBOL(__printk_ratelimit);
Andrew Mortonf46c4832006-11-02 22:07:16 -08001385
1386/**
1387 * printk_timed_ratelimit - caller-controlled printk ratelimiting
1388 * @caller_jiffies: pointer to caller's state
1389 * @interval_msecs: minimum interval between prints
1390 *
1391 * printk_timed_ratelimit() returns true if more than @interval_msecs
1392 * milliseconds have elapsed since the last time printk_timed_ratelimit()
1393 * returned true.
1394 */
1395bool printk_timed_ratelimit(unsigned long *caller_jiffies,
1396 unsigned int interval_msecs)
1397{
Guillaume Knispelf2d28a22009-03-17 16:18:42 +01001398 if (*caller_jiffies == 0
1399 || !time_in_range(jiffies, *caller_jiffies,
1400 *caller_jiffies
1401 + msecs_to_jiffies(interval_msecs))) {
1402 *caller_jiffies = jiffies;
Andrew Mortonf46c4832006-11-02 22:07:16 -08001403 return true;
1404 }
1405 return false;
1406}
1407EXPORT_SYMBOL(printk_timed_ratelimit);
Joe Perches7ef3d2f2008-02-08 04:21:25 -08001408#endif