p4
[opensuse:kernel.git] / kernel / printk.c
1 /*
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.
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>
17  */
18
19 #include <linux/mm.h>
20 #include <linux/tty.h>
21 #include <linux/tty_driver.h>
22 #include <linux/smp_lock.h>
23 #include <linux/console.h>
24 #include <linux/init.h>
25 #include <linux/module.h>
26 #include <linux/interrupt.h>                    /* For in_interrupt() */
27 #include <linux/config.h>
28 #include <linux/delay.h>
29
30 #include <asm/uaccess.h>
31
32 #if defined(CONFIG_MULTIQUAD) || defined(CONFIG_IA64)
33 #define LOG_BUF_LEN     (65536)
34 #elif defined(CONFIG_SMP)
35 #define LOG_BUF_LEN     (32768)
36 #else   
37 #define LOG_BUF_LEN     (16384)                 /* This must be a power of two */
38 #endif
39
40 #define LOG_BUF_MASK    (LOG_BUF_LEN-1)
41
42 /* printk's without a loglevel use this.. */
43 #define DEFAULT_MESSAGE_LOGLEVEL 4 /* KERN_WARNING */
44
45 /* We show everything that is MORE important than this.. */
46 #define MINIMUM_CONSOLE_LOGLEVEL 1 /* Minimum loglevel we let people use */
47 #define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */
48
49 DECLARE_WAIT_QUEUE_HEAD(log_wait);
50
51 /* Keep together for sysctl support */
52 int console_loglevel = DEFAULT_CONSOLE_LOGLEVEL;
53 int default_message_loglevel = DEFAULT_MESSAGE_LOGLEVEL;
54 int minimum_console_loglevel = MINIMUM_CONSOLE_LOGLEVEL;
55 int default_console_loglevel = DEFAULT_CONSOLE_LOGLEVEL;
56
57 int oops_in_progress;
58
59 /*
60  * console_sem protects the console_drivers list, and also
61  * provides serialisation for access to the entire console
62  * driver system.
63  */
64 static DECLARE_MUTEX(console_sem);
65 struct console *console_drivers;
66
67 /*
68  * logbuf_lock protects log_buf, log_start, log_end, con_start and logged_chars
69  * It is also used in interesting ways to provide interlocking in
70  * release_console_sem().
71  */
72 static spinlock_t logbuf_lock = SPIN_LOCK_UNLOCKED;
73
74 static char log_buf[LOG_BUF_LEN];
75 #define LOG_BUF(idx) (log_buf[(idx) & LOG_BUF_MASK])
76
77 /*
78  * The indices into log_buf are not constrained to LOG_BUF_LEN - they
79  * must be masked before subscripting
80  */
81 static unsigned long log_start;                 /* Index into log_buf: next char to be read by syslog() */
82 static unsigned long con_start;                 /* Index into log_buf: next char to be sent to consoles */
83 static unsigned long log_end;                   /* Index into log_buf: most-recently-written-char + 1 */
84 static unsigned long logged_chars;              /* Number of chars produced since last read+clear operation */
85
86 struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
87 static int preferred_console = -1;
88
89 /* Flag: console code may call schedule() */
90 static int console_may_schedule;
91
92 /*
93  *      Setup a list of consoles. Called from init/main.c
94  */
95 static int __init console_setup(char *str)
96 {
97         struct console_cmdline *c;
98         char name[sizeof(c->name)];
99         char *s, *options;
100         int i, idx;
101
102         /*
103          *      Decode str into name, index, options.
104          */
105         if (str[0] >= '0' && str[0] <= '9') {
106                 strcpy(name, "ttyS");
107                 strncpy(name + 4, str, sizeof(name) - 5);
108         } else
109                 strncpy(name, str, sizeof(name) - 1);
110         name[sizeof(name) - 1] = 0;
111         if ((options = strchr(str, ',')) != NULL)
112                 *(options++) = 0;
113 #ifdef __sparc__
114         if (!strcmp(str, "ttya"))
115                 strcpy(name, "ttyS0");
116         if (!strcmp(str, "ttyb"))
117                 strcpy(name, "ttyS1");
118 #endif
119         for(s = name; *s; s++)
120                 if (*s >= '0' && *s <= '9')
121                         break;
122         idx = simple_strtoul(s, NULL, 10);
123         *s = 0;
124
125         /*
126          *      See if this tty is not yet registered, and
127          *      if we have a slot free.
128          */
129         for(i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
130                 if (strcmp(console_cmdline[i].name, name) == 0 &&
131                           console_cmdline[i].index == idx) {
132                                 preferred_console = i;
133                                 return 1;
134                 }
135         if (i == MAX_CMDLINECONSOLES)
136                 return 1;
137         preferred_console = i;
138         c = &console_cmdline[i];
139         memcpy(c->name, name, sizeof(c->name));
140         c->options = options;
141         c->index = idx;
142         return 1;
143 }
144
145 __setup("console=", console_setup);
146
147 /*
148  * Commands to do_syslog:
149  *
150  *      0 -- Close the log.  Currently a NOP.
151  *      1 -- Open the log. Currently a NOP.
152  *      2 -- Read from the log.
153  *      3 -- Read all messages remaining in the ring buffer.
154  *      4 -- Read and clear all messages remaining in the ring buffer
155  *      5 -- Clear ring buffer.
156  *      6 -- Disable printk's to console
157  *      7 -- Enable printk's to console
158  *      8 -- Set level of messages printed to console
159  *      9 -- Return number of unread characters in the log buffer
160  */
161 int do_syslog(int type, char * buf, int len)
162 {
163         unsigned long i, j, limit, count;
164         int do_clear = 0;
165         char c;
166         int error = 0;
167
168         switch (type) {
169         case 0:         /* Close log */
170                 break;
171         case 1:         /* Open log */
172                 break;
173         case 2:         /* Read from log */
174                 error = -EINVAL;
175                 if (!buf || len < 0)
176                         goto out;
177                 error = 0;
178                 if (!len)
179                         goto out;
180                 error = verify_area(VERIFY_WRITE,buf,len);
181                 if (error)
182                         goto out;
183                 error = wait_event_interruptible(log_wait, (log_start - log_end));
184                 if (error)
185                         goto out;
186                 i = 0;
187                 spin_lock_irq(&logbuf_lock);
188                 while ((log_start != log_end) && i < len) {
189                         c = LOG_BUF(log_start);
190                         log_start++;
191                         spin_unlock_irq(&logbuf_lock);
192                         __put_user(c,buf);
193                         buf++;
194                         i++;
195                         spin_lock_irq(&logbuf_lock);
196                 }
197                 spin_unlock_irq(&logbuf_lock);
198                 error = i;
199                 break;
200         case 4:         /* Read/clear last kernel messages */
201                 do_clear = 1; 
202                 /* FALL THRU */
203         case 3:         /* Read last kernel messages */
204                 error = -EINVAL;
205                 if (!buf || len < 0)
206                         goto out;
207                 error = 0;
208                 if (!len)
209                         goto out;
210                 error = verify_area(VERIFY_WRITE,buf,len);
211                 if (error)
212                         goto out;
213                 count = len;
214                 if (count > LOG_BUF_LEN)
215                         count = LOG_BUF_LEN;
216                 spin_lock_irq(&logbuf_lock);
217                 if (count > logged_chars)
218                         count = logged_chars;
219                 if (do_clear)
220                         logged_chars = 0;
221                 limit = log_end;
222                 /*
223                  * __put_user() could sleep, and while we sleep
224                  * printk() could overwrite the messages 
225                  * we try to copy to user space. Therefore
226                  * the messages are copied in reverse. <manfreds>
227                  */
228                 for(i=0;i < count;i++) {
229                         j = limit-1-i;
230                         if (j+LOG_BUF_LEN < log_end)
231                                 break;
232                         c = LOG_BUF(j);
233                         spin_unlock_irq(&logbuf_lock);
234                         __put_user(c,&buf[count-1-i]);
235                         spin_lock_irq(&logbuf_lock);
236                 }
237                 spin_unlock_irq(&logbuf_lock);
238                 error = i;
239                 if(i != count) {
240                         int offset = count-error;
241                         /* buffer overflow during copy, correct user buffer. */
242                         for(i=0;i<error;i++) {
243                                 __get_user(c,&buf[i+offset]);
244                                 __put_user(c,&buf[i]);
245                         }
246                 }
247
248                 break;
249         case 5:         /* Clear ring buffer */
250                 spin_lock_irq(&logbuf_lock);
251                 logged_chars = 0;
252                 spin_unlock_irq(&logbuf_lock);
253                 break;
254         case 6:         /* Disable logging to console */
255                 spin_lock_irq(&logbuf_lock);
256                 console_loglevel = minimum_console_loglevel;
257                 spin_unlock_irq(&logbuf_lock);
258                 break;
259         case 7:         /* Enable logging to console */
260                 spin_lock_irq(&logbuf_lock);
261                 console_loglevel = default_console_loglevel;
262                 spin_unlock_irq(&logbuf_lock);
263                 break;
264         case 8:         /* Set level of messages printed to console */
265                 error = -EINVAL;
266                 if (len < 1 || len > 8)
267                         goto out;
268                 if (len < minimum_console_loglevel)
269                         len = minimum_console_loglevel;
270                 spin_lock_irq(&logbuf_lock);
271                 console_loglevel = len;
272                 spin_unlock_irq(&logbuf_lock);
273                 error = 0;
274                 break;
275         case 9:         /* Number of chars in the log buffer */
276                 spin_lock_irq(&logbuf_lock);
277                 error = log_end - log_start;
278                 spin_unlock_irq(&logbuf_lock);
279                 break;
280         default:
281                 error = -EINVAL;
282                 break;
283         }
284 out:
285         return error;
286 }
287
288 asmlinkage long sys_syslog(int type, char * buf, int len)
289 {
290         if ((type != 3) && !capable(CAP_SYS_ADMIN))
291                 return -EPERM;
292         return do_syslog(type, buf, len);
293 }
294
295 /*
296  * Call the console drivers on a range of log_buf
297  */
298 static void __call_console_drivers(unsigned long start, unsigned long end)
299 {
300         struct console *con;
301
302         for (con = console_drivers; con; con = con->next) {
303                 if ((con->flags & CON_ENABLED) && con->write)
304                         con->write(con, &LOG_BUF(start), end - start);
305         }
306 }
307
308 /*
309  * Write out chars from start to end - 1 inclusive
310  */
311 static void _call_console_drivers(unsigned long start, unsigned long end, int msg_log_level)
312 {
313         if (msg_log_level < console_loglevel && console_drivers && start != end) {
314                 if ((start & LOG_BUF_MASK) > (end & LOG_BUF_MASK)) {
315                         /* wrapped write */
316                         __call_console_drivers(start & LOG_BUF_MASK, LOG_BUF_LEN);
317                         __call_console_drivers(0, end & LOG_BUF_MASK);
318                 } else {
319                         __call_console_drivers(start, end);
320                 }
321         }
322 }
323
324 /*
325  * Call the console drivers, asking them to write out
326  * log_buf[start] to log_buf[end - 1].
327  * The console_sem must be held.
328  */
329 static void call_console_drivers(unsigned long start, unsigned long end)
330 {
331         unsigned long cur_index, start_print;
332         static int msg_level = -1;
333
334         if (((long)(start - end)) > 0)
335                 BUG();
336
337         cur_index = start;
338         start_print = start;
339         while (cur_index != end) {
340                 if (    msg_level < 0 &&
341                         ((end - cur_index) > 2) &&
342                         LOG_BUF(cur_index + 0) == '<' &&
343                         LOG_BUF(cur_index + 1) >= '0' &&
344                         LOG_BUF(cur_index + 1) <= '7' &&
345                         LOG_BUF(cur_index + 2) == '>')
346                 {
347                         msg_level = LOG_BUF(cur_index + 1) - '0';
348                         cur_index += 3;
349                         start_print = cur_index;
350                 }
351                 while (cur_index != end) {
352                         char c = LOG_BUF(cur_index);
353                         cur_index++;
354
355                         if (c == '\n') {
356                                 if (msg_level < 0) {
357                                         /*
358                                          * printk() has already given us loglevel tags in
359                                          * the buffer.  This code is here in case the
360                                          * log buffer has wrapped right round and scribbled
361                                          * on those tags
362                                          */
363                                         msg_level = default_message_loglevel;
364                                 }
365                                 _call_console_drivers(start_print, cur_index, msg_level);
366                                 msg_level = -1;
367                                 start_print = cur_index;
368                                 break;
369                         }
370                 }
371         }
372         _call_console_drivers(start_print, end, msg_level);
373 }
374
375 static void emit_log_char(char c)
376 {
377         LOG_BUF(log_end) = c;
378         log_end++;
379         if (log_end - log_start > LOG_BUF_LEN)
380                 log_start = log_end - LOG_BUF_LEN;
381         if (log_end - con_start > LOG_BUF_LEN)
382                 con_start = log_end - LOG_BUF_LEN;
383         if (logged_chars < LOG_BUF_LEN)
384                 logged_chars++;
385 }
386
387 /*
388  * This is printk.  It can be called from any context.  We want it to work.
389  * 
390  * We try to grab the console_sem.  If we succeed, it's easy - we log the output and
391  * call the console drivers.  If we fail to get the semaphore we place the output
392  * into the log buffer and return.  The current holder of the console_sem will
393  * notice the new output in release_console_sem() and will send it to the
394  * consoles before releasing the semaphore.
395  *
396  * One effect of this deferred printing is that code which calls printk() and
397  * then changes console_loglevel may break. This is because console_loglevel
398  * is inspected when the actual printing occurs.
399  */
400 asmlinkage int printk(const char *fmt, ...)
401 {
402         va_list args;
403         unsigned long flags;
404         int printed_len;
405         char *p;
406         static char printk_buf[1024];
407         static int log_level_unknown = 1;
408
409         if (oops_in_progress) {
410                 /* If a crash is occurring, make sure we can't deadlock */
411                 spin_lock_init(&logbuf_lock);
412                 /* And make sure that we print immediately */
413                 init_MUTEX(&console_sem);
414         }
415
416         /* This stops the holder of console_sem just where we want him */
417         spin_lock_irqsave(&logbuf_lock, flags);
418
419         /* Emit the output into the temporary buffer */
420         va_start(args, fmt);
421         printed_len = vsnprintf(printk_buf, sizeof(printk_buf), fmt, args);
422         va_end(args);
423
424         /*
425          * Copy the output into log_buf.  If the caller didn't provide
426          * appropriate log level tags, we insert them here
427          */
428         for (p = printk_buf; *p; p++) {
429                 if (log_level_unknown) {
430                         if (p[0] != '<' || p[1] < '0' || p[1] > '7' || p[2] != '>') {
431                                 emit_log_char('<');
432                                 emit_log_char(default_message_loglevel + '0');
433                                 emit_log_char('>');
434                         }
435                         log_level_unknown = 0;
436                 }
437                 emit_log_char(*p);
438                 if (*p == '\n')
439                         log_level_unknown = 1;
440         }
441
442         if (!down_trylock(&console_sem)) {
443                 /*
444                  * We own the drivers.  We can drop the spinlock and let
445                  * release_console_sem() print the text
446                  */
447                 spin_unlock_irqrestore(&logbuf_lock, flags);
448                 console_may_schedule = 0;
449                 release_console_sem();
450         } else {
451                 /*
452                  * Someone else owns the drivers.  We drop the spinlock, which
453                  * allows the semaphore holder to proceed and to call the
454                  * console drivers with the output which we just produced.
455                  */
456                 spin_unlock_irqrestore(&logbuf_lock, flags);
457         }
458         return printed_len;
459 }
460 EXPORT_SYMBOL(printk);
461
462 /**
463  * acquire_console_sem - lock the console system for exclusive use.
464  *
465  * Acquires a semaphore which guarantees that the caller has
466  * exclusive access to the console system and the console_drivers list.
467  *
468  * Can sleep, returns nothing.
469  */
470 void acquire_console_sem(void)
471 {
472         if (in_interrupt())
473                 BUG();
474         down(&console_sem);
475         console_may_schedule = 1;
476 }
477 EXPORT_SYMBOL(acquire_console_sem);
478
479 /**
480  * release_console_sem - unlock the console system
481  *
482  * Releases the semaphore which the caller holds on the console system
483  * and the console driver list.
484  *
485  * While the semaphore was held, console output may have been buffered
486  * by printk().  If this is the case, release_console_sem() emits
487  * the output prior to releasing the semaphore.
488  *
489  * If there is output waiting for klogd, we wake it up.
490  *
491  * release_console_sem() may be called from any context.
492  */
493 void release_console_sem(void)
494 {
495         unsigned long flags;
496         unsigned long _con_start, _log_end;
497         unsigned long must_wake_klogd = 0;
498
499         for ( ; ; ) {
500                 spin_lock_irqsave(&logbuf_lock, flags);
501                 must_wake_klogd |= log_start - log_end;
502                 if (con_start == log_end)
503                         break;                  /* Nothing to print */
504                 _con_start = con_start;
505                 _log_end = log_end;
506                 con_start = log_end;            /* Flush */
507                 spin_unlock_irqrestore(&logbuf_lock, flags);
508                 call_console_drivers(_con_start, _log_end);
509         }
510         console_may_schedule = 0;
511         up(&console_sem);
512         spin_unlock_irqrestore(&logbuf_lock, flags);
513         if (must_wake_klogd && !oops_in_progress)
514                 wake_up_interruptible(&log_wait);
515 }
516
517 /** console_conditional_schedule - yield the CPU if required
518  *
519  * If the console code is currently allowed to sleep, and
520  * if this CPU should yield the CPU to another task, do
521  * so here.
522  *
523  * Must be called within acquire_console_sem().
524  */
525 void console_conditional_schedule(void)
526 {
527         if (console_may_schedule && need_resched()) {
528                 set_current_state(TASK_RUNNING);
529                 schedule();
530         }
531 }
532
533 void console_print(const char *s)
534 {
535         printk(KERN_EMERG "%s", s);
536 }
537 EXPORT_SYMBOL(console_print);
538
539 void console_unblank(void)
540 {
541         struct console *c;
542
543         acquire_console_sem();
544         for (c = console_drivers; c != NULL; c = c->next)
545                 if ((c->flags & CON_ENABLED) && c->unblank)
546                         c->unblank();
547         release_console_sem();
548 }
549 EXPORT_SYMBOL(console_unblank);
550
551 /*
552  * The console driver calls this routine during kernel initialization
553  * to register the console printing procedure with printk() and to
554  * print any messages that were printed by the kernel before the
555  * console driver was initialized.
556  */
557 void register_console(struct console * console)
558 {
559         int     i;
560         unsigned long flags;
561
562         /*
563          *      See if we want to use this console driver. If we
564          *      didn't select a console we take the first one
565          *      that registers here.
566          */
567         if (preferred_console < 0) {
568                 if (console->index < 0)
569                         console->index = 0;
570                 if (console->setup == NULL ||
571                     console->setup(console, NULL) == 0) {
572                         console->flags |= CON_ENABLED | CON_CONSDEV;
573                         preferred_console = 0;
574                 }
575         }
576
577         /*
578          *      See if this console matches one we selected on
579          *      the command line.
580          */
581         for(i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++) {
582                 if (strcmp(console_cmdline[i].name, console->name) != 0)
583                         continue;
584                 if (console->index >= 0 &&
585                     console->index != console_cmdline[i].index)
586                         continue;
587                 if (console->index < 0)
588                         console->index = console_cmdline[i].index;
589                 if (console->setup &&
590                     console->setup(console, console_cmdline[i].options) != 0)
591                         break;
592                 console->flags |= CON_ENABLED;
593                 console->index = console_cmdline[i].index;
594                 if (i == preferred_console)
595                         console->flags |= CON_CONSDEV;
596                 break;
597         }
598
599         if (!(console->flags & CON_ENABLED))
600                 return;
601
602         /*
603          *      Put this console in the list - keep the
604          *      preferred driver at the head of the list.
605          */
606         acquire_console_sem();
607         if ((console->flags & CON_CONSDEV) || console_drivers == NULL) {
608                 console->next = console_drivers;
609                 console_drivers = console;
610         } else {
611                 console->next = console_drivers->next;
612                 console_drivers->next = console;
613         }
614         if (console->flags & CON_PRINTBUFFER) {
615                 /*
616                  * release_cosole_sem() will print out the buffered messages for us.
617                  */
618                 spin_lock_irqsave(&logbuf_lock, flags);
619                 con_start = log_start;
620                 spin_unlock_irqrestore(&logbuf_lock, flags);
621         }
622         release_console_sem();
623 }
624 EXPORT_SYMBOL(register_console);
625
626 int unregister_console(struct console * console)
627 {
628         struct console *a,*b;
629         int res = 1;
630
631         acquire_console_sem();
632         if (console_drivers == console) {
633                 console_drivers=console->next;
634                 res = 0;
635         } else {
636                 for (a=console_drivers->next, b=console_drivers ;
637                      a; b=a, a=b->next) {
638                         if (a == console) {
639                                 b->next = a->next;
640                                 res = 0;
641                                 break;
642                         }  
643                 }
644         }
645         
646         /* If last console is removed, we re-enable picking the first
647          * one that gets registered. Without that, pmac early boot console
648          * would prevent fbcon from taking over.
649          */
650         if (console_drivers == NULL)
651                 preferred_console = -1;
652                 
653
654         release_console_sem();
655         return res;
656 }
657 EXPORT_SYMBOL(unregister_console);
658         
659 /**
660  * tty_write_message - write a message to a certain tty, not just the console.
661  *
662  * This is used for messages that need to be redirected to a specific tty.
663  * We don't put it into the syslog queue right now maybe in the future if
664  * really needed.
665  */
666 void tty_write_message(struct tty_struct *tty, char *msg)
667 {
668         if (tty && tty->driver.write)
669                 tty->driver.write(tty, 0, msg, strlen(msg));
670         return;
671 }