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 #if !defined(CONFIG_LOG_BUF_SHIFT) || (CONFIG_LOG_BUF_SHIFT == 0)
33 #if defined(CONFIG_MULTIQUAD) || defined(CONFIG_IA64)
34 #define LOG_BUF_LEN (65536)
35 #elif defined(CONFIG_ARCH_S390)
36 #define LOG_BUF_LEN (131072)
37 #elif defined(CONFIG_SMP)
38 #define LOG_BUF_LEN (32768)
40 #define LOG_BUF_LEN (16384) /* This must be a power of two */
42 #else /* CONFIG_LOG_BUF_SHIFT */
43 #define LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT)
46 #define LOG_BUF_MASK (LOG_BUF_LEN-1)
48 #ifndef arch_consoles_callable
49 #define arch_consoles_callable() (1)
52 /* printk's without a loglevel use this.. */
53 #define DEFAULT_MESSAGE_LOGLEVEL 4 /* KERN_WARNING */
55 /* We show everything that is MORE important than this.. */
56 #define MINIMUM_CONSOLE_LOGLEVEL 1 /* Minimum loglevel we let people use */
57 #define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */
59 DECLARE_WAIT_QUEUE_HEAD(log_wait);
61 int console_printk[4] = {
62 DEFAULT_CONSOLE_LOGLEVEL, /* console_loglevel */
63 DEFAULT_MESSAGE_LOGLEVEL, /* default_message_loglevel */
64 MINIMUM_CONSOLE_LOGLEVEL, /* minimum_console_loglevel */
65 DEFAULT_CONSOLE_LOGLEVEL, /* default_console_loglevel */
71 * console_sem protects the console_drivers list, and also
72 * provides serialisation for access to the entire console
75 static DECLARE_MUTEX(console_sem);
76 struct console *console_drivers;
79 * logbuf_lock protects log_buf, log_start, log_end, con_start and logged_chars
80 * It is also used in interesting ways to provide interlocking in
81 * release_console_sem().
83 static spinlock_t logbuf_lock = SPIN_LOCK_UNLOCKED;
85 static char log_buf[LOG_BUF_LEN];
86 #define LOG_BUF(idx) (log_buf[(idx) & LOG_BUF_MASK])
89 * The indices into log_buf are not constrained to LOG_BUF_LEN - they
90 * must be masked before subscripting
92 static unsigned long log_start; /* Index into log_buf: next char to be read by syslog() */
93 static unsigned long con_start; /* Index into log_buf: next char to be sent to consoles */
94 static unsigned long log_end; /* Index into log_buf: most-recently-written-char + 1 */
95 static unsigned long logged_chars; /* Number of chars produced since last read+clear operation */
97 struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
98 static int selected_console = -1;
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 selected_console = i;
147 if (i == MAX_CMDLINECONSOLES)
149 selected_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);
437 * Copy the output into log_buf. If the caller didn't provide
438 * appropriate log level tags, we insert them here
440 for (p = printk_buf; *p; p++) {
441 if (log_level_unknown) {
442 if (p[0] != '<' || p[1] < '0' || p[1] > '7' || p[2] != '>') {
444 emit_log_char(default_message_loglevel + '0');
447 log_level_unknown = 0;
451 log_level_unknown = 1;
454 if (!arch_consoles_callable()) {
456 * On some architectures, the consoles are not usable
457 * on secondary CPUs early in the boot process.
459 spin_unlock_irqrestore(&logbuf_lock, flags);
462 if (!down_trylock(&console_sem)) {
464 * We own the drivers. We can drop the spinlock and let
465 * release_console_sem() print the text
467 spin_unlock_irqrestore(&logbuf_lock, flags);
468 console_may_schedule = 0;
469 release_console_sem();
472 * Someone else owns the drivers. We drop the spinlock, which
473 * allows the semaphore holder to proceed and to call the
474 * console drivers with the output which we just produced.
476 spin_unlock_irqrestore(&logbuf_lock, flags);
481 EXPORT_SYMBOL(printk);
484 * acquire_console_sem - lock the console system for exclusive use.
486 * Acquires a semaphore which guarantees that the caller has
487 * exclusive access to the console system and the console_drivers list.
489 * Can sleep, returns nothing.
491 void acquire_console_sem(void)
496 console_may_schedule = 1;
498 EXPORT_SYMBOL(acquire_console_sem);
501 * release_console_sem - unlock the console system
503 * Releases the semaphore which the caller holds on the console system
504 * and the console driver list.
506 * While the semaphore was held, console output may have been buffered
507 * by printk(). If this is the case, release_console_sem() emits
508 * the output prior to releasing the semaphore.
510 * If there is output waiting for klogd, we wake it up.
512 * release_console_sem() may be called from any context.
514 void release_console_sem(void)
517 unsigned long _con_start, _log_end;
518 unsigned long must_wake_klogd = 0;
521 spin_lock_irqsave(&logbuf_lock, flags);
522 must_wake_klogd |= log_start - log_end;
523 if (con_start == log_end)
524 break; /* Nothing to print */
525 _con_start = con_start;
527 con_start = log_end; /* Flush */
528 spin_unlock_irqrestore(&logbuf_lock, flags);
529 call_console_drivers(_con_start, _log_end);
531 console_may_schedule = 0;
533 spin_unlock_irqrestore(&logbuf_lock, flags);
534 if (must_wake_klogd && !oops_in_progress)
535 wake_up_interruptible(&log_wait);
538 /** console_conditional_schedule - yield the CPU if required
540 * If the console code is currently allowed to sleep, and
541 * if this CPU should yield the CPU to another task, do
544 * Must be called within acquire_console_sem().
546 void console_conditional_schedule(void)
548 if (console_may_schedule && current->need_resched) {
549 set_current_state(TASK_RUNNING);
554 void console_print(const char *s)
556 printk(KERN_EMERG "%s", s);
558 EXPORT_SYMBOL(console_print);
560 void console_unblank(void)
565 * Try to get the console semaphore. If someone else owns it
566 * we have to return without unblanking because console_unblank
567 * may be called in interrupt context.
569 if (down_trylock(&console_sem) != 0)
571 console_may_schedule = 0;
572 for (c = console_drivers; c != NULL; c = c->next)
573 if ((c->flags & CON_ENABLED) && c->unblank)
575 release_console_sem();
577 EXPORT_SYMBOL(console_unblank);
580 * The console driver calls this routine during kernel initialization
581 * to register the console printing procedure with printk() and to
582 * print any messages that were printed by the kernel before the
583 * console driver was initialized.
585 void register_console(struct console * console)
590 if (preferred_console < 0)
591 preferred_console = selected_console;
594 * See if we want to use this console driver. If we
595 * didn't select a console we take the first one
596 * that registers here.
598 if (preferred_console < 0) {
599 if (console->index < 0)
601 if (console->setup == NULL ||
602 console->setup(console, NULL) == 0) {
603 console->flags |= CON_ENABLED | CON_CONSDEV;
604 preferred_console = 0;
609 * See if this console matches one we selected on
612 for(i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++) {
613 if (strcmp(console_cmdline[i].name, console->name) != 0)
615 if (console->index >= 0 &&
616 console->index != console_cmdline[i].index)
618 if (console->index < 0)
619 console->index = console_cmdline[i].index;
620 if (console->setup &&
621 console->setup(console, console_cmdline[i].options) != 0)
623 console->flags |= CON_ENABLED;
624 console->index = console_cmdline[i].index;
625 if (i == preferred_console)
626 console->flags |= CON_CONSDEV;
630 if (!(console->flags & CON_ENABLED))
634 * Put this console in the list - keep the
635 * preferred driver at the head of the list.
637 acquire_console_sem();
638 if ((console->flags & CON_CONSDEV) || console_drivers == NULL) {
639 console->next = console_drivers;
640 console_drivers = console;
642 console->next = console_drivers->next;
643 console_drivers->next = console;
645 if (console->flags & CON_PRINTBUFFER) {
647 * release_console_sem() will print out the buffered messages for us.
649 spin_lock_irqsave(&logbuf_lock, flags);
650 con_start = log_start;
651 spin_unlock_irqrestore(&logbuf_lock, flags);
653 release_console_sem();
655 EXPORT_SYMBOL(register_console);
657 int unregister_console(struct console * console)
659 struct console *a,*b;
662 acquire_console_sem();
663 if (console_drivers == console) {
664 console_drivers=console->next;
667 for (a=console_drivers->next, b=console_drivers ;
677 /* If last console is removed, we re-enable picking the first
678 * one that gets registered. Without that, pmac early boot console
679 * would prevent fbcon from taking over.
681 if (console_drivers == NULL)
682 preferred_console = selected_console;
685 release_console_sem();
688 EXPORT_SYMBOL(unregister_console);
691 * tty_write_message - write a message to a certain tty, not just the console.
693 * This is used for messages that need to be redirected to a specific tty.
694 * We don't put it into the syslog queue right now maybe in the future if
697 void tty_write_message(struct tty_struct *tty, char *msg)
699 if (tty && tty->driver.write)
700 tty->driver.write(tty, 0, msg, strlen(msg));