]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
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. | |
40dc5651 | 13 | * Fixed SMP synchronization, 08/08/99, Manfred Spraul |
624dffcb | 14 | * manfred@colorfullife.com |
1da177e4 | 15 | * Rewrote bits to get rid of console_lock |
e1f8e874 | 16 | * 01Mar01 Andrew Morton |
1da177e4 LT |
17 | */ |
18 | ||
19 | #include <linux/kernel.h> | |
20 | #include <linux/mm.h> | |
21 | #include <linux/tty.h> | |
22 | #include <linux/tty_driver.h> | |
1da177e4 LT |
23 | #include <linux/console.h> |
24 | #include <linux/init.h> | |
bfe8df3d RD |
25 | #include <linux/jiffies.h> |
26 | #include <linux/nmi.h> | |
1da177e4 | 27 | #include <linux/module.h> |
3b9c0410 | 28 | #include <linux/moduleparam.h> |
1da177e4 | 29 | #include <linux/interrupt.h> /* For in_interrupt() */ |
1da177e4 LT |
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> | |
04d491ab | 35 | #include <linux/kexec.h> |
3fff4c42 | 36 | #include <linux/ratelimit.h> |
1da177e4 LT |
37 | |
38 | #include <asm/uaccess.h> | |
39 | ||
4d091611 RG |
40 | /* |
41 | * for_each_console() allows you to iterate on each console | |
42 | */ | |
43 | #define for_each_console(con) \ | |
44 | for (con = console_drivers; con != NULL; con = con->next) | |
45 | ||
076f9776 IM |
46 | /* |
47 | * Architectures can override it: | |
48 | */ | |
e17ba73b | 49 | void asmlinkage __attribute__((weak)) early_printk(const char *fmt, ...) |
076f9776 IM |
50 | { |
51 | } | |
52 | ||
1da177e4 LT |
53 | #define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT) |
54 | ||
55 | /* printk's without a loglevel use this.. */ | |
56 | #define DEFAULT_MESSAGE_LOGLEVEL 4 /* KERN_WARNING */ | |
57 | ||
58 | /* We show everything that is MORE important than this.. */ | |
59 | #define MINIMUM_CONSOLE_LOGLEVEL 1 /* Minimum loglevel we let people use */ | |
60 | #define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */ | |
61 | ||
62 | DECLARE_WAIT_QUEUE_HEAD(log_wait); | |
63 | ||
64 | int console_printk[4] = { | |
65 | DEFAULT_CONSOLE_LOGLEVEL, /* console_loglevel */ | |
66 | DEFAULT_MESSAGE_LOGLEVEL, /* default_message_loglevel */ | |
67 | MINIMUM_CONSOLE_LOGLEVEL, /* minimum_console_loglevel */ | |
68 | DEFAULT_CONSOLE_LOGLEVEL, /* default_console_loglevel */ | |
69 | }; | |
70 | ||
1aaad49e FP |
71 | static int saved_console_loglevel = -1; |
72 | ||
1da177e4 | 73 | /* |
0bbfb7c2 | 74 | * Low level drivers may need that to know if they can schedule in |
1da177e4 LT |
75 | * their unblank() callback or not. So let's export it. |
76 | */ | |
77 | int oops_in_progress; | |
78 | EXPORT_SYMBOL(oops_in_progress); | |
79 | ||
80 | /* | |
81 | * console_sem protects the console_drivers list, and also | |
82 | * provides serialisation for access to the entire console | |
83 | * driver system. | |
84 | */ | |
85 | static DECLARE_MUTEX(console_sem); | |
86 | struct console *console_drivers; | |
a29d1cfe IM |
87 | EXPORT_SYMBOL_GPL(console_drivers); |
88 | ||
1da177e4 LT |
89 | /* |
90 | * This is used for debugging the mess that is the VT code by | |
91 | * keeping track if we have the console semaphore held. It's | |
92 | * definitely not the perfect debug tool (we don't know if _WE_ | |
93 | * hold it are racing, but it helps tracking those weird code | |
94 | * path in the console code where we end up in places I want | |
95 | * locked without the console sempahore held | |
96 | */ | |
557240b4 | 97 | static int console_locked, console_suspended; |
1da177e4 LT |
98 | |
99 | /* | |
100 | * logbuf_lock protects log_buf, log_start, log_end, con_start and logged_chars | |
101 | * It is also used in interesting ways to provide interlocking in | |
102 | * release_console_sem(). | |
103 | */ | |
104 | static DEFINE_SPINLOCK(logbuf_lock); | |
105 | ||
eed4a2ab | 106 | #define LOG_BUF_MASK (log_buf_len-1) |
1da177e4 LT |
107 | #define LOG_BUF(idx) (log_buf[(idx) & LOG_BUF_MASK]) |
108 | ||
109 | /* | |
110 | * The indices into log_buf are not constrained to log_buf_len - they | |
111 | * must be masked before subscripting | |
112 | */ | |
eed4a2ab DV |
113 | static unsigned log_start; /* Index into log_buf: next char to be read by syslog() */ |
114 | static unsigned con_start; /* Index into log_buf: next char to be sent to consoles */ | |
115 | static unsigned log_end; /* Index into log_buf: most-recently-written-char + 1 */ | |
1da177e4 LT |
116 | |
117 | /* | |
118 | * Array of consoles built from command line options (console=) | |
119 | */ | |
120 | struct console_cmdline | |
121 | { | |
122 | char name[8]; /* Name of the driver */ | |
123 | int index; /* Minor dev. to use */ | |
124 | char *options; /* Options for the driver */ | |
f7511d5f ST |
125 | #ifdef CONFIG_A11Y_BRAILLE_CONSOLE |
126 | char *brl_options; /* Options for braille driver */ | |
127 | #endif | |
1da177e4 LT |
128 | }; |
129 | ||
130 | #define MAX_CMDLINECONSOLES 8 | |
131 | ||
132 | static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES]; | |
133 | static int selected_console = -1; | |
134 | static int preferred_console = -1; | |
9e124fe1 MA |
135 | int console_set_on_cmdline; |
136 | EXPORT_SYMBOL(console_set_on_cmdline); | |
1da177e4 LT |
137 | |
138 | /* Flag: console code may call schedule() */ | |
139 | static int console_may_schedule; | |
140 | ||
d59745ce MM |
141 | #ifdef CONFIG_PRINTK |
142 | ||
143 | static char __log_buf[__LOG_BUF_LEN]; | |
144 | static char *log_buf = __log_buf; | |
145 | static int log_buf_len = __LOG_BUF_LEN; | |
eed4a2ab | 146 | static unsigned logged_chars; /* Number of chars produced since last read+clear operation */ |
d59745ce | 147 | |
04d491ab NH |
148 | #ifdef CONFIG_KEXEC |
149 | /* | |
150 | * This appends the listed symbols to /proc/vmcoreinfo | |
151 | * | |
152 | * /proc/vmcoreinfo is used by various utiilties, like crash and makedumpfile to | |
153 | * obtain access to symbols that are otherwise very difficult to locate. These | |
154 | * symbols are specifically used so that utilities can access and extract the | |
155 | * dmesg log from a vmcore file after a crash. | |
156 | */ | |
157 | void log_buf_kexec_setup(void) | |
158 | { | |
159 | VMCOREINFO_SYMBOL(log_buf); | |
160 | VMCOREINFO_SYMBOL(log_end); | |
161 | VMCOREINFO_SYMBOL(log_buf_len); | |
162 | VMCOREINFO_SYMBOL(logged_chars); | |
163 | } | |
164 | #endif | |
165 | ||
1da177e4 LT |
166 | static int __init log_buf_len_setup(char *str) |
167 | { | |
eed4a2ab | 168 | unsigned size = memparse(str, &str); |
1da177e4 LT |
169 | unsigned long flags; |
170 | ||
171 | if (size) | |
172 | size = roundup_pow_of_two(size); | |
173 | if (size > log_buf_len) { | |
eed4a2ab | 174 | unsigned start, dest_idx, offset; |
40dc5651 | 175 | char *new_log_buf; |
1da177e4 LT |
176 | |
177 | new_log_buf = alloc_bootmem(size); | |
178 | if (!new_log_buf) { | |
40dc5651 | 179 | printk(KERN_WARNING "log_buf_len: allocation failed\n"); |
1da177e4 LT |
180 | goto out; |
181 | } | |
182 | ||
183 | spin_lock_irqsave(&logbuf_lock, flags); | |
184 | log_buf_len = size; | |
185 | log_buf = new_log_buf; | |
186 | ||
187 | offset = start = min(con_start, log_start); | |
188 | dest_idx = 0; | |
189 | while (start != log_end) { | |
190 | log_buf[dest_idx] = __log_buf[start & (__LOG_BUF_LEN - 1)]; | |
191 | start++; | |
192 | dest_idx++; | |
193 | } | |
194 | log_start -= offset; | |
195 | con_start -= offset; | |
196 | log_end -= offset; | |
197 | spin_unlock_irqrestore(&logbuf_lock, flags); | |
198 | ||
40dc5651 | 199 | printk(KERN_NOTICE "log_buf_len: %d\n", log_buf_len); |
1da177e4 LT |
200 | } |
201 | out: | |
1da177e4 LT |
202 | return 1; |
203 | } | |
204 | ||
205 | __setup("log_buf_len=", log_buf_len_setup); | |
206 | ||
bfe8df3d RD |
207 | #ifdef CONFIG_BOOT_PRINTK_DELAY |
208 | ||
209 | static unsigned int boot_delay; /* msecs delay after each printk during bootup */ | |
3a3b6ed2 | 210 | static unsigned long long loops_per_msec; /* based on boot_delay */ |
bfe8df3d RD |
211 | |
212 | static int __init boot_delay_setup(char *str) | |
213 | { | |
214 | unsigned long lpj; | |
bfe8df3d RD |
215 | |
216 | lpj = preset_lpj ? preset_lpj : 1000000; /* some guess */ | |
217 | loops_per_msec = (unsigned long long)lpj / 1000 * HZ; | |
218 | ||
219 | get_option(&str, &boot_delay); | |
220 | if (boot_delay > 10 * 1000) | |
221 | boot_delay = 0; | |
222 | ||
3a3b6ed2 DY |
223 | pr_debug("boot_delay: %u, preset_lpj: %ld, lpj: %lu, " |
224 | "HZ: %d, loops_per_msec: %llu\n", | |
225 | boot_delay, preset_lpj, lpj, HZ, loops_per_msec); | |
bfe8df3d RD |
226 | return 1; |
227 | } | |
228 | __setup("boot_delay=", boot_delay_setup); | |
229 | ||
230 | static void boot_delay_msec(void) | |
231 | { | |
232 | unsigned long long k; | |
233 | unsigned long timeout; | |
234 | ||
235 | if (boot_delay == 0 || system_state != SYSTEM_BOOTING) | |
236 | return; | |
237 | ||
3a3b6ed2 | 238 | k = (unsigned long long)loops_per_msec * boot_delay; |
bfe8df3d RD |
239 | |
240 | timeout = jiffies + msecs_to_jiffies(boot_delay); | |
241 | while (k) { | |
242 | k--; | |
243 | cpu_relax(); | |
244 | /* | |
245 | * use (volatile) jiffies to prevent | |
246 | * compiler reduction; loop termination via jiffies | |
247 | * is secondary and may or may not happen. | |
248 | */ | |
249 | if (time_after(jiffies, timeout)) | |
250 | break; | |
251 | touch_nmi_watchdog(); | |
252 | } | |
253 | } | |
254 | #else | |
255 | static inline void boot_delay_msec(void) | |
256 | { | |
257 | } | |
258 | #endif | |
259 | ||
1da177e4 LT |
260 | /* |
261 | * Commands to do_syslog: | |
262 | * | |
263 | * 0 -- Close the log. Currently a NOP. | |
264 | * 1 -- Open the log. Currently a NOP. | |
265 | * 2 -- Read from the log. | |
266 | * 3 -- Read all messages remaining in the ring buffer. | |
267 | * 4 -- Read and clear all messages remaining in the ring buffer | |
268 | * 5 -- Clear ring buffer. | |
269 | * 6 -- Disable printk's to console | |
270 | * 7 -- Enable printk's to console | |
271 | * 8 -- Set level of messages printed to console | |
272 | * 9 -- Return number of unread characters in the log buffer | |
273 | * 10 -- Return size of the log buffer | |
274 | */ | |
40dc5651 | 275 | int do_syslog(int type, char __user *buf, int len) |
1da177e4 | 276 | { |
eed4a2ab | 277 | unsigned i, j, limit, count; |
1da177e4 LT |
278 | int do_clear = 0; |
279 | char c; | |
280 | int error = 0; | |
281 | ||
282 | error = security_syslog(type); | |
283 | if (error) | |
284 | return error; | |
285 | ||
286 | switch (type) { | |
287 | case 0: /* Close log */ | |
288 | break; | |
289 | case 1: /* Open log */ | |
290 | break; | |
291 | case 2: /* Read from log */ | |
292 | error = -EINVAL; | |
293 | if (!buf || len < 0) | |
294 | goto out; | |
295 | error = 0; | |
296 | if (!len) | |
297 | goto out; | |
298 | if (!access_ok(VERIFY_WRITE, buf, len)) { | |
299 | error = -EFAULT; | |
300 | goto out; | |
301 | } | |
40dc5651 JJ |
302 | error = wait_event_interruptible(log_wait, |
303 | (log_start - log_end)); | |
1da177e4 LT |
304 | if (error) |
305 | goto out; | |
306 | i = 0; | |
307 | spin_lock_irq(&logbuf_lock); | |
308 | while (!error && (log_start != log_end) && i < len) { | |
309 | c = LOG_BUF(log_start); | |
310 | log_start++; | |
311 | spin_unlock_irq(&logbuf_lock); | |
312 | error = __put_user(c,buf); | |
313 | buf++; | |
314 | i++; | |
315 | cond_resched(); | |
316 | spin_lock_irq(&logbuf_lock); | |
317 | } | |
318 | spin_unlock_irq(&logbuf_lock); | |
319 | if (!error) | |
320 | error = i; | |
321 | break; | |
322 | case 4: /* Read/clear last kernel messages */ | |
40dc5651 | 323 | do_clear = 1; |
1da177e4 LT |
324 | /* FALL THRU */ |
325 | case 3: /* Read last kernel messages */ | |
326 | error = -EINVAL; | |
327 | if (!buf || len < 0) | |
328 | goto out; | |
329 | error = 0; | |
330 | if (!len) | |
331 | goto out; | |
332 | if (!access_ok(VERIFY_WRITE, buf, len)) { | |
333 | error = -EFAULT; | |
334 | goto out; | |
335 | } | |
336 | count = len; | |
337 | if (count > log_buf_len) | |
338 | count = log_buf_len; | |
339 | spin_lock_irq(&logbuf_lock); | |
340 | if (count > logged_chars) | |
341 | count = logged_chars; | |
342 | if (do_clear) | |
343 | logged_chars = 0; | |
344 | limit = log_end; | |
345 | /* | |
346 | * __put_user() could sleep, and while we sleep | |
40dc5651 | 347 | * printk() could overwrite the messages |
1da177e4 LT |
348 | * we try to copy to user space. Therefore |
349 | * the messages are copied in reverse. <manfreds> | |
350 | */ | |
40dc5651 | 351 | for (i = 0; i < count && !error; i++) { |
1da177e4 LT |
352 | j = limit-1-i; |
353 | if (j + log_buf_len < log_end) | |
354 | break; | |
355 | c = LOG_BUF(j); | |
356 | spin_unlock_irq(&logbuf_lock); | |
357 | error = __put_user(c,&buf[count-1-i]); | |
358 | cond_resched(); | |
359 | spin_lock_irq(&logbuf_lock); | |
360 | } | |
361 | spin_unlock_irq(&logbuf_lock); | |
362 | if (error) | |
363 | break; | |
364 | error = i; | |
40dc5651 | 365 | if (i != count) { |
1da177e4 LT |
366 | int offset = count-error; |
367 | /* buffer overflow during copy, correct user buffer. */ | |
40dc5651 | 368 | for (i = 0; i < error; i++) { |
1da177e4 LT |
369 | if (__get_user(c,&buf[i+offset]) || |
370 | __put_user(c,&buf[i])) { | |
371 | error = -EFAULT; | |
372 | break; | |
373 | } | |
374 | cond_resched(); | |
375 | } | |
376 | } | |
377 | break; | |
378 | case 5: /* Clear ring buffer */ | |
379 | logged_chars = 0; | |
380 | break; | |
381 | case 6: /* Disable logging to console */ | |
1aaad49e FP |
382 | if (saved_console_loglevel == -1) |
383 | saved_console_loglevel = console_loglevel; | |
1da177e4 LT |
384 | console_loglevel = minimum_console_loglevel; |
385 | break; | |
386 | case 7: /* Enable logging to console */ | |
1aaad49e FP |
387 | if (saved_console_loglevel != -1) { |
388 | console_loglevel = saved_console_loglevel; | |
389 | saved_console_loglevel = -1; | |
390 | } | |
1da177e4 LT |
391 | break; |
392 | case 8: /* Set level of messages printed to console */ | |
393 | error = -EINVAL; | |
394 | if (len < 1 || len > 8) | |
395 | goto out; | |
396 | if (len < minimum_console_loglevel) | |
397 | len = minimum_console_loglevel; | |
398 | console_loglevel = len; | |
1aaad49e FP |
399 | /* Implicitly re-enable logging to console */ |
400 | saved_console_loglevel = -1; | |
1da177e4 LT |
401 | error = 0; |
402 | break; | |
403 | case 9: /* Number of chars in the log buffer */ | |
404 | error = log_end - log_start; | |
405 | break; | |
406 | case 10: /* Size of the log buffer */ | |
407 | error = log_buf_len; | |
408 | break; | |
409 | default: | |
410 | error = -EINVAL; | |
411 | break; | |
412 | } | |
413 | out: | |
414 | return error; | |
415 | } | |
416 | ||
1e7bfb21 | 417 | SYSCALL_DEFINE3(syslog, int, type, char __user *, buf, int, len) |
1da177e4 LT |
418 | { |
419 | return do_syslog(type, buf, len); | |
420 | } | |
421 | ||
422 | /* | |
423 | * Call the console drivers on a range of log_buf | |
424 | */ | |
eed4a2ab | 425 | static void __call_console_drivers(unsigned start, unsigned end) |
1da177e4 LT |
426 | { |
427 | struct console *con; | |
428 | ||
4d091611 | 429 | for_each_console(con) { |
76a8ad29 ME |
430 | if ((con->flags & CON_ENABLED) && con->write && |
431 | (cpu_online(smp_processor_id()) || | |
432 | (con->flags & CON_ANYTIME))) | |
1da177e4 LT |
433 | con->write(con, &LOG_BUF(start), end - start); |
434 | } | |
435 | } | |
436 | ||
79290822 IM |
437 | static int __read_mostly ignore_loglevel; |
438 | ||
99eea6a1 | 439 | static int __init ignore_loglevel_setup(char *str) |
79290822 IM |
440 | { |
441 | ignore_loglevel = 1; | |
442 | printk(KERN_INFO "debug: ignoring loglevel setting.\n"); | |
443 | ||
c4772d99 | 444 | return 0; |
79290822 IM |
445 | } |
446 | ||
c4772d99 | 447 | early_param("ignore_loglevel", ignore_loglevel_setup); |
79290822 | 448 | |
1da177e4 LT |
449 | /* |
450 | * Write out chars from start to end - 1 inclusive | |
451 | */ | |
eed4a2ab DV |
452 | static void _call_console_drivers(unsigned start, |
453 | unsigned end, int msg_log_level) | |
1da177e4 | 454 | { |
79290822 | 455 | if ((msg_log_level < console_loglevel || ignore_loglevel) && |
1da177e4 LT |
456 | console_drivers && start != end) { |
457 | if ((start & LOG_BUF_MASK) > (end & LOG_BUF_MASK)) { | |
458 | /* wrapped write */ | |
459 | __call_console_drivers(start & LOG_BUF_MASK, | |
460 | log_buf_len); | |
461 | __call_console_drivers(0, end & LOG_BUF_MASK); | |
462 | } else { | |
463 | __call_console_drivers(start, end); | |
464 | } | |
465 | } | |
466 | } | |
467 | ||
468 | /* | |
469 | * Call the console drivers, asking them to write out | |
470 | * log_buf[start] to log_buf[end - 1]. | |
471 | * The console_sem must be held. | |
472 | */ | |
eed4a2ab | 473 | static void call_console_drivers(unsigned start, unsigned end) |
1da177e4 | 474 | { |
eed4a2ab | 475 | unsigned cur_index, start_print; |
1da177e4 LT |
476 | static int msg_level = -1; |
477 | ||
eed4a2ab | 478 | BUG_ON(((int)(start - end)) > 0); |
1da177e4 LT |
479 | |
480 | cur_index = start; | |
481 | start_print = start; | |
482 | while (cur_index != end) { | |
40dc5651 JJ |
483 | if (msg_level < 0 && ((end - cur_index) > 2) && |
484 | LOG_BUF(cur_index + 0) == '<' && | |
485 | LOG_BUF(cur_index + 1) >= '0' && | |
486 | LOG_BUF(cur_index + 1) <= '7' && | |
487 | LOG_BUF(cur_index + 2) == '>') { | |
1da177e4 LT |
488 | msg_level = LOG_BUF(cur_index + 1) - '0'; |
489 | cur_index += 3; | |
490 | start_print = cur_index; | |
491 | } | |
492 | while (cur_index != end) { | |
493 | char c = LOG_BUF(cur_index); | |
1da177e4 | 494 | |
40dc5651 | 495 | cur_index++; |
1da177e4 LT |
496 | if (c == '\n') { |
497 | if (msg_level < 0) { | |
498 | /* | |
499 | * printk() has already given us loglevel tags in | |
500 | * the buffer. This code is here in case the | |
501 | * log buffer has wrapped right round and scribbled | |
502 | * on those tags | |
503 | */ | |
504 | msg_level = default_message_loglevel; | |
505 | } | |
506 | _call_console_drivers(start_print, cur_index, msg_level); | |
507 | msg_level = -1; | |
508 | start_print = cur_index; | |
509 | break; | |
510 | } | |
511 | } | |
512 | } | |
513 | _call_console_drivers(start_print, end, msg_level); | |
514 | } | |
515 | ||
516 | static void emit_log_char(char c) | |
517 | { | |
518 | LOG_BUF(log_end) = c; | |
519 | log_end++; | |
520 | if (log_end - log_start > log_buf_len) | |
521 | log_start = log_end - log_buf_len; | |
522 | if (log_end - con_start > log_buf_len) | |
523 | con_start = log_end - log_buf_len; | |
524 | if (logged_chars < log_buf_len) | |
525 | logged_chars++; | |
526 | } | |
527 | ||
528 | /* | |
529 | * Zap console related locks when oopsing. Only zap at most once | |
530 | * every 10 seconds, to leave time for slow consoles to print a | |
531 | * full oops. | |
532 | */ | |
533 | static void zap_locks(void) | |
534 | { | |
535 | static unsigned long oops_timestamp; | |
536 | ||
537 | if (time_after_eq(jiffies, oops_timestamp) && | |
40dc5651 | 538 | !time_after(jiffies, oops_timestamp + 30 * HZ)) |
1da177e4 LT |
539 | return; |
540 | ||
541 | oops_timestamp = jiffies; | |
542 | ||
543 | /* If a crash is occurring, make sure we can't deadlock */ | |
544 | spin_lock_init(&logbuf_lock); | |
545 | /* And make sure that we print immediately */ | |
546 | init_MUTEX(&console_sem); | |
547 | } | |
548 | ||
549 | #if defined(CONFIG_PRINTK_TIME) | |
550 | static int printk_time = 1; | |
551 | #else | |
552 | static int printk_time = 0; | |
553 | #endif | |
e84845c4 | 554 | module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR); |
1da177e4 | 555 | |
76a8ad29 ME |
556 | /* Check if we have any console registered that can be called early in boot. */ |
557 | static int have_callable_console(void) | |
558 | { | |
559 | struct console *con; | |
560 | ||
4d091611 | 561 | for_each_console(con) |
76a8ad29 ME |
562 | if (con->flags & CON_ANYTIME) |
563 | return 1; | |
564 | ||
565 | return 0; | |
566 | } | |
567 | ||
ddad86c2 MW |
568 | /** |
569 | * printk - print a kernel message | |
570 | * @fmt: format string | |
571 | * | |
72fd4a35 | 572 | * This is printk(). It can be called from any context. We want it to work. |
40dc5651 | 573 | * |
1da177e4 LT |
574 | * We try to grab the console_sem. If we succeed, it's easy - we log the output and |
575 | * call the console drivers. If we fail to get the semaphore we place the output | |
576 | * into the log buffer and return. The current holder of the console_sem will | |
577 | * notice the new output in release_console_sem() and will send it to the | |
578 | * consoles before releasing the semaphore. | |
579 | * | |
580 | * One effect of this deferred printing is that code which calls printk() and | |
581 | * then changes console_loglevel may break. This is because console_loglevel | |
582 | * is inspected when the actual printing occurs. | |
ddad86c2 MW |
583 | * |
584 | * See also: | |
585 | * printf(3) | |
20036fdc AK |
586 | * |
587 | * See the vsnprintf() documentation for format string extensions over C99. | |
1da177e4 | 588 | */ |
d59745ce | 589 | |
1da177e4 LT |
590 | asmlinkage int printk(const char *fmt, ...) |
591 | { | |
592 | va_list args; | |
593 | int r; | |
594 | ||
595 | va_start(args, fmt); | |
596 | r = vprintk(fmt, args); | |
597 | va_end(args); | |
598 | ||
599 | return r; | |
600 | } | |
601 | ||
fe21773d DH |
602 | /* cpu currently holding logbuf_lock */ |
603 | static volatile unsigned int printk_cpu = UINT_MAX; | |
604 | ||
266c2e0a LT |
605 | /* |
606 | * Can we actually use the console at this time on this cpu? | |
607 | * | |
608 | * Console drivers may assume that per-cpu resources have | |
609 | * been allocated. So unless they're explicitly marked as | |
610 | * being able to cope (CON_ANYTIME) don't call them until | |
611 | * this CPU is officially up. | |
612 | */ | |
613 | static inline int can_use_console(unsigned int cpu) | |
614 | { | |
615 | return cpu_online(cpu) || have_callable_console(); | |
616 | } | |
617 | ||
618 | /* | |
619 | * Try to get console ownership to actually show the kernel | |
620 | * messages from a 'printk'. Return true (and with the | |
621 | * console_semaphore held, and 'console_locked' set) if it | |
622 | * is successful, false otherwise. | |
623 | * | |
624 | * This gets called with the 'logbuf_lock' spinlock held and | |
625 | * interrupts disabled. It should return with 'lockbuf_lock' | |
626 | * released but interrupts still disabled. | |
627 | */ | |
628 | static int acquire_console_semaphore_for_printk(unsigned int cpu) | |
629 | { | |
630 | int retval = 0; | |
631 | ||
093a07e2 LT |
632 | if (!try_acquire_console_sem()) { |
633 | retval = 1; | |
634 | ||
635 | /* | |
636 | * If we can't use the console, we need to release | |
637 | * the console semaphore by hand to avoid flushing | |
638 | * the buffer. We need to hold the console semaphore | |
639 | * in order to do this test safely. | |
640 | */ | |
641 | if (!can_use_console(cpu)) { | |
642 | console_locked = 0; | |
643 | up(&console_sem); | |
644 | retval = 0; | |
645 | } | |
646 | } | |
266c2e0a LT |
647 | printk_cpu = UINT_MAX; |
648 | spin_unlock(&logbuf_lock); | |
649 | return retval; | |
650 | } | |
3b8945e8 TH |
651 | static const char recursion_bug_msg [] = |
652 | KERN_CRIT "BUG: recent printk recursion!\n"; | |
653 | static int recursion_bug; | |
edb123e1 | 654 | static int new_text_line = 1; |
3b8945e8 | 655 | static char printk_buf[1024]; |
32a76006 | 656 | |
af91322e DY |
657 | int printk_delay_msec __read_mostly; |
658 | ||
659 | static inline void printk_delay(void) | |
660 | { | |
661 | if (unlikely(printk_delay_msec)) { | |
662 | int m = printk_delay_msec; | |
663 | ||
664 | while (m--) { | |
665 | mdelay(1); | |
666 | touch_nmi_watchdog(); | |
667 | } | |
668 | } | |
669 | } | |
670 | ||
1da177e4 LT |
671 | asmlinkage int vprintk(const char *fmt, va_list args) |
672 | { | |
32a76006 | 673 | int printed_len = 0; |
09159308 | 674 | int current_log_level = default_message_loglevel; |
ac60ad74 | 675 | unsigned long flags; |
32a76006 | 676 | int this_cpu; |
1da177e4 | 677 | char *p; |
1da177e4 | 678 | |
bfe8df3d | 679 | boot_delay_msec(); |
af91322e | 680 | printk_delay(); |
bfe8df3d | 681 | |
fe21773d | 682 | preempt_disable(); |
1da177e4 | 683 | /* This stops the holder of console_sem just where we want him */ |
1efc5da3 | 684 | raw_local_irq_save(flags); |
32a76006 IM |
685 | this_cpu = smp_processor_id(); |
686 | ||
687 | /* | |
688 | * Ouch, printk recursed into itself! | |
689 | */ | |
690 | if (unlikely(printk_cpu == this_cpu)) { | |
691 | /* | |
692 | * If a crash is occurring during printk() on this CPU, | |
693 | * then try to get the crash message out but make sure | |
694 | * we can't deadlock. Otherwise just return to avoid the | |
695 | * recursion and return - but flag the recursion so that | |
696 | * it can be printed at the next appropriate moment: | |
697 | */ | |
698 | if (!oops_in_progress) { | |
3b8945e8 | 699 | recursion_bug = 1; |
32a76006 IM |
700 | goto out_restore_irqs; |
701 | } | |
702 | zap_locks(); | |
703 | } | |
704 | ||
a0f1ccfd IM |
705 | lockdep_off(); |
706 | spin_lock(&logbuf_lock); | |
32a76006 | 707 | printk_cpu = this_cpu; |
1da177e4 | 708 | |
3b8945e8 TH |
709 | if (recursion_bug) { |
710 | recursion_bug = 0; | |
711 | strcpy(printk_buf, recursion_bug_msg); | |
26cc271d | 712 | printed_len = strlen(recursion_bug_msg); |
32a76006 | 713 | } |
1da177e4 | 714 | /* Emit the output into the temporary buffer */ |
32a76006 | 715 | printed_len += vscnprintf(printk_buf + printed_len, |
cf3680b9 | 716 | sizeof(printk_buf) - printed_len, fmt, args); |
1da177e4 | 717 | |
ac60ad74 | 718 | |
5fd29d6c LT |
719 | p = printk_buf; |
720 | ||
721 | /* Do we have a loglevel in the string? */ | |
722 | if (p[0] == '<') { | |
723 | unsigned char c = p[1]; | |
724 | if (c && p[2] == '>') { | |
725 | switch (c) { | |
726 | case '0' ... '7': /* loglevel */ | |
727 | current_log_level = c - '0'; | |
e28d7137 LT |
728 | /* Fallthrough - make sure we're on a new line */ |
729 | case 'd': /* KERN_DEFAULT */ | |
5fd29d6c LT |
730 | if (!new_text_line) { |
731 | emit_log_char('\n'); | |
732 | new_text_line = 1; | |
733 | } | |
734 | /* Fallthrough - skip the loglevel */ | |
735 | case 'c': /* KERN_CONT */ | |
736 | p += 3; | |
737 | break; | |
738 | } | |
739 | } | |
740 | } | |
741 | ||
1da177e4 LT |
742 | /* |
743 | * Copy the output into log_buf. If the caller didn't provide | |
744 | * appropriate log level tags, we insert them here | |
745 | */ | |
5fd29d6c | 746 | for ( ; *p; p++) { |
ac60ad74 | 747 | if (new_text_line) { |
ac60ad74 NA |
748 | /* Always output the token */ |
749 | emit_log_char('<'); | |
750 | emit_log_char(current_log_level + '0'); | |
751 | emit_log_char('>'); | |
752 | printed_len += 3; | |
753 | new_text_line = 0; | |
754 | ||
1da177e4 | 755 | if (printk_time) { |
ac60ad74 | 756 | /* Follow the token with the time */ |
1da177e4 LT |
757 | char tbuf[50], *tp; |
758 | unsigned tlen; | |
759 | unsigned long long t; | |
760 | unsigned long nanosec_rem; | |
761 | ||
326e96b9 | 762 | t = cpu_clock(printk_cpu); |
1da177e4 | 763 | nanosec_rem = do_div(t, 1000000000); |
ac60ad74 NA |
764 | tlen = sprintf(tbuf, "[%5lu.%06lu] ", |
765 | (unsigned long) t, | |
766 | nanosec_rem / 1000); | |
1da177e4 LT |
767 | |
768 | for (tp = tbuf; tp < tbuf + tlen; tp++) | |
769 | emit_log_char(*tp); | |
025510cd | 770 | printed_len += tlen; |
1da177e4 | 771 | } |
ac60ad74 | 772 | |
1da177e4 LT |
773 | if (!*p) |
774 | break; | |
775 | } | |
ac60ad74 | 776 | |
1da177e4 LT |
777 | emit_log_char(*p); |
778 | if (*p == '\n') | |
ac60ad74 | 779 | new_text_line = 1; |
1da177e4 LT |
780 | } |
781 | ||
266c2e0a LT |
782 | /* |
783 | * Try to acquire and then immediately release the | |
784 | * console semaphore. The release will do all the | |
785 | * actual magic (print out buffers, wake up klogd, | |
786 | * etc). | |
787 | * | |
788 | * The acquire_console_semaphore_for_printk() function | |
789 | * will release 'logbuf_lock' regardless of whether it | |
790 | * actually gets the semaphore or not. | |
791 | */ | |
792 | if (acquire_console_semaphore_for_printk(this_cpu)) | |
793 | release_console_sem(); | |
76a8ad29 | 794 | |
266c2e0a | 795 | lockdep_on(); |
32a76006 | 796 | out_restore_irqs: |
266c2e0a | 797 | raw_local_irq_restore(flags); |
76a8ad29 | 798 | |
fe21773d | 799 | preempt_enable(); |
1da177e4 LT |
800 | return printed_len; |
801 | } | |
802 | EXPORT_SYMBOL(printk); | |
803 | EXPORT_SYMBOL(vprintk); | |
804 | ||
d59745ce MM |
805 | #else |
806 | ||
eed4a2ab | 807 | static void call_console_drivers(unsigned start, unsigned end) |
40dc5651 JJ |
808 | { |
809 | } | |
d59745ce MM |
810 | |
811 | #endif | |
812 | ||
f7511d5f ST |
813 | static int __add_preferred_console(char *name, int idx, char *options, |
814 | char *brl_options) | |
815 | { | |
816 | struct console_cmdline *c; | |
817 | int i; | |
818 | ||
819 | /* | |
820 | * See if this tty is not yet registered, and | |
821 | * if we have a slot free. | |
822 | */ | |
823 | for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++) | |
824 | if (strcmp(console_cmdline[i].name, name) == 0 && | |
825 | console_cmdline[i].index == idx) { | |
826 | if (!brl_options) | |
827 | selected_console = i; | |
828 | return 0; | |
829 | } | |
830 | if (i == MAX_CMDLINECONSOLES) | |
831 | return -E2BIG; | |
832 | if (!brl_options) | |
833 | selected_console = i; | |
834 | c = &console_cmdline[i]; | |
835 | strlcpy(c->name, name, sizeof(c->name)); | |
836 | c->options = options; | |
837 | #ifdef CONFIG_A11Y_BRAILLE_CONSOLE | |
838 | c->brl_options = brl_options; | |
839 | #endif | |
840 | c->index = idx; | |
841 | return 0; | |
842 | } | |
2ea1c539 JB |
843 | /* |
844 | * Set up a list of consoles. Called from init/main.c | |
845 | */ | |
846 | static int __init console_setup(char *str) | |
847 | { | |
eaa944af | 848 | char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for index */ |
f7511d5f | 849 | char *s, *options, *brl_options = NULL; |
2ea1c539 JB |
850 | int idx; |
851 | ||
f7511d5f ST |
852 | #ifdef CONFIG_A11Y_BRAILLE_CONSOLE |
853 | if (!memcmp(str, "brl,", 4)) { | |
854 | brl_options = ""; | |
855 | str += 4; | |
856 | } else if (!memcmp(str, "brl=", 4)) { | |
857 | brl_options = str + 4; | |
858 | str = strchr(brl_options, ','); | |
859 | if (!str) { | |
860 | printk(KERN_ERR "need port name after brl=\n"); | |
861 | return 1; | |
862 | } | |
863 | *(str++) = 0; | |
864 | } | |
865 | #endif | |
866 | ||
2ea1c539 JB |
867 | /* |
868 | * Decode str into name, index, options. | |
869 | */ | |
870 | if (str[0] >= '0' && str[0] <= '9') { | |
eaa944af YL |
871 | strcpy(buf, "ttyS"); |
872 | strncpy(buf + 4, str, sizeof(buf) - 5); | |
2ea1c539 | 873 | } else { |
eaa944af | 874 | strncpy(buf, str, sizeof(buf) - 1); |
2ea1c539 | 875 | } |
eaa944af | 876 | buf[sizeof(buf) - 1] = 0; |
2ea1c539 JB |
877 | if ((options = strchr(str, ',')) != NULL) |
878 | *(options++) = 0; | |
879 | #ifdef __sparc__ | |
880 | if (!strcmp(str, "ttya")) | |
eaa944af | 881 | strcpy(buf, "ttyS0"); |
2ea1c539 | 882 | if (!strcmp(str, "ttyb")) |
eaa944af | 883 | strcpy(buf, "ttyS1"); |
2ea1c539 | 884 | #endif |
eaa944af | 885 | for (s = buf; *s; s++) |
2ea1c539 JB |
886 | if ((*s >= '0' && *s <= '9') || *s == ',') |
887 | break; | |
888 | idx = simple_strtoul(s, NULL, 10); | |
889 | *s = 0; | |
890 | ||
f7511d5f | 891 | __add_preferred_console(buf, idx, options, brl_options); |
9e124fe1 | 892 | console_set_on_cmdline = 1; |
2ea1c539 JB |
893 | return 1; |
894 | } | |
895 | __setup("console=", console_setup); | |
896 | ||
3c0547ba MM |
897 | /** |
898 | * add_preferred_console - add a device to the list of preferred consoles. | |
ddad86c2 MW |
899 | * @name: device name |
900 | * @idx: device index | |
901 | * @options: options for this console | |
3c0547ba MM |
902 | * |
903 | * The last preferred console added will be used for kernel messages | |
904 | * and stdin/out/err for init. Normally this is used by console_setup | |
905 | * above to handle user-supplied console arguments; however it can also | |
906 | * be used by arch-specific code either to override the user or more | |
907 | * commonly to provide a default console (ie from PROM variables) when | |
908 | * the user has not supplied one. | |
909 | */ | |
fb445ee5 | 910 | int add_preferred_console(char *name, int idx, char *options) |
3c0547ba | 911 | { |
f7511d5f | 912 | return __add_preferred_console(name, idx, options, NULL); |
3c0547ba MM |
913 | } |
914 | ||
b6b1d877 | 915 | int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options) |
18a8bd94 YL |
916 | { |
917 | struct console_cmdline *c; | |
918 | int i; | |
919 | ||
920 | for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++) | |
921 | if (strcmp(console_cmdline[i].name, name) == 0 && | |
922 | console_cmdline[i].index == idx) { | |
923 | c = &console_cmdline[i]; | |
f735295b | 924 | strlcpy(c->name, name_new, sizeof(c->name)); |
18a8bd94 YL |
925 | c->name[sizeof(c->name) - 1] = 0; |
926 | c->options = options; | |
927 | c->index = idx_new; | |
928 | return i; | |
929 | } | |
930 | /* not found */ | |
931 | return -1; | |
932 | } | |
933 | ||
8f4ce8c3 AS |
934 | int console_suspend_enabled = 1; |
935 | EXPORT_SYMBOL(console_suspend_enabled); | |
936 | ||
937 | static int __init console_suspend_disable(char *str) | |
938 | { | |
939 | console_suspend_enabled = 0; | |
940 | return 1; | |
941 | } | |
942 | __setup("no_console_suspend", console_suspend_disable); | |
943 | ||
557240b4 LT |
944 | /** |
945 | * suspend_console - suspend the console subsystem | |
946 | * | |
947 | * This disables printk() while we go into suspend states | |
948 | */ | |
949 | void suspend_console(void) | |
950 | { | |
8f4ce8c3 AS |
951 | if (!console_suspend_enabled) |
952 | return; | |
0d63081d | 953 | printk("Suspending console(s) (use no_console_suspend to debug)\n"); |
557240b4 LT |
954 | acquire_console_sem(); |
955 | console_suspended = 1; | |
403f3075 | 956 | up(&console_sem); |
557240b4 LT |
957 | } |
958 | ||
959 | void resume_console(void) | |
960 | { | |
8f4ce8c3 AS |
961 | if (!console_suspend_enabled) |
962 | return; | |
403f3075 | 963 | down(&console_sem); |
557240b4 LT |
964 | console_suspended = 0; |
965 | release_console_sem(); | |
966 | } | |
967 | ||
1da177e4 LT |
968 | /** |
969 | * acquire_console_sem - lock the console system for exclusive use. | |
970 | * | |
971 | * Acquires a semaphore which guarantees that the caller has | |
972 | * exclusive access to the console system and the console_drivers list. | |
973 | * | |
974 | * Can sleep, returns nothing. | |
975 | */ | |
976 | void acquire_console_sem(void) | |
977 | { | |
8abd8e29 | 978 | BUG_ON(in_interrupt()); |
1da177e4 | 979 | down(&console_sem); |
403f3075 AH |
980 | if (console_suspended) |
981 | return; | |
1da177e4 LT |
982 | console_locked = 1; |
983 | console_may_schedule = 1; | |
984 | } | |
985 | EXPORT_SYMBOL(acquire_console_sem); | |
986 | ||
987 | int try_acquire_console_sem(void) | |
988 | { | |
989 | if (down_trylock(&console_sem)) | |
990 | return -1; | |
403f3075 AH |
991 | if (console_suspended) { |
992 | up(&console_sem); | |
993 | return -1; | |
994 | } | |
1da177e4 LT |
995 | console_locked = 1; |
996 | console_may_schedule = 0; | |
997 | return 0; | |
998 | } | |
999 | EXPORT_SYMBOL(try_acquire_console_sem); | |
1000 | ||
1001 | int is_console_locked(void) | |
1002 | { | |
1003 | return console_locked; | |
1004 | } | |
1da177e4 | 1005 | |
b845b517 PZ |
1006 | static DEFINE_PER_CPU(int, printk_pending); |
1007 | ||
1008 | void printk_tick(void) | |
e3e8a75d | 1009 | { |
b845b517 PZ |
1010 | if (__get_cpu_var(printk_pending)) { |
1011 | __get_cpu_var(printk_pending) = 0; | |
e3e8a75d | 1012 | wake_up_interruptible(&log_wait); |
b845b517 PZ |
1013 | } |
1014 | } | |
1015 | ||
1016 | int printk_needs_cpu(int cpu) | |
1017 | { | |
1018 | return per_cpu(printk_pending, cpu); | |
1019 | } | |
1020 | ||
1021 | void wake_up_klogd(void) | |
1022 | { | |
1023 | if (waitqueue_active(&log_wait)) | |
fa33507a | 1024 | __raw_get_cpu_var(printk_pending) = 1; |
e3e8a75d KK |
1025 | } |
1026 | ||
1da177e4 LT |
1027 | /** |
1028 | * release_console_sem - unlock the console system | |
1029 | * | |
1030 | * Releases the semaphore which the caller holds on the console system | |
1031 | * and the console driver list. | |
1032 | * | |
1033 | * While the semaphore was held, console output may have been buffered | |
1034 | * by printk(). If this is the case, release_console_sem() emits | |
1035 | * the output prior to releasing the semaphore. | |
1036 | * | |
1037 | * If there is output waiting for klogd, we wake it up. | |
1038 | * | |
1039 | * release_console_sem() may be called from any context. | |
1040 | */ | |
1041 | void release_console_sem(void) | |
1042 | { | |
1043 | unsigned long flags; | |
eed4a2ab DV |
1044 | unsigned _con_start, _log_end; |
1045 | unsigned wake_klogd = 0; | |
1da177e4 | 1046 | |
557240b4 | 1047 | if (console_suspended) { |
403f3075 | 1048 | up(&console_sem); |
557240b4 LT |
1049 | return; |
1050 | } | |
78944e54 AD |
1051 | |
1052 | console_may_schedule = 0; | |
1053 | ||
1da177e4 LT |
1054 | for ( ; ; ) { |
1055 | spin_lock_irqsave(&logbuf_lock, flags); | |
1056 | wake_klogd |= log_start - log_end; | |
1057 | if (con_start == log_end) | |
1058 | break; /* Nothing to print */ | |
1059 | _con_start = con_start; | |
1060 | _log_end = log_end; | |
1061 | con_start = log_end; /* Flush */ | |
1062 | spin_unlock(&logbuf_lock); | |
81d68a96 | 1063 | stop_critical_timings(); /* don't trace print latency */ |
1da177e4 | 1064 | call_console_drivers(_con_start, _log_end); |
81d68a96 | 1065 | start_critical_timings(); |
1da177e4 LT |
1066 | local_irq_restore(flags); |
1067 | } | |
1068 | console_locked = 0; | |
1da177e4 LT |
1069 | up(&console_sem); |
1070 | spin_unlock_irqrestore(&logbuf_lock, flags); | |
e3e8a75d KK |
1071 | if (wake_klogd) |
1072 | wake_up_klogd(); | |
1da177e4 LT |
1073 | } |
1074 | EXPORT_SYMBOL(release_console_sem); | |
1075 | ||
ddad86c2 MW |
1076 | /** |
1077 | * console_conditional_schedule - yield the CPU if required | |
1da177e4 LT |
1078 | * |
1079 | * If the console code is currently allowed to sleep, and | |
1080 | * if this CPU should yield the CPU to another task, do | |
1081 | * so here. | |
1082 | * | |
1083 | * Must be called within acquire_console_sem(). | |
1084 | */ | |
1085 | void __sched console_conditional_schedule(void) | |
1086 | { | |
1087 | if (console_may_schedule) | |
1088 | cond_resched(); | |
1089 | } | |
1090 | EXPORT_SYMBOL(console_conditional_schedule); | |
1091 | ||
1da177e4 LT |
1092 | void console_unblank(void) |
1093 | { | |
1094 | struct console *c; | |
1095 | ||
1096 | /* | |
1097 | * console_unblank can no longer be called in interrupt context unless | |
1098 | * oops_in_progress is set to 1.. | |
1099 | */ | |
1100 | if (oops_in_progress) { | |
1101 | if (down_trylock(&console_sem) != 0) | |
1102 | return; | |
1103 | } else | |
1104 | acquire_console_sem(); | |
1105 | ||
1106 | console_locked = 1; | |
1107 | console_may_schedule = 0; | |
4d091611 | 1108 | for_each_console(c) |
1da177e4 LT |
1109 | if ((c->flags & CON_ENABLED) && c->unblank) |
1110 | c->unblank(); | |
1111 | release_console_sem(); | |
1112 | } | |
1da177e4 LT |
1113 | |
1114 | /* | |
1115 | * Return the console tty driver structure and its associated index | |
1116 | */ | |
1117 | struct tty_driver *console_device(int *index) | |
1118 | { | |
1119 | struct console *c; | |
1120 | struct tty_driver *driver = NULL; | |
1121 | ||
1122 | acquire_console_sem(); | |
4d091611 | 1123 | for_each_console(c) { |
1da177e4 LT |
1124 | if (!c->device) |
1125 | continue; | |
1126 | driver = c->device(c, index); | |
1127 | if (driver) | |
1128 | break; | |
1129 | } | |
1130 | release_console_sem(); | |
1131 | return driver; | |
1132 | } | |
1133 | ||
1134 | /* | |
1135 | * Prevent further output on the passed console device so that (for example) | |
1136 | * serial drivers can disable console output before suspending a port, and can | |
1137 | * re-enable output afterwards. | |
1138 | */ | |
1139 | void console_stop(struct console *console) | |
1140 | { | |
1141 | acquire_console_sem(); | |
1142 | console->flags &= ~CON_ENABLED; | |
1143 | release_console_sem(); | |
1144 | } | |
1145 | EXPORT_SYMBOL(console_stop); | |
1146 | ||
1147 | void console_start(struct console *console) | |
1148 | { | |
1149 | acquire_console_sem(); | |
1150 | console->flags |= CON_ENABLED; | |
1151 | release_console_sem(); | |
1152 | } | |
1153 | EXPORT_SYMBOL(console_start); | |
1154 | ||
1155 | /* | |
1156 | * The console driver calls this routine during kernel initialization | |
1157 | * to register the console printing procedure with printk() and to | |
1158 | * print any messages that were printed by the kernel before the | |
1159 | * console driver was initialized. | |
4d091611 RG |
1160 | * |
1161 | * This can happen pretty early during the boot process (because of | |
1162 | * early_printk) - sometimes before setup_arch() completes - be careful | |
1163 | * of what kernel features are used - they may not be initialised yet. | |
1164 | * | |
1165 | * There are two types of consoles - bootconsoles (early_printk) and | |
1166 | * "real" consoles (everything which is not a bootconsole) which are | |
1167 | * handled differently. | |
1168 | * - Any number of bootconsoles can be registered at any time. | |
1169 | * - As soon as a "real" console is registered, all bootconsoles | |
1170 | * will be unregistered automatically. | |
1171 | * - Once a "real" console is registered, any attempt to register a | |
1172 | * bootconsoles will be rejected | |
1da177e4 | 1173 | */ |
4d091611 | 1174 | void register_console(struct console *newcon) |
1da177e4 | 1175 | { |
40dc5651 | 1176 | int i; |
1da177e4 | 1177 | unsigned long flags; |
4d091611 | 1178 | struct console *bcon = NULL; |
1da177e4 | 1179 | |
4d091611 RG |
1180 | /* |
1181 | * before we register a new CON_BOOT console, make sure we don't | |
1182 | * already have a valid console | |
1183 | */ | |
1184 | if (console_drivers && newcon->flags & CON_BOOT) { | |
1185 | /* find the last or real console */ | |
1186 | for_each_console(bcon) { | |
1187 | if (!(bcon->flags & CON_BOOT)) { | |
1188 | printk(KERN_INFO "Too late to register bootconsole %s%d\n", | |
1189 | newcon->name, newcon->index); | |
1190 | return; | |
1191 | } | |
1192 | } | |
69331af7 GH |
1193 | } |
1194 | ||
4d091611 RG |
1195 | if (console_drivers && console_drivers->flags & CON_BOOT) |
1196 | bcon = console_drivers; | |
1197 | ||
1198 | if (preferred_console < 0 || bcon || !console_drivers) | |
1da177e4 LT |
1199 | preferred_console = selected_console; |
1200 | ||
4d091611 RG |
1201 | if (newcon->early_setup) |
1202 | newcon->early_setup(); | |
18a8bd94 | 1203 | |
1da177e4 LT |
1204 | /* |
1205 | * See if we want to use this console driver. If we | |
1206 | * didn't select a console we take the first one | |
1207 | * that registers here. | |
1208 | */ | |
1209 | if (preferred_console < 0) { | |
4d091611 RG |
1210 | if (newcon->index < 0) |
1211 | newcon->index = 0; | |
1212 | if (newcon->setup == NULL || | |
1213 | newcon->setup(newcon, NULL) == 0) { | |
1214 | newcon->flags |= CON_ENABLED; | |
1215 | if (newcon->device) { | |
1216 | newcon->flags |= CON_CONSDEV; | |
cd3a1b85 JK |
1217 | preferred_console = 0; |
1218 | } | |
1da177e4 LT |
1219 | } |
1220 | } | |
1221 | ||
1222 | /* | |
1223 | * See if this console matches one we selected on | |
1224 | * the command line. | |
1225 | */ | |
40dc5651 JJ |
1226 | for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; |
1227 | i++) { | |
4d091611 | 1228 | if (strcmp(console_cmdline[i].name, newcon->name) != 0) |
1da177e4 | 1229 | continue; |
4d091611 RG |
1230 | if (newcon->index >= 0 && |
1231 | newcon->index != console_cmdline[i].index) | |
1da177e4 | 1232 | continue; |
4d091611 RG |
1233 | if (newcon->index < 0) |
1234 | newcon->index = console_cmdline[i].index; | |
f7511d5f ST |
1235 | #ifdef CONFIG_A11Y_BRAILLE_CONSOLE |
1236 | if (console_cmdline[i].brl_options) { | |
4d091611 RG |
1237 | newcon->flags |= CON_BRL; |
1238 | braille_register_console(newcon, | |
f7511d5f ST |
1239 | console_cmdline[i].index, |
1240 | console_cmdline[i].options, | |
1241 | console_cmdline[i].brl_options); | |
1242 | return; | |
1243 | } | |
1244 | #endif | |
4d091611 RG |
1245 | if (newcon->setup && |
1246 | newcon->setup(newcon, console_cmdline[i].options) != 0) | |
1da177e4 | 1247 | break; |
4d091611 RG |
1248 | newcon->flags |= CON_ENABLED; |
1249 | newcon->index = console_cmdline[i].index; | |
ab4af03a | 1250 | if (i == selected_console) { |
4d091611 | 1251 | newcon->flags |= CON_CONSDEV; |
ab4af03a GE |
1252 | preferred_console = selected_console; |
1253 | } | |
1da177e4 LT |
1254 | break; |
1255 | } | |
1256 | ||
4d091611 | 1257 | if (!(newcon->flags & CON_ENABLED)) |
1da177e4 LT |
1258 | return; |
1259 | ||
8259cf43 RG |
1260 | /* |
1261 | * If we have a bootconsole, and are switching to a real console, | |
1262 | * don't print everything out again, since when the boot console, and | |
1263 | * the real console are the same physical device, it's annoying to | |
1264 | * see the beginning boot messages twice | |
1265 | */ | |
1266 | if (bcon && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV)) | |
4d091611 | 1267 | newcon->flags &= ~CON_PRINTBUFFER; |
1da177e4 LT |
1268 | |
1269 | /* | |
1270 | * Put this console in the list - keep the | |
1271 | * preferred driver at the head of the list. | |
1272 | */ | |
1273 | acquire_console_sem(); | |
4d091611 RG |
1274 | if ((newcon->flags & CON_CONSDEV) || console_drivers == NULL) { |
1275 | newcon->next = console_drivers; | |
1276 | console_drivers = newcon; | |
1277 | if (newcon->next) | |
1278 | newcon->next->flags &= ~CON_CONSDEV; | |
1da177e4 | 1279 | } else { |
4d091611 RG |
1280 | newcon->next = console_drivers->next; |
1281 | console_drivers->next = newcon; | |
1da177e4 | 1282 | } |
4d091611 | 1283 | if (newcon->flags & CON_PRINTBUFFER) { |
1da177e4 LT |
1284 | /* |
1285 | * release_console_sem() will print out the buffered messages | |
1286 | * for us. | |
1287 | */ | |
1288 | spin_lock_irqsave(&logbuf_lock, flags); | |
1289 | con_start = log_start; | |
1290 | spin_unlock_irqrestore(&logbuf_lock, flags); | |
1291 | } | |
1292 | release_console_sem(); | |
8259cf43 RG |
1293 | |
1294 | /* | |
1295 | * By unregistering the bootconsoles after we enable the real console | |
1296 | * we get the "console xxx enabled" message on all the consoles - | |
1297 | * boot consoles, real consoles, etc - this is to ensure that end | |
1298 | * users know there might be something in the kernel's log buffer that | |
1299 | * went to the bootconsole (that they do not see on the real console) | |
1300 | */ | |
1301 | if (bcon && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV)) { | |
1302 | /* we need to iterate through twice, to make sure we print | |
1303 | * everything out, before we unregister the console(s) | |
1304 | */ | |
1305 | printk(KERN_INFO "console [%s%d] enabled, bootconsole disabled\n", | |
1306 | newcon->name, newcon->index); | |
1307 | for_each_console(bcon) | |
1308 | if (bcon->flags & CON_BOOT) | |
1309 | unregister_console(bcon); | |
1310 | } else { | |
1311 | printk(KERN_INFO "%sconsole [%s%d] enabled\n", | |
1312 | (newcon->flags & CON_BOOT) ? "boot" : "" , | |
1313 | newcon->name, newcon->index); | |
1314 | } | |
1da177e4 LT |
1315 | } |
1316 | EXPORT_SYMBOL(register_console); | |
1317 | ||
40dc5651 | 1318 | int unregister_console(struct console *console) |
1da177e4 | 1319 | { |
40dc5651 | 1320 | struct console *a, *b; |
1da177e4 LT |
1321 | int res = 1; |
1322 | ||
f7511d5f ST |
1323 | #ifdef CONFIG_A11Y_BRAILLE_CONSOLE |
1324 | if (console->flags & CON_BRL) | |
1325 | return braille_unregister_console(console); | |
1326 | #endif | |
1327 | ||
1da177e4 LT |
1328 | acquire_console_sem(); |
1329 | if (console_drivers == console) { | |
1330 | console_drivers=console->next; | |
1331 | res = 0; | |
e9b15b54 | 1332 | } else if (console_drivers) { |
1da177e4 LT |
1333 | for (a=console_drivers->next, b=console_drivers ; |
1334 | a; b=a, a=b->next) { | |
1335 | if (a == console) { | |
1336 | b->next = a->next; | |
1337 | res = 0; | |
1338 | break; | |
40dc5651 | 1339 | } |
1da177e4 LT |
1340 | } |
1341 | } | |
40dc5651 | 1342 | |
69331af7 | 1343 | /* |
ab4af03a GE |
1344 | * If this isn't the last console and it has CON_CONSDEV set, we |
1345 | * need to set it on the next preferred console. | |
1da177e4 | 1346 | */ |
69331af7 | 1347 | if (console_drivers != NULL && console->flags & CON_CONSDEV) |
ab4af03a | 1348 | console_drivers->flags |= CON_CONSDEV; |
1da177e4 LT |
1349 | |
1350 | release_console_sem(); | |
1351 | return res; | |
1352 | } | |
1353 | EXPORT_SYMBOL(unregister_console); | |
d59745ce | 1354 | |
0c5564bd RG |
1355 | static int __init disable_boot_consoles(void) |
1356 | { | |
4d091611 RG |
1357 | struct console *con; |
1358 | ||
1359 | for_each_console(con) { | |
1360 | if (con->flags & CON_BOOT) { | |
cb00e99c | 1361 | printk(KERN_INFO "turn off boot console %s%d\n", |
4d091611 | 1362 | con->name, con->index); |
42c2c8c8 | 1363 | unregister_console(con); |
cb00e99c | 1364 | } |
0c5564bd RG |
1365 | } |
1366 | return 0; | |
1367 | } | |
1368 | late_initcall(disable_boot_consoles); | |
1369 | ||
7ef3d2fd | 1370 | #if defined CONFIG_PRINTK |
717115e1 | 1371 | |
1da177e4 LT |
1372 | /* |
1373 | * printk rate limiting, lifted from the networking subsystem. | |
1374 | * | |
641de9d8 UKK |
1375 | * This enforces a rate limit: not more than 10 kernel messages |
1376 | * every 5s to make a denial-of-service attack impossible. | |
1da177e4 | 1377 | */ |
641de9d8 UKK |
1378 | DEFINE_RATELIMIT_STATE(printk_ratelimit_state, 5 * HZ, 10); |
1379 | ||
5c828713 | 1380 | int __printk_ratelimit(const char *func) |
1da177e4 | 1381 | { |
5c828713 | 1382 | return ___ratelimit(&printk_ratelimit_state, func); |
1da177e4 | 1383 | } |
5c828713 | 1384 | EXPORT_SYMBOL(__printk_ratelimit); |
f46c4833 AM |
1385 | |
1386 | /** | |
1387 | * printk_timed_ratelimit - caller-controlled printk ratelimiting | |
1388 | * @caller_jiffies: pointer to caller's state | |
1389 | * @interval_msecs: minimum interval between prints | |
1390 | * | |
1391 | * printk_timed_ratelimit() returns true if more than @interval_msecs | |
1392 | * milliseconds have elapsed since the last time printk_timed_ratelimit() | |
1393 | * returned true. | |
1394 | */ | |
1395 | bool printk_timed_ratelimit(unsigned long *caller_jiffies, | |
1396 | unsigned int interval_msecs) | |
1397 | { | |
f2d28a2e GK |
1398 | if (*caller_jiffies == 0 |
1399 | || !time_in_range(jiffies, *caller_jiffies, | |
1400 | *caller_jiffies | |
1401 | + msecs_to_jiffies(interval_msecs))) { | |
1402 | *caller_jiffies = jiffies; | |
f46c4833 AM |
1403 | return true; |
1404 | } | |
1405 | return false; | |
1406 | } | |
1407 | EXPORT_SYMBOL(printk_timed_ratelimit); | |
7ef3d2fd | 1408 | #endif |