blob: e3602d0755b0dd99c9fb47887fd67d371cdd5424 [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>
35
36#include <asm/uaccess.h>
37
Ingo Molnar076f9772008-01-30 13:33:06 +010038/*
39 * Architectures can override it:
40 */
Jiri Slabye17ba732008-05-12 15:44:40 +020041void asmlinkage __attribute__((weak)) early_printk(const char *fmt, ...)
Ingo Molnar076f9772008-01-30 13:33:06 +010042{
43}
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT)
46
47/* printk's without a loglevel use this.. */
48#define DEFAULT_MESSAGE_LOGLEVEL 4 /* KERN_WARNING */
49
50/* We show everything that is MORE important than this.. */
51#define MINIMUM_CONSOLE_LOGLEVEL 1 /* Minimum loglevel we let people use */
52#define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */
53
54DECLARE_WAIT_QUEUE_HEAD(log_wait);
55
56int console_printk[4] = {
57 DEFAULT_CONSOLE_LOGLEVEL, /* console_loglevel */
58 DEFAULT_MESSAGE_LOGLEVEL, /* default_message_loglevel */
59 MINIMUM_CONSOLE_LOGLEVEL, /* minimum_console_loglevel */
60 DEFAULT_CONSOLE_LOGLEVEL, /* default_console_loglevel */
61};
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063/*
Patrick Pletscher0bbfb7c2007-02-17 20:10:16 +010064 * Low level drivers may need that to know if they can schedule in
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 * their unblank() callback or not. So let's export it.
66 */
67int oops_in_progress;
68EXPORT_SYMBOL(oops_in_progress);
69
70/*
71 * console_sem protects the console_drivers list, and also
72 * provides serialisation for access to the entire console
73 * driver system.
74 */
75static DECLARE_MUTEX(console_sem);
76struct console *console_drivers;
Ingo Molnara29d1cf2008-06-02 13:19:08 +020077EXPORT_SYMBOL_GPL(console_drivers);
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079/*
80 * This is used for debugging the mess that is the VT code by
81 * keeping track if we have the console semaphore held. It's
82 * definitely not the perfect debug tool (we don't know if _WE_
83 * hold it are racing, but it helps tracking those weird code
84 * path in the console code where we end up in places I want
85 * locked without the console sempahore held
86 */
Linus Torvalds557240b2006-06-19 18:16:01 -070087static int console_locked, console_suspended;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89/*
90 * logbuf_lock protects log_buf, log_start, log_end, con_start and logged_chars
91 * It is also used in interesting ways to provide interlocking in
92 * release_console_sem().
93 */
94static DEFINE_SPINLOCK(logbuf_lock);
95
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -080096#define LOG_BUF_MASK (log_buf_len-1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097#define LOG_BUF(idx) (log_buf[(idx) & LOG_BUF_MASK])
98
99/*
100 * The indices into log_buf are not constrained to log_buf_len - they
101 * must be masked before subscripting
102 */
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800103static unsigned log_start; /* Index into log_buf: next char to be read by syslog() */
104static unsigned con_start; /* Index into log_buf: next char to be sent to consoles */
105static unsigned log_end; /* Index into log_buf: most-recently-written-char + 1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107/*
108 * Array of consoles built from command line options (console=)
109 */
110struct console_cmdline
111{
112 char name[8]; /* Name of the driver */
113 int index; /* Minor dev. to use */
114 char *options; /* Options for the driver */
Samuel Thibaultf7511d52008-04-30 00:54:51 -0700115#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
116 char *brl_options; /* Options for braille driver */
117#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118};
119
120#define MAX_CMDLINECONSOLES 8
121
122static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
123static int selected_console = -1;
124static int preferred_console = -1;
Markus Armbruster9e124fe2008-05-26 23:31:07 +0100125int console_set_on_cmdline;
126EXPORT_SYMBOL(console_set_on_cmdline);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128/* Flag: console code may call schedule() */
129static int console_may_schedule;
130
Matt Mackalld59745c2005-05-01 08:59:02 -0700131#ifdef CONFIG_PRINTK
132
133static char __log_buf[__LOG_BUF_LEN];
134static char *log_buf = __log_buf;
135static int log_buf_len = __LOG_BUF_LEN;
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800136static unsigned logged_chars; /* Number of chars produced since last read+clear operation */
Matt Mackalld59745c2005-05-01 08:59:02 -0700137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138static int __init log_buf_len_setup(char *str)
139{
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800140 unsigned size = memparse(str, &str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 unsigned long flags;
142
143 if (size)
144 size = roundup_pow_of_two(size);
145 if (size > log_buf_len) {
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800146 unsigned start, dest_idx, offset;
Jesper Juhl40dc5652005-10-30 15:02:46 -0800147 char *new_log_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 new_log_buf = alloc_bootmem(size);
150 if (!new_log_buf) {
Jesper Juhl40dc5652005-10-30 15:02:46 -0800151 printk(KERN_WARNING "log_buf_len: allocation failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 goto out;
153 }
154
155 spin_lock_irqsave(&logbuf_lock, flags);
156 log_buf_len = size;
157 log_buf = new_log_buf;
158
159 offset = start = min(con_start, log_start);
160 dest_idx = 0;
161 while (start != log_end) {
162 log_buf[dest_idx] = __log_buf[start & (__LOG_BUF_LEN - 1)];
163 start++;
164 dest_idx++;
165 }
166 log_start -= offset;
167 con_start -= offset;
168 log_end -= offset;
169 spin_unlock_irqrestore(&logbuf_lock, flags);
170
Jesper Juhl40dc5652005-10-30 15:02:46 -0800171 printk(KERN_NOTICE "log_buf_len: %d\n", log_buf_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 }
173out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 return 1;
175}
176
177__setup("log_buf_len=", log_buf_len_setup);
178
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700179#ifdef CONFIG_BOOT_PRINTK_DELAY
180
181static unsigned int boot_delay; /* msecs delay after each printk during bootup */
182static unsigned long long printk_delay_msec; /* per msec, based on boot_delay */
183
184static int __init boot_delay_setup(char *str)
185{
186 unsigned long lpj;
187 unsigned long long loops_per_msec;
188
189 lpj = preset_lpj ? preset_lpj : 1000000; /* some guess */
190 loops_per_msec = (unsigned long long)lpj / 1000 * HZ;
191
192 get_option(&str, &boot_delay);
193 if (boot_delay > 10 * 1000)
194 boot_delay = 0;
195
196 printk_delay_msec = loops_per_msec;
197 printk(KERN_DEBUG "boot_delay: %u, preset_lpj: %ld, lpj: %lu, "
198 "HZ: %d, printk_delay_msec: %llu\n",
199 boot_delay, preset_lpj, lpj, HZ, printk_delay_msec);
200 return 1;
201}
202__setup("boot_delay=", boot_delay_setup);
203
204static void boot_delay_msec(void)
205{
206 unsigned long long k;
207 unsigned long timeout;
208
209 if (boot_delay == 0 || system_state != SYSTEM_BOOTING)
210 return;
211
212 k = (unsigned long long)printk_delay_msec * boot_delay;
213
214 timeout = jiffies + msecs_to_jiffies(boot_delay);
215 while (k) {
216 k--;
217 cpu_relax();
218 /*
219 * use (volatile) jiffies to prevent
220 * compiler reduction; loop termination via jiffies
221 * is secondary and may or may not happen.
222 */
223 if (time_after(jiffies, timeout))
224 break;
225 touch_nmi_watchdog();
226 }
227}
228#else
229static inline void boot_delay_msec(void)
230{
231}
232#endif
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234/*
235 * Commands to do_syslog:
236 *
237 * 0 -- Close the log. Currently a NOP.
238 * 1 -- Open the log. Currently a NOP.
239 * 2 -- Read from the log.
240 * 3 -- Read all messages remaining in the ring buffer.
241 * 4 -- Read and clear all messages remaining in the ring buffer
242 * 5 -- Clear ring buffer.
243 * 6 -- Disable printk's to console
244 * 7 -- Enable printk's to console
245 * 8 -- Set level of messages printed to console
246 * 9 -- Return number of unread characters in the log buffer
247 * 10 -- Return size of the log buffer
248 */
Jesper Juhl40dc5652005-10-30 15:02:46 -0800249int do_syslog(int type, char __user *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800251 unsigned i, j, limit, count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 int do_clear = 0;
253 char c;
254 int error = 0;
255
256 error = security_syslog(type);
257 if (error)
258 return error;
259
260 switch (type) {
261 case 0: /* Close log */
262 break;
263 case 1: /* Open log */
264 break;
265 case 2: /* Read from log */
266 error = -EINVAL;
267 if (!buf || len < 0)
268 goto out;
269 error = 0;
270 if (!len)
271 goto out;
272 if (!access_ok(VERIFY_WRITE, buf, len)) {
273 error = -EFAULT;
274 goto out;
275 }
Jesper Juhl40dc5652005-10-30 15:02:46 -0800276 error = wait_event_interruptible(log_wait,
277 (log_start - log_end));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (error)
279 goto out;
280 i = 0;
281 spin_lock_irq(&logbuf_lock);
282 while (!error && (log_start != log_end) && i < len) {
283 c = LOG_BUF(log_start);
284 log_start++;
285 spin_unlock_irq(&logbuf_lock);
286 error = __put_user(c,buf);
287 buf++;
288 i++;
289 cond_resched();
290 spin_lock_irq(&logbuf_lock);
291 }
292 spin_unlock_irq(&logbuf_lock);
293 if (!error)
294 error = i;
295 break;
296 case 4: /* Read/clear last kernel messages */
Jesper Juhl40dc5652005-10-30 15:02:46 -0800297 do_clear = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 /* FALL THRU */
299 case 3: /* Read last kernel messages */
300 error = -EINVAL;
301 if (!buf || len < 0)
302 goto out;
303 error = 0;
304 if (!len)
305 goto out;
306 if (!access_ok(VERIFY_WRITE, buf, len)) {
307 error = -EFAULT;
308 goto out;
309 }
310 count = len;
311 if (count > log_buf_len)
312 count = log_buf_len;
313 spin_lock_irq(&logbuf_lock);
314 if (count > logged_chars)
315 count = logged_chars;
316 if (do_clear)
317 logged_chars = 0;
318 limit = log_end;
319 /*
320 * __put_user() could sleep, and while we sleep
Jesper Juhl40dc5652005-10-30 15:02:46 -0800321 * printk() could overwrite the messages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 * we try to copy to user space. Therefore
323 * the messages are copied in reverse. <manfreds>
324 */
Jesper Juhl40dc5652005-10-30 15:02:46 -0800325 for (i = 0; i < count && !error; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 j = limit-1-i;
327 if (j + log_buf_len < log_end)
328 break;
329 c = LOG_BUF(j);
330 spin_unlock_irq(&logbuf_lock);
331 error = __put_user(c,&buf[count-1-i]);
332 cond_resched();
333 spin_lock_irq(&logbuf_lock);
334 }
335 spin_unlock_irq(&logbuf_lock);
336 if (error)
337 break;
338 error = i;
Jesper Juhl40dc5652005-10-30 15:02:46 -0800339 if (i != count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 int offset = count-error;
341 /* buffer overflow during copy, correct user buffer. */
Jesper Juhl40dc5652005-10-30 15:02:46 -0800342 for (i = 0; i < error; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 if (__get_user(c,&buf[i+offset]) ||
344 __put_user(c,&buf[i])) {
345 error = -EFAULT;
346 break;
347 }
348 cond_resched();
349 }
350 }
351 break;
352 case 5: /* Clear ring buffer */
353 logged_chars = 0;
354 break;
355 case 6: /* Disable logging to console */
356 console_loglevel = minimum_console_loglevel;
357 break;
358 case 7: /* Enable logging to console */
359 console_loglevel = default_console_loglevel;
360 break;
361 case 8: /* Set level of messages printed to console */
362 error = -EINVAL;
363 if (len < 1 || len > 8)
364 goto out;
365 if (len < minimum_console_loglevel)
366 len = minimum_console_loglevel;
367 console_loglevel = len;
368 error = 0;
369 break;
370 case 9: /* Number of chars in the log buffer */
371 error = log_end - log_start;
372 break;
373 case 10: /* Size of the log buffer */
374 error = log_buf_len;
375 break;
376 default:
377 error = -EINVAL;
378 break;
379 }
380out:
381 return error;
382}
383
Heiko Carstens1e7bfb22009-01-14 14:14:29 +0100384SYSCALL_DEFINE3(syslog, int, type, char __user *, buf, int, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
386 return do_syslog(type, buf, len);
387}
388
389/*
390 * Call the console drivers on a range of log_buf
391 */
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800392static void __call_console_drivers(unsigned start, unsigned end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
394 struct console *con;
395
396 for (con = console_drivers; con; con = con->next) {
Michael Ellerman76a8ad292006-06-25 05:47:40 -0700397 if ((con->flags & CON_ENABLED) && con->write &&
398 (cpu_online(smp_processor_id()) ||
399 (con->flags & CON_ANYTIME)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 con->write(con, &LOG_BUF(start), end - start);
401 }
402}
403
Ingo Molnar792908222006-12-06 20:40:51 -0800404static int __read_mostly ignore_loglevel;
405
Adrian Bunk99eea6a2006-12-22 01:07:00 -0800406static int __init ignore_loglevel_setup(char *str)
Ingo Molnar792908222006-12-06 20:40:51 -0800407{
408 ignore_loglevel = 1;
409 printk(KERN_INFO "debug: ignoring loglevel setting.\n");
410
Ingo Molnarc4772d92008-01-31 22:45:23 +0100411 return 0;
Ingo Molnar792908222006-12-06 20:40:51 -0800412}
413
Ingo Molnarc4772d92008-01-31 22:45:23 +0100414early_param("ignore_loglevel", ignore_loglevel_setup);
Ingo Molnar792908222006-12-06 20:40:51 -0800415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416/*
417 * Write out chars from start to end - 1 inclusive
418 */
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800419static void _call_console_drivers(unsigned start,
420 unsigned end, int msg_log_level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
Ingo Molnar792908222006-12-06 20:40:51 -0800422 if ((msg_log_level < console_loglevel || ignore_loglevel) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 console_drivers && start != end) {
424 if ((start & LOG_BUF_MASK) > (end & LOG_BUF_MASK)) {
425 /* wrapped write */
426 __call_console_drivers(start & LOG_BUF_MASK,
427 log_buf_len);
428 __call_console_drivers(0, end & LOG_BUF_MASK);
429 } else {
430 __call_console_drivers(start, end);
431 }
432 }
433}
434
435/*
436 * Call the console drivers, asking them to write out
437 * log_buf[start] to log_buf[end - 1].
438 * The console_sem must be held.
439 */
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800440static void call_console_drivers(unsigned start, unsigned end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800442 unsigned cur_index, start_print;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 static int msg_level = -1;
444
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800445 BUG_ON(((int)(start - end)) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 cur_index = start;
448 start_print = start;
449 while (cur_index != end) {
Jesper Juhl40dc5652005-10-30 15:02:46 -0800450 if (msg_level < 0 && ((end - cur_index) > 2) &&
451 LOG_BUF(cur_index + 0) == '<' &&
452 LOG_BUF(cur_index + 1) >= '0' &&
453 LOG_BUF(cur_index + 1) <= '7' &&
454 LOG_BUF(cur_index + 2) == '>') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 msg_level = LOG_BUF(cur_index + 1) - '0';
456 cur_index += 3;
457 start_print = cur_index;
458 }
459 while (cur_index != end) {
460 char c = LOG_BUF(cur_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Jesper Juhl40dc5652005-10-30 15:02:46 -0800462 cur_index++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 if (c == '\n') {
464 if (msg_level < 0) {
465 /*
466 * printk() has already given us loglevel tags in
467 * the buffer. This code is here in case the
468 * log buffer has wrapped right round and scribbled
469 * on those tags
470 */
471 msg_level = default_message_loglevel;
472 }
473 _call_console_drivers(start_print, cur_index, msg_level);
474 msg_level = -1;
475 start_print = cur_index;
476 break;
477 }
478 }
479 }
480 _call_console_drivers(start_print, end, msg_level);
481}
482
483static void emit_log_char(char c)
484{
485 LOG_BUF(log_end) = c;
486 log_end++;
487 if (log_end - log_start > log_buf_len)
488 log_start = log_end - log_buf_len;
489 if (log_end - con_start > log_buf_len)
490 con_start = log_end - log_buf_len;
491 if (logged_chars < log_buf_len)
492 logged_chars++;
493}
494
495/*
496 * Zap console related locks when oopsing. Only zap at most once
497 * every 10 seconds, to leave time for slow consoles to print a
498 * full oops.
499 */
500static void zap_locks(void)
501{
502 static unsigned long oops_timestamp;
503
504 if (time_after_eq(jiffies, oops_timestamp) &&
Jesper Juhl40dc5652005-10-30 15:02:46 -0800505 !time_after(jiffies, oops_timestamp + 30 * HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 return;
507
508 oops_timestamp = jiffies;
509
510 /* If a crash is occurring, make sure we can't deadlock */
511 spin_lock_init(&logbuf_lock);
512 /* And make sure that we print immediately */
513 init_MUTEX(&console_sem);
514}
515
516#if defined(CONFIG_PRINTK_TIME)
517static int printk_time = 1;
518#else
519static int printk_time = 0;
520#endif
Randy Dunlape84845c2007-07-15 23:40:25 -0700521module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Michael Ellerman76a8ad292006-06-25 05:47:40 -0700523/* Check if we have any console registered that can be called early in boot. */
524static int have_callable_console(void)
525{
526 struct console *con;
527
528 for (con = console_drivers; con; con = con->next)
529 if (con->flags & CON_ANYTIME)
530 return 1;
531
532 return 0;
533}
534
Martin Waitzddad86c2005-11-13 16:08:14 -0800535/**
536 * printk - print a kernel message
537 * @fmt: format string
538 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800539 * This is printk(). It can be called from any context. We want it to work.
Jesper Juhl40dc5652005-10-30 15:02:46 -0800540 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 * We try to grab the console_sem. If we succeed, it's easy - we log the output and
542 * call the console drivers. If we fail to get the semaphore we place the output
543 * into the log buffer and return. The current holder of the console_sem will
544 * notice the new output in release_console_sem() and will send it to the
545 * consoles before releasing the semaphore.
546 *
547 * One effect of this deferred printing is that code which calls printk() and
548 * then changes console_loglevel may break. This is because console_loglevel
549 * is inspected when the actual printing occurs.
Martin Waitzddad86c2005-11-13 16:08:14 -0800550 *
551 * See also:
552 * printf(3)
Andi Kleen20036fd2008-10-15 22:02:02 -0700553 *
554 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 */
Matt Mackalld59745c2005-05-01 08:59:02 -0700556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557asmlinkage int printk(const char *fmt, ...)
558{
559 va_list args;
560 int r;
561
562 va_start(args, fmt);
563 r = vprintk(fmt, args);
564 va_end(args);
565
566 return r;
567}
568
David Howellsfe217732005-09-06 15:16:34 -0700569/* cpu currently holding logbuf_lock */
570static volatile unsigned int printk_cpu = UINT_MAX;
571
Linus Torvalds266c2e02008-03-24 19:25:08 -0700572/*
573 * Can we actually use the console at this time on this cpu?
574 *
575 * Console drivers may assume that per-cpu resources have
576 * been allocated. So unless they're explicitly marked as
577 * being able to cope (CON_ANYTIME) don't call them until
578 * this CPU is officially up.
579 */
580static inline int can_use_console(unsigned int cpu)
581{
582 return cpu_online(cpu) || have_callable_console();
583}
584
585/*
586 * Try to get console ownership to actually show the kernel
587 * messages from a 'printk'. Return true (and with the
588 * console_semaphore held, and 'console_locked' set) if it
589 * is successful, false otherwise.
590 *
591 * This gets called with the 'logbuf_lock' spinlock held and
592 * interrupts disabled. It should return with 'lockbuf_lock'
593 * released but interrupts still disabled.
594 */
595static int acquire_console_semaphore_for_printk(unsigned int cpu)
596{
597 int retval = 0;
598
Linus Torvalds093a07e2008-04-15 13:09:54 -0700599 if (!try_acquire_console_sem()) {
600 retval = 1;
601
602 /*
603 * If we can't use the console, we need to release
604 * the console semaphore by hand to avoid flushing
605 * the buffer. We need to hold the console semaphore
606 * in order to do this test safely.
607 */
608 if (!can_use_console(cpu)) {
609 console_locked = 0;
610 up(&console_sem);
611 retval = 0;
612 }
613 }
Linus Torvalds266c2e02008-03-24 19:25:08 -0700614 printk_cpu = UINT_MAX;
615 spin_unlock(&logbuf_lock);
616 return retval;
617}
Tejun Heo3b8945e2008-05-12 21:21:04 +0200618static const char recursion_bug_msg [] =
619 KERN_CRIT "BUG: recent printk recursion!\n";
620static int recursion_bug;
Jiri Kosinaedb123e2008-12-04 12:39:49 +0100621static int new_text_line = 1;
Tejun Heo3b8945e2008-05-12 21:21:04 +0200622static char printk_buf[1024];
Ingo Molnar32a76002008-01-25 21:07:58 +0100623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624asmlinkage int vprintk(const char *fmt, va_list args)
625{
Ingo Molnar32a76002008-01-25 21:07:58 +0100626 int printed_len = 0;
Nick Andrew09159302008-05-12 21:21:04 +0200627 int current_log_level = default_message_loglevel;
Nick Andrewac60ad72008-05-12 21:21:04 +0200628 unsigned long flags;
Ingo Molnar32a76002008-01-25 21:07:58 +0100629 int this_cpu;
630 char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700632 boot_delay_msec();
633
David Howellsfe217732005-09-06 15:16:34 -0700634 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 /* This stops the holder of console_sem just where we want him */
Mathieu Desnoyers1efc5da2007-02-10 01:46:29 -0800636 raw_local_irq_save(flags);
Ingo Molnar32a76002008-01-25 21:07:58 +0100637 this_cpu = smp_processor_id();
638
639 /*
640 * Ouch, printk recursed into itself!
641 */
642 if (unlikely(printk_cpu == this_cpu)) {
643 /*
644 * If a crash is occurring during printk() on this CPU,
645 * then try to get the crash message out but make sure
646 * we can't deadlock. Otherwise just return to avoid the
647 * recursion and return - but flag the recursion so that
648 * it can be printed at the next appropriate moment:
649 */
650 if (!oops_in_progress) {
Tejun Heo3b8945e2008-05-12 21:21:04 +0200651 recursion_bug = 1;
Ingo Molnar32a76002008-01-25 21:07:58 +0100652 goto out_restore_irqs;
653 }
654 zap_locks();
655 }
656
Ingo Molnara0f1ccf2006-07-03 00:24:58 -0700657 lockdep_off();
658 spin_lock(&logbuf_lock);
Ingo Molnar32a76002008-01-25 21:07:58 +0100659 printk_cpu = this_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Tejun Heo3b8945e2008-05-12 21:21:04 +0200661 if (recursion_bug) {
662 recursion_bug = 0;
663 strcpy(printk_buf, recursion_bug_msg);
Hiroshi Shimamoto26cc2712008-12-19 10:23:03 -0800664 printed_len = strlen(recursion_bug_msg);
Ingo Molnar32a76002008-01-25 21:07:58 +0100665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 /* Emit the output into the temporary buffer */
Ingo Molnar32a76002008-01-25 21:07:58 +0100667 printed_len += vscnprintf(printk_buf + printed_len,
Tejun Heocf3680b2008-02-14 10:32:07 +0900668 sizeof(printk_buf) - printed_len, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Nick Andrewac60ad72008-05-12 21:21:04 +0200670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 /*
672 * Copy the output into log_buf. If the caller didn't provide
673 * appropriate log level tags, we insert them here
674 */
675 for (p = printk_buf; *p; p++) {
Nick Andrewac60ad72008-05-12 21:21:04 +0200676 if (new_text_line) {
Nick Andrewac60ad72008-05-12 21:21:04 +0200677 /* If a token, set current_log_level and skip over */
678 if (p[0] == '<' && p[1] >= '0' && p[1] <= '7' &&
679 p[2] == '>') {
680 current_log_level = p[1] - '0';
681 p += 3;
682 printed_len -= 3;
683 }
684
685 /* Always output the token */
686 emit_log_char('<');
687 emit_log_char(current_log_level + '0');
688 emit_log_char('>');
689 printed_len += 3;
690 new_text_line = 0;
691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 if (printk_time) {
Nick Andrewac60ad72008-05-12 21:21:04 +0200693 /* Follow the token with the time */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 char tbuf[50], *tp;
695 unsigned tlen;
696 unsigned long long t;
697 unsigned long nanosec_rem;
698
Ingo Molnar326e96b2008-01-27 08:03:54 +0100699 t = cpu_clock(printk_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 nanosec_rem = do_div(t, 1000000000);
Nick Andrewac60ad72008-05-12 21:21:04 +0200701 tlen = sprintf(tbuf, "[%5lu.%06lu] ",
702 (unsigned long) t,
703 nanosec_rem / 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 for (tp = tbuf; tp < tbuf + tlen; tp++)
706 emit_log_char(*tp);
Guillaume Chazarain025510c2006-01-08 01:02:41 -0800707 printed_len += tlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
Nick Andrewac60ad72008-05-12 21:21:04 +0200709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 if (!*p)
711 break;
712 }
Nick Andrewac60ad72008-05-12 21:21:04 +0200713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 emit_log_char(*p);
715 if (*p == '\n')
Nick Andrewac60ad72008-05-12 21:21:04 +0200716 new_text_line = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 }
718
Linus Torvalds266c2e02008-03-24 19:25:08 -0700719 /*
720 * Try to acquire and then immediately release the
721 * console semaphore. The release will do all the
722 * actual magic (print out buffers, wake up klogd,
723 * etc).
724 *
725 * The acquire_console_semaphore_for_printk() function
726 * will release 'logbuf_lock' regardless of whether it
727 * actually gets the semaphore or not.
728 */
729 if (acquire_console_semaphore_for_printk(this_cpu))
730 release_console_sem();
Michael Ellerman76a8ad292006-06-25 05:47:40 -0700731
Linus Torvalds266c2e02008-03-24 19:25:08 -0700732 lockdep_on();
Ingo Molnar32a76002008-01-25 21:07:58 +0100733out_restore_irqs:
Linus Torvalds266c2e02008-03-24 19:25:08 -0700734 raw_local_irq_restore(flags);
Michael Ellerman76a8ad292006-06-25 05:47:40 -0700735
David Howellsfe217732005-09-06 15:16:34 -0700736 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 return printed_len;
738}
739EXPORT_SYMBOL(printk);
740EXPORT_SYMBOL(vprintk);
741
Matt Mackalld59745c2005-05-01 08:59:02 -0700742#else
743
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800744static void call_console_drivers(unsigned start, unsigned end)
Jesper Juhl40dc5652005-10-30 15:02:46 -0800745{
746}
Matt Mackalld59745c2005-05-01 08:59:02 -0700747
748#endif
749
Samuel Thibaultf7511d52008-04-30 00:54:51 -0700750static int __add_preferred_console(char *name, int idx, char *options,
751 char *brl_options)
752{
753 struct console_cmdline *c;
754 int i;
755
756 /*
757 * See if this tty is not yet registered, and
758 * if we have a slot free.
759 */
760 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
761 if (strcmp(console_cmdline[i].name, name) == 0 &&
762 console_cmdline[i].index == idx) {
763 if (!brl_options)
764 selected_console = i;
765 return 0;
766 }
767 if (i == MAX_CMDLINECONSOLES)
768 return -E2BIG;
769 if (!brl_options)
770 selected_console = i;
771 c = &console_cmdline[i];
772 strlcpy(c->name, name, sizeof(c->name));
773 c->options = options;
774#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
775 c->brl_options = brl_options;
776#endif
777 c->index = idx;
778 return 0;
779}
John Z. Bohach2ea1c532006-03-24 03:18:19 -0800780/*
781 * Set up a list of consoles. Called from init/main.c
782 */
783static int __init console_setup(char *str)
784{
Yinghai Lueaa944a2007-07-15 23:37:27 -0700785 char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for index */
Samuel Thibaultf7511d52008-04-30 00:54:51 -0700786 char *s, *options, *brl_options = NULL;
John Z. Bohach2ea1c532006-03-24 03:18:19 -0800787 int idx;
788
Samuel Thibaultf7511d52008-04-30 00:54:51 -0700789#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
790 if (!memcmp(str, "brl,", 4)) {
791 brl_options = "";
792 str += 4;
793 } else if (!memcmp(str, "brl=", 4)) {
794 brl_options = str + 4;
795 str = strchr(brl_options, ',');
796 if (!str) {
797 printk(KERN_ERR "need port name after brl=\n");
798 return 1;
799 }
800 *(str++) = 0;
801 }
802#endif
803
John Z. Bohach2ea1c532006-03-24 03:18:19 -0800804 /*
805 * Decode str into name, index, options.
806 */
807 if (str[0] >= '0' && str[0] <= '9') {
Yinghai Lueaa944a2007-07-15 23:37:27 -0700808 strcpy(buf, "ttyS");
809 strncpy(buf + 4, str, sizeof(buf) - 5);
John Z. Bohach2ea1c532006-03-24 03:18:19 -0800810 } else {
Yinghai Lueaa944a2007-07-15 23:37:27 -0700811 strncpy(buf, str, sizeof(buf) - 1);
John Z. Bohach2ea1c532006-03-24 03:18:19 -0800812 }
Yinghai Lueaa944a2007-07-15 23:37:27 -0700813 buf[sizeof(buf) - 1] = 0;
John Z. Bohach2ea1c532006-03-24 03:18:19 -0800814 if ((options = strchr(str, ',')) != NULL)
815 *(options++) = 0;
816#ifdef __sparc__
817 if (!strcmp(str, "ttya"))
Yinghai Lueaa944a2007-07-15 23:37:27 -0700818 strcpy(buf, "ttyS0");
John Z. Bohach2ea1c532006-03-24 03:18:19 -0800819 if (!strcmp(str, "ttyb"))
Yinghai Lueaa944a2007-07-15 23:37:27 -0700820 strcpy(buf, "ttyS1");
John Z. Bohach2ea1c532006-03-24 03:18:19 -0800821#endif
Yinghai Lueaa944a2007-07-15 23:37:27 -0700822 for (s = buf; *s; s++)
John Z. Bohach2ea1c532006-03-24 03:18:19 -0800823 if ((*s >= '0' && *s <= '9') || *s == ',')
824 break;
825 idx = simple_strtoul(s, NULL, 10);
826 *s = 0;
827
Samuel Thibaultf7511d52008-04-30 00:54:51 -0700828 __add_preferred_console(buf, idx, options, brl_options);
Markus Armbruster9e124fe2008-05-26 23:31:07 +0100829 console_set_on_cmdline = 1;
John Z. Bohach2ea1c532006-03-24 03:18:19 -0800830 return 1;
831}
832__setup("console=", console_setup);
833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834/**
Matt Mackall3c0547b2005-05-16 21:53:47 -0700835 * add_preferred_console - add a device to the list of preferred consoles.
Martin Waitzddad86c2005-11-13 16:08:14 -0800836 * @name: device name
837 * @idx: device index
838 * @options: options for this console
Matt Mackall3c0547b2005-05-16 21:53:47 -0700839 *
840 * The last preferred console added will be used for kernel messages
841 * and stdin/out/err for init. Normally this is used by console_setup
842 * above to handle user-supplied console arguments; however it can also
843 * be used by arch-specific code either to override the user or more
844 * commonly to provide a default console (ie from PROM variables) when
845 * the user has not supplied one.
846 */
David S. Millerfb445ee2007-12-29 01:19:49 -0800847int add_preferred_console(char *name, int idx, char *options)
Matt Mackall3c0547b2005-05-16 21:53:47 -0700848{
Samuel Thibaultf7511d52008-04-30 00:54:51 -0700849 return __add_preferred_console(name, idx, options, NULL);
Matt Mackall3c0547b2005-05-16 21:53:47 -0700850}
851
Daniel Ritzb6b1d872007-08-03 16:07:43 +0200852int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options)
Yinghai Lu18a8bd92007-07-15 23:37:59 -0700853{
854 struct console_cmdline *c;
855 int i;
856
857 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
858 if (strcmp(console_cmdline[i].name, name) == 0 &&
859 console_cmdline[i].index == idx) {
860 c = &console_cmdline[i];
Markus Armbrusterf7352952008-04-30 00:54:52 -0700861 strlcpy(c->name, name_new, sizeof(c->name));
Yinghai Lu18a8bd92007-07-15 23:37:59 -0700862 c->name[sizeof(c->name) - 1] = 0;
863 c->options = options;
864 c->index = idx_new;
865 return i;
866 }
867 /* not found */
868 return -1;
869}
870
Andres Salomon8f4ce8c2007-10-18 03:04:50 -0700871int console_suspend_enabled = 1;
872EXPORT_SYMBOL(console_suspend_enabled);
873
874static int __init console_suspend_disable(char *str)
875{
876 console_suspend_enabled = 0;
877 return 1;
878}
879__setup("no_console_suspend", console_suspend_disable);
880
Matt Mackall3c0547b2005-05-16 21:53:47 -0700881/**
Linus Torvalds557240b2006-06-19 18:16:01 -0700882 * suspend_console - suspend the console subsystem
883 *
884 * This disables printk() while we go into suspend states
885 */
886void suspend_console(void)
887{
Andres Salomon8f4ce8c2007-10-18 03:04:50 -0700888 if (!console_suspend_enabled)
889 return;
Pavel Machek0d630812008-07-23 21:28:32 -0700890 printk("Suspending console(s) (use no_console_suspend to debug)\n");
Linus Torvalds557240b2006-06-19 18:16:01 -0700891 acquire_console_sem();
892 console_suspended = 1;
Arve Hjønnevåg403f3072009-02-14 02:07:24 +0100893 up(&console_sem);
Linus Torvalds557240b2006-06-19 18:16:01 -0700894}
895
896void resume_console(void)
897{
Andres Salomon8f4ce8c2007-10-18 03:04:50 -0700898 if (!console_suspend_enabled)
899 return;
Arve Hjønnevåg403f3072009-02-14 02:07:24 +0100900 down(&console_sem);
Linus Torvalds557240b2006-06-19 18:16:01 -0700901 console_suspended = 0;
902 release_console_sem();
903}
904
905/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 * acquire_console_sem - lock the console system for exclusive use.
907 *
908 * Acquires a semaphore which guarantees that the caller has
909 * exclusive access to the console system and the console_drivers list.
910 *
911 * Can sleep, returns nothing.
912 */
913void acquire_console_sem(void)
914{
Eric Sesterhenn8abd8e22006-04-01 01:21:17 +0200915 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 down(&console_sem);
Arve Hjønnevåg403f3072009-02-14 02:07:24 +0100917 if (console_suspended)
918 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 console_locked = 1;
920 console_may_schedule = 1;
921}
922EXPORT_SYMBOL(acquire_console_sem);
923
924int try_acquire_console_sem(void)
925{
926 if (down_trylock(&console_sem))
927 return -1;
Arve Hjønnevåg403f3072009-02-14 02:07:24 +0100928 if (console_suspended) {
929 up(&console_sem);
930 return -1;
931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 console_locked = 1;
933 console_may_schedule = 0;
934 return 0;
935}
936EXPORT_SYMBOL(try_acquire_console_sem);
937
938int is_console_locked(void)
939{
940 return console_locked;
941}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Peter Zijlstrab845b512008-08-08 21:47:09 +0200943static DEFINE_PER_CPU(int, printk_pending);
944
945void printk_tick(void)
946{
947 if (__get_cpu_var(printk_pending)) {
948 __get_cpu_var(printk_pending) = 0;
949 wake_up_interruptible(&log_wait);
950 }
951}
952
953int printk_needs_cpu(int cpu)
954{
955 return per_cpu(printk_pending, cpu);
956}
957
Kirill Korotaeve3e8a752007-02-10 01:46:19 -0800958void wake_up_klogd(void)
959{
Peter Zijlstrab845b512008-08-08 21:47:09 +0200960 if (waitqueue_active(&log_wait))
Peter Zijlstrafa335072008-08-20 09:31:26 +0200961 __raw_get_cpu_var(printk_pending) = 1;
Kirill Korotaeve3e8a752007-02-10 01:46:19 -0800962}
963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964/**
965 * release_console_sem - unlock the console system
966 *
967 * Releases the semaphore which the caller holds on the console system
968 * and the console driver list.
969 *
970 * While the semaphore was held, console output may have been buffered
971 * by printk(). If this is the case, release_console_sem() emits
972 * the output prior to releasing the semaphore.
973 *
974 * If there is output waiting for klogd, we wake it up.
975 *
976 * release_console_sem() may be called from any context.
977 */
978void release_console_sem(void)
979{
980 unsigned long flags;
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800981 unsigned _con_start, _log_end;
982 unsigned wake_klogd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Linus Torvalds557240b2006-06-19 18:16:01 -0700984 if (console_suspended) {
Arve Hjønnevåg403f3072009-02-14 02:07:24 +0100985 up(&console_sem);
Linus Torvalds557240b2006-06-19 18:16:01 -0700986 return;
987 }
Antonino A. Daplas78944e542006-08-05 12:14:16 -0700988
989 console_may_schedule = 0;
990
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 for ( ; ; ) {
992 spin_lock_irqsave(&logbuf_lock, flags);
993 wake_klogd |= log_start - log_end;
994 if (con_start == log_end)
995 break; /* Nothing to print */
996 _con_start = con_start;
997 _log_end = log_end;
998 con_start = log_end; /* Flush */
999 spin_unlock(&logbuf_lock);
Steven Rostedt81d68a92008-05-12 21:20:42 +02001000 stop_critical_timings(); /* don't trace print latency */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 call_console_drivers(_con_start, _log_end);
Steven Rostedt81d68a92008-05-12 21:20:42 +02001002 start_critical_timings();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 local_irq_restore(flags);
1004 }
1005 console_locked = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 up(&console_sem);
1007 spin_unlock_irqrestore(&logbuf_lock, flags);
Kirill Korotaeve3e8a752007-02-10 01:46:19 -08001008 if (wake_klogd)
1009 wake_up_klogd();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010}
1011EXPORT_SYMBOL(release_console_sem);
1012
Martin Waitzddad86c2005-11-13 16:08:14 -08001013/**
1014 * console_conditional_schedule - yield the CPU if required
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 *
1016 * If the console code is currently allowed to sleep, and
1017 * if this CPU should yield the CPU to another task, do
1018 * so here.
1019 *
1020 * Must be called within acquire_console_sem().
1021 */
1022void __sched console_conditional_schedule(void)
1023{
1024 if (console_may_schedule)
1025 cond_resched();
1026}
1027EXPORT_SYMBOL(console_conditional_schedule);
1028
1029void console_print(const char *s)
1030{
1031 printk(KERN_EMERG "%s", s);
1032}
1033EXPORT_SYMBOL(console_print);
1034
1035void console_unblank(void)
1036{
1037 struct console *c;
1038
1039 /*
1040 * console_unblank can no longer be called in interrupt context unless
1041 * oops_in_progress is set to 1..
1042 */
1043 if (oops_in_progress) {
1044 if (down_trylock(&console_sem) != 0)
1045 return;
1046 } else
1047 acquire_console_sem();
1048
1049 console_locked = 1;
1050 console_may_schedule = 0;
1051 for (c = console_drivers; c != NULL; c = c->next)
1052 if ((c->flags & CON_ENABLED) && c->unblank)
1053 c->unblank();
1054 release_console_sem();
1055}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
1057/*
1058 * Return the console tty driver structure and its associated index
1059 */
1060struct tty_driver *console_device(int *index)
1061{
1062 struct console *c;
1063 struct tty_driver *driver = NULL;
1064
1065 acquire_console_sem();
1066 for (c = console_drivers; c != NULL; c = c->next) {
1067 if (!c->device)
1068 continue;
1069 driver = c->device(c, index);
1070 if (driver)
1071 break;
1072 }
1073 release_console_sem();
1074 return driver;
1075}
1076
1077/*
1078 * Prevent further output on the passed console device so that (for example)
1079 * serial drivers can disable console output before suspending a port, and can
1080 * re-enable output afterwards.
1081 */
1082void console_stop(struct console *console)
1083{
1084 acquire_console_sem();
1085 console->flags &= ~CON_ENABLED;
1086 release_console_sem();
1087}
1088EXPORT_SYMBOL(console_stop);
1089
1090void console_start(struct console *console)
1091{
1092 acquire_console_sem();
1093 console->flags |= CON_ENABLED;
1094 release_console_sem();
1095}
1096EXPORT_SYMBOL(console_start);
1097
1098/*
1099 * The console driver calls this routine during kernel initialization
1100 * to register the console printing procedure with printk() and to
1101 * print any messages that were printed by the kernel before the
1102 * console driver was initialized.
1103 */
Jesper Juhl40dc5652005-10-30 15:02:46 -08001104void register_console(struct console *console)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105{
Jesper Juhl40dc5652005-10-30 15:02:46 -08001106 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 unsigned long flags;
Gerd Hoffmann69331af2007-05-08 00:26:49 -07001108 struct console *bootconsole = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Gerd Hoffmann69331af2007-05-08 00:26:49 -07001110 if (console_drivers) {
1111 if (console->flags & CON_BOOT)
1112 return;
1113 if (console_drivers->flags & CON_BOOT)
1114 bootconsole = console_drivers;
1115 }
1116
1117 if (preferred_console < 0 || bootconsole || !console_drivers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 preferred_console = selected_console;
1119
Yinghai Lu18a8bd92007-07-15 23:37:59 -07001120 if (console->early_setup)
1121 console->early_setup();
1122
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 /*
1124 * See if we want to use this console driver. If we
1125 * didn't select a console we take the first one
1126 * that registers here.
1127 */
1128 if (preferred_console < 0) {
1129 if (console->index < 0)
1130 console->index = 0;
1131 if (console->setup == NULL ||
1132 console->setup(console, NULL) == 0) {
Jan Kiszkacd3a1b82008-05-12 21:21:04 +02001133 console->flags |= CON_ENABLED;
1134 if (console->device) {
1135 console->flags |= CON_CONSDEV;
1136 preferred_console = 0;
1137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 }
1139 }
1140
1141 /*
1142 * See if this console matches one we selected on
1143 * the command line.
1144 */
Jesper Juhl40dc5652005-10-30 15:02:46 -08001145 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0];
1146 i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 if (strcmp(console_cmdline[i].name, console->name) != 0)
1148 continue;
1149 if (console->index >= 0 &&
1150 console->index != console_cmdline[i].index)
1151 continue;
1152 if (console->index < 0)
1153 console->index = console_cmdline[i].index;
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001154#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1155 if (console_cmdline[i].brl_options) {
1156 console->flags |= CON_BRL;
1157 braille_register_console(console,
1158 console_cmdline[i].index,
1159 console_cmdline[i].options,
1160 console_cmdline[i].brl_options);
1161 return;
1162 }
1163#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 if (console->setup &&
1165 console->setup(console, console_cmdline[i].options) != 0)
1166 break;
1167 console->flags |= CON_ENABLED;
1168 console->index = console_cmdline[i].index;
Greg Edwardsab4af032005-06-23 00:09:05 -07001169 if (i == selected_console) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 console->flags |= CON_CONSDEV;
Greg Edwardsab4af032005-06-23 00:09:05 -07001171 preferred_console = selected_console;
1172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 break;
1174 }
1175
1176 if (!(console->flags & CON_ENABLED))
1177 return;
1178
Yinghai Lud37bf602007-07-15 23:37:28 -07001179 if (bootconsole && (console->flags & CON_CONSDEV)) {
Gerd Hoffmann69331af2007-05-08 00:26:49 -07001180 printk(KERN_INFO "console handover: boot [%s%d] -> real [%s%d]\n",
1181 bootconsole->name, bootconsole->index,
1182 console->name, console->index);
1183 unregister_console(bootconsole);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 console->flags &= ~CON_PRINTBUFFER;
Yinghai Lud37bf602007-07-15 23:37:28 -07001185 } else {
1186 printk(KERN_INFO "console [%s%d] enabled\n",
1187 console->name, console->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 }
1189
1190 /*
1191 * Put this console in the list - keep the
1192 * preferred driver at the head of the list.
1193 */
1194 acquire_console_sem();
1195 if ((console->flags & CON_CONSDEV) || console_drivers == NULL) {
1196 console->next = console_drivers;
1197 console_drivers = console;
Greg Edwardsab4af032005-06-23 00:09:05 -07001198 if (console->next)
1199 console->next->flags &= ~CON_CONSDEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 } else {
1201 console->next = console_drivers->next;
1202 console_drivers->next = console;
1203 }
1204 if (console->flags & CON_PRINTBUFFER) {
1205 /*
1206 * release_console_sem() will print out the buffered messages
1207 * for us.
1208 */
1209 spin_lock_irqsave(&logbuf_lock, flags);
1210 con_start = log_start;
1211 spin_unlock_irqrestore(&logbuf_lock, flags);
1212 }
1213 release_console_sem();
1214}
1215EXPORT_SYMBOL(register_console);
1216
Jesper Juhl40dc5652005-10-30 15:02:46 -08001217int unregister_console(struct console *console)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
Jesper Juhl40dc5652005-10-30 15:02:46 -08001219 struct console *a, *b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 int res = 1;
1221
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001222#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1223 if (console->flags & CON_BRL)
1224 return braille_unregister_console(console);
1225#endif
1226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 acquire_console_sem();
1228 if (console_drivers == console) {
1229 console_drivers=console->next;
1230 res = 0;
Benjamin Herrenschmidte9b15b52005-11-23 13:37:44 -08001231 } else if (console_drivers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 for (a=console_drivers->next, b=console_drivers ;
1233 a; b=a, a=b->next) {
1234 if (a == console) {
1235 b->next = a->next;
1236 res = 0;
1237 break;
Jesper Juhl40dc5652005-10-30 15:02:46 -08001238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 }
1240 }
Jesper Juhl40dc5652005-10-30 15:02:46 -08001241
Gerd Hoffmann69331af2007-05-08 00:26:49 -07001242 /*
Greg Edwardsab4af032005-06-23 00:09:05 -07001243 * If this isn't the last console and it has CON_CONSDEV set, we
1244 * need to set it on the next preferred console.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 */
Gerd Hoffmann69331af2007-05-08 00:26:49 -07001246 if (console_drivers != NULL && console->flags & CON_CONSDEV)
Greg Edwardsab4af032005-06-23 00:09:05 -07001247 console_drivers->flags |= CON_CONSDEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
1249 release_console_sem();
1250 return res;
1251}
1252EXPORT_SYMBOL(unregister_console);
Matt Mackalld59745c2005-05-01 08:59:02 -07001253
Robin Getz0c5564b2007-08-20 15:22:47 -04001254static int __init disable_boot_consoles(void)
1255{
Robin Getzcb00e992007-08-21 23:14:58 -04001256 if (console_drivers != NULL) {
1257 if (console_drivers->flags & CON_BOOT) {
1258 printk(KERN_INFO "turn off boot console %s%d\n",
1259 console_drivers->name, console_drivers->index);
1260 return unregister_console(console_drivers);
1261 }
Robin Getz0c5564b2007-08-20 15:22:47 -04001262 }
1263 return 0;
1264}
1265late_initcall(disable_boot_consoles);
1266
Joe Perches7ef3d2f2008-02-08 04:21:25 -08001267#if defined CONFIG_PRINTK
Dave Young717115e2008-07-25 01:45:58 -07001268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269/*
1270 * printk rate limiting, lifted from the networking subsystem.
1271 *
Uwe Kleine-König641de9d2008-07-29 22:33:38 -07001272 * This enforces a rate limit: not more than 10 kernel messages
1273 * every 5s to make a denial-of-service attack impossible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 */
Uwe Kleine-König641de9d2008-07-29 22:33:38 -07001275DEFINE_RATELIMIT_STATE(printk_ratelimit_state, 5 * HZ, 10);
1276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277int printk_ratelimit(void)
1278{
Dave Young717115e2008-07-25 01:45:58 -07001279 return __ratelimit(&printk_ratelimit_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280}
1281EXPORT_SYMBOL(printk_ratelimit);
Andrew Mortonf46c4832006-11-02 22:07:16 -08001282
1283/**
1284 * printk_timed_ratelimit - caller-controlled printk ratelimiting
1285 * @caller_jiffies: pointer to caller's state
1286 * @interval_msecs: minimum interval between prints
1287 *
1288 * printk_timed_ratelimit() returns true if more than @interval_msecs
1289 * milliseconds have elapsed since the last time printk_timed_ratelimit()
1290 * returned true.
1291 */
1292bool printk_timed_ratelimit(unsigned long *caller_jiffies,
1293 unsigned int interval_msecs)
1294{
1295 if (*caller_jiffies == 0 || time_after(jiffies, *caller_jiffies)) {
1296 *caller_jiffies = jiffies + msecs_to_jiffies(interval_msecs);
1297 return true;
1298 }
1299 return false;
1300}
1301EXPORT_SYMBOL(printk_timed_ratelimit);
Joe Perches7ef3d2f2008-02-08 04:21:25 -08001302#endif