added a lot of printk output to ease writing of emulator
[linux-2.4.21-pre4.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/kernel.h>
20 #include <linux/mm.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>
29
30 #include <asm/uaccess.h>
31
32 #ifdef CONFIG_BOOTX_TEXT
33 #include <asm/btext.h>
34 extern int force_printk_to_btext;
35 #endif
36
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)
43 #else   
44 #define LOG_BUF_LEN     (16384)                 /* This must be a power of two */
45 #endif
46
47 #define LOG_BUF_MASK    (LOG_BUF_LEN-1)
48
49 #ifndef arch_consoles_callable
50 #define arch_consoles_callable() (1)
51 #endif
52
53 /* printk's without a loglevel use this.. */
54 #define DEFAULT_MESSAGE_LOGLEVEL 4 /* KERN_WARNING */
55
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 */
59
60 DECLARE_WAIT_QUEUE_HEAD(log_wait);
61
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 */
67 };
68
69 int oops_in_progress;
70
71 /*
72  * console_sem protects the console_drivers list, and also
73  * provides serialisation for access to the entire console
74  * driver system.
75  */
76 static DECLARE_MUTEX(console_sem);
77 struct console *console_drivers;
78
79 /*
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().
83  */
84 static spinlock_t logbuf_lock = SPIN_LOCK_UNLOCKED;
85
86 static char log_buf[LOG_BUF_LEN];
87 #define LOG_BUF(idx) (log_buf[(idx) & LOG_BUF_MASK])
88
89 /*
90  * The indices into log_buf are not constrained to LOG_BUF_LEN - they
91  * must be masked before subscripting
92  */
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 */
97
98 struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
99 static int preferred_console = -1;
100
101 /* Flag: console code may call schedule() */
102 static int console_may_schedule;
103
104 /*
105  *      Setup a list of consoles. Called from init/main.c
106  */
107 static int __init console_setup(char *str)
108 {
109         struct console_cmdline *c;
110         char name[sizeof(c->name)];
111         char *s, *options;
112         int i, idx;
113
114         /*
115          *      Decode str into name, index, options.
116          */
117         if (str[0] >= '0' && str[0] <= '9') {
118                 strcpy(name, "ttyS");
119                 strncpy(name + 4, str, sizeof(name) - 5);
120         } else
121                 strncpy(name, str, sizeof(name) - 1);
122         name[sizeof(name) - 1] = 0;
123         if ((options = strchr(str, ',')) != NULL)
124                 *(options++) = 0;
125 #ifdef __sparc__
126         if (!strcmp(str, "ttya"))
127                 strcpy(name, "ttyS0");
128         if (!strcmp(str, "ttyb"))
129                 strcpy(name, "ttyS1");
130 #endif
131         for(s = name; *s; s++)
132                 if (*s >= '0' && *s <= '9')
133                         break;
134         idx = simple_strtoul(s, NULL, 10);
135         *s = 0;
136
137         /*
138          *      See if this tty is not yet registered, and
139          *      if we have a slot free.
140          */
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;
145                                 return 1;
146                 }
147         if (i == MAX_CMDLINECONSOLES)
148                 return 1;
149         preferred_console = i;
150         c = &console_cmdline[i];
151         memcpy(c->name, name, sizeof(c->name));
152         c->options = options;
153         c->index = idx;
154         return 1;
155 }
156
157 __setup("console=", console_setup);
158
159 /*
160  * Commands to do_syslog:
161  *
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
172  */
173 int do_syslog(int type, char * buf, int len)
174 {
175         unsigned long i, j, limit, count;
176         int do_clear = 0;
177         char c;
178         int error = 0;
179
180         switch (type) {
181         case 0:         /* Close log */
182                 break;
183         case 1:         /* Open log */
184                 break;
185         case 2:         /* Read from log */
186                 error = -EINVAL;
187                 if (!buf || len < 0)
188                         goto out;
189                 error = 0;
190                 if (!len)
191                         goto out;
192                 error = verify_area(VERIFY_WRITE,buf,len);
193                 if (error)
194                         goto out;
195                 error = wait_event_interruptible(log_wait, (log_start - log_end));
196                 if (error)
197                         goto out;
198                 i = 0;
199                 spin_lock_irq(&logbuf_lock);
200                 while ((log_start != log_end) && i < len) {
201                         c = LOG_BUF(log_start);
202                         log_start++;
203                         spin_unlock_irq(&logbuf_lock);
204                         __put_user(c,buf);
205                         buf++;
206                         i++;
207                         spin_lock_irq(&logbuf_lock);
208                 }
209                 spin_unlock_irq(&logbuf_lock);
210                 error = i;
211                 break;
212         case 4:         /* Read/clear last kernel messages */
213                 do_clear = 1; 
214                 /* FALL THRU */
215         case 3:         /* Read last kernel messages */
216                 error = -EINVAL;
217                 if (!buf || len < 0)
218                         goto out;
219                 error = 0;
220                 if (!len)
221                         goto out;
222                 error = verify_area(VERIFY_WRITE,buf,len);
223                 if (error)
224                         goto out;
225                 count = len;
226                 if (count > LOG_BUF_LEN)
227                         count = LOG_BUF_LEN;
228                 spin_lock_irq(&logbuf_lock);
229                 if (count > logged_chars)
230                         count = logged_chars;
231                 if (do_clear)
232                         logged_chars = 0;
233                 limit = log_end;
234                 /*
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>
239                  */
240                 for(i=0;i < count;i++) {
241                         j = limit-1-i;
242                         if (j+LOG_BUF_LEN < log_end)
243                                 break;
244                         c = LOG_BUF(j);
245                         spin_unlock_irq(&logbuf_lock);
246                         __put_user(c,&buf[count-1-i]);
247                         spin_lock_irq(&logbuf_lock);
248                 }
249                 spin_unlock_irq(&logbuf_lock);
250                 error = i;
251                 if(i != count) {
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]);
257                         }
258                 }
259
260                 break;
261         case 5:         /* Clear ring buffer */
262                 spin_lock_irq(&logbuf_lock);
263                 logged_chars = 0;
264                 spin_unlock_irq(&logbuf_lock);
265                 break;
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);
270                 break;
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);
275                 break;
276         case 8:         /* Set level of messages printed to console */
277                 error = -EINVAL;
278                 if (len < 1 || len > 8)
279                         goto out;
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);
285                 error = 0;
286                 break;
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);
291                 break;
292         default:
293                 error = -EINVAL;
294                 break;
295         }
296 out:
297         return error;
298 }
299
300 asmlinkage long sys_syslog(int type, char * buf, int len)
301 {
302         if ((type != 3) && !capable(CAP_SYS_ADMIN))
303                 return -EPERM;
304         return do_syslog(type, buf, len);
305 }
306
307 /*
308  * Call the console drivers on a range of log_buf
309  */
310 static void __call_console_drivers(unsigned long start, unsigned long end)
311 {
312         struct console *con;
313
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);
317         }
318 }
319
320 /*
321  * Write out chars from start to end - 1 inclusive
322  */
323 static void _call_console_drivers(unsigned long start, unsigned long end, int msg_log_level)
324 {
325         if (msg_log_level < console_loglevel && console_drivers && start != end) {
326                 if ((start & LOG_BUF_MASK) > (end & LOG_BUF_MASK)) {
327                         /* wrapped write */
328                         __call_console_drivers(start & LOG_BUF_MASK, LOG_BUF_LEN);
329                         __call_console_drivers(0, end & LOG_BUF_MASK);
330                 } else {
331                         __call_console_drivers(start, end);
332                 }
333         }
334 }
335
336 /*
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.
340  */
341 static void call_console_drivers(unsigned long start, unsigned long end)
342 {
343         unsigned long cur_index, start_print;
344         static int msg_level = -1;
345
346         if (((long)(start - end)) > 0)
347                 BUG();
348
349         cur_index = start;
350         start_print = start;
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) == '>')
358                 {
359                         msg_level = LOG_BUF(cur_index + 1) - '0';
360                         cur_index += 3;
361                         start_print = cur_index;
362                 }
363                 while (cur_index != end) {
364                         char c = LOG_BUF(cur_index);
365                         cur_index++;
366
367                         if (c == '\n') {
368                                 if (msg_level < 0) {
369                                         /*
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
373                                          * on those tags
374                                          */
375                                         msg_level = default_message_loglevel;
376                                 }
377                                 _call_console_drivers(start_print, cur_index, msg_level);
378                                 msg_level = -1;
379                                 start_print = cur_index;
380                                 break;
381                         }
382                 }
383         }
384         _call_console_drivers(start_print, end, msg_level);
385 }
386
387 static void emit_log_char(char c)
388 {
389         LOG_BUF(log_end) = c;
390         log_end++;
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)
396                 logged_chars++;
397 }
398
399 /*
400  * This is printk.  It can be called from any context.  We want it to work.
401  * 
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.
407  *
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.
411  */
412 asmlinkage int printk(const char *fmt, ...)
413 {
414         va_list args;
415         unsigned long flags;
416         int printed_len;
417         char *p;
418         static char printk_buf[1024];
419         static int log_level_unknown = 1;
420
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);
426         }
427
428         /* This stops the holder of console_sem just where we want him */
429         spin_lock_irqsave(&logbuf_lock, flags);
430
431         /* Emit the output into the temporary buffer */
432         va_start(args, fmt);
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);
437 #endif  
438         va_end(args);
439
440         /*
441          * Copy the output into log_buf.  If the caller didn't provide
442          * appropriate log level tags, we insert them here
443          */
444         for (p = printk_buf; *p; p++) {
445                 if (log_level_unknown) {
446                         if (p[0] != '<' || p[1] < '0' || p[1] > '7' || p[2] != '>') {
447                                 emit_log_char('<');
448                                 emit_log_char(default_message_loglevel + '0');
449                                 emit_log_char('>');
450                         }
451                         log_level_unknown = 0;
452                 }
453                 emit_log_char(*p);
454                 if (*p == '\n')
455                         log_level_unknown = 1;
456         }
457
458         if (!arch_consoles_callable()) {
459                 /*
460                  * On some architectures, the consoles are not usable
461                  * on secondary CPUs early in the boot process.
462                  */
463                 spin_unlock_irqrestore(&logbuf_lock, flags);
464                 goto out;
465         }
466         if (!down_trylock(&console_sem)) {
467                 /*
468                  * We own the drivers.  We can drop the spinlock and let
469                  * release_console_sem() print the text
470                  */
471                 spin_unlock_irqrestore(&logbuf_lock, flags);
472                 console_may_schedule = 0;
473                 release_console_sem();
474         } else {
475                 /*
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.
479                  */
480                 spin_unlock_irqrestore(&logbuf_lock, flags);
481         }
482 out:
483         return printed_len;
484 }
485 EXPORT_SYMBOL(printk);
486
487 /**
488  * acquire_console_sem - lock the console system for exclusive use.
489  *
490  * Acquires a semaphore which guarantees that the caller has
491  * exclusive access to the console system and the console_drivers list.
492  *
493  * Can sleep, returns nothing.
494  */
495 void acquire_console_sem(void)
496 {
497         if (in_interrupt())
498                 BUG();
499         down(&console_sem);
500         console_may_schedule = 1;
501 }
502 EXPORT_SYMBOL(acquire_console_sem);
503
504 /**
505  * release_console_sem - unlock the console system
506  *
507  * Releases the semaphore which the caller holds on the console system
508  * and the console driver list.
509  *
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.
513  *
514  * If there is output waiting for klogd, we wake it up.
515  *
516  * release_console_sem() may be called from any context.
517  */
518 void release_console_sem(void)
519 {
520         unsigned long flags;
521         unsigned long _con_start, _log_end;
522         unsigned long must_wake_klogd = 0;
523
524         for ( ; ; ) {
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;
530                 _log_end = log_end;
531                 con_start = log_end;            /* Flush */
532                 spin_unlock_irqrestore(&logbuf_lock, flags);
533                 call_console_drivers(_con_start, _log_end);
534         }
535         console_may_schedule = 0;
536         up(&console_sem);
537         spin_unlock_irqrestore(&logbuf_lock, flags);
538         if (must_wake_klogd && !oops_in_progress)
539                 wake_up_interruptible(&log_wait);
540 }
541
542 /** console_conditional_schedule - yield the CPU if required
543  *
544  * If the console code is currently allowed to sleep, and
545  * if this CPU should yield the CPU to another task, do
546  * so here.
547  *
548  * Must be called within acquire_console_sem().
549  */
550 void console_conditional_schedule(void)
551 {
552         if (console_may_schedule && current->need_resched) {
553                 set_current_state(TASK_RUNNING);
554                 schedule();
555         }
556 }
557
558 void console_print(const char *s)
559 {
560         printk(KERN_EMERG "%s", s);
561 }
562 EXPORT_SYMBOL(console_print);
563
564 void console_unblank(void)
565 {
566         struct console *c;
567
568         acquire_console_sem();
569         for (c = console_drivers; c != NULL; c = c->next)
570                 if ((c->flags & CON_ENABLED) && c->unblank)
571                         c->unblank();
572         release_console_sem();
573 }
574 EXPORT_SYMBOL(console_unblank);
575
576 /*
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.
581  */
582 void register_console(struct console * console)
583 {
584         int     i;
585         unsigned long flags;
586
587         /*
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.
591          */
592         if (preferred_console < 0) {
593                 if (console->index < 0)
594                         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;
599                 }
600         }
601
602         /*
603          *      See if this console matches one we selected on
604          *      the command line.
605          */
606         for(i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++) {
607                 if (strcmp(console_cmdline[i].name, console->name) != 0)
608                         continue;
609                 if (console->index >= 0 &&
610                     console->index != console_cmdline[i].index)
611                         continue;
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)
616                         break;
617                 console->flags |= CON_ENABLED;
618                 console->index = console_cmdline[i].index;
619                 if (i == preferred_console)
620                         console->flags |= CON_CONSDEV;
621                 break;
622         }
623
624         if (!(console->flags & CON_ENABLED))
625                 return;
626
627         /*
628          *      Put this console in the list - keep the
629          *      preferred driver at the head of the list.
630          */
631         acquire_console_sem();
632         if ((console->flags & CON_CONSDEV) || console_drivers == NULL) {
633                 console->next = console_drivers;
634                 console_drivers = console;
635         } else {
636                 console->next = console_drivers->next;
637                 console_drivers->next = console;
638         }
639         if (console->flags & CON_PRINTBUFFER) {
640                 /*
641                  * release_console_sem() will print out the buffered messages for us.
642                  */
643                 spin_lock_irqsave(&logbuf_lock, flags);
644                 con_start = log_start;
645                 spin_unlock_irqrestore(&logbuf_lock, flags);
646         }
647         release_console_sem();
648 }
649 EXPORT_SYMBOL(register_console);
650
651 int unregister_console(struct console * console)
652 {
653         struct console *a,*b;
654         int res = 1;
655
656         acquire_console_sem();
657         if (console_drivers == console) {
658                 console_drivers=console->next;
659                 res = 0;
660         } else {
661                 for (a=console_drivers->next, b=console_drivers ;
662                      a; b=a, a=b->next) {
663                         if (a == console) {
664                                 b->next = a->next;
665                                 res = 0;
666                                 break;
667                         }  
668                 }
669         }
670         
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.
674          */
675         if (console_drivers == NULL)
676                 preferred_console = -1;
677                 
678
679         release_console_sem();
680         return res;
681 }
682 EXPORT_SYMBOL(unregister_console);
683         
684 /**
685  * tty_write_message - write a message to a certain tty, not just the console.
686  *
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
689  * really needed.
690  */
691 void tty_write_message(struct tty_struct *tty, char *msg)
692 {
693         if (tty && tty->driver.write)
694                 tty->driver.write(tty, 0, msg, strlen(msg));
695         return;
696 }