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