blob: 028ed75d4864a6812deca019f592774eb991711a [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;
78/*
79 * This is used for debugging the mess that is the VT code by
80 * keeping track if we have the console semaphore held. It's
81 * definitely not the perfect debug tool (we don't know if _WE_
82 * hold it are racing, but it helps tracking those weird code
83 * path in the console code where we end up in places I want
84 * locked without the console sempahore held
85 */
Linus Torvalds557240b2006-06-19 18:16:01 -070086static int console_locked, console_suspended;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88/*
89 * logbuf_lock protects log_buf, log_start, log_end, con_start and logged_chars
90 * It is also used in interesting ways to provide interlocking in
91 * release_console_sem().
92 */
93static DEFINE_SPINLOCK(logbuf_lock);
94
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -080095#define LOG_BUF_MASK (log_buf_len-1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096#define LOG_BUF(idx) (log_buf[(idx) & LOG_BUF_MASK])
97
98/*
99 * The indices into log_buf are not constrained to log_buf_len - they
100 * must be masked before subscripting
101 */
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800102static unsigned log_start; /* Index into log_buf: next char to be read by syslog() */
103static unsigned con_start; /* Index into log_buf: next char to be sent to consoles */
104static unsigned log_end; /* Index into log_buf: most-recently-written-char + 1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106/*
107 * Array of consoles built from command line options (console=)
108 */
109struct console_cmdline
110{
111 char name[8]; /* Name of the driver */
112 int index; /* Minor dev. to use */
113 char *options; /* Options for the driver */
Samuel Thibaultf7511d52008-04-30 00:54:51 -0700114#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
115 char *brl_options; /* Options for braille driver */
116#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117};
118
119#define MAX_CMDLINECONSOLES 8
120
121static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
122static int selected_console = -1;
123static int preferred_console = -1;
Markus Armbruster9e124fe2008-05-26 23:31:07 +0100124int console_set_on_cmdline;
125EXPORT_SYMBOL(console_set_on_cmdline);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127/* Flag: console code may call schedule() */
128static int console_may_schedule;
129
Matt Mackalld59745c2005-05-01 08:59:02 -0700130#ifdef CONFIG_PRINTK
131
132static char __log_buf[__LOG_BUF_LEN];
133static char *log_buf = __log_buf;
134static int log_buf_len = __LOG_BUF_LEN;
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800135static unsigned logged_chars; /* Number of chars produced since last read+clear operation */
Matt Mackalld59745c2005-05-01 08:59:02 -0700136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137static int __init log_buf_len_setup(char *str)
138{
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800139 unsigned size = memparse(str, &str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 unsigned long flags;
141
142 if (size)
143 size = roundup_pow_of_two(size);
144 if (size > log_buf_len) {
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800145 unsigned start, dest_idx, offset;
Jesper Juhl40dc5652005-10-30 15:02:46 -0800146 char *new_log_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 new_log_buf = alloc_bootmem(size);
149 if (!new_log_buf) {
Jesper Juhl40dc5652005-10-30 15:02:46 -0800150 printk(KERN_WARNING "log_buf_len: allocation failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 goto out;
152 }
153
154 spin_lock_irqsave(&logbuf_lock, flags);
155 log_buf_len = size;
156 log_buf = new_log_buf;
157
158 offset = start = min(con_start, log_start);
159 dest_idx = 0;
160 while (start != log_end) {
161 log_buf[dest_idx] = __log_buf[start & (__LOG_BUF_LEN - 1)];
162 start++;
163 dest_idx++;
164 }
165 log_start -= offset;
166 con_start -= offset;
167 log_end -= offset;
168 spin_unlock_irqrestore(&logbuf_lock, flags);
169
Jesper Juhl40dc5652005-10-30 15:02:46 -0800170 printk(KERN_NOTICE "log_buf_len: %d\n", log_buf_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 }
172out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 return 1;
174}
175
176__setup("log_buf_len=", log_buf_len_setup);
177
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700178#ifdef CONFIG_BOOT_PRINTK_DELAY
179
180static unsigned int boot_delay; /* msecs delay after each printk during bootup */
181static unsigned long long printk_delay_msec; /* per msec, based on boot_delay */
182
183static int __init boot_delay_setup(char *str)
184{
185 unsigned long lpj;
186 unsigned long long loops_per_msec;
187
188 lpj = preset_lpj ? preset_lpj : 1000000; /* some guess */
189 loops_per_msec = (unsigned long long)lpj / 1000 * HZ;
190
191 get_option(&str, &boot_delay);
192 if (boot_delay > 10 * 1000)
193 boot_delay = 0;
194
195 printk_delay_msec = loops_per_msec;
196 printk(KERN_DEBUG "boot_delay: %u, preset_lpj: %ld, lpj: %lu, "
197 "HZ: %d, printk_delay_msec: %llu\n",
198 boot_delay, preset_lpj, lpj, HZ, printk_delay_msec);
199 return 1;
200}
201__setup("boot_delay=", boot_delay_setup);
202
203static void boot_delay_msec(void)
204{
205 unsigned long long k;
206 unsigned long timeout;
207
208 if (boot_delay == 0 || system_state != SYSTEM_BOOTING)
209 return;
210
211 k = (unsigned long long)printk_delay_msec * boot_delay;
212
213 timeout = jiffies + msecs_to_jiffies(boot_delay);
214 while (k) {
215 k--;
216 cpu_relax();
217 /*
218 * use (volatile) jiffies to prevent
219 * compiler reduction; loop termination via jiffies
220 * is secondary and may or may not happen.
221 */
222 if (time_after(jiffies, timeout))
223 break;
224 touch_nmi_watchdog();
225 }
226}
227#else
228static inline void boot_delay_msec(void)
229{
230}
231#endif
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233/*
Mike Frysinger0b15d042007-10-16 23:26:43 -0700234 * Return the number of unread characters in the log buffer.
235 */
236int log_buf_get_len(void)
237{
238 return logged_chars;
239}
240
241/*
242 * Copy a range of characters from the log buffer.
243 */
244int log_buf_copy(char *dest, int idx, int len)
245{
246 int ret, max;
247 bool took_lock = false;
248
249 if (!oops_in_progress) {
250 spin_lock_irq(&logbuf_lock);
251 took_lock = true;
252 }
253
254 max = log_buf_get_len();
255 if (idx < 0 || idx >= max) {
256 ret = -1;
257 } else {
258 if (len > max)
259 len = max;
260 ret = len;
261 idx += (log_end - max);
262 while (len-- > 0)
263 dest[len] = LOG_BUF(idx + len);
264 }
265
266 if (took_lock)
267 spin_unlock_irq(&logbuf_lock);
268
269 return ret;
270}
271
272/*
273 * Extract a single character from the log buffer.
274 */
275int log_buf_read(int idx)
276{
277 char ret;
278
279 if (log_buf_copy(&ret, idx, 1) == 1)
280 return ret;
281 else
282 return -1;
283}
284
285/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 * Commands to do_syslog:
287 *
288 * 0 -- Close the log. Currently a NOP.
289 * 1 -- Open the log. Currently a NOP.
290 * 2 -- Read from the log.
291 * 3 -- Read all messages remaining in the ring buffer.
292 * 4 -- Read and clear all messages remaining in the ring buffer
293 * 5 -- Clear ring buffer.
294 * 6 -- Disable printk's to console
295 * 7 -- Enable printk's to console
296 * 8 -- Set level of messages printed to console
297 * 9 -- Return number of unread characters in the log buffer
298 * 10 -- Return size of the log buffer
299 */
Jesper Juhl40dc5652005-10-30 15:02:46 -0800300int do_syslog(int type, char __user *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800302 unsigned i, j, limit, count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 int do_clear = 0;
304 char c;
305 int error = 0;
306
307 error = security_syslog(type);
308 if (error)
309 return error;
310
311 switch (type) {
312 case 0: /* Close log */
313 break;
314 case 1: /* Open log */
315 break;
316 case 2: /* Read from log */
317 error = -EINVAL;
318 if (!buf || len < 0)
319 goto out;
320 error = 0;
321 if (!len)
322 goto out;
323 if (!access_ok(VERIFY_WRITE, buf, len)) {
324 error = -EFAULT;
325 goto out;
326 }
Jesper Juhl40dc5652005-10-30 15:02:46 -0800327 error = wait_event_interruptible(log_wait,
328 (log_start - log_end));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 if (error)
330 goto out;
331 i = 0;
332 spin_lock_irq(&logbuf_lock);
333 while (!error && (log_start != log_end) && i < len) {
334 c = LOG_BUF(log_start);
335 log_start++;
336 spin_unlock_irq(&logbuf_lock);
337 error = __put_user(c,buf);
338 buf++;
339 i++;
340 cond_resched();
341 spin_lock_irq(&logbuf_lock);
342 }
343 spin_unlock_irq(&logbuf_lock);
344 if (!error)
345 error = i;
346 break;
347 case 4: /* Read/clear last kernel messages */
Jesper Juhl40dc5652005-10-30 15:02:46 -0800348 do_clear = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 /* FALL THRU */
350 case 3: /* Read last kernel messages */
351 error = -EINVAL;
352 if (!buf || len < 0)
353 goto out;
354 error = 0;
355 if (!len)
356 goto out;
357 if (!access_ok(VERIFY_WRITE, buf, len)) {
358 error = -EFAULT;
359 goto out;
360 }
361 count = len;
362 if (count > log_buf_len)
363 count = log_buf_len;
364 spin_lock_irq(&logbuf_lock);
365 if (count > logged_chars)
366 count = logged_chars;
367 if (do_clear)
368 logged_chars = 0;
369 limit = log_end;
370 /*
371 * __put_user() could sleep, and while we sleep
Jesper Juhl40dc5652005-10-30 15:02:46 -0800372 * printk() could overwrite the messages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 * we try to copy to user space. Therefore
374 * the messages are copied in reverse. <manfreds>
375 */
Jesper Juhl40dc5652005-10-30 15:02:46 -0800376 for (i = 0; i < count && !error; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 j = limit-1-i;
378 if (j + log_buf_len < log_end)
379 break;
380 c = LOG_BUF(j);
381 spin_unlock_irq(&logbuf_lock);
382 error = __put_user(c,&buf[count-1-i]);
383 cond_resched();
384 spin_lock_irq(&logbuf_lock);
385 }
386 spin_unlock_irq(&logbuf_lock);
387 if (error)
388 break;
389 error = i;
Jesper Juhl40dc5652005-10-30 15:02:46 -0800390 if (i != count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 int offset = count-error;
392 /* buffer overflow during copy, correct user buffer. */
Jesper Juhl40dc5652005-10-30 15:02:46 -0800393 for (i = 0; i < error; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 if (__get_user(c,&buf[i+offset]) ||
395 __put_user(c,&buf[i])) {
396 error = -EFAULT;
397 break;
398 }
399 cond_resched();
400 }
401 }
402 break;
403 case 5: /* Clear ring buffer */
404 logged_chars = 0;
405 break;
406 case 6: /* Disable logging to console */
407 console_loglevel = minimum_console_loglevel;
408 break;
409 case 7: /* Enable logging to console */
410 console_loglevel = default_console_loglevel;
411 break;
412 case 8: /* Set level of messages printed to console */
413 error = -EINVAL;
414 if (len < 1 || len > 8)
415 goto out;
416 if (len < minimum_console_loglevel)
417 len = minimum_console_loglevel;
418 console_loglevel = len;
419 error = 0;
420 break;
421 case 9: /* Number of chars in the log buffer */
422 error = log_end - log_start;
423 break;
424 case 10: /* Size of the log buffer */
425 error = log_buf_len;
426 break;
427 default:
428 error = -EINVAL;
429 break;
430 }
431out:
432 return error;
433}
434
Jesper Juhl40dc5652005-10-30 15:02:46 -0800435asmlinkage long sys_syslog(int type, char __user *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
437 return do_syslog(type, buf, len);
438}
439
440/*
441 * Call the console drivers on a range of log_buf
442 */
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800443static void __call_console_drivers(unsigned start, unsigned end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
445 struct console *con;
446
447 for (con = console_drivers; con; con = con->next) {
Michael Ellerman76a8ad292006-06-25 05:47:40 -0700448 if ((con->flags & CON_ENABLED) && con->write &&
449 (cpu_online(smp_processor_id()) ||
450 (con->flags & CON_ANYTIME)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 con->write(con, &LOG_BUF(start), end - start);
452 }
453}
454
Ingo Molnar792908222006-12-06 20:40:51 -0800455static int __read_mostly ignore_loglevel;
456
Adrian Bunk99eea6a2006-12-22 01:07:00 -0800457static int __init ignore_loglevel_setup(char *str)
Ingo Molnar792908222006-12-06 20:40:51 -0800458{
459 ignore_loglevel = 1;
460 printk(KERN_INFO "debug: ignoring loglevel setting.\n");
461
Ingo Molnarc4772d92008-01-31 22:45:23 +0100462 return 0;
Ingo Molnar792908222006-12-06 20:40:51 -0800463}
464
Ingo Molnarc4772d92008-01-31 22:45:23 +0100465early_param("ignore_loglevel", ignore_loglevel_setup);
Ingo Molnar792908222006-12-06 20:40:51 -0800466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467/*
468 * Write out chars from start to end - 1 inclusive
469 */
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800470static void _call_console_drivers(unsigned start,
471 unsigned end, int msg_log_level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
Ingo Molnar792908222006-12-06 20:40:51 -0800473 if ((msg_log_level < console_loglevel || ignore_loglevel) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 console_drivers && start != end) {
475 if ((start & LOG_BUF_MASK) > (end & LOG_BUF_MASK)) {
476 /* wrapped write */
477 __call_console_drivers(start & LOG_BUF_MASK,
478 log_buf_len);
479 __call_console_drivers(0, end & LOG_BUF_MASK);
480 } else {
481 __call_console_drivers(start, end);
482 }
483 }
484}
485
486/*
487 * Call the console drivers, asking them to write out
488 * log_buf[start] to log_buf[end - 1].
489 * The console_sem must be held.
490 */
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800491static void call_console_drivers(unsigned start, unsigned end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800493 unsigned cur_index, start_print;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 static int msg_level = -1;
495
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800496 BUG_ON(((int)(start - end)) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 cur_index = start;
499 start_print = start;
500 while (cur_index != end) {
Jesper Juhl40dc5652005-10-30 15:02:46 -0800501 if (msg_level < 0 && ((end - cur_index) > 2) &&
502 LOG_BUF(cur_index + 0) == '<' &&
503 LOG_BUF(cur_index + 1) >= '0' &&
504 LOG_BUF(cur_index + 1) <= '7' &&
505 LOG_BUF(cur_index + 2) == '>') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 msg_level = LOG_BUF(cur_index + 1) - '0';
507 cur_index += 3;
508 start_print = cur_index;
509 }
510 while (cur_index != end) {
511 char c = LOG_BUF(cur_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Jesper Juhl40dc5652005-10-30 15:02:46 -0800513 cur_index++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 if (c == '\n') {
515 if (msg_level < 0) {
516 /*
517 * printk() has already given us loglevel tags in
518 * the buffer. This code is here in case the
519 * log buffer has wrapped right round and scribbled
520 * on those tags
521 */
522 msg_level = default_message_loglevel;
523 }
524 _call_console_drivers(start_print, cur_index, msg_level);
525 msg_level = -1;
526 start_print = cur_index;
527 break;
528 }
529 }
530 }
531 _call_console_drivers(start_print, end, msg_level);
532}
533
534static void emit_log_char(char c)
535{
536 LOG_BUF(log_end) = c;
537 log_end++;
538 if (log_end - log_start > log_buf_len)
539 log_start = log_end - log_buf_len;
540 if (log_end - con_start > log_buf_len)
541 con_start = log_end - log_buf_len;
542 if (logged_chars < log_buf_len)
543 logged_chars++;
544}
545
546/*
547 * Zap console related locks when oopsing. Only zap at most once
548 * every 10 seconds, to leave time for slow consoles to print a
549 * full oops.
550 */
551static void zap_locks(void)
552{
553 static unsigned long oops_timestamp;
554
555 if (time_after_eq(jiffies, oops_timestamp) &&
Jesper Juhl40dc5652005-10-30 15:02:46 -0800556 !time_after(jiffies, oops_timestamp + 30 * HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 return;
558
559 oops_timestamp = jiffies;
560
561 /* If a crash is occurring, make sure we can't deadlock */
562 spin_lock_init(&logbuf_lock);
563 /* And make sure that we print immediately */
564 init_MUTEX(&console_sem);
565}
566
567#if defined(CONFIG_PRINTK_TIME)
568static int printk_time = 1;
569#else
570static int printk_time = 0;
571#endif
Randy Dunlape84845c2007-07-15 23:40:25 -0700572module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Michael Ellerman76a8ad292006-06-25 05:47:40 -0700574/* Check if we have any console registered that can be called early in boot. */
575static int have_callable_console(void)
576{
577 struct console *con;
578
579 for (con = console_drivers; con; con = con->next)
580 if (con->flags & CON_ANYTIME)
581 return 1;
582
583 return 0;
584}
585
Martin Waitzddad86c2005-11-13 16:08:14 -0800586/**
587 * printk - print a kernel message
588 * @fmt: format string
589 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800590 * This is printk(). It can be called from any context. We want it to work.
Jiri Kosina14921922007-07-15 23:41:51 -0700591 * Be aware of the fact that if oops_in_progress is not set, we might try to
592 * wake klogd up which could deadlock on runqueue lock if printk() is called
593 * from scheduler code.
Jesper Juhl40dc5652005-10-30 15:02:46 -0800594 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 * We try to grab the console_sem. If we succeed, it's easy - we log the output and
596 * call the console drivers. If we fail to get the semaphore we place the output
597 * into the log buffer and return. The current holder of the console_sem will
598 * notice the new output in release_console_sem() and will send it to the
599 * consoles before releasing the semaphore.
600 *
601 * One effect of this deferred printing is that code which calls printk() and
602 * then changes console_loglevel may break. This is because console_loglevel
603 * is inspected when the actual printing occurs.
Martin Waitzddad86c2005-11-13 16:08:14 -0800604 *
605 * See also:
606 * printf(3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 */
Matt Mackalld59745c2005-05-01 08:59:02 -0700608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609asmlinkage int printk(const char *fmt, ...)
610{
611 va_list args;
612 int r;
613
614 va_start(args, fmt);
615 r = vprintk(fmt, args);
616 va_end(args);
617
618 return r;
619}
620
David Howellsfe217732005-09-06 15:16:34 -0700621/* cpu currently holding logbuf_lock */
622static volatile unsigned int printk_cpu = UINT_MAX;
623
Linus Torvalds266c2e02008-03-24 19:25:08 -0700624/*
625 * Can we actually use the console at this time on this cpu?
626 *
627 * Console drivers may assume that per-cpu resources have
628 * been allocated. So unless they're explicitly marked as
629 * being able to cope (CON_ANYTIME) don't call them until
630 * this CPU is officially up.
631 */
632static inline int can_use_console(unsigned int cpu)
633{
634 return cpu_online(cpu) || have_callable_console();
635}
636
637/*
638 * Try to get console ownership to actually show the kernel
639 * messages from a 'printk'. Return true (and with the
640 * console_semaphore held, and 'console_locked' set) if it
641 * is successful, false otherwise.
642 *
643 * This gets called with the 'logbuf_lock' spinlock held and
644 * interrupts disabled. It should return with 'lockbuf_lock'
645 * released but interrupts still disabled.
646 */
647static int acquire_console_semaphore_for_printk(unsigned int cpu)
648{
649 int retval = 0;
650
Linus Torvalds093a07e2008-04-15 13:09:54 -0700651 if (!try_acquire_console_sem()) {
652 retval = 1;
653
654 /*
655 * If we can't use the console, we need to release
656 * the console semaphore by hand to avoid flushing
657 * the buffer. We need to hold the console semaphore
658 * in order to do this test safely.
659 */
660 if (!can_use_console(cpu)) {
661 console_locked = 0;
662 up(&console_sem);
663 retval = 0;
664 }
665 }
Linus Torvalds266c2e02008-03-24 19:25:08 -0700666 printk_cpu = UINT_MAX;
667 spin_unlock(&logbuf_lock);
668 return retval;
669}
670
Ingo Molnar32a76002008-01-25 21:07:58 +0100671const char printk_recursion_bug_msg [] =
672 KERN_CRIT "BUG: recent printk recursion!\n";
673static int printk_recursion_bug;
674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675asmlinkage int vprintk(const char *fmt, va_list args)
676{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 static int log_level_unknown = 1;
Ingo Molnar32a76002008-01-25 21:07:58 +0100678 static char printk_buf[1024];
679
680 unsigned long flags;
681 int printed_len = 0;
682 int this_cpu;
683 char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700685 boot_delay_msec();
686
David Howellsfe217732005-09-06 15:16:34 -0700687 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 /* This stops the holder of console_sem just where we want him */
Mathieu Desnoyers1efc5da2007-02-10 01:46:29 -0800689 raw_local_irq_save(flags);
Ingo Molnar32a76002008-01-25 21:07:58 +0100690 this_cpu = smp_processor_id();
691
692 /*
693 * Ouch, printk recursed into itself!
694 */
695 if (unlikely(printk_cpu == this_cpu)) {
696 /*
697 * If a crash is occurring during printk() on this CPU,
698 * then try to get the crash message out but make sure
699 * we can't deadlock. Otherwise just return to avoid the
700 * recursion and return - but flag the recursion so that
701 * it can be printed at the next appropriate moment:
702 */
703 if (!oops_in_progress) {
704 printk_recursion_bug = 1;
705 goto out_restore_irqs;
706 }
707 zap_locks();
708 }
709
Ingo Molnara0f1ccf2006-07-03 00:24:58 -0700710 lockdep_off();
711 spin_lock(&logbuf_lock);
Ingo Molnar32a76002008-01-25 21:07:58 +0100712 printk_cpu = this_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Ingo Molnar32a76002008-01-25 21:07:58 +0100714 if (printk_recursion_bug) {
715 printk_recursion_bug = 0;
716 strcpy(printk_buf, printk_recursion_bug_msg);
717 printed_len = sizeof(printk_recursion_bug_msg);
718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 /* Emit the output into the temporary buffer */
Ingo Molnar32a76002008-01-25 21:07:58 +0100720 printed_len += vscnprintf(printk_buf + printed_len,
Tejun Heocf3680b2008-02-14 10:32:07 +0900721 sizeof(printk_buf) - printed_len, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 /*
724 * Copy the output into log_buf. If the caller didn't provide
725 * appropriate log level tags, we insert them here
726 */
727 for (p = printk_buf; *p; p++) {
728 if (log_level_unknown) {
729 /* log_level_unknown signals the start of a new line */
730 if (printk_time) {
731 int loglev_char;
732 char tbuf[50], *tp;
733 unsigned tlen;
734 unsigned long long t;
735 unsigned long nanosec_rem;
736
737 /*
738 * force the log level token to be
739 * before the time output.
740 */
741 if (p[0] == '<' && p[1] >='0' &&
742 p[1] <= '7' && p[2] == '>') {
743 loglev_char = p[1];
744 p += 3;
Guillaume Chazarain025510c2006-01-08 01:02:41 -0800745 printed_len -= 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 } else {
747 loglev_char = default_message_loglevel
748 + '0';
749 }
Ingo Molnar326e96b2008-01-27 08:03:54 +0100750 t = cpu_clock(printk_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 nanosec_rem = do_div(t, 1000000000);
752 tlen = sprintf(tbuf,
753 "<%c>[%5lu.%06lu] ",
754 loglev_char,
755 (unsigned long)t,
756 nanosec_rem/1000);
757
758 for (tp = tbuf; tp < tbuf + tlen; tp++)
759 emit_log_char(*tp);
Guillaume Chazarain025510c2006-01-08 01:02:41 -0800760 printed_len += tlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 } else {
762 if (p[0] != '<' || p[1] < '0' ||
763 p[1] > '7' || p[2] != '>') {
764 emit_log_char('<');
765 emit_log_char(default_message_loglevel
766 + '0');
767 emit_log_char('>');
Guillaume Chazarain025510c2006-01-08 01:02:41 -0800768 printed_len += 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 }
771 log_level_unknown = 0;
772 if (!*p)
773 break;
774 }
775 emit_log_char(*p);
776 if (*p == '\n')
777 log_level_unknown = 1;
778 }
779
Linus Torvalds266c2e02008-03-24 19:25:08 -0700780 /*
781 * Try to acquire and then immediately release the
782 * console semaphore. The release will do all the
783 * actual magic (print out buffers, wake up klogd,
784 * etc).
785 *
786 * The acquire_console_semaphore_for_printk() function
787 * will release 'logbuf_lock' regardless of whether it
788 * actually gets the semaphore or not.
789 */
790 if (acquire_console_semaphore_for_printk(this_cpu))
791 release_console_sem();
Michael Ellerman76a8ad292006-06-25 05:47:40 -0700792
Linus Torvalds266c2e02008-03-24 19:25:08 -0700793 lockdep_on();
Ingo Molnar32a76002008-01-25 21:07:58 +0100794out_restore_irqs:
Linus Torvalds266c2e02008-03-24 19:25:08 -0700795 raw_local_irq_restore(flags);
Michael Ellerman76a8ad292006-06-25 05:47:40 -0700796
David Howellsfe217732005-09-06 15:16:34 -0700797 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 return printed_len;
799}
800EXPORT_SYMBOL(printk);
801EXPORT_SYMBOL(vprintk);
802
Matt Mackalld59745c2005-05-01 08:59:02 -0700803#else
804
Jesper Juhl40dc5652005-10-30 15:02:46 -0800805asmlinkage long sys_syslog(int type, char __user *buf, int len)
Matt Mackalld59745c2005-05-01 08:59:02 -0700806{
Mike Galbraithc36264d2006-12-06 20:37:42 -0800807 return -ENOSYS;
Jesper Juhl40dc5652005-10-30 15:02:46 -0800808}
809
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800810static void call_console_drivers(unsigned start, unsigned end)
Jesper Juhl40dc5652005-10-30 15:02:46 -0800811{
812}
Matt Mackalld59745c2005-05-01 08:59:02 -0700813
814#endif
815
Samuel Thibaultf7511d52008-04-30 00:54:51 -0700816static int __add_preferred_console(char *name, int idx, char *options,
817 char *brl_options)
818{
819 struct console_cmdline *c;
820 int i;
821
822 /*
823 * See if this tty is not yet registered, and
824 * if we have a slot free.
825 */
826 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
827 if (strcmp(console_cmdline[i].name, name) == 0 &&
828 console_cmdline[i].index == idx) {
829 if (!brl_options)
830 selected_console = i;
831 return 0;
832 }
833 if (i == MAX_CMDLINECONSOLES)
834 return -E2BIG;
835 if (!brl_options)
836 selected_console = i;
837 c = &console_cmdline[i];
838 strlcpy(c->name, name, sizeof(c->name));
839 c->options = options;
840#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
841 c->brl_options = brl_options;
842#endif
843 c->index = idx;
844 return 0;
845}
John Z. Bohach2ea1c532006-03-24 03:18:19 -0800846/*
847 * Set up a list of consoles. Called from init/main.c
848 */
849static int __init console_setup(char *str)
850{
Yinghai Lueaa944a2007-07-15 23:37:27 -0700851 char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for index */
Samuel Thibaultf7511d52008-04-30 00:54:51 -0700852 char *s, *options, *brl_options = NULL;
John Z. Bohach2ea1c532006-03-24 03:18:19 -0800853 int idx;
854
Samuel Thibaultf7511d52008-04-30 00:54:51 -0700855#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
856 if (!memcmp(str, "brl,", 4)) {
857 brl_options = "";
858 str += 4;
859 } else if (!memcmp(str, "brl=", 4)) {
860 brl_options = str + 4;
861 str = strchr(brl_options, ',');
862 if (!str) {
863 printk(KERN_ERR "need port name after brl=\n");
864 return 1;
865 }
866 *(str++) = 0;
867 }
868#endif
869
John Z. Bohach2ea1c532006-03-24 03:18:19 -0800870 /*
871 * Decode str into name, index, options.
872 */
873 if (str[0] >= '0' && str[0] <= '9') {
Yinghai Lueaa944a2007-07-15 23:37:27 -0700874 strcpy(buf, "ttyS");
875 strncpy(buf + 4, str, sizeof(buf) - 5);
John Z. Bohach2ea1c532006-03-24 03:18:19 -0800876 } else {
Yinghai Lueaa944a2007-07-15 23:37:27 -0700877 strncpy(buf, str, sizeof(buf) - 1);
John Z. Bohach2ea1c532006-03-24 03:18:19 -0800878 }
Yinghai Lueaa944a2007-07-15 23:37:27 -0700879 buf[sizeof(buf) - 1] = 0;
John Z. Bohach2ea1c532006-03-24 03:18:19 -0800880 if ((options = strchr(str, ',')) != NULL)
881 *(options++) = 0;
882#ifdef __sparc__
883 if (!strcmp(str, "ttya"))
Yinghai Lueaa944a2007-07-15 23:37:27 -0700884 strcpy(buf, "ttyS0");
John Z. Bohach2ea1c532006-03-24 03:18:19 -0800885 if (!strcmp(str, "ttyb"))
Yinghai Lueaa944a2007-07-15 23:37:27 -0700886 strcpy(buf, "ttyS1");
John Z. Bohach2ea1c532006-03-24 03:18:19 -0800887#endif
Yinghai Lueaa944a2007-07-15 23:37:27 -0700888 for (s = buf; *s; s++)
John Z. Bohach2ea1c532006-03-24 03:18:19 -0800889 if ((*s >= '0' && *s <= '9') || *s == ',')
890 break;
891 idx = simple_strtoul(s, NULL, 10);
892 *s = 0;
893
Samuel Thibaultf7511d52008-04-30 00:54:51 -0700894 __add_preferred_console(buf, idx, options, brl_options);
Markus Armbruster9e124fe2008-05-26 23:31:07 +0100895 console_set_on_cmdline = 1;
John Z. Bohach2ea1c532006-03-24 03:18:19 -0800896 return 1;
897}
898__setup("console=", console_setup);
899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900/**
Matt Mackall3c0547b2005-05-16 21:53:47 -0700901 * add_preferred_console - add a device to the list of preferred consoles.
Martin Waitzddad86c2005-11-13 16:08:14 -0800902 * @name: device name
903 * @idx: device index
904 * @options: options for this console
Matt Mackall3c0547b2005-05-16 21:53:47 -0700905 *
906 * The last preferred console added will be used for kernel messages
907 * and stdin/out/err for init. Normally this is used by console_setup
908 * above to handle user-supplied console arguments; however it can also
909 * be used by arch-specific code either to override the user or more
910 * commonly to provide a default console (ie from PROM variables) when
911 * the user has not supplied one.
912 */
David S. Millerfb445ee2007-12-29 01:19:49 -0800913int add_preferred_console(char *name, int idx, char *options)
Matt Mackall3c0547b2005-05-16 21:53:47 -0700914{
Samuel Thibaultf7511d52008-04-30 00:54:51 -0700915 return __add_preferred_console(name, idx, options, NULL);
Matt Mackall3c0547b2005-05-16 21:53:47 -0700916}
917
Daniel Ritzb6b1d872007-08-03 16:07:43 +0200918int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options)
Yinghai Lu18a8bd92007-07-15 23:37:59 -0700919{
920 struct console_cmdline *c;
921 int i;
922
923 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
924 if (strcmp(console_cmdline[i].name, name) == 0 &&
925 console_cmdline[i].index == idx) {
926 c = &console_cmdline[i];
Markus Armbrusterf7352952008-04-30 00:54:52 -0700927 strlcpy(c->name, name_new, sizeof(c->name));
Yinghai Lu18a8bd92007-07-15 23:37:59 -0700928 c->name[sizeof(c->name) - 1] = 0;
929 c->options = options;
930 c->index = idx_new;
931 return i;
932 }
933 /* not found */
934 return -1;
935}
936
Andres Salomon8f4ce8c2007-10-18 03:04:50 -0700937int console_suspend_enabled = 1;
938EXPORT_SYMBOL(console_suspend_enabled);
939
940static int __init console_suspend_disable(char *str)
941{
942 console_suspend_enabled = 0;
943 return 1;
944}
945__setup("no_console_suspend", console_suspend_disable);
946
Matt Mackall3c0547b2005-05-16 21:53:47 -0700947/**
Linus Torvalds557240b2006-06-19 18:16:01 -0700948 * suspend_console - suspend the console subsystem
949 *
950 * This disables printk() while we go into suspend states
951 */
952void suspend_console(void)
953{
Andres Salomon8f4ce8c2007-10-18 03:04:50 -0700954 if (!console_suspend_enabled)
955 return;
Rafael J. Wysockic8eb8b42006-09-25 23:32:56 -0700956 printk("Suspending console(s)\n");
Linus Torvalds557240b2006-06-19 18:16:01 -0700957 acquire_console_sem();
958 console_suspended = 1;
959}
960
961void resume_console(void)
962{
Andres Salomon8f4ce8c2007-10-18 03:04:50 -0700963 if (!console_suspend_enabled)
964 return;
Linus Torvalds557240b2006-06-19 18:16:01 -0700965 console_suspended = 0;
966 release_console_sem();
967}
968
969/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 * acquire_console_sem - lock the console system for exclusive use.
971 *
972 * Acquires a semaphore which guarantees that the caller has
973 * exclusive access to the console system and the console_drivers list.
974 *
975 * Can sleep, returns nothing.
976 */
977void acquire_console_sem(void)
978{
Eric Sesterhenn8abd8e22006-04-01 01:21:17 +0200979 BUG_ON(in_interrupt());
Linus Torvalds557240b2006-06-19 18:16:01 -0700980 if (console_suspended) {
981 down(&secondary_console_sem);
982 return;
983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 down(&console_sem);
985 console_locked = 1;
986 console_may_schedule = 1;
987}
988EXPORT_SYMBOL(acquire_console_sem);
989
990int try_acquire_console_sem(void)
991{
992 if (down_trylock(&console_sem))
993 return -1;
994 console_locked = 1;
995 console_may_schedule = 0;
996 return 0;
997}
998EXPORT_SYMBOL(try_acquire_console_sem);
999
1000int is_console_locked(void)
1001{
1002 return console_locked;
1003}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Kirill Korotaeve3e8a752007-02-10 01:46:19 -08001005void wake_up_klogd(void)
1006{
1007 if (!oops_in_progress && waitqueue_active(&log_wait))
1008 wake_up_interruptible(&log_wait);
1009}
1010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011/**
1012 * release_console_sem - unlock the console system
1013 *
1014 * Releases the semaphore which the caller holds on the console system
1015 * and the console driver list.
1016 *
1017 * While the semaphore was held, console output may have been buffered
1018 * by printk(). If this is the case, release_console_sem() emits
1019 * the output prior to releasing the semaphore.
1020 *
1021 * If there is output waiting for klogd, we wake it up.
1022 *
1023 * release_console_sem() may be called from any context.
1024 */
1025void release_console_sem(void)
1026{
1027 unsigned long flags;
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -08001028 unsigned _con_start, _log_end;
1029 unsigned wake_klogd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Linus Torvalds557240b2006-06-19 18:16:01 -07001031 if (console_suspended) {
1032 up(&secondary_console_sem);
1033 return;
1034 }
Antonino A. Daplas78944e52006-08-05 12:14:16 -07001035
1036 console_may_schedule = 0;
1037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 for ( ; ; ) {
1039 spin_lock_irqsave(&logbuf_lock, flags);
1040 wake_klogd |= log_start - log_end;
1041 if (con_start == log_end)
1042 break; /* Nothing to print */
1043 _con_start = con_start;
1044 _log_end = log_end;
1045 con_start = log_end; /* Flush */
1046 spin_unlock(&logbuf_lock);
1047 call_console_drivers(_con_start, _log_end);
1048 local_irq_restore(flags);
1049 }
1050 console_locked = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 up(&console_sem);
1052 spin_unlock_irqrestore(&logbuf_lock, flags);
Kirill Korotaeve3e8a752007-02-10 01:46:19 -08001053 if (wake_klogd)
1054 wake_up_klogd();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055}
1056EXPORT_SYMBOL(release_console_sem);
1057
Martin Waitzddad86c2005-11-13 16:08:14 -08001058/**
1059 * console_conditional_schedule - yield the CPU if required
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 *
1061 * If the console code is currently allowed to sleep, and
1062 * if this CPU should yield the CPU to another task, do
1063 * so here.
1064 *
1065 * Must be called within acquire_console_sem().
1066 */
1067void __sched console_conditional_schedule(void)
1068{
1069 if (console_may_schedule)
1070 cond_resched();
1071}
1072EXPORT_SYMBOL(console_conditional_schedule);
1073
1074void console_print(const char *s)
1075{
1076 printk(KERN_EMERG "%s", s);
1077}
1078EXPORT_SYMBOL(console_print);
1079
1080void console_unblank(void)
1081{
1082 struct console *c;
1083
1084 /*
1085 * console_unblank can no longer be called in interrupt context unless
1086 * oops_in_progress is set to 1..
1087 */
1088 if (oops_in_progress) {
1089 if (down_trylock(&console_sem) != 0)
1090 return;
1091 } else
1092 acquire_console_sem();
1093
1094 console_locked = 1;
1095 console_may_schedule = 0;
1096 for (c = console_drivers; c != NULL; c = c->next)
1097 if ((c->flags & CON_ENABLED) && c->unblank)
1098 c->unblank();
1099 release_console_sem();
1100}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
1102/*
1103 * Return the console tty driver structure and its associated index
1104 */
1105struct tty_driver *console_device(int *index)
1106{
1107 struct console *c;
1108 struct tty_driver *driver = NULL;
1109
1110 acquire_console_sem();
1111 for (c = console_drivers; c != NULL; c = c->next) {
1112 if (!c->device)
1113 continue;
1114 driver = c->device(c, index);
1115 if (driver)
1116 break;
1117 }
1118 release_console_sem();
1119 return driver;
1120}
1121
1122/*
1123 * Prevent further output on the passed console device so that (for example)
1124 * serial drivers can disable console output before suspending a port, and can
1125 * re-enable output afterwards.
1126 */
1127void console_stop(struct console *console)
1128{
1129 acquire_console_sem();
1130 console->flags &= ~CON_ENABLED;
1131 release_console_sem();
1132}
1133EXPORT_SYMBOL(console_stop);
1134
1135void console_start(struct console *console)
1136{
1137 acquire_console_sem();
1138 console->flags |= CON_ENABLED;
1139 release_console_sem();
1140}
1141EXPORT_SYMBOL(console_start);
1142
1143/*
1144 * The console driver calls this routine during kernel initialization
1145 * to register the console printing procedure with printk() and to
1146 * print any messages that were printed by the kernel before the
1147 * console driver was initialized.
1148 */
Jesper Juhl40dc5652005-10-30 15:02:46 -08001149void register_console(struct console *console)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150{
Jesper Juhl40dc5652005-10-30 15:02:46 -08001151 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 unsigned long flags;
Gerd Hoffmann69331af2007-05-08 00:26:49 -07001153 struct console *bootconsole = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
Gerd Hoffmann69331af2007-05-08 00:26:49 -07001155 if (console_drivers) {
1156 if (console->flags & CON_BOOT)
1157 return;
1158 if (console_drivers->flags & CON_BOOT)
1159 bootconsole = console_drivers;
1160 }
1161
1162 if (preferred_console < 0 || bootconsole || !console_drivers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 preferred_console = selected_console;
1164
Yinghai Lu18a8bd92007-07-15 23:37:59 -07001165 if (console->early_setup)
1166 console->early_setup();
1167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 /*
1169 * See if we want to use this console driver. If we
1170 * didn't select a console we take the first one
1171 * that registers here.
1172 */
1173 if (preferred_console < 0) {
1174 if (console->index < 0)
1175 console->index = 0;
1176 if (console->setup == NULL ||
1177 console->setup(console, NULL) == 0) {
1178 console->flags |= CON_ENABLED | CON_CONSDEV;
1179 preferred_console = 0;
1180 }
1181 }
1182
1183 /*
1184 * See if this console matches one we selected on
1185 * the command line.
1186 */
Jesper Juhl40dc5652005-10-30 15:02:46 -08001187 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0];
1188 i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 if (strcmp(console_cmdline[i].name, console->name) != 0)
1190 continue;
1191 if (console->index >= 0 &&
1192 console->index != console_cmdline[i].index)
1193 continue;
1194 if (console->index < 0)
1195 console->index = console_cmdline[i].index;
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001196#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1197 if (console_cmdline[i].brl_options) {
1198 console->flags |= CON_BRL;
1199 braille_register_console(console,
1200 console_cmdline[i].index,
1201 console_cmdline[i].options,
1202 console_cmdline[i].brl_options);
1203 return;
1204 }
1205#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 if (console->setup &&
1207 console->setup(console, console_cmdline[i].options) != 0)
1208 break;
1209 console->flags |= CON_ENABLED;
1210 console->index = console_cmdline[i].index;
Greg Edwardsab4af032005-06-23 00:09:05 -07001211 if (i == selected_console) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 console->flags |= CON_CONSDEV;
Greg Edwardsab4af032005-06-23 00:09:05 -07001213 preferred_console = selected_console;
1214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 break;
1216 }
1217
1218 if (!(console->flags & CON_ENABLED))
1219 return;
1220
Yinghai Lud37bf602007-07-15 23:37:28 -07001221 if (bootconsole && (console->flags & CON_CONSDEV)) {
Gerd Hoffmann69331af2007-05-08 00:26:49 -07001222 printk(KERN_INFO "console handover: boot [%s%d] -> real [%s%d]\n",
1223 bootconsole->name, bootconsole->index,
1224 console->name, console->index);
1225 unregister_console(bootconsole);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 console->flags &= ~CON_PRINTBUFFER;
Yinghai Lud37bf602007-07-15 23:37:28 -07001227 } else {
1228 printk(KERN_INFO "console [%s%d] enabled\n",
1229 console->name, console->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 }
1231
1232 /*
1233 * Put this console in the list - keep the
1234 * preferred driver at the head of the list.
1235 */
1236 acquire_console_sem();
1237 if ((console->flags & CON_CONSDEV) || console_drivers == NULL) {
1238 console->next = console_drivers;
1239 console_drivers = console;
Greg Edwardsab4af032005-06-23 00:09:05 -07001240 if (console->next)
1241 console->next->flags &= ~CON_CONSDEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 } else {
1243 console->next = console_drivers->next;
1244 console_drivers->next = console;
1245 }
1246 if (console->flags & CON_PRINTBUFFER) {
1247 /*
1248 * release_console_sem() will print out the buffered messages
1249 * for us.
1250 */
1251 spin_lock_irqsave(&logbuf_lock, flags);
1252 con_start = log_start;
1253 spin_unlock_irqrestore(&logbuf_lock, flags);
1254 }
1255 release_console_sem();
1256}
1257EXPORT_SYMBOL(register_console);
1258
Jesper Juhl40dc5652005-10-30 15:02:46 -08001259int unregister_console(struct console *console)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260{
Jesper Juhl40dc5652005-10-30 15:02:46 -08001261 struct console *a, *b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 int res = 1;
1263
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001264#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1265 if (console->flags & CON_BRL)
1266 return braille_unregister_console(console);
1267#endif
1268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 acquire_console_sem();
1270 if (console_drivers == console) {
1271 console_drivers=console->next;
1272 res = 0;
Benjamin Herrenschmidte9b15b52005-11-23 13:37:44 -08001273 } else if (console_drivers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 for (a=console_drivers->next, b=console_drivers ;
1275 a; b=a, a=b->next) {
1276 if (a == console) {
1277 b->next = a->next;
1278 res = 0;
1279 break;
Jesper Juhl40dc5652005-10-30 15:02:46 -08001280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 }
1282 }
Jesper Juhl40dc5652005-10-30 15:02:46 -08001283
Gerd Hoffmann69331af2007-05-08 00:26:49 -07001284 /*
Greg Edwardsab4af032005-06-23 00:09:05 -07001285 * If this isn't the last console and it has CON_CONSDEV set, we
1286 * need to set it on the next preferred console.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 */
Gerd Hoffmann69331af2007-05-08 00:26:49 -07001288 if (console_drivers != NULL && console->flags & CON_CONSDEV)
Greg Edwardsab4af032005-06-23 00:09:05 -07001289 console_drivers->flags |= CON_CONSDEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
1291 release_console_sem();
1292 return res;
1293}
1294EXPORT_SYMBOL(unregister_console);
Matt Mackalld59745c2005-05-01 08:59:02 -07001295
Robin Getz0c5564b2007-08-20 15:22:47 -04001296static int __init disable_boot_consoles(void)
1297{
Robin Getzcb00e992007-08-21 23:14:58 -04001298 if (console_drivers != NULL) {
1299 if (console_drivers->flags & CON_BOOT) {
1300 printk(KERN_INFO "turn off boot console %s%d\n",
1301 console_drivers->name, console_drivers->index);
1302 return unregister_console(console_drivers);
1303 }
Robin Getz0c5564b2007-08-20 15:22:47 -04001304 }
1305 return 0;
1306}
1307late_initcall(disable_boot_consoles);
1308
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309/**
1310 * tty_write_message - write a message to a certain tty, not just the console.
Martin Waitzddad86c2005-11-13 16:08:14 -08001311 * @tty: the destination tty_struct
1312 * @msg: the message to write
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 *
1314 * This is used for messages that need to be redirected to a specific tty.
1315 * We don't put it into the syslog queue right now maybe in the future if
1316 * really needed.
1317 */
1318void tty_write_message(struct tty_struct *tty, char *msg)
1319{
Alan Coxf34d7a52008-04-30 00:54:13 -07001320 if (tty && tty->ops->write)
1321 tty->ops->write(tty, msg, strlen(msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 return;
1323}
1324
Joe Perches7ef3d2f2008-02-08 04:21:25 -08001325#if defined CONFIG_PRINTK
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326/*
1327 * printk rate limiting, lifted from the networking subsystem.
1328 *
1329 * This enforces a rate limit: not more than one kernel message
1330 * every printk_ratelimit_jiffies to make a denial-of-service
1331 * attack impossible.
1332 */
1333int __printk_ratelimit(int ratelimit_jiffies, int ratelimit_burst)
1334{
Dave Young5f97a5a2008-04-29 00:59:43 -07001335 return __ratelimit(ratelimit_jiffies, ratelimit_burst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336}
1337EXPORT_SYMBOL(__printk_ratelimit);
1338
1339/* minimum time in jiffies between messages */
Jesper Juhl40dc5652005-10-30 15:02:46 -08001340int printk_ratelimit_jiffies = 5 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
1342/* number of messages we send before ratelimiting */
1343int printk_ratelimit_burst = 10;
1344
1345int printk_ratelimit(void)
1346{
1347 return __printk_ratelimit(printk_ratelimit_jiffies,
1348 printk_ratelimit_burst);
1349}
1350EXPORT_SYMBOL(printk_ratelimit);
Andrew Mortonf46c4832006-11-02 22:07:16 -08001351
1352/**
1353 * printk_timed_ratelimit - caller-controlled printk ratelimiting
1354 * @caller_jiffies: pointer to caller's state
1355 * @interval_msecs: minimum interval between prints
1356 *
1357 * printk_timed_ratelimit() returns true if more than @interval_msecs
1358 * milliseconds have elapsed since the last time printk_timed_ratelimit()
1359 * returned true.
1360 */
1361bool printk_timed_ratelimit(unsigned long *caller_jiffies,
1362 unsigned int interval_msecs)
1363{
1364 if (*caller_jiffies == 0 || time_after(jiffies, *caller_jiffies)) {
1365 *caller_jiffies = jiffies + msecs_to_jiffies(interval_msecs);
1366 return true;
1367 }
1368 return false;
1369}
1370EXPORT_SYMBOL(printk_timed_ratelimit);
Joe Perches7ef3d2f2008-02-08 04:21:25 -08001371#endif