1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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/console.h>
24#include <linux/init.h>
25#include <linux/jiffies.h>
26#include <linux/nmi.h>
27#include <linux/module.h>
28#include <linux/moduleparam.h>
29#include <linux/interrupt.h>
30#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#include <linux/jiffies.h>
36
37#include <asm/uaccess.h>
38
39#define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT)
40
41
42#define DEFAULT_MESSAGE_LOGLEVEL 4
43
44
45#define MINIMUM_CONSOLE_LOGLEVEL 1
46#define DEFAULT_CONSOLE_LOGLEVEL 7
47
48DECLARE_WAIT_QUEUE_HEAD(log_wait);
49
50int console_printk[4] = {
51 DEFAULT_CONSOLE_LOGLEVEL,
52 DEFAULT_MESSAGE_LOGLEVEL,
53 MINIMUM_CONSOLE_LOGLEVEL,
54 DEFAULT_CONSOLE_LOGLEVEL,
55};
56
57
58
59
60
61int oops_in_progress;
62EXPORT_SYMBOL(oops_in_progress);
63
64
65
66
67
68
69static DECLARE_MUTEX(console_sem);
70static DECLARE_MUTEX(secondary_console_sem);
71struct console *console_drivers;
72
73
74
75
76
77
78
79
80static int console_locked, console_suspended;
81
82
83
84
85
86
87static DEFINE_SPINLOCK(logbuf_lock);
88
89#define LOG_BUF_MASK (log_buf_len-1)
90#define LOG_BUF(idx) (log_buf[(idx) & LOG_BUF_MASK])
91
92
93
94
95
96static unsigned long log_start;
97static unsigned long con_start;
98static unsigned long log_end;
99
100
101
102
103struct console_cmdline
104{
105 char name[8];
106 int index;
107 char *options;
108};
109
110#define MAX_CMDLINECONSOLES 8
111
112static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
113static int selected_console = -1;
114static int preferred_console = -1;
115
116
117static int console_may_schedule;
118
119#ifdef CONFIG_PRINTK
120
121static char __log_buf[__LOG_BUF_LEN];
122static char *log_buf = __log_buf;
123static int log_buf_len = __LOG_BUF_LEN;
124static unsigned long logged_chars;
125
126static int __init log_buf_len_setup(char *str)
127{
128 unsigned long size = memparse(str, &str);
129 unsigned long flags;
130
131 if (size)
132 size = roundup_pow_of_two(size);
133 if (size > log_buf_len) {
134 unsigned long start, dest_idx, offset;
135 char *new_log_buf;
136
137 new_log_buf = alloc_bootmem(size);
138 if (!new_log_buf) {
139 printk(KERN_WARNING "log_buf_len: allocation failed\n");
140 goto out;
141 }
142
143 spin_lock_irqsave(&logbuf_lock, flags);
144 log_buf_len = size;
145 log_buf = new_log_buf;
146
147 offset = start = min(con_start, log_start);
148 dest_idx = 0;
149 while (start != log_end) {
150 log_buf[dest_idx] = __log_buf[start & (__LOG_BUF_LEN - 1)];
151 start++;
152 dest_idx++;
153 }
154 log_start -= offset;
155 con_start -= offset;
156 log_end -= offset;
157 spin_unlock_irqrestore(&logbuf_lock, flags);
158
159 printk(KERN_NOTICE "log_buf_len: %d\n", log_buf_len);
160 }
161out:
162 return 1;
163}
164
165__setup("log_buf_len=", log_buf_len_setup);
166
167#ifdef CONFIG_BOOT_PRINTK_DELAY
168
169static unsigned int boot_delay;
170static unsigned long long printk_delay_msec;
171
172static int __init boot_delay_setup(char *str)
173{
174 unsigned long lpj;
175 unsigned long long loops_per_msec;
176
177 lpj = preset_lpj ? preset_lpj : 1000000;
178 loops_per_msec = (unsigned long long)lpj / 1000 * HZ;
179
180 get_option(&str, &boot_delay);
181 if (boot_delay > 10 * 1000)
182 boot_delay = 0;
183
184 printk_delay_msec = loops_per_msec;
185 printk(KERN_DEBUG "boot_delay: %u, preset_lpj: %ld, lpj: %lu, "
186 "HZ: %d, printk_delay_msec: %llu\n",
187 boot_delay, preset_lpj, lpj, HZ, printk_delay_msec);
188 return 1;
189}
190__setup("boot_delay=", boot_delay_setup);
191
192static void boot_delay_msec(void)
193{
194 unsigned long long k;
195 unsigned long timeout;
196
197 if (boot_delay == 0 || system_state != SYSTEM_BOOTING)
198 return;
199
200 k = (unsigned long long)printk_delay_msec * boot_delay;
201
202 timeout = jiffies + msecs_to_jiffies(boot_delay);
203 while (k) {
204 k--;
205 cpu_relax();
206
207
208
209
210
211 if (time_after(jiffies, timeout))
212 break;
213 touch_nmi_watchdog();
214 }
215}
216#else
217static inline void boot_delay_msec(void)
218{
219}
220#endif
221
222
223
224
225int log_buf_get_len(void)
226{
227 return logged_chars;
228}
229
230
231
232
233int log_buf_copy(char *dest, int idx, int len)
234{
235 int ret, max;
236 bool took_lock = false;
237
238 if (!oops_in_progress) {
239 spin_lock_irq(&logbuf_lock);
240 took_lock = true;
241 }
242
243 max = log_buf_get_len();
244 if (idx < 0 || idx >= max) {
245 ret = -1;
246 } else {
247 if (len > max)
248 len = max;
249 ret = len;
250 idx += (log_end - max);
251 while (len-- > 0)
252 dest[len] = LOG_BUF(idx + len);
253 }
254
255 if (took_lock)
256 spin_unlock_irq(&logbuf_lock);
257
258 return ret;
259}
260
261
262
263
264int log_buf_read(int idx)
265{
266 char ret;
267
268 if (log_buf_copy(&ret, idx, 1) == 1)
269 return ret;
270 else
271 return -1;
272}
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289int do_syslog(int type, char __user *buf, int len)
290{
291 unsigned long i, j, limit, count;
292 int do_clear = 0;
293 char c;
294 int error = 0;
295
296 error = security_syslog(type);
297 if (error)
298 return error;
299
300 switch (type) {
301 case 0:
302 break;
303 case 1:
304 break;
305 case 2:
306 error = -EINVAL;
307 if (!buf || len < 0)
308 goto out;
309 error = 0;
310 if (!len)
311 goto out;
312 if (!access_ok(VERIFY_WRITE, buf, len)) {
313 error = -EFAULT;
314 goto out;
315 }
316 error = wait_event_interruptible(log_wait,
317 (log_start - log_end));
318 if (error)
319 goto out;
320 i = 0;
321 spin_lock_irq(&logbuf_lock);
322 while (!error && (log_start != log_end) && i < len) {
323 c = LOG_BUF(log_start);
324 log_start++;
325 spin_unlock_irq(&logbuf_lock);
326 error = __put_user(c,buf);
327 buf++;
328 i++;
329 cond_resched();
330 spin_lock_irq(&logbuf_lock);
331 }
332 spin_unlock_irq(&logbuf_lock);
333 if (!error)
334 error = i;
335 break;
336 case 4:
337 do_clear = 1;
338
339 case 3:
340 error = -EINVAL;
341 if (!buf || len < 0)
342 goto out;
343 error = 0;
344 if (!len)
345 goto out;
346 if (!access_ok(VERIFY_WRITE, buf, len)) {
347 error = -EFAULT;
348 goto out;
349 }
350 count = len;
351 if (count > log_buf_len)
352 count = log_buf_len;
353 spin_lock_irq(&logbuf_lock);
354 if (count > logged_chars)
355 count = logged_chars;
356 if (do_clear)
357 logged_chars = 0;
358 limit = log_end;
359
360
361
362
363
364
365 for (i = 0; i < count && !error; i++) {
366 j = limit-1-i;
367 if (j + log_buf_len < log_end)
368 break;
369 c = LOG_BUF(j);
370 spin_unlock_irq(&logbuf_lock);
371 error = __put_user(c,&buf[count-1-i]);
372 cond_resched();
373 spin_lock_irq(&logbuf_lock);
374 }
375 spin_unlock_irq(&logbuf_lock);
376 if (error)
377 break;
378 error = i;
379 if (i != count) {
380 int offset = count-error;
381
382 for (i = 0; i < error; i++) {
383 if (__get_user(c,&buf[i+offset]) ||
384 __put_user(c,&buf[i])) {
385 error = -EFAULT;
386 break;
387 }
388 cond_resched();
389 }
390 }
391 break;
392 case 5:
393 logged_chars = 0;
394 break;
395 case 6:
396 console_loglevel = minimum_console_loglevel;
397 break;
398 case 7:
399 console_loglevel = default_console_loglevel;
400 break;
401 case 8:
402 error = -EINVAL;
403 if (len < 1 || len > 8)
404 goto out;
405 if (len < minimum_console_loglevel)
406 len = minimum_console_loglevel;
407 console_loglevel = len;
408 error = 0;
409 break;
410 case 9:
411 error = log_end - log_start;
412 break;
413 case 10:
414 error = log_buf_len;
415 break;
416 default:
417 error = -EINVAL;
418 break;
419 }
420out:
421 return error;
422}
423
424asmlinkage long sys_syslog(int type, char __user *buf, int len)
425{
426 return do_syslog(type, buf, len);
427}
428
429
430
431
432static void __call_console_drivers(unsigned long start, unsigned long end)
433{
434 struct console *con;
435
436 for (con = console_drivers; con; con = con->next) {
437 if ((con->flags & CON_ENABLED) && con->write &&
438 (cpu_online(smp_processor_id()) ||
439 (con->flags & CON_ANYTIME)))
440 con->write(con, &LOG_BUF(start), end - start);
441 }
442}
443
444static int __read_mostly ignore_loglevel;
445
446static int __init ignore_loglevel_setup(char *str)
447{
448 ignore_loglevel = 1;
449 printk(KERN_INFO "debug: ignoring loglevel setting.\n");
450
451 return 1;
452}
453
454__setup("ignore_loglevel", ignore_loglevel_setup);
455
456
457
458
459static void _call_console_drivers(unsigned long start,
460 unsigned long end, int msg_log_level)
461{
462 if ((msg_log_level < console_loglevel || ignore_loglevel) &&
463 console_drivers && start != end) {
464 if ((start & LOG_BUF_MASK) > (end & LOG_BUF_MASK)) {
465
466 __call_console_drivers(start & LOG_BUF_MASK,
467 log_buf_len);
468 __call_console_drivers(0, end & LOG_BUF_MASK);
469 } else {
470 __call_console_drivers(start, end);
471 }
472 }
473}
474
475
476
477
478
479
480static void call_console_drivers(unsigned long start, unsigned long end)
481{
482 unsigned long cur_index, start_print;
483 static int msg_level = -1;
484
485 BUG_ON(((long)(start - end)) > 0);
486
487 cur_index = start;
488 start_print = start;
489 while (cur_index != end) {
490 if (msg_level < 0 && ((end - cur_index) > 2) &&
491 LOG_BUF(cur_index + 0) == '<' &&
492 LOG_BUF(cur_index + 1) >= '0' &&
493 LOG_BUF(cur_index + 1) <= '7' &&
494 LOG_BUF(cur_index + 2) == '>') {
495 msg_level = LOG_BUF(cur_index + 1) - '0';
496 cur_index += 3;
497 start_print = cur_index;
498 }
499 while (cur_index != end) {
500 char c = LOG_BUF(cur_index);
501
502 cur_index++;
503 if (c == '\n') {
504 if (msg_level < 0) {
505
506
507
508
509
510
511 msg_level = default_message_loglevel;
512 }
513 _call_console_drivers(start_print, cur_index, msg_level);
514 msg_level = -1;
515 start_print = cur_index;
516 break;
517 }
518 }
519 }
520 _call_console_drivers(start_print, end, msg_level);
521}
522
523static void emit_log_char(char c)
524{
525 LOG_BUF(log_end) = c;
526 log_end++;
527 if (log_end - log_start > log_buf_len)
528 log_start = log_end - log_buf_len;
529 if (log_end - con_start > log_buf_len)
530 con_start = log_end - log_buf_len;
531 if (logged_chars < log_buf_len)
532 logged_chars++;
533}
534
535
536
537
538
539
540static void zap_locks(void)
541{
542 static unsigned long oops_timestamp;
543
544 if (time_after_eq(jiffies, oops_timestamp) &&
545 !time_after(jiffies, oops_timestamp + 30 * HZ))
546 return;
547
548 oops_timestamp = jiffies;
549
550
551 spin_lock_init(&logbuf_lock);
552
553 init_MUTEX(&console_sem);
554}
555
556#if defined(CONFIG_PRINTK_TIME)
557static int printk_time = 1;
558#else
559static int printk_time = 0;
560#endif
561module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR);
562
563static int __init printk_time_setup(char *str)
564{
565 if (*str)
566 return 0;
567 printk_time = 1;
568 printk(KERN_NOTICE "The 'time' option is deprecated and "
569 "is scheduled for removal in early 2008\n");
570 printk(KERN_NOTICE "Use 'printk.time=<value>' instead\n");
571 return 1;
572}
573
574__setup("time", printk_time_setup);
575
576__attribute__((weak)) unsigned long long printk_clock(void)
577{
578 return sched_clock();
579}
580
581
582static int have_callable_console(void)
583{
584 struct console *con;
585
586 for (con = console_drivers; con; con = con->next)
587 if (con->flags & CON_ANYTIME)
588 return 1;
589
590 return 0;
591}
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616asmlinkage int printk(const char *fmt, ...)
617{
618 va_list args;
619 int r;
620
621 va_start(args, fmt);
622 r = vprintk(fmt, args);
623 va_end(args);
624
625 return r;
626}
627
628
629static volatile unsigned int printk_cpu = UINT_MAX;
630
631asmlinkage int vprintk(const char *fmt, va_list args)
632{
633 unsigned long flags;
634 int printed_len;
635 char *p;
636 static char printk_buf[1024];
637 static int log_level_unknown = 1;
638
639 boot_delay_msec();
640
641 preempt_disable();
642 if (unlikely(oops_in_progress) && printk_cpu == smp_processor_id())
643
644
645 zap_locks();
646
647
648 raw_local_irq_save(flags);
649 lockdep_off();
650 spin_lock(&logbuf_lock);
651 printk_cpu = smp_processor_id();
652
653
654 printed_len = vscnprintf(printk_buf, sizeof(printk_buf), fmt, args);
655
656
657
658
659
660 for (p = printk_buf; *p; p++) {
661 if (log_level_unknown) {
662
663 if (printk_time) {
664 int loglev_char;
665 char tbuf[50], *tp;
666 unsigned tlen;
667 unsigned long long t;
668 unsigned long nanosec_rem;
669
670
671
672
673
674 if (p[0] == '<' && p[1] >='0' &&
675 p[1] <= '7' && p[2] == '>') {
676 loglev_char = p[1];
677 p += 3;
678 printed_len -= 3;
679 } else {
680 loglev_char = default_message_loglevel
681 + '0';
682 }
683 t = printk_clock();
684 nanosec_rem = do_div(t, 1000000000);
685 tlen = sprintf(tbuf,
686 "<%c>[%5lu.%06lu] ",
687 loglev_char,
688 (unsigned long)t,
689 nanosec_rem/1000);
690
691 for (tp = tbuf; tp < tbuf + tlen; tp++)
692 emit_log_char(*tp);
693 printed_len += tlen;
694 } else {
695 if (p[0] != '<' || p[1] < '0' ||
696 p[1] > '7' || p[2] != '>') {
697 emit_log_char('<');
698 emit_log_char(default_message_loglevel
699 + '0');
700 emit_log_char('>');
701 printed_len += 3;
702 }
703 }
704 log_level_unknown = 0;
705 if (!*p)
706 break;
707 }
708 emit_log_char(*p);
709 if (*p == '\n')
710 log_level_unknown = 1;
711 }
712
713 if (!down_trylock(&console_sem)) {
714
715
716
717
718 console_locked = 1;
719 printk_cpu = UINT_MAX;
720 spin_unlock(&logbuf_lock);
721
722
723
724
725
726
727
728 if (cpu_online(smp_processor_id()) || have_callable_console()) {
729 console_may_schedule = 0;
730 release_console_sem();
731 } else {
732
733 console_locked = 0;
734 up(&console_sem);
735 }
736 lockdep_on();
737 raw_local_irq_restore(flags);
738 } else {
739
740
741
742
743
744 printk_cpu = UINT_MAX;
745 spin_unlock(&logbuf_lock);
746 lockdep_on();
747 raw_local_irq_restore(flags);
748 }
749
750 preempt_enable();
751 return printed_len;
752}
753EXPORT_SYMBOL(printk);
754EXPORT_SYMBOL(vprintk);
755
756#else
757
758asmlinkage long sys_syslog(int type, char __user *buf, int len)
759{
760 return -ENOSYS;
761}
762
763static void call_console_drivers(unsigned long start, unsigned long end)
764{
765}
766
767#endif
768
769
770
771
772static int __init console_setup(char *str)
773{
774 char buf[sizeof(console_cmdline[0].name) + 4];
775 char *s, *options;
776 int idx;
777
778
779
780
781 if (str[0] >= '0' && str[0] <= '9') {
782 strcpy(buf, "ttyS");
783 strncpy(buf + 4, str, sizeof(buf) - 5);
784 } else {
785 strncpy(buf, str, sizeof(buf) - 1);
786 }
787 buf[sizeof(buf) - 1] = 0;
788 if ((options = strchr(str, ',')) != NULL)
789 *(options++) = 0;
790#ifdef __sparc__
791 if (!strcmp(str, "ttya"))
792 strcpy(buf, "ttyS0");
793 if (!strcmp(str, "ttyb"))
794 strcpy(buf, "ttyS1");
795#endif
796 for (s = buf; *s; s++)
797 if ((*s >= '0' && *s <= '9') || *s == ',')
798 break;
799 idx = simple_strtoul(s, NULL, 10);
800 *s = 0;
801
802 add_preferred_console(buf, idx, options);
803 return 1;
804}
805__setup("console=", console_setup);
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820int add_preferred_console(char *name, int idx, char *options)
821{
822 struct console_cmdline *c;
823 int i;
824
825
826
827
828
829 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
830 if (strcmp(console_cmdline[i].name, name) == 0 &&
831 console_cmdline[i].index == idx) {
832 selected_console = i;
833 return 0;
834 }
835 if (i == MAX_CMDLINECONSOLES)
836 return -E2BIG;
837 selected_console = i;
838 c = &console_cmdline[i];
839 memcpy(c->name, name, sizeof(c->name));
840 c->name[sizeof(c->name) - 1] = 0;
841 c->options = options;
842 c->index = idx;
843 return 0;
844}
845
846int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options)
847{
848 struct console_cmdline *c;
849 int i;
850
851 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
852 if (strcmp(console_cmdline[i].name, name) == 0 &&
853 console_cmdline[i].index == idx) {
854 c = &console_cmdline[i];
855 memcpy(c->name, name_new, sizeof(c->name));
856 c->name[sizeof(c->name) - 1] = 0;
857 c->options = options;
858 c->index = idx_new;
859 return i;
860 }
861
862 return -1;
863}
864
865int console_suspend_enabled = 1;
866EXPORT_SYMBOL(console_suspend_enabled);
867
868static int __init console_suspend_disable(char *str)
869{
870 console_suspend_enabled = 0;
871 return 1;
872}
873__setup("no_console_suspend", console_suspend_disable);
874
875
876
877
878
879
880void suspend_console(void)
881{
882 if (!console_suspend_enabled)
883 return;
884 printk("Suspending console(s)\n");
885 acquire_console_sem();
886 console_suspended = 1;
887}
888
889void resume_console(void)
890{
891 if (!console_suspend_enabled)
892 return;
893 console_suspended = 0;
894 release_console_sem();
895}
896
897
898
899
900
901
902
903
904
905void acquire_console_sem(void)
906{
907 BUG_ON(in_interrupt());
908 if (console_suspended) {
909 down(&secondary_console_sem);
910 return;
911 }
912 down(&console_sem);
913 console_locked = 1;
914 console_may_schedule = 1;
915}
916EXPORT_SYMBOL(acquire_console_sem);
917
918int try_acquire_console_sem(void)
919{
920 if (down_trylock(&console_sem))
921 return -1;
922 console_locked = 1;
923 console_may_schedule = 0;
924 return 0;
925}
926EXPORT_SYMBOL(try_acquire_console_sem);
927
928int is_console_locked(void)
929{
930 return console_locked;
931}
932
933void wake_up_klogd(void)
934{
935 if (!oops_in_progress && waitqueue_active(&log_wait))
936 wake_up_interruptible(&log_wait);
937}
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953void release_console_sem(void)
954{
955 unsigned long flags;
956 unsigned long _con_start, _log_end;
957 unsigned long wake_klogd = 0;
958
959 if (console_suspended) {
960 up(&secondary_console_sem);
961 return;
962 }
963
964 console_may_schedule = 0;
965
966 for ( ; ; ) {
967 spin_lock_irqsave(&logbuf_lock, flags);
968 wake_klogd |= log_start - log_end;
969 if (con_start == log_end)
970 break;
971 _con_start = con_start;
972 _log_end = log_end;
973 con_start = log_end;
974 spin_unlock(&logbuf_lock);
975 call_console_drivers(_con_start, _log_end);
976 local_irq_restore(flags);
977 }
978 console_locked = 0;
979 up(&console_sem);
980 spin_unlock_irqrestore(&logbuf_lock, flags);
981 if (wake_klogd)
982 wake_up_klogd();
983}
984EXPORT_SYMBOL(release_console_sem);
985
986
987
988
989
990
991
992
993
994
995void __sched console_conditional_schedule(void)
996{
997 if (console_may_schedule)
998 cond_resched();
999}
1000EXPORT_SYMBOL(console_conditional_schedule);
1001
1002void console_print(const char *s)
1003{
1004 printk(KERN_EMERG "%s", s);
1005}
1006EXPORT_SYMBOL(console_print);
1007
1008void console_unblank(void)
1009{
1010 struct console *c;
1011
1012
1013
1014
1015
1016 if (oops_in_progress) {
1017 if (down_trylock(&console_sem) != 0)
1018 return;
1019 } else
1020 acquire_console_sem();
1021
1022 console_locked = 1;
1023 console_may_schedule = 0;
1024 for (c = console_drivers; c != NULL; c = c->next)
1025 if ((c->flags & CON_ENABLED) && c->unblank)
1026 c->unblank();
1027 release_console_sem();
1028}
1029
1030
1031
1032
1033struct tty_driver *console_device(int *index)
1034{
1035 struct console *c;
1036 struct tty_driver *driver = NULL;
1037
1038 acquire_console_sem();
1039 for (c = console_drivers; c != NULL; c = c->next) {
1040 if (!c->device)
1041 continue;
1042 driver = c->device(c, index);
1043 if (driver)
1044 break;
1045 }
1046 release_console_sem();
1047 return driver;
1048}
1049
1050
1051
1052
1053
1054
1055void console_stop(struct console *console)
1056{
1057 acquire_console_sem();
1058 console->flags &= ~CON_ENABLED;
1059 release_console_sem();
1060}
1061EXPORT_SYMBOL(console_stop);
1062
1063void console_start(struct console *console)
1064{
1065 acquire_console_sem();
1066 console->flags |= CON_ENABLED;
1067 release_console_sem();
1068}
1069EXPORT_SYMBOL(console_start);
1070
1071
1072
1073
1074
1075
1076
1077void register_console(struct console *console)
1078{
1079 int i;
1080 unsigned long flags;
1081 struct console *bootconsole = NULL;
1082
1083 if (console_drivers) {
1084 if (console->flags & CON_BOOT)
1085 return;
1086 if (console_drivers->flags & CON_BOOT)
1087 bootconsole = console_drivers;
1088 }
1089
1090 if (preferred_console < 0 || bootconsole || !console_drivers)
1091 preferred_console = selected_console;
1092
1093 if (console->early_setup)
1094 console->early_setup();
1095
1096
1097
1098
1099
1100
1101 if (preferred_console < 0) {
1102 if (console->index < 0)
1103 console->index = 0;
1104 if (console->setup == NULL ||
1105 console->setup(console, NULL) == 0) {
1106 console->flags |= CON_ENABLED | CON_CONSDEV;
1107 preferred_console = 0;
1108 }
1109 }
1110
1111
1112
1113
1114
1115 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0];
1116 i++) {
1117 if (strcmp(console_cmdline[i].name, console->name) != 0)
1118 continue;
1119 if (console->index >= 0 &&
1120 console->index != console_cmdline[i].index)
1121 continue;
1122 if (console->index < 0)
1123 console->index = console_cmdline[i].index;
1124 if (console->setup &&
1125 console->setup(console, console_cmdline[i].options) != 0)
1126 break;
1127 console->flags |= CON_ENABLED;
1128 console->index = console_cmdline[i].index;
1129 if (i == selected_console) {
1130 console->flags |= CON_CONSDEV;
1131 preferred_console = selected_console;
1132 }
1133 break;
1134 }
1135
1136 if (!(console->flags & CON_ENABLED))
1137 return;
1138
1139 if (bootconsole && (console->flags & CON_CONSDEV)) {
1140 printk(KERN_INFO "console handover: boot [%s%d] -> real [%s%d]\n",
1141 bootconsole->name, bootconsole->index,
1142 console->name, console->index);
1143 unregister_console(bootconsole);
1144 console->flags &= ~CON_PRINTBUFFER;
1145 } else {
1146 printk(KERN_INFO "console [%s%d] enabled\n",
1147 console->name, console->index);
1148 }
1149
1150
1151
1152
1153
1154 acquire_console_sem();
1155 if ((console->flags & CON_CONSDEV) || console_drivers == NULL) {
1156 console->next = console_drivers;
1157 console_drivers = console;
1158 if (console->next)
1159 console->next->flags &= ~CON_CONSDEV;
1160 } else {
1161 console->next = console_drivers->next;
1162 console_drivers->next = console;
1163 }
1164 if (console->flags & CON_PRINTBUFFER) {
1165
1166
1167
1168
1169 spin_lock_irqsave(&logbuf_lock, flags);
1170 con_start = log_start;
1171 spin_unlock_irqrestore(&logbuf_lock, flags);
1172 }
1173 release_console_sem();
1174}
1175EXPORT_SYMBOL(register_console);
1176
1177int unregister_console(struct console *console)
1178{
1179 struct console *a, *b;
1180 int res = 1;
1181
1182 acquire_console_sem();
1183 if (console_drivers == console) {
1184 console_drivers=console->next;
1185 res = 0;
1186 } else if (console_drivers) {
1187 for (a=console_drivers->next, b=console_drivers ;
1188 a; b=a, a=b->next) {
1189 if (a == console) {
1190 b->next = a->next;
1191 res = 0;
1192 break;
1193 }
1194 }
1195 }
1196
1197
1198
1199
1200
1201 if (console_drivers != NULL && console->flags & CON_CONSDEV)
1202 console_drivers->flags |= CON_CONSDEV;
1203
1204 release_console_sem();
1205 return res;
1206}
1207EXPORT_SYMBOL(unregister_console);
1208
1209static int __init disable_boot_consoles(void)
1210{
1211 if (console_drivers != NULL) {
1212 if (console_drivers->flags & CON_BOOT) {
1213 printk(KERN_INFO "turn off boot console %s%d\n",
1214 console_drivers->name, console_drivers->index);
1215 return unregister_console(console_drivers);
1216 }
1217 }
1218 return 0;
1219}
1220late_initcall(disable_boot_consoles);
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231void tty_write_message(struct tty_struct *tty, char *msg)
1232{
1233 if (tty && tty->driver->write)
1234 tty->driver->write(tty, msg, strlen(msg));
1235 return;
1236}
1237
1238
1239
1240
1241
1242
1243
1244
1245int __printk_ratelimit(int ratelimit_jiffies, int ratelimit_burst)
1246{
1247 static DEFINE_SPINLOCK(ratelimit_lock);
1248 static unsigned long toks = 10 * 5 * HZ;
1249 static unsigned long last_msg;
1250 static int missed;
1251 unsigned long flags;
1252 unsigned long now = jiffies;
1253
1254 spin_lock_irqsave(&ratelimit_lock, flags);
1255 toks += now - last_msg;
1256 last_msg = now;
1257 if (toks > (ratelimit_burst * ratelimit_jiffies))
1258 toks = ratelimit_burst * ratelimit_jiffies;
1259 if (toks >= ratelimit_jiffies) {
1260 int lost = missed;
1261
1262 missed = 0;
1263 toks -= ratelimit_jiffies;
1264 spin_unlock_irqrestore(&ratelimit_lock, flags);
1265 if (lost)
1266 printk(KERN_WARNING "printk: %d messages suppressed.\n", lost);
1267 return 1;
1268 }
1269 missed++;
1270 spin_unlock_irqrestore(&ratelimit_lock, flags);
1271 return 0;
1272}
1273EXPORT_SYMBOL(__printk_ratelimit);
1274
1275
1276int printk_ratelimit_jiffies = 5 * HZ;
1277
1278
1279int printk_ratelimit_burst = 10;
1280
1281int printk_ratelimit(void)
1282{
1283 return __printk_ratelimit(printk_ratelimit_jiffies,
1284 printk_ratelimit_burst);
1285}
1286EXPORT_SYMBOL(printk_ratelimit);
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297bool printk_timed_ratelimit(unsigned long *caller_jiffies,
1298 unsigned int interval_msecs)
1299{
1300 if (*caller_jiffies == 0 || time_after(jiffies, *caller_jiffies)) {
1301 *caller_jiffies = jiffies + msecs_to_jiffies(interval_msecs);
1302 return true;
1303 }
1304 return false;
1305}
1306EXPORT_SYMBOL(printk_timed_ratelimit);
1307