blob: 1fb1382009f3d020c346ff8f063d72a6b49cce43 [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
16 * 01Mar01 Andrew Morton <andrewm@uow.edu.au>
17 */
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 */
41void __attribute__((weak)) early_printk(const char *fmt, ...)
42{
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);
Linus Torvalds557240b2006-06-19 18:16:01 -070076static DECLARE_MUTEX(secondary_console_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077struct console *console_drivers;
Ingo Molnara29d1cf2008-06-02 13:19:08 +020078EXPORT_SYMBOL_GPL(console_drivers);
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080/*
81 * This is used for debugging the mess that is the VT code by
82 * keeping track if we have the console semaphore held. It's
83 * definitely not the perfect debug tool (we don't know if _WE_
84 * hold it are racing, but it helps tracking those weird code
85 * path in the console code where we end up in places I want
86 * locked without the console sempahore held
87 */
Linus Torvalds557240b2006-06-19 18:16:01 -070088static int console_locked, console_suspended;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90/*
91 * logbuf_lock protects log_buf, log_start, log_end, con_start and logged_chars
92 * It is also used in interesting ways to provide interlocking in
93 * release_console_sem().
94 */
95static DEFINE_SPINLOCK(logbuf_lock);
96
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -080097#define LOG_BUF_MASK (log_buf_len-1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098#define LOG_BUF(idx) (log_buf[(idx) & LOG_BUF_MASK])
99
100/*
101 * The indices into log_buf are not constrained to log_buf_len - they
102 * must be masked before subscripting
103 */
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800104static unsigned log_start; /* Index into log_buf: next char to be read by syslog() */
105static unsigned con_start; /* Index into log_buf: next char to be sent to consoles */
106static unsigned log_end; /* Index into log_buf: most-recently-written-char + 1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108/*
109 * Array of consoles built from command line options (console=)
110 */
111struct console_cmdline
112{
113 char name[8]; /* Name of the driver */
114 int index; /* Minor dev. to use */
115 char *options; /* Options for the driver */
Samuel Thibaultf7511d52008-04-30 00:54:51 -0700116#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
117 char *brl_options; /* Options for braille driver */
118#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119};
120
121#define MAX_CMDLINECONSOLES 8
122
123static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
124static int selected_console = -1;
125static int preferred_console = -1;
Markus Armbruster9e124fe2008-05-26 23:31:07 +0100126int console_set_on_cmdline;
127EXPORT_SYMBOL(console_set_on_cmdline);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129/* Flag: console code may call schedule() */
130static int console_may_schedule;
131
Matt Mackalld59745c2005-05-01 08:59:02 -0700132#ifdef CONFIG_PRINTK
133
134static char __log_buf[__LOG_BUF_LEN];
135static char *log_buf = __log_buf;
136static int log_buf_len = __LOG_BUF_LEN;
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800137static unsigned logged_chars; /* Number of chars produced since last read+clear operation */
Matt Mackalld59745c2005-05-01 08:59:02 -0700138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139static int __init log_buf_len_setup(char *str)
140{
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800141 unsigned size = memparse(str, &str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 unsigned long flags;
143
144 if (size)
145 size = roundup_pow_of_two(size);
146 if (size > log_buf_len) {
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800147 unsigned start, dest_idx, offset;
Jesper Juhl40dc5652005-10-30 15:02:46 -0800148 char *new_log_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150 new_log_buf = alloc_bootmem(size);
151 if (!new_log_buf) {
Jesper Juhl40dc5652005-10-30 15:02:46 -0800152 printk(KERN_WARNING "log_buf_len: allocation failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 goto out;
154 }
155
156 spin_lock_irqsave(&logbuf_lock, flags);
157 log_buf_len = size;
158 log_buf = new_log_buf;
159
160 offset = start = min(con_start, log_start);
161 dest_idx = 0;
162 while (start != log_end) {
163 log_buf[dest_idx] = __log_buf[start & (__LOG_BUF_LEN - 1)];
164 start++;
165 dest_idx++;
166 }
167 log_start -= offset;
168 con_start -= offset;
169 log_end -= offset;
170 spin_unlock_irqrestore(&logbuf_lock, flags);
171
Jesper Juhl40dc5652005-10-30 15:02:46 -0800172 printk(KERN_NOTICE "log_buf_len: %d\n", log_buf_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 }
174out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 return 1;
176}
177
178__setup("log_buf_len=", log_buf_len_setup);
179
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700180#ifdef CONFIG_BOOT_PRINTK_DELAY
181
182static unsigned int boot_delay; /* msecs delay after each printk during bootup */
183static unsigned long long printk_delay_msec; /* per msec, based on boot_delay */
184
185static int __init boot_delay_setup(char *str)
186{
187 unsigned long lpj;
188 unsigned long long loops_per_msec;
189
190 lpj = preset_lpj ? preset_lpj : 1000000; /* some guess */
191 loops_per_msec = (unsigned long long)lpj / 1000 * HZ;
192
193 get_option(&str, &boot_delay);
194 if (boot_delay > 10 * 1000)
195 boot_delay = 0;
196
197 printk_delay_msec = loops_per_msec;
198 printk(KERN_DEBUG "boot_delay: %u, preset_lpj: %ld, lpj: %lu, "
199 "HZ: %d, printk_delay_msec: %llu\n",
200 boot_delay, preset_lpj, lpj, HZ, printk_delay_msec);
201 return 1;
202}
203__setup("boot_delay=", boot_delay_setup);
204
205static void boot_delay_msec(void)
206{
207 unsigned long long k;
208 unsigned long timeout;
209
210 if (boot_delay == 0 || system_state != SYSTEM_BOOTING)
211 return;
212
213 k = (unsigned long long)printk_delay_msec * boot_delay;
214
215 timeout = jiffies + msecs_to_jiffies(boot_delay);
216 while (k) {
217 k--;
218 cpu_relax();
219 /*
220 * use (volatile) jiffies to prevent
221 * compiler reduction; loop termination via jiffies
222 * is secondary and may or may not happen.
223 */
224 if (time_after(jiffies, timeout))
225 break;
226 touch_nmi_watchdog();
227 }
228}
229#else
230static inline void boot_delay_msec(void)
231{
232}
233#endif
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235/*
Mike Frysinger0b15d042007-10-16 23:26:43 -0700236 * Return the number of unread characters in the log buffer.
237 */
238int log_buf_get_len(void)
239{
240 return logged_chars;
241}
242
243/*
244 * Copy a range of characters from the log buffer.
245 */
246int log_buf_copy(char *dest, int idx, int len)
247{
248 int ret, max;
249 bool took_lock = false;
250
251 if (!oops_in_progress) {
252 spin_lock_irq(&logbuf_lock);
253 took_lock = true;
254 }
255
256 max = log_buf_get_len();
257 if (idx < 0 || idx >= max) {
258 ret = -1;
259 } else {
260 if (len > max)
261 len = max;
262 ret = len;
263 idx += (log_end - max);
264 while (len-- > 0)
265 dest[len] = LOG_BUF(idx + len);
266 }
267
268 if (took_lock)
269 spin_unlock_irq(&logbuf_lock);
270
271 return ret;
272}
273
274/*
275 * Extract a single character from the log buffer.
276 */
277int log_buf_read(int idx)
278{
279 char ret;
280
281 if (log_buf_copy(&ret, idx, 1) == 1)
282 return ret;
283 else
284 return -1;
285}
286
287/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 * Commands to do_syslog:
289 *
290 * 0 -- Close the log. Currently a NOP.
291 * 1 -- Open the log. Currently a NOP.
292 * 2 -- Read from the log.
293 * 3 -- Read all messages remaining in the ring buffer.
294 * 4 -- Read and clear all messages remaining in the ring buffer
295 * 5 -- Clear ring buffer.
296 * 6 -- Disable printk's to console
297 * 7 -- Enable printk's to console
298 * 8 -- Set level of messages printed to console
299 * 9 -- Return number of unread characters in the log buffer
300 * 10 -- Return size of the log buffer
301 */
Jesper Juhl40dc5652005-10-30 15:02:46 -0800302int do_syslog(int type, char __user *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800304 unsigned i, j, limit, count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 int do_clear = 0;
306 char c;
307 int error = 0;
308
309 error = security_syslog(type);
310 if (error)
311 return error;
312
313 switch (type) {
314 case 0: /* Close log */
315 break;
316 case 1: /* Open log */
317 break;
318 case 2: /* Read from log */
319 error = -EINVAL;
320 if (!buf || len < 0)
321 goto out;
322 error = 0;
323 if (!len)
324 goto out;
325 if (!access_ok(VERIFY_WRITE, buf, len)) {
326 error = -EFAULT;
327 goto out;
328 }
Jesper Juhl40dc5652005-10-30 15:02:46 -0800329 error = wait_event_interruptible(log_wait,
330 (log_start - log_end));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 if (error)
332 goto out;
333 i = 0;
334 spin_lock_irq(&logbuf_lock);
335 while (!error && (log_start != log_end) && i < len) {
336 c = LOG_BUF(log_start);
337 log_start++;
338 spin_unlock_irq(&logbuf_lock);
339 error = __put_user(c,buf);
340 buf++;
341 i++;
342 cond_resched();
343 spin_lock_irq(&logbuf_lock);
344 }
345 spin_unlock_irq(&logbuf_lock);
346 if (!error)
347 error = i;
348 break;
349 case 4: /* Read/clear last kernel messages */
Jesper Juhl40dc5652005-10-30 15:02:46 -0800350 do_clear = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 /* FALL THRU */
352 case 3: /* Read last kernel messages */
353 error = -EINVAL;
354 if (!buf || len < 0)
355 goto out;
356 error = 0;
357 if (!len)
358 goto out;
359 if (!access_ok(VERIFY_WRITE, buf, len)) {
360 error = -EFAULT;
361 goto out;
362 }
363 count = len;
364 if (count > log_buf_len)
365 count = log_buf_len;
366 spin_lock_irq(&logbuf_lock);
367 if (count > logged_chars)
368 count = logged_chars;
369 if (do_clear)
370 logged_chars = 0;
371 limit = log_end;
372 /*
373 * __put_user() could sleep, and while we sleep
Jesper Juhl40dc5652005-10-30 15:02:46 -0800374 * printk() could overwrite the messages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 * we try to copy to user space. Therefore
376 * the messages are copied in reverse. <manfreds>
377 */
Jesper Juhl40dc5652005-10-30 15:02:46 -0800378 for (i = 0; i < count && !error; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 j = limit-1-i;
380 if (j + log_buf_len < log_end)
381 break;
382 c = LOG_BUF(j);
383 spin_unlock_irq(&logbuf_lock);
384 error = __put_user(c,&buf[count-1-i]);
385 cond_resched();
386 spin_lock_irq(&logbuf_lock);
387 }
388 spin_unlock_irq(&logbuf_lock);
389 if (error)
390 break;
391 error = i;
Jesper Juhl40dc5652005-10-30 15:02:46 -0800392 if (i != count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 int offset = count-error;
394 /* buffer overflow during copy, correct user buffer. */
Jesper Juhl40dc5652005-10-30 15:02:46 -0800395 for (i = 0; i < error; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if (__get_user(c,&buf[i+offset]) ||
397 __put_user(c,&buf[i])) {
398 error = -EFAULT;
399 break;
400 }
401 cond_resched();
402 }
403 }
404 break;
405 case 5: /* Clear ring buffer */
406 logged_chars = 0;
407 break;
408 case 6: /* Disable logging to console */
409 console_loglevel = minimum_console_loglevel;
410 break;
411 case 7: /* Enable logging to console */
412 console_loglevel = default_console_loglevel;
413 break;
414 case 8: /* Set level of messages printed to console */
415 error = -EINVAL;
416 if (len < 1 || len > 8)
417 goto out;
418 if (len < minimum_console_loglevel)
419 len = minimum_console_loglevel;
420 console_loglevel = len;
421 error = 0;
422 break;
423 case 9: /* Number of chars in the log buffer */
424 error = log_end - log_start;
425 break;
426 case 10: /* Size of the log buffer */
427 error = log_buf_len;
428 break;
429 default:
430 error = -EINVAL;
431 break;
432 }
433out:
434 return error;
435}
436
Jesper Juhl40dc5652005-10-30 15:02:46 -0800437asmlinkage long sys_syslog(int type, char __user *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
439 return do_syslog(type, buf, len);
440}
441
442/*
443 * Call the console drivers on a range of log_buf
444 */
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800445static void __call_console_drivers(unsigned start, unsigned end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
447 struct console *con;
448
449 for (con = console_drivers; con; con = con->next) {
Michael Ellerman76a8ad22006-06-25 05:47:40 -0700450 if ((con->flags & CON_ENABLED) && con->write &&
451 (cpu_online(smp_processor_id()) ||
452 (con->flags & CON_ANYTIME)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 con->write(con, &LOG_BUF(start), end - start);
454 }
455}
456
Ingo Molnar792908222006-12-06 20:40:51 -0800457static int __read_mostly ignore_loglevel;
458
Adrian Bunk99eea6a2006-12-22 01:07:00 -0800459static int __init ignore_loglevel_setup(char *str)
Ingo Molnar792908222006-12-06 20:40:51 -0800460{
461 ignore_loglevel = 1;
462 printk(KERN_INFO "debug: ignoring loglevel setting.\n");
463
Ingo Molnarc4772d92008-01-31 22:45:23 +0100464 return 0;
Ingo Molnar792908222006-12-06 20:40:51 -0800465}
466
Ingo Molnarc4772d92008-01-31 22:45:23 +0100467early_param("ignore_loglevel", ignore_loglevel_setup);
Ingo Molnar792908222006-12-06 20:40:51 -0800468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469/*
470 * Write out chars from start to end - 1 inclusive
471 */
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800472static void _call_console_drivers(unsigned start,
473 unsigned end, int msg_log_level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
Ingo Molnar792908222006-12-06 20:40:51 -0800475 if ((msg_log_level < console_loglevel || ignore_loglevel) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 console_drivers && start != end) {
477 if ((start & LOG_BUF_MASK) > (end & LOG_BUF_MASK)) {
478 /* wrapped write */
479 __call_console_drivers(start & LOG_BUF_MASK,
480 log_buf_len);
481 __call_console_drivers(0, end & LOG_BUF_MASK);
482 } else {
483 __call_console_drivers(start, end);
484 }
485 }
486}
487
488/*
489 * Call the console drivers, asking them to write out
490 * log_buf[start] to log_buf[end - 1].
491 * The console_sem must be held.
492 */
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800493static void call_console_drivers(unsigned start, unsigned end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800495 unsigned cur_index, start_print;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 static int msg_level = -1;
497
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800498 BUG_ON(((int)(start - end)) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 cur_index = start;
501 start_print = start;
502 while (cur_index != end) {
Jesper Juhl40dc5652005-10-30 15:02:46 -0800503 if (msg_level < 0 && ((end - cur_index) > 2) &&
504 LOG_BUF(cur_index + 0) == '<' &&
505 LOG_BUF(cur_index + 1) >= '0' &&
506 LOG_BUF(cur_index + 1) <= '7' &&
507 LOG_BUF(cur_index + 2) == '>') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 msg_level = LOG_BUF(cur_index + 1) - '0';
509 cur_index += 3;
510 start_print = cur_index;
511 }
512 while (cur_index != end) {
513 char c = LOG_BUF(cur_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Jesper Juhl40dc5652005-10-30 15:02:46 -0800515 cur_index++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 if (c == '\n') {
517 if (msg_level < 0) {
518 /*
519 * printk() has already given us loglevel tags in
520 * the buffer. This code is here in case the
521 * log buffer has wrapped right round and scribbled
522 * on those tags
523 */
524 msg_level = default_message_loglevel;
525 }
526 _call_console_drivers(start_print, cur_index, msg_level);
527 msg_level = -1;
528 start_print = cur_index;
529 break;
530 }
531 }
532 }
533 _call_console_drivers(start_print, end, msg_level);
534}
535
536static void emit_log_char(char c)
537{
538 LOG_BUF(log_end) = c;
539 log_end++;
540 if (log_end - log_start > log_buf_len)
541 log_start = log_end - log_buf_len;
542 if (log_end - con_start > log_buf_len)
543 con_start = log_end - log_buf_len;
544 if (logged_chars < log_buf_len)
545 logged_chars++;
546}
547
548/*
549 * Zap console related locks when oopsing. Only zap at most once
550 * every 10 seconds, to leave time for slow consoles to print a
551 * full oops.
552 */
553static void zap_locks(void)
554{
555 static unsigned long oops_timestamp;
556
557 if (time_after_eq(jiffies, oops_timestamp) &&
Jesper Juhl40dc5652005-10-30 15:02:46 -0800558 !time_after(jiffies, oops_timestamp + 30 * HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 return;
560
561 oops_timestamp = jiffies;
562
563 /* If a crash is occurring, make sure we can't deadlock */
564 spin_lock_init(&logbuf_lock);
565 /* And make sure that we print immediately */
566 init_MUTEX(&console_sem);
567}
568
569#if defined(CONFIG_PRINTK_TIME)
570static int printk_time = 1;
571#else
572static int printk_time = 0;
573#endif
Randy Dunlape84845c2007-07-15 23:40:25 -0700574module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Michael Ellerman76a8ad22006-06-25 05:47:40 -0700576/* Check if we have any console registered that can be called early in boot. */
577static int have_callable_console(void)
578{
579 struct console *con;
580
581 for (con = console_drivers; con; con = con->next)
582 if (con->flags & CON_ANYTIME)
583 return 1;
584
585 return 0;
586}
587
Martin Waitzddad86c2005-11-13 16:08:14 -0800588/**
589 * printk - print a kernel message
590 * @fmt: format string
591 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800592 * This is printk(). It can be called from any context. We want it to work.
Jiri Kosina14921922007-07-15 23:41:51 -0700593 * Be aware of the fact that if oops_in_progress is not set, we might try to
594 * wake klogd up which could deadlock on runqueue lock if printk() is called
595 * from scheduler code.
Jesper Juhl40dc5652005-10-30 15:02:46 -0800596 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 * We try to grab the console_sem. If we succeed, it's easy - we log the output and
598 * call the console drivers. If we fail to get the semaphore we place the output
599 * into the log buffer and return. The current holder of the console_sem will
600 * notice the new output in release_console_sem() and will send it to the
601 * consoles before releasing the semaphore.
602 *
603 * One effect of this deferred printing is that code which calls printk() and
604 * then changes console_loglevel may break. This is because console_loglevel
605 * is inspected when the actual printing occurs.
Martin Waitzddad86c2005-11-13 16:08:14 -0800606 *
607 * See also:
608 * printf(3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 */
Matt Mackalld59745c2005-05-01 08:59:02 -0700610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611asmlinkage int printk(const char *fmt, ...)
612{
613 va_list args;
614 int r;
615
616 va_start(args, fmt);
617 r = vprintk(fmt, args);
618 va_end(args);
619
620 return r;
621}
622
David Howellsfe217732005-09-06 15:16:34 -0700623/* cpu currently holding logbuf_lock */
624static volatile unsigned int printk_cpu = UINT_MAX;
625
Linus Torvalds266c2e02008-03-24 19:25:08 -0700626/*
627 * Can we actually use the console at this time on this cpu?
628 *
629 * Console drivers may assume that per-cpu resources have
630 * been allocated. So unless they're explicitly marked as
631 * being able to cope (CON_ANYTIME) don't call them until
632 * this CPU is officially up.
633 */
634static inline int can_use_console(unsigned int cpu)
635{
636 return cpu_online(cpu) || have_callable_console();
637}
638
639/*
640 * Try to get console ownership to actually show the kernel
641 * messages from a 'printk'. Return true (and with the
642 * console_semaphore held, and 'console_locked' set) if it
643 * is successful, false otherwise.
644 *
645 * This gets called with the 'logbuf_lock' spinlock held and
646 * interrupts disabled. It should return with 'lockbuf_lock'
647 * released but interrupts still disabled.
648 */
649static int acquire_console_semaphore_for_printk(unsigned int cpu)
650{
651 int retval = 0;
652
Linus Torvalds093a07e2008-04-15 13:09:54 -0700653 if (!try_acquire_console_sem()) {
654 retval = 1;
655
656 /*
657 * If we can't use the console, we need to release
658 * the console semaphore by hand to avoid flushing
659 * the buffer. We need to hold the console semaphore
660 * in order to do this test safely.
661 */
662 if (!can_use_console(cpu)) {
663 console_locked = 0;
664 up(&console_sem);
665 retval = 0;
666 }
667 }
Linus Torvalds266c2e02008-03-24 19:25:08 -0700668 printk_cpu = UINT_MAX;
669 spin_unlock(&logbuf_lock);
670 return retval;
671}
672
Ingo Molnar32a76002008-01-25 21:07:58 +0100673const char printk_recursion_bug_msg [] =
674 KERN_CRIT "BUG: recent printk recursion!\n";
675static int printk_recursion_bug;
676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677asmlinkage int vprintk(const char *fmt, va_list args)
678{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 static int log_level_unknown = 1;
Ingo Molnar32a76002008-01-25 21:07:58 +0100680 static char printk_buf[1024];
681
682 unsigned long flags;
683 int printed_len = 0;
684 int this_cpu;
685 char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700687 boot_delay_msec();
688
David Howellsfe217732005-09-06 15:16:34 -0700689 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 /* This stops the holder of console_sem just where we want him */
Mathieu Desnoyers1efc5da2007-02-10 01:46:29 -0800691 raw_local_irq_save(flags);
Ingo Molnar32a76002008-01-25 21:07:58 +0100692 this_cpu = smp_processor_id();
693
694 /*
695 * Ouch, printk recursed into itself!
696 */
697 if (unlikely(printk_cpu == this_cpu)) {
698 /*
699 * If a crash is occurring during printk() on this CPU,
700 * then try to get the crash message out but make sure
701 * we can't deadlock. Otherwise just return to avoid the
702 * recursion and return - but flag the recursion so that
703 * it can be printed at the next appropriate moment:
704 */
705 if (!oops_in_progress) {
706 printk_recursion_bug = 1;
707 goto out_restore_irqs;
708 }
709 zap_locks();
710 }
711
Ingo Molnara0f1ccf2006-07-03 00:24:58 -0700712 lockdep_off();
713 spin_lock(&logbuf_lock);
Ingo Molnar32a76002008-01-25 21:07:58 +0100714 printk_cpu = this_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Ingo Molnar32a76002008-01-25 21:07:58 +0100716 if (printk_recursion_bug) {
717 printk_recursion_bug = 0;
718 strcpy(printk_buf, printk_recursion_bug_msg);
719 printed_len = sizeof(printk_recursion_bug_msg);
720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 /* Emit the output into the temporary buffer */
Ingo Molnar32a76002008-01-25 21:07:58 +0100722 printed_len += vscnprintf(printk_buf + printed_len,
Tejun Heocf3680b2008-02-14 10:32:07 +0900723 sizeof(printk_buf) - printed_len, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 /*
726 * Copy the output into log_buf. If the caller didn't provide
727 * appropriate log level tags, we insert them here
728 */
729 for (p = printk_buf; *p; p++) {
730 if (log_level_unknown) {
731 /* log_level_unknown signals the start of a new line */
732 if (printk_time) {
733 int loglev_char;
734 char tbuf[50], *tp;
735 unsigned tlen;
736 unsigned long long t;
737 unsigned long nanosec_rem;
738
739 /*
740 * force the log level token to be
741 * before the time output.
742 */
743 if (p[0] == '<' && p[1] >='0' &&
744 p[1] <= '7' && p[2] == '>') {
745 loglev_char = p[1];
746 p += 3;
Guillaume Chazarain025510c2006-01-08 01:02:41 -0800747 printed_len -= 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 } else {
749 loglev_char = default_message_loglevel
750 + '0';
751 }
Ingo Molnar326e96b2008-01-27 08:03:54 +0100752 t = cpu_clock(printk_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 nanosec_rem = do_div(t, 1000000000);
754 tlen = sprintf(tbuf,
755 "<%c>[%5lu.%06lu] ",
756 loglev_char,
757 (unsigned long)t,
758 nanosec_rem/1000);
759
760 for (tp = tbuf; tp < tbuf + tlen; tp++)
761 emit_log_char(*tp);
Guillaume Chazarain025510c2006-01-08 01:02:41 -0800762 printed_len += tlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 } else {
764 if (p[0] != '<' || p[1] < '0' ||
765 p[1] > '7' || p[2] != '>') {
766 emit_log_char('<');
767 emit_log_char(default_message_loglevel
768 + '0');
769 emit_log_char('>');
Guillaume Chazarain025510c2006-01-08 01:02:41 -0800770 printed_len += 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 }
773 log_level_unknown = 0;
774 if (!*p)
775 break;
776 }
777 emit_log_char(*p);
778 if (*p == '\n')
779 log_level_unknown = 1;
780 }
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
Jesper Juhl40dc5652005-10-30 15:02:46 -0800807asmlinkage long sys_syslog(int type, char __user *buf, int len)
Matt Mackalld59745c2005-05-01 08:59:02 -0700808{
Mike Galbraithc36264d2006-12-06 20:37:42 -0800809 return -ENOSYS;
Jesper Juhl40dc5652005-10-30 15:02:46 -0800810}
811
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800812static void call_console_drivers(unsigned start, unsigned end)
Jesper Juhl40dc5652005-10-30 15:02:46 -0800813{
814}
Matt Mackalld59745c2005-05-01 08:59:02 -0700815
816#endif
817
Samuel Thibaultf7511d52008-04-30 00:54:51 -0700818static int __add_preferred_console(char *name, int idx, char *options,
819 char *brl_options)
820{
821 struct console_cmdline *c;
822 int i;
823
824 /*
825 * See if this tty is not yet registered, and
826 * if we have a slot free.
827 */
828 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
829 if (strcmp(console_cmdline[i].name, name) == 0 &&
830 console_cmdline[i].index == idx) {
831 if (!brl_options)
832 selected_console = i;
833 return 0;
834 }
835 if (i == MAX_CMDLINECONSOLES)
836 return -E2BIG;
837 if (!brl_options)
838 selected_console = i;
839 c = &console_cmdline[i];
840 strlcpy(c->name, name, sizeof(c->name));
841 c->options = options;
842#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
843 c->brl_options = brl_options;
844#endif
845 c->index = idx;
846 return 0;
847}
John Z. Bohach2ea1c532006-03-24 03:18:19 -0800848/*
849 * Set up a list of consoles. Called from init/main.c
850 */
851static int __init console_setup(char *str)
852{
Yinghai Lueaa944a2007-07-15 23:37:27 -0700853 char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for index */
Samuel Thibaultf7511d52008-04-30 00:54:51 -0700854 char *s, *options, *brl_options = NULL;
John Z. Bohach2ea1c532006-03-24 03:18:19 -0800855 int idx;
856
Samuel Thibaultf7511d52008-04-30 00:54:51 -0700857#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
858 if (!memcmp(str, "brl,", 4)) {
859 brl_options = "";
860 str += 4;
861 } else if (!memcmp(str, "brl=", 4)) {
862 brl_options = str + 4;
863 str = strchr(brl_options, ',');
864 if (!str) {
865 printk(KERN_ERR "need port name after brl=\n");
866 return 1;
867 }
868 *(str++) = 0;
869 }
870#endif
871
John Z. Bohach2ea1c532006-03-24 03:18:19 -0800872 /*
873 * Decode str into name, index, options.
874 */
875 if (str[0] >= '0' && str[0] <= '9') {
Yinghai Lueaa944a2007-07-15 23:37:27 -0700876 strcpy(buf, "ttyS");
877 strncpy(buf + 4, str, sizeof(buf) - 5);
John Z. Bohach2ea1c532006-03-24 03:18:19 -0800878 } else {
Yinghai Lueaa944a2007-07-15 23:37:27 -0700879 strncpy(buf, str, sizeof(buf) - 1);
John Z. Bohach2ea1c532006-03-24 03:18:19 -0800880 }
Yinghai Lueaa944a2007-07-15 23:37:27 -0700881 buf[sizeof(buf) - 1] = 0;
John Z. Bohach2ea1c532006-03-24 03:18:19 -0800882 if ((options = strchr(str, ',')) != NULL)
883 *(options++) = 0;
884#ifdef __sparc__
885 if (!strcmp(str, "ttya"))
Yinghai Lueaa944a2007-07-15 23:37:27 -0700886 strcpy(buf, "ttyS0");
John Z. Bohach2ea1c532006-03-24 03:18:19 -0800887 if (!strcmp(str, "ttyb"))
Yinghai Lueaa944a2007-07-15 23:37:27 -0700888 strcpy(buf, "ttyS1");
John Z. Bohach2ea1c532006-03-24 03:18:19 -0800889#endif
Yinghai Lueaa944a2007-07-15 23:37:27 -0700890 for (s = buf; *s; s++)
John Z. Bohach2ea1c532006-03-24 03:18:19 -0800891 if ((*s >= '0' && *s <= '9') || *s == ',')
892 break;
893 idx = simple_strtoul(s, NULL, 10);
894 *s = 0;
895
Samuel Thibaultf7511d52008-04-30 00:54:51 -0700896 __add_preferred_console(buf, idx, options, brl_options);
Markus Armbruster9e124fe2008-05-26 23:31:07 +0100897 console_set_on_cmdline = 1;
John Z. Bohach2ea1c532006-03-24 03:18:19 -0800898 return 1;
899}
900__setup("console=", console_setup);
901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902/**
Matt Mackall3c0547b2005-05-16 21:53:47 -0700903 * add_preferred_console - add a device to the list of preferred consoles.
Martin Waitzddad86c2005-11-13 16:08:14 -0800904 * @name: device name
905 * @idx: device index
906 * @options: options for this console
Matt Mackall3c0547b2005-05-16 21:53:47 -0700907 *
908 * The last preferred console added will be used for kernel messages
909 * and stdin/out/err for init. Normally this is used by console_setup
910 * above to handle user-supplied console arguments; however it can also
911 * be used by arch-specific code either to override the user or more
912 * commonly to provide a default console (ie from PROM variables) when
913 * the user has not supplied one.
914 */
David S. Millerfb445ee2007-12-29 01:19:49 -0800915int add_preferred_console(char *name, int idx, char *options)
Matt Mackall3c0547b2005-05-16 21:53:47 -0700916{
Samuel Thibaultf7511d52008-04-30 00:54:51 -0700917 return __add_preferred_console(name, idx, options, NULL);
Matt Mackall3c0547b2005-05-16 21:53:47 -0700918}
919
Daniel Ritzb6b1d872007-08-03 16:07:43 +0200920int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options)
Yinghai Lu18a8bd92007-07-15 23:37:59 -0700921{
922 struct console_cmdline *c;
923 int i;
924
925 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
926 if (strcmp(console_cmdline[i].name, name) == 0 &&
927 console_cmdline[i].index == idx) {
928 c = &console_cmdline[i];
Markus Armbrusterf7352952008-04-30 00:54:52 -0700929 strlcpy(c->name, name_new, sizeof(c->name));
Yinghai Lu18a8bd92007-07-15 23:37:59 -0700930 c->name[sizeof(c->name) - 1] = 0;
931 c->options = options;
932 c->index = idx_new;
933 return i;
934 }
935 /* not found */
936 return -1;
937}
938
Andres Salomon8f4ce8c2007-10-18 03:04:50 -0700939int console_suspend_enabled = 1;
940EXPORT_SYMBOL(console_suspend_enabled);
941
942static int __init console_suspend_disable(char *str)
943{
944 console_suspend_enabled = 0;
945 return 1;
946}
947__setup("no_console_suspend", console_suspend_disable);
948
Matt Mackall3c0547b2005-05-16 21:53:47 -0700949/**
Linus Torvalds557240b2006-06-19 18:16:01 -0700950 * suspend_console - suspend the console subsystem
951 *
952 * This disables printk() while we go into suspend states
953 */
954void suspend_console(void)
955{
Andres Salomon8f4ce8c2007-10-18 03:04:50 -0700956 if (!console_suspend_enabled)
957 return;
Rafael J. Wysockic8eb8b42006-09-25 23:32:56 -0700958 printk("Suspending console(s)\n");
Linus Torvalds557240b2006-06-19 18:16:01 -0700959 acquire_console_sem();
960 console_suspended = 1;
961}
962
963void resume_console(void)
964{
Andres Salomon8f4ce8c2007-10-18 03:04:50 -0700965 if (!console_suspend_enabled)
966 return;
Linus Torvalds557240b2006-06-19 18:16:01 -0700967 console_suspended = 0;
968 release_console_sem();
969}
970
971/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 * acquire_console_sem - lock the console system for exclusive use.
973 *
974 * Acquires a semaphore which guarantees that the caller has
975 * exclusive access to the console system and the console_drivers list.
976 *
977 * Can sleep, returns nothing.
978 */
979void acquire_console_sem(void)
980{
Eric Sesterhenn8abd8e22006-04-01 01:21:17 +0200981 BUG_ON(in_interrupt());
Linus Torvalds557240b2006-06-19 18:16:01 -0700982 if (console_suspended) {
983 down(&secondary_console_sem);
984 return;
985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 down(&console_sem);
987 console_locked = 1;
988 console_may_schedule = 1;
989}
990EXPORT_SYMBOL(acquire_console_sem);
991
992int try_acquire_console_sem(void)
993{
994 if (down_trylock(&console_sem))
995 return -1;
996 console_locked = 1;
997 console_may_schedule = 0;
998 return 0;
999}
1000EXPORT_SYMBOL(try_acquire_console_sem);
1001
1002int is_console_locked(void)
1003{
1004 return console_locked;
1005}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Kirill Korotaeve3e8a752007-02-10 01:46:19 -08001007void wake_up_klogd(void)
1008{
1009 if (!oops_in_progress && waitqueue_active(&log_wait))
1010 wake_up_interruptible(&log_wait);
1011}
1012
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013/**
1014 * release_console_sem - unlock the console system
1015 *
1016 * Releases the semaphore which the caller holds on the console system
1017 * and the console driver list.
1018 *
1019 * While the semaphore was held, console output may have been buffered
1020 * by printk(). If this is the case, release_console_sem() emits
1021 * the output prior to releasing the semaphore.
1022 *
1023 * If there is output waiting for klogd, we wake it up.
1024 *
1025 * release_console_sem() may be called from any context.
1026 */
1027void release_console_sem(void)
1028{
1029 unsigned long flags;
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -08001030 unsigned _con_start, _log_end;
1031 unsigned wake_klogd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
Linus Torvalds557240b2006-06-19 18:16:01 -07001033 if (console_suspended) {
1034 up(&secondary_console_sem);
1035 return;
1036 }
Antonino A. Daplas78944e52006-08-05 12:14:16 -07001037
1038 console_may_schedule = 0;
1039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 for ( ; ; ) {
1041 spin_lock_irqsave(&logbuf_lock, flags);
1042 wake_klogd |= log_start - log_end;
1043 if (con_start == log_end)
1044 break; /* Nothing to print */
1045 _con_start = con_start;
1046 _log_end = log_end;
1047 con_start = log_end; /* Flush */
1048 spin_unlock(&logbuf_lock);
1049 call_console_drivers(_con_start, _log_end);
1050 local_irq_restore(flags);
1051 }
1052 console_locked = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 up(&console_sem);
1054 spin_unlock_irqrestore(&logbuf_lock, flags);
Kirill Korotaeve3e8a752007-02-10 01:46:19 -08001055 if (wake_klogd)
1056 wake_up_klogd();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057}
1058EXPORT_SYMBOL(release_console_sem);
1059
Martin Waitzddad86c2005-11-13 16:08:14 -08001060/**
1061 * console_conditional_schedule - yield the CPU if required
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 *
1063 * If the console code is currently allowed to sleep, and
1064 * if this CPU should yield the CPU to another task, do
1065 * so here.
1066 *
1067 * Must be called within acquire_console_sem().
1068 */
1069void __sched console_conditional_schedule(void)
1070{
1071 if (console_may_schedule)
1072 cond_resched();
1073}
1074EXPORT_SYMBOL(console_conditional_schedule);
1075
1076void console_print(const char *s)
1077{
1078 printk(KERN_EMERG "%s", s);
1079}
1080EXPORT_SYMBOL(console_print);
1081
1082void console_unblank(void)
1083{
1084 struct console *c;
1085
1086 /*
1087 * console_unblank can no longer be called in interrupt context unless
1088 * oops_in_progress is set to 1..
1089 */
1090 if (oops_in_progress) {
1091 if (down_trylock(&console_sem) != 0)
1092 return;
1093 } else
1094 acquire_console_sem();
1095
1096 console_locked = 1;
1097 console_may_schedule = 0;
1098 for (c = console_drivers; c != NULL; c = c->next)
1099 if ((c->flags & CON_ENABLED) && c->unblank)
1100 c->unblank();
1101 release_console_sem();
1102}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
1104/*
1105 * Return the console tty driver structure and its associated index
1106 */
1107struct tty_driver *console_device(int *index)
1108{
1109 struct console *c;
1110 struct tty_driver *driver = NULL;
1111
1112 acquire_console_sem();
1113 for (c = console_drivers; c != NULL; c = c->next) {
1114 if (!c->device)
1115 continue;
1116 driver = c->device(c, index);
1117 if (driver)
1118 break;
1119 }
1120 release_console_sem();
1121 return driver;
1122}
1123
1124/*
1125 * Prevent further output on the passed console device so that (for example)
1126 * serial drivers can disable console output before suspending a port, and can
1127 * re-enable output afterwards.
1128 */
1129void console_stop(struct console *console)
1130{
1131 acquire_console_sem();
1132 console->flags &= ~CON_ENABLED;
1133 release_console_sem();
1134}
1135EXPORT_SYMBOL(console_stop);
1136
1137void console_start(struct console *console)
1138{
1139 acquire_console_sem();
1140 console->flags |= CON_ENABLED;
1141 release_console_sem();
1142}
1143EXPORT_SYMBOL(console_start);
1144
1145/*
1146 * The console driver calls this routine during kernel initialization
1147 * to register the console printing procedure with printk() and to
1148 * print any messages that were printed by the kernel before the
1149 * console driver was initialized.
1150 */
Jesper Juhl40dc5652005-10-30 15:02:46 -08001151void register_console(struct console *console)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152{
Jesper Juhl40dc5652005-10-30 15:02:46 -08001153 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 unsigned long flags;
Gerd Hoffmann69331af2007-05-08 00:26:49 -07001155 struct console *bootconsole = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
Gerd Hoffmann69331af2007-05-08 00:26:49 -07001157 if (console_drivers) {
1158 if (console->flags & CON_BOOT)
1159 return;
1160 if (console_drivers->flags & CON_BOOT)
1161 bootconsole = console_drivers;
1162 }
1163
1164 if (preferred_console < 0 || bootconsole || !console_drivers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 preferred_console = selected_console;
1166
Yinghai Lu18a8bd92007-07-15 23:37:59 -07001167 if (console->early_setup)
1168 console->early_setup();
1169
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 /*
1171 * See if we want to use this console driver. If we
1172 * didn't select a console we take the first one
1173 * that registers here.
1174 */
1175 if (preferred_console < 0) {
1176 if (console->index < 0)
1177 console->index = 0;
1178 if (console->setup == NULL ||
1179 console->setup(console, NULL) == 0) {
1180 console->flags |= CON_ENABLED | CON_CONSDEV;
1181 preferred_console = 0;
1182 }
1183 }
1184
1185 /*
1186 * See if this console matches one we selected on
1187 * the command line.
1188 */
Jesper Juhl40dc5652005-10-30 15:02:46 -08001189 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0];
1190 i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 if (strcmp(console_cmdline[i].name, console->name) != 0)
1192 continue;
1193 if (console->index >= 0 &&
1194 console->index != console_cmdline[i].index)
1195 continue;
1196 if (console->index < 0)
1197 console->index = console_cmdline[i].index;
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001198#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1199 if (console_cmdline[i].brl_options) {
1200 console->flags |= CON_BRL;
1201 braille_register_console(console,
1202 console_cmdline[i].index,
1203 console_cmdline[i].options,
1204 console_cmdline[i].brl_options);
1205 return;
1206 }
1207#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 if (console->setup &&
1209 console->setup(console, console_cmdline[i].options) != 0)
1210 break;
1211 console->flags |= CON_ENABLED;
1212 console->index = console_cmdline[i].index;
Greg Edwardsab4af032005-06-23 00:09:05 -07001213 if (i == selected_console) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 console->flags |= CON_CONSDEV;
Greg Edwardsab4af032005-06-23 00:09:05 -07001215 preferred_console = selected_console;
1216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 break;
1218 }
1219
1220 if (!(console->flags & CON_ENABLED))
1221 return;
1222
Yinghai Lud37bf602007-07-15 23:37:28 -07001223 if (bootconsole && (console->flags & CON_CONSDEV)) {
Gerd Hoffmann69331af2007-05-08 00:26:49 -07001224 printk(KERN_INFO "console handover: boot [%s%d] -> real [%s%d]\n",
1225 bootconsole->name, bootconsole->index,
1226 console->name, console->index);
1227 unregister_console(bootconsole);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 console->flags &= ~CON_PRINTBUFFER;
Yinghai Lud37bf602007-07-15 23:37:28 -07001229 } else {
1230 printk(KERN_INFO "console [%s%d] enabled\n",
1231 console->name, console->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 }
1233
1234 /*
1235 * Put this console in the list - keep the
1236 * preferred driver at the head of the list.
1237 */
1238 acquire_console_sem();
1239 if ((console->flags & CON_CONSDEV) || console_drivers == NULL) {
1240 console->next = console_drivers;
1241 console_drivers = console;
Greg Edwardsab4af032005-06-23 00:09:05 -07001242 if (console->next)
1243 console->next->flags &= ~CON_CONSDEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 } else {
1245 console->next = console_drivers->next;
1246 console_drivers->next = console;
1247 }
1248 if (console->flags & CON_PRINTBUFFER) {
1249 /*
1250 * release_console_sem() will print out the buffered messages
1251 * for us.
1252 */
1253 spin_lock_irqsave(&logbuf_lock, flags);
1254 con_start = log_start;
1255 spin_unlock_irqrestore(&logbuf_lock, flags);
1256 }
1257 release_console_sem();
1258}
1259EXPORT_SYMBOL(register_console);
1260
Jesper Juhl40dc5652005-10-30 15:02:46 -08001261int unregister_console(struct console *console)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262{
Jesper Juhl40dc5652005-10-30 15:02:46 -08001263 struct console *a, *b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 int res = 1;
1265
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001266#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1267 if (console->flags & CON_BRL)
1268 return braille_unregister_console(console);
1269#endif
1270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 acquire_console_sem();
1272 if (console_drivers == console) {
1273 console_drivers=console->next;
1274 res = 0;
Benjamin Herrenschmidte9b15b52005-11-23 13:37:44 -08001275 } else if (console_drivers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 for (a=console_drivers->next, b=console_drivers ;
1277 a; b=a, a=b->next) {
1278 if (a == console) {
1279 b->next = a->next;
1280 res = 0;
1281 break;
Jesper Juhl40dc5652005-10-30 15:02:46 -08001282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 }
1284 }
Jesper Juhl40dc5652005-10-30 15:02:46 -08001285
Gerd Hoffmann69331af2007-05-08 00:26:49 -07001286 /*
Greg Edwardsab4af032005-06-23 00:09:05 -07001287 * If this isn't the last console and it has CON_CONSDEV set, we
1288 * need to set it on the next preferred console.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 */
Gerd Hoffmann69331af2007-05-08 00:26:49 -07001290 if (console_drivers != NULL && console->flags & CON_CONSDEV)
Greg Edwardsab4af032005-06-23 00:09:05 -07001291 console_drivers->flags |= CON_CONSDEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
1293 release_console_sem();
1294 return res;
1295}
1296EXPORT_SYMBOL(unregister_console);
Matt Mackalld59745c2005-05-01 08:59:02 -07001297
Robin Getz0c5564b2007-08-20 15:22:47 -04001298static int __init disable_boot_consoles(void)
1299{
Robin Getzcb00e992007-08-21 23:14:58 -04001300 if (console_drivers != NULL) {
1301 if (console_drivers->flags & CON_BOOT) {
1302 printk(KERN_INFO "turn off boot console %s%d\n",
1303 console_drivers->name, console_drivers->index);
1304 return unregister_console(console_drivers);
1305 }
Robin Getz0c5564b2007-08-20 15:22:47 -04001306 }
1307 return 0;
1308}
1309late_initcall(disable_boot_consoles);
1310
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311/**
1312 * tty_write_message - write a message to a certain tty, not just the console.
Martin Waitzddad86c2005-11-13 16:08:14 -08001313 * @tty: the destination tty_struct
1314 * @msg: the message to write
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 *
1316 * This is used for messages that need to be redirected to a specific tty.
1317 * We don't put it into the syslog queue right now maybe in the future if
1318 * really needed.
1319 */
1320void tty_write_message(struct tty_struct *tty, char *msg)
1321{
Alan Coxf34d7a52008-04-30 00:54:13 -07001322 if (tty && tty->ops->write)
1323 tty->ops->write(tty, msg, strlen(msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 return;
1325}
1326
Joe Perches7ef3d2f2008-02-08 04:21:25 -08001327#if defined CONFIG_PRINTK
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328/*
1329 * printk rate limiting, lifted from the networking subsystem.
1330 *
1331 * This enforces a rate limit: not more than one kernel message
1332 * every printk_ratelimit_jiffies to make a denial-of-service
1333 * attack impossible.
1334 */
1335int __printk_ratelimit(int ratelimit_jiffies, int ratelimit_burst)
1336{
Dave Young5f97a5a2008-04-29 00:59:43 -07001337 return __ratelimit(ratelimit_jiffies, ratelimit_burst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338}
1339EXPORT_SYMBOL(__printk_ratelimit);
1340
1341/* minimum time in jiffies between messages */
Jesper Juhl40dc5652005-10-30 15:02:46 -08001342int printk_ratelimit_jiffies = 5 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
1344/* number of messages we send before ratelimiting */
1345int printk_ratelimit_burst = 10;
1346
1347int printk_ratelimit(void)
1348{
1349 return __printk_ratelimit(printk_ratelimit_jiffies,
1350 printk_ratelimit_burst);
1351}
1352EXPORT_SYMBOL(printk_ratelimit);
Andrew Mortonf46c4832006-11-02 22:07:16 -08001353
1354/**
1355 * printk_timed_ratelimit - caller-controlled printk ratelimiting
1356 * @caller_jiffies: pointer to caller's state
1357 * @interval_msecs: minimum interval between prints
1358 *
1359 * printk_timed_ratelimit() returns true if more than @interval_msecs
1360 * milliseconds have elapsed since the last time printk_timed_ratelimit()
1361 * returned true.
1362 */
1363bool printk_timed_ratelimit(unsigned long *caller_jiffies,
1364 unsigned int interval_msecs)
1365{
1366 if (*caller_jiffies == 0 || time_after(jiffies, *caller_jiffies)) {
1367 *caller_jiffies = jiffies + msecs_to_jiffies(interval_msecs);
1368 return true;
1369 }
1370 return false;
1371}
1372EXPORT_SYMBOL(printk_timed_ratelimit);
Joe Perches7ef3d2f2008-02-08 04:21:25 -08001373#endif