2 * linux/kernel/printk.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
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).
12 * Modified for sysctl support, 1/8/97, Chris Horn.
13 * Fixed SMP synchronization, 08/08/99, Manfred Spraul
14 * manfreds@colorfullife.com
15 * Rewrote bits to get rid of console_lock
16 * 01Mar01 Andrew Morton <andrewm@uow.edu.au>
19 #include <linux/kernel.h>
21 #include <linux/tty.h>
22 #include <linux/tty_driver.h>
23 #include <linux/smp_lock.h>
24 #include <linux/console.h>
25 #include <linux/init.h>
26 #include <linux/module.h>
27 #include <linux/interrupt.h> /* For in_interrupt() */
28 #include <linux/config.h>
30 #include <asm/uaccess.h>
32 #ifdef CONFIG_BOOTX_TEXT
33 #include <asm/btext.h>
34 extern int force_printk_to_btext;
37 #if defined(CONFIG_MULTIQUAD) || defined(CONFIG_IA64)
38 #define LOG_BUF_LEN (65536)
39 #elif defined(CONFIG_ARCH_S390)
40 #define LOG_BUF_LEN (131072)
41 #elif defined(CONFIG_SMP)
42 #define LOG_BUF_LEN (32768)
44 #define LOG_BUF_LEN (16384) /* This must be a power of two */
47 #define LOG_BUF_MASK (LOG_BUF_LEN-1)
49 #ifndef arch_consoles_callable
50 #define arch_consoles_callable() (1)
53 /* printk's without a loglevel use this.. */
54 #define DEFAULT_MESSAGE_LOGLEVEL 4 /* KERN_WARNING */
56 /* We show everything that is MORE important than this.. */
57 #define MINIMUM_CONSOLE_LOGLEVEL 1 /* Minimum loglevel we let people use */
58 #define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */
60 DECLARE_WAIT_QUEUE_HEAD(log_wait);
62 int console_printk[4] = {
63 DEFAULT_CONSOLE_LOGLEVEL, /* console_loglevel */
64 DEFAULT_MESSAGE_LOGLEVEL, /* default_message_loglevel */
65 MINIMUM_CONSOLE_LOGLEVEL, /* minimum_console_loglevel */
66 DEFAULT_CONSOLE_LOGLEVEL, /* default_console_loglevel */
72 * console_sem protects the console_drivers list, and also
73 * provides serialisation for access to the entire console
76 static DECLARE_MUTEX(console_sem);
77 struct console *console_drivers;
80 * logbuf_lock protects log_buf, log_start, log_end, con_start and logged_chars
81 * It is also used in interesting ways to provide interlocking in
82 * release_console_sem().
84 static spinlock_t logbuf_lock = SPIN_LOCK_UNLOCKED;
86 static char log_buf[LOG_BUF_LEN];
87 #define LOG_BUF(idx) (log_buf[(idx) & LOG_BUF_MASK])
90 * The indices into log_buf are not constrained to LOG_BUF_LEN - they
91 * must be masked before subscripting
93 static unsigned long log_start; /* Index into log_buf: next char to be read by syslog() */
94 static unsigned long con_start; /* Index into log_buf: next char to be sent to consoles */
95 static unsigned long log_end; /* Index into log_buf: most-recently-written-char + 1 */
96 static unsigned long logged_chars; /* Number of chars produced since last read+clear operation */
98 struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
99 static int preferred_console = -1;
101 /* Flag: console code may call schedule() */
102 static int console_may_schedule;
105 * Setup a list of consoles. Called from init/main.c
107 static int __init console_setup(char *str)
109 struct console_cmdline *c;
110 char name[sizeof(c->name)];
115 * Decode str into name, index, options.
117 if (str[0] >= '0' && str[0] <= '9') {
118 strcpy(name, "ttyS");
119 strncpy(name + 4, str, sizeof(name) - 5);
121 strncpy(name, str, sizeof(name) - 1);
122 name[sizeof(name) - 1] = 0;
123 if ((options = strchr(str, ',')) != NULL)
126 if (!strcmp(str, "ttya"))
127 strcpy(name, "ttyS0");
128 if (!strcmp(str, "ttyb"))
129 strcpy(name, "ttyS1");
131 for(s = name; *s; s++)
132 if (*s >= '0' && *s <= '9')
134 idx = simple_strtoul(s, NULL, 10);
138 * See if this tty is not yet registered, and
139 * if we have a slot free.
141 for(i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
142 if (strcmp(console_cmdline[i].name, name) == 0 &&
143 console_cmdline[i].index == idx) {
144 preferred_console = i;
147 if (i == MAX_CMDLINECONSOLES)
149 preferred_console = i;
150 c = &console_cmdline[i];
151 memcpy(c->name, name, sizeof(c->name));
152 c->options = options;
157 __setup("console=", console_setup);
160 * Commands to do_syslog:
162 * 0 -- Close the log. Currently a NOP.
163 * 1 -- Open the log. Currently a NOP.
164 * 2 -- Read from the log.
165 * 3 -- Read all messages remaining in the ring buffer.
166 * 4 -- Read and clear all messages remaining in the ring buffer
167 * 5 -- Clear ring buffer.
168 * 6 -- Disable printk's to console
169 * 7 -- Enable printk's to console
170 * 8 -- Set level of messages printed to console
171 * 9 -- Return number of unread characters in the log buffer
173 int do_syslog(int type, char * buf, int len)
175 unsigned long i, j, limit, count;
181 case 0: /* Close log */
183 case 1: /* Open log */
185 case 2: /* Read from log */
192 error = verify_area(VERIFY_WRITE,buf,len);
195 error = wait_event_interruptible(log_wait, (log_start - log_end));
199 spin_lock_irq(&logbuf_lock);
200 while ((log_start != log_end) && i < len) {
201 c = LOG_BUF(log_start);
203 spin_unlock_irq(&logbuf_lock);
207 spin_lock_irq(&logbuf_lock);
209 spin_unlock_irq(&logbuf_lock);
212 case 4: /* Read/clear last kernel messages */
215 case 3: /* Read last kernel messages */
222 error = verify_area(VERIFY_WRITE,buf,len);
226 if (count > LOG_BUF_LEN)
228 spin_lock_irq(&logbuf_lock);
229 if (count > logged_chars)
230 count = logged_chars;
235 * __put_user() could sleep, and while we sleep
236 * printk() could overwrite the messages
237 * we try to copy to user space. Therefore
238 * the messages are copied in reverse. <manfreds>
240 for(i=0;i < count;i++) {
242 if (j+LOG_BUF_LEN < log_end)
245 spin_unlock_irq(&logbuf_lock);
246 __put_user(c,&buf[count-1-i]);
247 spin_lock_irq(&logbuf_lock);
249 spin_unlock_irq(&logbuf_lock);
252 int offset = count-error;
253 /* buffer overflow during copy, correct user buffer. */
254 for(i=0;i<error;i++) {
255 __get_user(c,&buf[i+offset]);
256 __put_user(c,&buf[i]);
261 case 5: /* Clear ring buffer */
262 spin_lock_irq(&logbuf_lock);
264 spin_unlock_irq(&logbuf_lock);
266 case 6: /* Disable logging to console */
267 spin_lock_irq(&logbuf_lock);
268 console_loglevel = minimum_console_loglevel;
269 spin_unlock_irq(&logbuf_lock);
271 case 7: /* Enable logging to console */
272 spin_lock_irq(&logbuf_lock);
273 console_loglevel = default_console_loglevel;
274 spin_unlock_irq(&logbuf_lock);
276 case 8: /* Set level of messages printed to console */
278 if (len < 1 || len > 8)
280 if (len < minimum_console_loglevel)
281 len = minimum_console_loglevel;
282 spin_lock_irq(&logbuf_lock);
283 console_loglevel = len;
284 spin_unlock_irq(&logbuf_lock);
287 case 9: /* Number of chars in the log buffer */
288 spin_lock_irq(&logbuf_lock);
289 error = log_end - log_start;
290 spin_unlock_irq(&logbuf_lock);
300 asmlinkage long sys_syslog(int type, char * buf, int len)
302 if ((type != 3) && !capable(CAP_SYS_ADMIN))
304 return do_syslog(type, buf, len);
308 * Call the console drivers on a range of log_buf
310 static void __call_console_drivers(unsigned long start, unsigned long end)
314 for (con = console_drivers; con; con = con->next) {
315 if ((con->flags & CON_ENABLED) && con->write)
316 con->write(con, &LOG_BUF(start), end - start);
321 * Write out chars from start to end - 1 inclusive
323 static void _call_console_drivers(unsigned long start, unsigned long end, int msg_log_level)
325 if (msg_log_level < console_loglevel && console_drivers && start != end) {
326 if ((start & LOG_BUF_MASK) > (end & LOG_BUF_MASK)) {
328 __call_console_drivers(start & LOG_BUF_MASK, LOG_BUF_LEN);
329 __call_console_drivers(0, end & LOG_BUF_MASK);
331 __call_console_drivers(start, end);
337 * Call the console drivers, asking them to write out
338 * log_buf[start] to log_buf[end - 1].
339 * The console_sem must be held.
341 static void call_console_drivers(unsigned long start, unsigned long end)
343 unsigned long cur_index, start_print;
344 static int msg_level = -1;
346 if (((long)(start - end)) > 0)
351 while (cur_index != end) {
352 if ( msg_level < 0 &&
353 ((end - cur_index) > 2) &&
354 LOG_BUF(cur_index + 0) == '<' &&
355 LOG_BUF(cur_index + 1) >= '0' &&
356 LOG_BUF(cur_index + 1) <= '7' &&
357 LOG_BUF(cur_index + 2) == '>')
359 msg_level = LOG_BUF(cur_index + 1) - '0';
361 start_print = cur_index;
363 while (cur_index != end) {
364 char c = LOG_BUF(cur_index);
370 * printk() has already given us loglevel tags in
371 * the buffer. This code is here in case the
372 * log buffer has wrapped right round and scribbled
375 msg_level = default_message_loglevel;
377 _call_console_drivers(start_print, cur_index, msg_level);
379 start_print = cur_index;
384 _call_console_drivers(start_print, end, msg_level);
387 static void emit_log_char(char c)
389 LOG_BUF(log_end) = c;
391 if (log_end - log_start > LOG_BUF_LEN)
392 log_start = log_end - LOG_BUF_LEN;
393 if (log_end - con_start > LOG_BUF_LEN)
394 con_start = log_end - LOG_BUF_LEN;
395 if (logged_chars < LOG_BUF_LEN)
400 * This is printk. It can be called from any context. We want it to work.
402 * We try to grab the console_sem. If we succeed, it's easy - we log the output and
403 * call the console drivers. If we fail to get the semaphore we place the output
404 * into the log buffer and return. The current holder of the console_sem will
405 * notice the new output in release_console_sem() and will send it to the
406 * consoles before releasing the semaphore.
408 * One effect of this deferred printing is that code which calls printk() and
409 * then changes console_loglevel may break. This is because console_loglevel
410 * is inspected when the actual printing occurs.
412 asmlinkage int printk(const char *fmt, ...)
418 static char printk_buf[1024];
419 static int log_level_unknown = 1;
421 if (oops_in_progress) {
422 /* If a crash is occurring, make sure we can't deadlock */
423 spin_lock_init(&logbuf_lock);
424 /* And make sure that we print immediately */
425 init_MUTEX(&console_sem);
428 /* This stops the holder of console_sem just where we want him */
429 spin_lock_irqsave(&logbuf_lock, flags);
431 /* Emit the output into the temporary buffer */
433 printed_len = vsnprintf(printk_buf, sizeof(printk_buf), fmt, args);
434 #ifdef CONFIG_BOOTX_TEXT
435 if (force_printk_to_btext)
436 btext_drawstring(printk_buf);
441 * Copy the output into log_buf. If the caller didn't provide
442 * appropriate log level tags, we insert them here
444 for (p = printk_buf; *p; p++) {
445 if (log_level_unknown) {
446 if (p[0] != '<' || p[1] < '0' || p[1] > '7' || p[2] != '>') {
448 emit_log_char(default_message_loglevel + '0');
451 log_level_unknown = 0;
455 log_level_unknown = 1;
458 if (!arch_consoles_callable()) {
460 * On some architectures, the consoles are not usable
461 * on secondary CPUs early in the boot process.
463 spin_unlock_irqrestore(&logbuf_lock, flags);
466 if (!down_trylock(&console_sem)) {
468 * We own the drivers. We can drop the spinlock and let
469 * release_console_sem() print the text
471 spin_unlock_irqrestore(&logbuf_lock, flags);
472 console_may_schedule = 0;
473 release_console_sem();
476 * Someone else owns the drivers. We drop the spinlock, which
477 * allows the semaphore holder to proceed and to call the
478 * console drivers with the output which we just produced.
480 spin_unlock_irqrestore(&logbuf_lock, flags);
485 EXPORT_SYMBOL(printk);
488 * acquire_console_sem - lock the console system for exclusive use.
490 * Acquires a semaphore which guarantees that the caller has
491 * exclusive access to the console system and the console_drivers list.
493 * Can sleep, returns nothing.
495 void acquire_console_sem(void)
500 console_may_schedule = 1;
502 EXPORT_SYMBOL(acquire_console_sem);
505 * release_console_sem - unlock the console system
507 * Releases the semaphore which the caller holds on the console system
508 * and the console driver list.
510 * While the semaphore was held, console output may have been buffered
511 * by printk(). If this is the case, release_console_sem() emits
512 * the output prior to releasing the semaphore.
514 * If there is output waiting for klogd, we wake it up.
516 * release_console_sem() may be called from any context.
518 void release_console_sem(void)
521 unsigned long _con_start, _log_end;
522 unsigned long must_wake_klogd = 0;
525 spin_lock_irqsave(&logbuf_lock, flags);
526 must_wake_klogd |= log_start - log_end;
527 if (con_start == log_end)
528 break; /* Nothing to print */
529 _con_start = con_start;
531 con_start = log_end; /* Flush */
532 spin_unlock_irqrestore(&logbuf_lock, flags);
533 call_console_drivers(_con_start, _log_end);
535 console_may_schedule = 0;
537 spin_unlock_irqrestore(&logbuf_lock, flags);
538 if (must_wake_klogd && !oops_in_progress)
539 wake_up_interruptible(&log_wait);
542 /** console_conditional_schedule - yield the CPU if required
544 * If the console code is currently allowed to sleep, and
545 * if this CPU should yield the CPU to another task, do
548 * Must be called within acquire_console_sem().
550 void console_conditional_schedule(void)
552 if (console_may_schedule && current->need_resched) {
553 set_current_state(TASK_RUNNING);
558 void console_print(const char *s)
560 printk(KERN_EMERG "%s", s);
562 EXPORT_SYMBOL(console_print);
564 void console_unblank(void)
568 acquire_console_sem();
569 for (c = console_drivers; c != NULL; c = c->next)
570 if ((c->flags & CON_ENABLED) && c->unblank)
572 release_console_sem();
574 EXPORT_SYMBOL(console_unblank);
577 * The console driver calls this routine during kernel initialization
578 * to register the console printing procedure with printk() and to
579 * print any messages that were printed by the kernel before the
580 * console driver was initialized.
582 void register_console(struct console * console)
588 * See if we want to use this console driver. If we
589 * didn't select a console we take the first one
590 * that registers here.
592 if (preferred_console < 0) {
593 if (console->index < 0)
595 if (console->setup == NULL ||
596 console->setup(console, NULL) == 0) {
597 console->flags |= CON_ENABLED | CON_CONSDEV;
598 preferred_console = 0;
603 * See if this console matches one we selected on
606 for(i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++) {
607 if (strcmp(console_cmdline[i].name, console->name) != 0)
609 if (console->index >= 0 &&
610 console->index != console_cmdline[i].index)
612 if (console->index < 0)
613 console->index = console_cmdline[i].index;
614 if (console->setup &&
615 console->setup(console, console_cmdline[i].options) != 0)
617 console->flags |= CON_ENABLED;
618 console->index = console_cmdline[i].index;
619 if (i == preferred_console)
620 console->flags |= CON_CONSDEV;
624 if (!(console->flags & CON_ENABLED))
628 * Put this console in the list - keep the
629 * preferred driver at the head of the list.
631 acquire_console_sem();
632 if ((console->flags & CON_CONSDEV) || console_drivers == NULL) {
633 console->next = console_drivers;
634 console_drivers = console;
636 console->next = console_drivers->next;
637 console_drivers->next = console;
639 if (console->flags & CON_PRINTBUFFER) {
641 * release_console_sem() will print out the buffered messages for us.
643 spin_lock_irqsave(&logbuf_lock, flags);
644 con_start = log_start;
645 spin_unlock_irqrestore(&logbuf_lock, flags);
647 release_console_sem();
649 EXPORT_SYMBOL(register_console);
651 int unregister_console(struct console * console)
653 struct console *a,*b;
656 acquire_console_sem();
657 if (console_drivers == console) {
658 console_drivers=console->next;
661 for (a=console_drivers->next, b=console_drivers ;
671 /* If last console is removed, we re-enable picking the first
672 * one that gets registered. Without that, pmac early boot console
673 * would prevent fbcon from taking over.
675 if (console_drivers == NULL)
676 preferred_console = -1;
679 release_console_sem();
682 EXPORT_SYMBOL(unregister_console);
685 * tty_write_message - write a message to a certain tty, not just the console.
687 * This is used for messages that need to be redirected to a specific tty.
688 * We don't put it into the syslog queue right now maybe in the future if
691 void tty_write_message(struct tty_struct *tty, char *msg)
693 if (tty && tty->driver.write)
694 tty->driver.write(tty, 0, msg, strlen(msg));