Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. |
Jesper Juhl | 40dc565 | 2005-10-30 15:02:46 -0800 | [diff] [blame] | 13 | * Fixed SMP synchronization, 08/08/99, Manfred Spraul |
Christian Kujau | 624dffc | 2006-01-15 02:43:54 +0100 | [diff] [blame] | 14 | * manfred@colorfullife.com |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | * Rewrote bits to get rid of console_lock |
Francois Cami | e1f8e87 | 2008-10-15 22:01:59 -0700 | [diff] [blame] | 16 | * 01Mar01 Andrew Morton |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/mm.h> |
| 21 | #include <linux/tty.h> |
| 22 | #include <linux/tty_driver.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/console.h> |
| 24 | #include <linux/init.h> |
Randy Dunlap | bfe8df3 | 2007-10-16 01:23:46 -0700 | [diff] [blame] | 25 | #include <linux/jiffies.h> |
| 26 | #include <linux/nmi.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <linux/module.h> |
Jan Engelhardt | 3b9c041 | 2006-06-25 05:48:15 -0700 | [diff] [blame] | 28 | #include <linux/moduleparam.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/interrupt.h> /* For in_interrupt() */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <linux/delay.h> |
| 31 | #include <linux/smp.h> |
| 32 | #include <linux/security.h> |
| 33 | #include <linux/bootmem.h> |
Mike Travis | 162a7e7 | 2011-05-24 17:13:20 -0700 | [diff] [blame] | 34 | #include <linux/memblock.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <linux/syscalls.h> |
Neil Horman | 04d491a | 2009-04-02 16:58:57 -0700 | [diff] [blame] | 36 | #include <linux/kexec.h> |
Jason Wessel | d37d39a | 2010-05-20 21:04:27 -0500 | [diff] [blame] | 37 | #include <linux/kdb.h> |
Ingo Molnar | 3fff4c4 | 2009-09-22 16:18:09 +0200 | [diff] [blame] | 38 | #include <linux/ratelimit.h> |
Simon Kagstrom | 456b565 | 2009-10-16 14:09:18 +0200 | [diff] [blame] | 39 | #include <linux/kmsg_dump.h> |
Kees Cook | 0023459 | 2010-02-03 15:36:43 -0800 | [diff] [blame] | 40 | #include <linux/syslog.h> |
Kevin Cernekee | 034260d | 2010-06-03 22:11:25 -0700 | [diff] [blame] | 41 | #include <linux/cpu.h> |
| 42 | #include <linux/notifier.h> |
Huang Ying | fb842b0 | 2011-01-12 16:59:43 -0800 | [diff] [blame] | 43 | #include <linux/rculist.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
| 45 | #include <asm/uaccess.h> |
| 46 | |
Johannes Berg | 9510035 | 2011-11-24 20:03:08 +0100 | [diff] [blame] | 47 | #define CREATE_TRACE_POINTS |
| 48 | #include <trace/events/printk.h> |
| 49 | |
Ingo Molnar | 076f977 | 2008-01-30 13:33:06 +0100 | [diff] [blame] | 50 | /* |
| 51 | * Architectures can override it: |
| 52 | */ |
Jiri Slaby | e17ba73 | 2008-05-12 15:44:40 +0200 | [diff] [blame] | 53 | void asmlinkage __attribute__((weak)) early_printk(const char *fmt, ...) |
Ingo Molnar | 076f977 | 2008-01-30 13:33:06 +0100 | [diff] [blame] | 54 | { |
| 55 | } |
| 56 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | /* printk's without a loglevel use this.. */ |
Mandeep Singh Baines | 5af5bcb | 2011-03-22 16:34:23 -0700 | [diff] [blame] | 58 | #define DEFAULT_MESSAGE_LOGLEVEL CONFIG_DEFAULT_MESSAGE_LOGLEVEL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
| 60 | /* We show everything that is MORE important than this.. */ |
| 61 | #define MINIMUM_CONSOLE_LOGLEVEL 1 /* Minimum loglevel we let people use */ |
| 62 | #define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */ |
| 63 | |
| 64 | DECLARE_WAIT_QUEUE_HEAD(log_wait); |
| 65 | |
| 66 | int console_printk[4] = { |
| 67 | DEFAULT_CONSOLE_LOGLEVEL, /* console_loglevel */ |
| 68 | DEFAULT_MESSAGE_LOGLEVEL, /* default_message_loglevel */ |
| 69 | MINIMUM_CONSOLE_LOGLEVEL, /* minimum_console_loglevel */ |
| 70 | DEFAULT_CONSOLE_LOGLEVEL, /* default_console_loglevel */ |
| 71 | }; |
| 72 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | /* |
Patrick Pletscher | 0bbfb7c | 2007-02-17 20:10:16 +0100 | [diff] [blame] | 74 | * Low level drivers may need that to know if they can schedule in |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Thomas Gleixner | 5b8c4f2 | 2010-09-07 14:33:43 +0000 | [diff] [blame] | 85 | static DEFINE_SEMAPHORE(console_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | struct console *console_drivers; |
Ingo Molnar | a29d1cf | 2008-06-02 13:19:08 +0200 | [diff] [blame] | 87 | EXPORT_SYMBOL_GPL(console_drivers); |
| 88 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Linus Torvalds | 557240b | 2006-06-19 18:16:01 -0700 | [diff] [blame] | 97 | static int console_locked, console_suspended; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | |
| 99 | /* |
Feng Tang | fe3d8ad | 2011-03-22 16:34:21 -0700 | [diff] [blame] | 100 | * If exclusive_console is non-NULL then only this console is to be printed to. |
| 101 | */ |
| 102 | static struct console *exclusive_console; |
| 103 | |
| 104 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | * Array of consoles built from command line options (console=) |
| 106 | */ |
| 107 | struct console_cmdline |
| 108 | { |
| 109 | char name[8]; /* Name of the driver */ |
| 110 | int index; /* Minor dev. to use */ |
| 111 | char *options; /* Options for the driver */ |
Samuel Thibault | f7511d5 | 2008-04-30 00:54:51 -0700 | [diff] [blame] | 112 | #ifdef CONFIG_A11Y_BRAILLE_CONSOLE |
| 113 | char *brl_options; /* Options for braille driver */ |
| 114 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | #define MAX_CMDLINECONSOLES 8 |
| 118 | |
| 119 | static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES]; |
| 120 | static int selected_console = -1; |
| 121 | static int preferred_console = -1; |
Markus Armbruster | 9e124fe | 2008-05-26 23:31:07 +0100 | [diff] [blame] | 122 | int console_set_on_cmdline; |
| 123 | EXPORT_SYMBOL(console_set_on_cmdline); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | |
| 125 | /* Flag: console code may call schedule() */ |
| 126 | static int console_may_schedule; |
| 127 | |
Matt Mackall | d59745c | 2005-05-01 08:59:02 -0700 | [diff] [blame] | 128 | #ifdef CONFIG_PRINTK |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame^] | 129 | /* |
| 130 | * The printk log buffer consists of a chain of concatenated variable |
| 131 | * length records. Every record starts with a record header, containing |
| 132 | * the overall length of the record. |
| 133 | * |
| 134 | * The heads to the first and last entry in the buffer, as well as the |
| 135 | * sequence numbers of these both entries are maintained when messages |
| 136 | * are stored.. |
| 137 | * |
| 138 | * If the heads indicate available messages, the length in the header |
| 139 | * tells the start next message. A length == 0 for the next message |
| 140 | * indicates a wrap-around to the beginning of the buffer. |
| 141 | * |
| 142 | * Every record carries the monotonic timestamp in microseconds, as well as |
| 143 | * the standard userspace syslog level and syslog facility. The usual |
| 144 | * kernel messages use LOG_KERN; userspace-injected messages always carry |
| 145 | * a matching syslog facility, by default LOG_USER. The origin of every |
| 146 | * message can be reliably determined that way. |
| 147 | * |
| 148 | * The human readable log message directly follows the message header. The |
| 149 | * length of the message text is stored in the header, the stored message |
| 150 | * is not terminated. |
| 151 | * |
| 152 | */ |
Matt Mackall | d59745c | 2005-05-01 08:59:02 -0700 | [diff] [blame] | 153 | |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame^] | 154 | struct log { |
| 155 | u64 ts_nsec; /* timestamp in nanoseconds */ |
| 156 | u16 len; /* length of entire record */ |
| 157 | u16 text_len; /* length of text buffer */ |
| 158 | u16 dict_len; /* length of dictionary buffer */ |
| 159 | u16 level; /* syslog level + facility */ |
| 160 | }; |
| 161 | |
| 162 | /* |
| 163 | * The logbuf_lock protects kmsg buffer, indices, counters. It is also |
| 164 | * used in interesting ways to provide interlocking in console_unlock(); |
| 165 | */ |
| 166 | static DEFINE_RAW_SPINLOCK(logbuf_lock); |
| 167 | |
| 168 | /* cpu currently holding logbuf_lock */ |
| 169 | static volatile unsigned int logbuf_cpu = UINT_MAX; |
| 170 | |
| 171 | #define LOG_LINE_MAX 1024 |
| 172 | |
| 173 | /* record buffer */ |
| 174 | #define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT) |
Matt Mackall | d59745c | 2005-05-01 08:59:02 -0700 | [diff] [blame] | 175 | static char __log_buf[__LOG_BUF_LEN]; |
| 176 | static char *log_buf = __log_buf; |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame^] | 177 | static u32 log_buf_len = __LOG_BUF_LEN; |
| 178 | |
| 179 | /* index and sequence number of the first record stored in the buffer */ |
| 180 | static u64 log_first_seq; |
| 181 | static u32 log_first_idx; |
| 182 | |
| 183 | /* index and sequence number of the next record to store in the buffer */ |
| 184 | static u64 log_next_seq; |
| 185 | static u32 log_next_idx; |
| 186 | |
| 187 | /* the next printk record to read after the last 'clear' command */ |
| 188 | static u64 clear_seq; |
| 189 | static u32 clear_idx; |
| 190 | |
| 191 | /* the next printk record to read by syslog(READ) or /proc/kmsg */ |
| 192 | static u64 syslog_seq; |
| 193 | static u32 syslog_idx; |
| 194 | |
| 195 | /* human readable text of the record */ |
| 196 | static char *log_text(const struct log *msg) |
| 197 | { |
| 198 | return (char *)msg + sizeof(struct log); |
| 199 | } |
| 200 | |
| 201 | /* optional key/value pair dictionary attached to the record */ |
| 202 | static char *log_dict(const struct log *msg) |
| 203 | { |
| 204 | return (char *)msg + sizeof(struct log) + msg->text_len; |
| 205 | } |
| 206 | |
| 207 | /* get record by index; idx must point to valid msg */ |
| 208 | static struct log *log_from_idx(u32 idx) |
| 209 | { |
| 210 | struct log *msg = (struct log *)(log_buf + idx); |
| 211 | |
| 212 | /* |
| 213 | * A length == 0 record is the end of buffer marker. Wrap around and |
| 214 | * read the message at the start of the buffer. |
| 215 | */ |
| 216 | if (!msg->len) |
| 217 | return (struct log *)log_buf; |
| 218 | return msg; |
| 219 | } |
| 220 | |
| 221 | /* get next record; idx must point to valid msg */ |
| 222 | static u32 log_next(u32 idx) |
| 223 | { |
| 224 | struct log *msg = (struct log *)(log_buf + idx); |
| 225 | |
| 226 | /* length == 0 indicates the end of the buffer; wrap */ |
| 227 | /* |
| 228 | * A length == 0 record is the end of buffer marker. Wrap around and |
| 229 | * read the message at the start of the buffer as *this* one, and |
| 230 | * return the one after that. |
| 231 | */ |
| 232 | if (!msg->len) { |
| 233 | msg = (struct log *)log_buf; |
| 234 | return msg->len; |
| 235 | } |
| 236 | return idx + msg->len; |
| 237 | } |
| 238 | |
| 239 | #if !defined(CONFIG_64BIT) || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) |
| 240 | #define LOG_ALIGN 4 |
| 241 | #else |
| 242 | #define LOG_ALIGN 8 |
| 243 | #endif |
| 244 | |
| 245 | /* insert record into the buffer, discard old ones, update heads */ |
| 246 | static void log_store(int facility, int level, |
| 247 | const char *dict, u16 dict_len, |
| 248 | const char *text, u16 text_len) |
| 249 | { |
| 250 | struct log *msg; |
| 251 | u32 size, pad_len; |
| 252 | |
| 253 | /* number of '\0' padding bytes to next message */ |
| 254 | size = sizeof(struct log) + text_len + dict_len; |
| 255 | pad_len = (-size) & (LOG_ALIGN - 1); |
| 256 | size += pad_len; |
| 257 | |
| 258 | while (log_first_seq < log_next_seq) { |
| 259 | u32 free; |
| 260 | |
| 261 | if (log_next_idx > log_first_idx) |
| 262 | free = max(log_buf_len - log_next_idx, log_first_idx); |
| 263 | else |
| 264 | free = log_first_idx - log_next_idx; |
| 265 | |
| 266 | if (free > size + sizeof(struct log)) |
| 267 | break; |
| 268 | |
| 269 | /* drop old messages until we have enough contiuous space */ |
| 270 | log_first_idx = log_next(log_first_idx); |
| 271 | log_first_seq++; |
| 272 | } |
| 273 | |
| 274 | if (log_next_idx + size + sizeof(struct log) >= log_buf_len) { |
| 275 | /* |
| 276 | * This message + an additional empty header does not fit |
| 277 | * at the end of the buffer. Add an empty header with len == 0 |
| 278 | * to signify a wrap around. |
| 279 | */ |
| 280 | memset(log_buf + log_next_idx, 0, sizeof(struct log)); |
| 281 | log_next_idx = 0; |
| 282 | } |
| 283 | |
| 284 | /* fill message */ |
| 285 | msg = (struct log *)(log_buf + log_next_idx); |
| 286 | memcpy(log_text(msg), text, text_len); |
| 287 | msg->text_len = text_len; |
| 288 | memcpy(log_dict(msg), dict, dict_len); |
| 289 | msg->dict_len = dict_len; |
| 290 | msg->level = (facility << 3) | (level & 7); |
| 291 | msg->ts_nsec = local_clock(); |
| 292 | memset(log_dict(msg) + dict_len, 0, pad_len); |
| 293 | msg->len = sizeof(struct log) + text_len + dict_len + pad_len; |
| 294 | |
| 295 | /* insert message */ |
| 296 | log_next_idx += msg->len; |
| 297 | log_next_seq++; |
| 298 | } |
Matt Mackall | d59745c | 2005-05-01 08:59:02 -0700 | [diff] [blame] | 299 | |
Neil Horman | 04d491a | 2009-04-02 16:58:57 -0700 | [diff] [blame] | 300 | #ifdef CONFIG_KEXEC |
| 301 | /* |
| 302 | * This appends the listed symbols to /proc/vmcoreinfo |
| 303 | * |
| 304 | * /proc/vmcoreinfo is used by various utiilties, like crash and makedumpfile to |
| 305 | * obtain access to symbols that are otherwise very difficult to locate. These |
| 306 | * symbols are specifically used so that utilities can access and extract the |
| 307 | * dmesg log from a vmcore file after a crash. |
| 308 | */ |
| 309 | void log_buf_kexec_setup(void) |
| 310 | { |
| 311 | VMCOREINFO_SYMBOL(log_buf); |
Neil Horman | 04d491a | 2009-04-02 16:58:57 -0700 | [diff] [blame] | 312 | VMCOREINFO_SYMBOL(log_buf_len); |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame^] | 313 | VMCOREINFO_SYMBOL(log_first_idx); |
| 314 | VMCOREINFO_SYMBOL(log_next_idx); |
Neil Horman | 04d491a | 2009-04-02 16:58:57 -0700 | [diff] [blame] | 315 | } |
| 316 | #endif |
| 317 | |
Mike Travis | 162a7e7 | 2011-05-24 17:13:20 -0700 | [diff] [blame] | 318 | /* requested log_buf_len from kernel cmdline */ |
| 319 | static unsigned long __initdata new_log_buf_len; |
| 320 | |
| 321 | /* save requested log_buf_len since it's too early to process it */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | static int __init log_buf_len_setup(char *str) |
| 323 | { |
Denys Vlasenko | eed4a2a | 2008-02-06 01:37:02 -0800 | [diff] [blame] | 324 | unsigned size = memparse(str, &str); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | |
| 326 | if (size) |
| 327 | size = roundup_pow_of_two(size); |
Mike Travis | 162a7e7 | 2011-05-24 17:13:20 -0700 | [diff] [blame] | 328 | if (size > log_buf_len) |
| 329 | new_log_buf_len = size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | |
Mike Travis | 162a7e7 | 2011-05-24 17:13:20 -0700 | [diff] [blame] | 331 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | } |
Mike Travis | 162a7e7 | 2011-05-24 17:13:20 -0700 | [diff] [blame] | 333 | early_param("log_buf_len", log_buf_len_setup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | |
Mike Travis | 162a7e7 | 2011-05-24 17:13:20 -0700 | [diff] [blame] | 335 | void __init setup_log_buf(int early) |
| 336 | { |
| 337 | unsigned long flags; |
Mike Travis | 162a7e7 | 2011-05-24 17:13:20 -0700 | [diff] [blame] | 338 | char *new_log_buf; |
| 339 | int free; |
| 340 | |
| 341 | if (!new_log_buf_len) |
| 342 | return; |
| 343 | |
| 344 | if (early) { |
| 345 | unsigned long mem; |
| 346 | |
| 347 | mem = memblock_alloc(new_log_buf_len, PAGE_SIZE); |
Tejun Heo | 1f5026a | 2011-07-12 09:58:09 +0200 | [diff] [blame] | 348 | if (!mem) |
Mike Travis | 162a7e7 | 2011-05-24 17:13:20 -0700 | [diff] [blame] | 349 | return; |
| 350 | new_log_buf = __va(mem); |
| 351 | } else { |
| 352 | new_log_buf = alloc_bootmem_nopanic(new_log_buf_len); |
| 353 | } |
| 354 | |
| 355 | if (unlikely(!new_log_buf)) { |
| 356 | pr_err("log_buf_len: %ld bytes not available\n", |
| 357 | new_log_buf_len); |
| 358 | return; |
| 359 | } |
| 360 | |
Thomas Gleixner | 07354eb | 2009-07-25 17:50:36 +0200 | [diff] [blame] | 361 | raw_spin_lock_irqsave(&logbuf_lock, flags); |
Mike Travis | 162a7e7 | 2011-05-24 17:13:20 -0700 | [diff] [blame] | 362 | log_buf_len = new_log_buf_len; |
| 363 | log_buf = new_log_buf; |
| 364 | new_log_buf_len = 0; |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame^] | 365 | free = __LOG_BUF_LEN - log_next_idx; |
| 366 | memcpy(log_buf, __log_buf, __LOG_BUF_LEN); |
Thomas Gleixner | 07354eb | 2009-07-25 17:50:36 +0200 | [diff] [blame] | 367 | raw_spin_unlock_irqrestore(&logbuf_lock, flags); |
Mike Travis | 162a7e7 | 2011-05-24 17:13:20 -0700 | [diff] [blame] | 368 | |
| 369 | pr_info("log_buf_len: %d\n", log_buf_len); |
| 370 | pr_info("early log buf free: %d(%d%%)\n", |
| 371 | free, (free * 100) / __LOG_BUF_LEN); |
| 372 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | |
Randy Dunlap | bfe8df3 | 2007-10-16 01:23:46 -0700 | [diff] [blame] | 374 | #ifdef CONFIG_BOOT_PRINTK_DELAY |
| 375 | |
Namhyung Kim | 674dff6 | 2010-10-26 14:22:48 -0700 | [diff] [blame] | 376 | static int boot_delay; /* msecs delay after each printk during bootup */ |
Dave Young | 3a3b6ed | 2009-09-22 16:43:31 -0700 | [diff] [blame] | 377 | static unsigned long long loops_per_msec; /* based on boot_delay */ |
Randy Dunlap | bfe8df3 | 2007-10-16 01:23:46 -0700 | [diff] [blame] | 378 | |
| 379 | static int __init boot_delay_setup(char *str) |
| 380 | { |
| 381 | unsigned long lpj; |
Randy Dunlap | bfe8df3 | 2007-10-16 01:23:46 -0700 | [diff] [blame] | 382 | |
| 383 | lpj = preset_lpj ? preset_lpj : 1000000; /* some guess */ |
| 384 | loops_per_msec = (unsigned long long)lpj / 1000 * HZ; |
| 385 | |
| 386 | get_option(&str, &boot_delay); |
| 387 | if (boot_delay > 10 * 1000) |
| 388 | boot_delay = 0; |
| 389 | |
Dave Young | 3a3b6ed | 2009-09-22 16:43:31 -0700 | [diff] [blame] | 390 | pr_debug("boot_delay: %u, preset_lpj: %ld, lpj: %lu, " |
| 391 | "HZ: %d, loops_per_msec: %llu\n", |
| 392 | boot_delay, preset_lpj, lpj, HZ, loops_per_msec); |
Randy Dunlap | bfe8df3 | 2007-10-16 01:23:46 -0700 | [diff] [blame] | 393 | return 1; |
| 394 | } |
| 395 | __setup("boot_delay=", boot_delay_setup); |
| 396 | |
| 397 | static void boot_delay_msec(void) |
| 398 | { |
| 399 | unsigned long long k; |
| 400 | unsigned long timeout; |
| 401 | |
| 402 | if (boot_delay == 0 || system_state != SYSTEM_BOOTING) |
| 403 | return; |
| 404 | |
Dave Young | 3a3b6ed | 2009-09-22 16:43:31 -0700 | [diff] [blame] | 405 | k = (unsigned long long)loops_per_msec * boot_delay; |
Randy Dunlap | bfe8df3 | 2007-10-16 01:23:46 -0700 | [diff] [blame] | 406 | |
| 407 | timeout = jiffies + msecs_to_jiffies(boot_delay); |
| 408 | while (k) { |
| 409 | k--; |
| 410 | cpu_relax(); |
| 411 | /* |
| 412 | * use (volatile) jiffies to prevent |
| 413 | * compiler reduction; loop termination via jiffies |
| 414 | * is secondary and may or may not happen. |
| 415 | */ |
| 416 | if (time_after(jiffies, timeout)) |
| 417 | break; |
| 418 | touch_nmi_watchdog(); |
| 419 | } |
| 420 | } |
| 421 | #else |
| 422 | static inline void boot_delay_msec(void) |
| 423 | { |
| 424 | } |
| 425 | #endif |
| 426 | |
Dan Rosenberg | eaf06b2 | 2010-11-11 14:05:18 -0800 | [diff] [blame] | 427 | #ifdef CONFIG_SECURITY_DMESG_RESTRICT |
| 428 | int dmesg_restrict = 1; |
| 429 | #else |
| 430 | int dmesg_restrict; |
| 431 | #endif |
| 432 | |
Linus Torvalds | ee24aeb | 2011-02-10 17:53:55 -0800 | [diff] [blame] | 433 | static int syslog_action_restricted(int type) |
| 434 | { |
| 435 | if (dmesg_restrict) |
| 436 | return 1; |
| 437 | /* Unless restricted, we allow "read all" and "get buffer size" for everybody */ |
| 438 | return type != SYSLOG_ACTION_READ_ALL && type != SYSLOG_ACTION_SIZE_BUFFER; |
| 439 | } |
| 440 | |
| 441 | static int check_syslog_permissions(int type, bool from_file) |
| 442 | { |
| 443 | /* |
| 444 | * If this is from /proc/kmsg and we've already opened it, then we've |
| 445 | * already done the capabilities checks at open time. |
| 446 | */ |
| 447 | if (from_file && type != SYSLOG_ACTION_OPEN) |
| 448 | return 0; |
| 449 | |
| 450 | if (syslog_action_restricted(type)) { |
| 451 | if (capable(CAP_SYSLOG)) |
| 452 | return 0; |
| 453 | /* For historical reasons, accept CAP_SYS_ADMIN too, with a warning */ |
| 454 | if (capable(CAP_SYS_ADMIN)) { |
Jonathan Nieder | f2c0d02 | 2011-08-08 06:22:43 +0200 | [diff] [blame] | 455 | printk_once(KERN_WARNING "%s (%d): " |
| 456 | "Attempt to access syslog with CAP_SYS_ADMIN " |
| 457 | "but no CAP_SYSLOG (deprecated).\n", |
| 458 | current->comm, task_pid_nr(current)); |
Linus Torvalds | ee24aeb | 2011-02-10 17:53:55 -0800 | [diff] [blame] | 459 | return 0; |
| 460 | } |
| 461 | return -EPERM; |
| 462 | } |
| 463 | return 0; |
| 464 | } |
| 465 | |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame^] | 466 | #if defined(CONFIG_PRINTK_TIME) |
| 467 | static bool printk_time = 1; |
| 468 | #else |
| 469 | static bool printk_time; |
| 470 | #endif |
| 471 | module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR); |
| 472 | |
| 473 | static int syslog_print_line(u32 idx, char *text, size_t size) |
| 474 | { |
| 475 | struct log *msg; |
| 476 | size_t len; |
| 477 | |
| 478 | msg = log_from_idx(idx); |
| 479 | if (!text) { |
| 480 | /* calculate length only */ |
| 481 | len = 3; |
| 482 | |
| 483 | if (msg->level > 9) |
| 484 | len++; |
| 485 | if (msg->level > 99) |
| 486 | len++; |
| 487 | |
| 488 | if (printk_time) |
| 489 | len += 15; |
| 490 | |
| 491 | len += msg->text_len; |
| 492 | len++; |
| 493 | return len; |
| 494 | } |
| 495 | |
| 496 | len = sprintf(text, "<%u>", msg->level); |
| 497 | |
| 498 | if (printk_time) { |
| 499 | unsigned long long t = msg->ts_nsec; |
| 500 | unsigned long rem_ns = do_div(t, 1000000000); |
| 501 | |
| 502 | len += sprintf(text + len, "[%5lu.%06lu] ", |
| 503 | (unsigned long) t, rem_ns / 1000); |
| 504 | } |
| 505 | |
| 506 | if (len + msg->text_len > size) |
| 507 | return -EINVAL; |
| 508 | memcpy(text + len, log_text(msg), msg->text_len); |
| 509 | len += msg->text_len; |
| 510 | text[len++] = '\n'; |
| 511 | return len; |
| 512 | } |
| 513 | |
| 514 | static int syslog_print(char __user *buf, int size) |
| 515 | { |
| 516 | char *text; |
| 517 | int len; |
| 518 | |
| 519 | text = kmalloc(LOG_LINE_MAX, GFP_KERNEL); |
| 520 | if (!text) |
| 521 | return -ENOMEM; |
| 522 | |
| 523 | raw_spin_lock_irq(&logbuf_lock); |
| 524 | if (syslog_seq < log_first_seq) { |
| 525 | /* messages are gone, move to first one */ |
| 526 | syslog_seq = log_first_seq; |
| 527 | syslog_idx = log_first_idx; |
| 528 | } |
| 529 | len = syslog_print_line(syslog_idx, text, LOG_LINE_MAX); |
| 530 | syslog_idx = log_next(syslog_idx); |
| 531 | syslog_seq++; |
| 532 | raw_spin_unlock_irq(&logbuf_lock); |
| 533 | |
| 534 | if (len > 0 && copy_to_user(buf, text, len)) |
| 535 | len = -EFAULT; |
| 536 | |
| 537 | kfree(text); |
| 538 | return len; |
| 539 | } |
| 540 | |
| 541 | static int syslog_print_all(char __user *buf, int size, bool clear) |
| 542 | { |
| 543 | char *text; |
| 544 | int len = 0; |
| 545 | |
| 546 | text = kmalloc(LOG_LINE_MAX, GFP_KERNEL); |
| 547 | if (!text) |
| 548 | return -ENOMEM; |
| 549 | |
| 550 | raw_spin_lock_irq(&logbuf_lock); |
| 551 | if (buf) { |
| 552 | u64 next_seq; |
| 553 | u64 seq; |
| 554 | u32 idx; |
| 555 | |
| 556 | if (clear_seq < log_first_seq) { |
| 557 | /* messages are gone, move to first available one */ |
| 558 | clear_seq = log_first_seq; |
| 559 | clear_idx = log_first_idx; |
| 560 | } |
| 561 | |
| 562 | /* |
| 563 | * Find first record that fits, including all following records, |
| 564 | * into the user-provided buffer for this dump. |
| 565 | */ |
| 566 | seq = clear_seq; |
| 567 | idx = clear_idx; |
| 568 | while (seq < log_next_seq) { |
| 569 | len += syslog_print_line(idx, NULL, 0); |
| 570 | idx = log_next(idx); |
| 571 | seq++; |
| 572 | } |
| 573 | seq = clear_seq; |
| 574 | idx = clear_idx; |
| 575 | while (len > size && seq < log_next_seq) { |
| 576 | len -= syslog_print_line(idx, NULL, 0); |
| 577 | idx = log_next(idx); |
| 578 | seq++; |
| 579 | } |
| 580 | |
| 581 | /* last message in this dump */ |
| 582 | next_seq = log_next_seq; |
| 583 | |
| 584 | len = 0; |
| 585 | while (len >= 0 && seq < next_seq) { |
| 586 | int textlen; |
| 587 | |
| 588 | textlen = syslog_print_line(idx, text, LOG_LINE_MAX); |
| 589 | if (textlen < 0) { |
| 590 | len = textlen; |
| 591 | break; |
| 592 | } |
| 593 | idx = log_next(idx); |
| 594 | seq++; |
| 595 | |
| 596 | raw_spin_unlock_irq(&logbuf_lock); |
| 597 | if (copy_to_user(buf + len, text, textlen)) |
| 598 | len = -EFAULT; |
| 599 | else |
| 600 | len += textlen; |
| 601 | raw_spin_lock_irq(&logbuf_lock); |
| 602 | |
| 603 | if (seq < log_first_seq) { |
| 604 | /* messages are gone, move to next one */ |
| 605 | seq = log_first_seq; |
| 606 | idx = log_first_idx; |
| 607 | } |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | if (clear) { |
| 612 | clear_seq = log_next_seq; |
| 613 | clear_idx = log_next_idx; |
| 614 | } |
| 615 | raw_spin_unlock_irq(&logbuf_lock); |
| 616 | |
| 617 | kfree(text); |
| 618 | return len; |
| 619 | } |
| 620 | |
Kees Cook | 0023459 | 2010-02-03 15:36:43 -0800 | [diff] [blame] | 621 | int do_syslog(int type, char __user *buf, int len, bool from_file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | { |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame^] | 623 | bool clear = false; |
| 624 | static int saved_console_loglevel = -1; |
Linus Torvalds | ee24aeb | 2011-02-10 17:53:55 -0800 | [diff] [blame] | 625 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | |
Linus Torvalds | ee24aeb | 2011-02-10 17:53:55 -0800 | [diff] [blame] | 627 | error = check_syslog_permissions(type, from_file); |
| 628 | if (error) |
| 629 | goto out; |
Eric Paris | 12b3052 | 2010-11-15 18:36:29 -0500 | [diff] [blame] | 630 | |
| 631 | error = security_syslog(type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | if (error) |
| 633 | return error; |
| 634 | |
| 635 | switch (type) { |
Kees Cook | d78ca3c | 2010-02-03 15:37:13 -0800 | [diff] [blame] | 636 | case SYSLOG_ACTION_CLOSE: /* Close log */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | break; |
Kees Cook | d78ca3c | 2010-02-03 15:37:13 -0800 | [diff] [blame] | 638 | case SYSLOG_ACTION_OPEN: /* Open log */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | break; |
Kees Cook | d78ca3c | 2010-02-03 15:37:13 -0800 | [diff] [blame] | 640 | case SYSLOG_ACTION_READ: /* Read from log */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | error = -EINVAL; |
| 642 | if (!buf || len < 0) |
| 643 | goto out; |
| 644 | error = 0; |
| 645 | if (!len) |
| 646 | goto out; |
| 647 | if (!access_ok(VERIFY_WRITE, buf, len)) { |
| 648 | error = -EFAULT; |
| 649 | goto out; |
| 650 | } |
Jesper Juhl | 40dc565 | 2005-10-30 15:02:46 -0800 | [diff] [blame] | 651 | error = wait_event_interruptible(log_wait, |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame^] | 652 | syslog_seq != log_next_seq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | if (error) |
| 654 | goto out; |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame^] | 655 | error = syslog_print(buf, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | break; |
Kees Cook | d78ca3c | 2010-02-03 15:37:13 -0800 | [diff] [blame] | 657 | /* Read/clear last kernel messages */ |
| 658 | case SYSLOG_ACTION_READ_CLEAR: |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame^] | 659 | clear = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | /* FALL THRU */ |
Kees Cook | d78ca3c | 2010-02-03 15:37:13 -0800 | [diff] [blame] | 661 | /* Read last kernel messages */ |
| 662 | case SYSLOG_ACTION_READ_ALL: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | error = -EINVAL; |
| 664 | if (!buf || len < 0) |
| 665 | goto out; |
| 666 | error = 0; |
| 667 | if (!len) |
| 668 | goto out; |
| 669 | if (!access_ok(VERIFY_WRITE, buf, len)) { |
| 670 | error = -EFAULT; |
| 671 | goto out; |
| 672 | } |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame^] | 673 | error = syslog_print_all(buf, len, clear); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | break; |
Kees Cook | d78ca3c | 2010-02-03 15:37:13 -0800 | [diff] [blame] | 675 | /* Clear ring buffer */ |
| 676 | case SYSLOG_ACTION_CLEAR: |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame^] | 677 | syslog_print_all(NULL, 0, true); |
Kees Cook | d78ca3c | 2010-02-03 15:37:13 -0800 | [diff] [blame] | 678 | /* Disable logging to console */ |
| 679 | case SYSLOG_ACTION_CONSOLE_OFF: |
Frans Pop | 1aaad49 | 2009-07-06 13:31:48 +0200 | [diff] [blame] | 680 | if (saved_console_loglevel == -1) |
| 681 | saved_console_loglevel = console_loglevel; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | console_loglevel = minimum_console_loglevel; |
| 683 | break; |
Kees Cook | d78ca3c | 2010-02-03 15:37:13 -0800 | [diff] [blame] | 684 | /* Enable logging to console */ |
| 685 | case SYSLOG_ACTION_CONSOLE_ON: |
Frans Pop | 1aaad49 | 2009-07-06 13:31:48 +0200 | [diff] [blame] | 686 | if (saved_console_loglevel != -1) { |
| 687 | console_loglevel = saved_console_loglevel; |
| 688 | saved_console_loglevel = -1; |
| 689 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | break; |
Kees Cook | d78ca3c | 2010-02-03 15:37:13 -0800 | [diff] [blame] | 691 | /* Set level of messages printed to console */ |
| 692 | case SYSLOG_ACTION_CONSOLE_LEVEL: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | error = -EINVAL; |
| 694 | if (len < 1 || len > 8) |
| 695 | goto out; |
| 696 | if (len < minimum_console_loglevel) |
| 697 | len = minimum_console_loglevel; |
| 698 | console_loglevel = len; |
Frans Pop | 1aaad49 | 2009-07-06 13:31:48 +0200 | [diff] [blame] | 699 | /* Implicitly re-enable logging to console */ |
| 700 | saved_console_loglevel = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | error = 0; |
| 702 | break; |
Kees Cook | d78ca3c | 2010-02-03 15:37:13 -0800 | [diff] [blame] | 703 | /* Number of chars in the log buffer */ |
| 704 | case SYSLOG_ACTION_SIZE_UNREAD: |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame^] | 705 | raw_spin_lock_irq(&logbuf_lock); |
| 706 | if (syslog_seq < log_first_seq) { |
| 707 | /* messages are gone, move to first one */ |
| 708 | syslog_seq = log_first_seq; |
| 709 | syslog_idx = log_first_idx; |
| 710 | } |
| 711 | if (from_file) { |
| 712 | /* |
| 713 | * Short-cut for poll(/"proc/kmsg") which simply checks |
| 714 | * for pending data, not the size; return the count of |
| 715 | * records, not the length. |
| 716 | */ |
| 717 | error = log_next_idx - syslog_idx; |
| 718 | } else { |
| 719 | u64 seq; |
| 720 | u32 idx; |
| 721 | |
| 722 | error = 0; |
| 723 | seq = syslog_seq; |
| 724 | idx = syslog_idx; |
| 725 | while (seq < log_next_seq) { |
| 726 | error += syslog_print_line(idx, NULL, 0); |
| 727 | idx = log_next(idx); |
| 728 | seq++; |
| 729 | } |
| 730 | } |
| 731 | raw_spin_unlock_irq(&logbuf_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | break; |
Kees Cook | d78ca3c | 2010-02-03 15:37:13 -0800 | [diff] [blame] | 733 | /* Size of the log buffer */ |
| 734 | case SYSLOG_ACTION_SIZE_BUFFER: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | error = log_buf_len; |
| 736 | break; |
| 737 | default: |
| 738 | error = -EINVAL; |
| 739 | break; |
| 740 | } |
| 741 | out: |
| 742 | return error; |
| 743 | } |
| 744 | |
Heiko Carstens | 1e7bfb2 | 2009-01-14 14:14:29 +0100 | [diff] [blame] | 745 | SYSCALL_DEFINE3(syslog, int, type, char __user *, buf, int, len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | { |
Kees Cook | 0023459 | 2010-02-03 15:36:43 -0800 | [diff] [blame] | 747 | return do_syslog(type, buf, len, SYSLOG_FROM_CALL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | } |
| 749 | |
Jason Wessel | 67fc4e0 | 2010-05-20 21:04:21 -0500 | [diff] [blame] | 750 | #ifdef CONFIG_KGDB_KDB |
| 751 | /* kdb dmesg command needs access to the syslog buffer. do_syslog() |
| 752 | * uses locks so it cannot be used during debugging. Just tell kdb |
| 753 | * where the start and end of the physical and logical logs are. This |
| 754 | * is equivalent to do_syslog(3). |
| 755 | */ |
| 756 | void kdb_syslog_data(char *syslog_data[4]) |
| 757 | { |
| 758 | syslog_data[0] = log_buf; |
| 759 | syslog_data[1] = log_buf + log_buf_len; |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame^] | 760 | syslog_data[2] = log_buf + log_first_idx; |
| 761 | syslog_data[3] = log_buf + log_next_idx; |
Jason Wessel | 67fc4e0 | 2010-05-20 21:04:21 -0500 | [diff] [blame] | 762 | } |
| 763 | #endif /* CONFIG_KGDB_KDB */ |
| 764 | |
Rusty Russell | 2329abf | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 765 | static bool __read_mostly ignore_loglevel; |
Ingo Molnar | 79290822 | 2006-12-06 20:40:51 -0800 | [diff] [blame] | 766 | |
Adrian Bunk | 99eea6a | 2006-12-22 01:07:00 -0800 | [diff] [blame] | 767 | static int __init ignore_loglevel_setup(char *str) |
Ingo Molnar | 79290822 | 2006-12-06 20:40:51 -0800 | [diff] [blame] | 768 | { |
| 769 | ignore_loglevel = 1; |
| 770 | printk(KERN_INFO "debug: ignoring loglevel setting.\n"); |
| 771 | |
Ingo Molnar | c4772d9 | 2008-01-31 22:45:23 +0100 | [diff] [blame] | 772 | return 0; |
Ingo Molnar | 79290822 | 2006-12-06 20:40:51 -0800 | [diff] [blame] | 773 | } |
| 774 | |
Ingo Molnar | c4772d9 | 2008-01-31 22:45:23 +0100 | [diff] [blame] | 775 | early_param("ignore_loglevel", ignore_loglevel_setup); |
Rusty Russell | 29d4d6d | 2012-01-13 09:32:17 +1030 | [diff] [blame] | 776 | module_param(ignore_loglevel, bool, S_IRUGO | S_IWUSR); |
Yanmin Zhang | 0eca6b7 | 2011-10-31 17:11:25 -0700 | [diff] [blame] | 777 | MODULE_PARM_DESC(ignore_loglevel, "ignore loglevel setting, to" |
| 778 | "print all kernel messages to the console."); |
Ingo Molnar | 79290822 | 2006-12-06 20:40:51 -0800 | [diff] [blame] | 779 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | * Call the console drivers, asking them to write out |
| 782 | * log_buf[start] to log_buf[end - 1]. |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 783 | * The console_lock must be held. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | */ |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame^] | 785 | static void call_console_drivers(int level, const char *text, size_t len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | { |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame^] | 787 | struct console *con; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame^] | 789 | trace_console(text, 0, len, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame^] | 791 | if (level >= console_loglevel && !ignore_loglevel) |
| 792 | return; |
| 793 | if (!console_drivers) |
| 794 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame^] | 796 | for_each_console(con) { |
| 797 | if (exclusive_console && con != exclusive_console) |
| 798 | continue; |
| 799 | if (!(con->flags & CON_ENABLED)) |
| 800 | continue; |
| 801 | if (!con->write) |
| 802 | continue; |
| 803 | if (!cpu_online(smp_processor_id()) && |
| 804 | !(con->flags & CON_ANYTIME)) |
| 805 | continue; |
| 806 | con->write(con, text, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | } |
| 809 | |
| 810 | /* |
| 811 | * Zap console related locks when oopsing. Only zap at most once |
| 812 | * every 10 seconds, to leave time for slow consoles to print a |
| 813 | * full oops. |
| 814 | */ |
| 815 | static void zap_locks(void) |
| 816 | { |
| 817 | static unsigned long oops_timestamp; |
| 818 | |
| 819 | if (time_after_eq(jiffies, oops_timestamp) && |
Jesper Juhl | 40dc565 | 2005-10-30 15:02:46 -0800 | [diff] [blame] | 820 | !time_after(jiffies, oops_timestamp + 30 * HZ)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | return; |
| 822 | |
| 823 | oops_timestamp = jiffies; |
| 824 | |
Peter Zijlstra | 94d24fc | 2011-06-07 11:17:30 +0200 | [diff] [blame] | 825 | debug_locks_off(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | /* If a crash is occurring, make sure we can't deadlock */ |
Thomas Gleixner | 07354eb | 2009-07-25 17:50:36 +0200 | [diff] [blame] | 827 | raw_spin_lock_init(&logbuf_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | /* And make sure that we print immediately */ |
Thomas Gleixner | 5b8c4f2 | 2010-09-07 14:33:43 +0000 | [diff] [blame] | 829 | sema_init(&console_sem, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | } |
| 831 | |
Michael Ellerman | 76a8ad2 | 2006-06-25 05:47:40 -0700 | [diff] [blame] | 832 | /* Check if we have any console registered that can be called early in boot. */ |
| 833 | static int have_callable_console(void) |
| 834 | { |
| 835 | struct console *con; |
| 836 | |
Robin Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 837 | for_each_console(con) |
Michael Ellerman | 76a8ad2 | 2006-06-25 05:47:40 -0700 | [diff] [blame] | 838 | if (con->flags & CON_ANYTIME) |
| 839 | return 1; |
| 840 | |
| 841 | return 0; |
| 842 | } |
| 843 | |
Linus Torvalds | 266c2e0 | 2008-03-24 19:25:08 -0700 | [diff] [blame] | 844 | /* |
| 845 | * Can we actually use the console at this time on this cpu? |
| 846 | * |
| 847 | * Console drivers may assume that per-cpu resources have |
| 848 | * been allocated. So unless they're explicitly marked as |
| 849 | * being able to cope (CON_ANYTIME) don't call them until |
| 850 | * this CPU is officially up. |
| 851 | */ |
| 852 | static inline int can_use_console(unsigned int cpu) |
| 853 | { |
| 854 | return cpu_online(cpu) || have_callable_console(); |
| 855 | } |
| 856 | |
| 857 | /* |
| 858 | * Try to get console ownership to actually show the kernel |
| 859 | * messages from a 'printk'. Return true (and with the |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 860 | * console_lock held, and 'console_locked' set) if it |
Linus Torvalds | 266c2e0 | 2008-03-24 19:25:08 -0700 | [diff] [blame] | 861 | * is successful, false otherwise. |
| 862 | * |
| 863 | * This gets called with the 'logbuf_lock' spinlock held and |
| 864 | * interrupts disabled. It should return with 'lockbuf_lock' |
| 865 | * released but interrupts still disabled. |
| 866 | */ |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 867 | static int console_trylock_for_printk(unsigned int cpu) |
Namhyung Kim | 8155c02 | 2010-10-26 14:22:47 -0700 | [diff] [blame] | 868 | __releases(&logbuf_lock) |
Linus Torvalds | 266c2e0 | 2008-03-24 19:25:08 -0700 | [diff] [blame] | 869 | { |
Peter Zijlstra | 0b5e1c5 | 2011-06-07 11:15:33 +0200 | [diff] [blame] | 870 | int retval = 0, wake = 0; |
Linus Torvalds | 266c2e0 | 2008-03-24 19:25:08 -0700 | [diff] [blame] | 871 | |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 872 | if (console_trylock()) { |
Linus Torvalds | 093a07e | 2008-04-15 13:09:54 -0700 | [diff] [blame] | 873 | retval = 1; |
| 874 | |
| 875 | /* |
| 876 | * If we can't use the console, we need to release |
| 877 | * the console semaphore by hand to avoid flushing |
| 878 | * the buffer. We need to hold the console semaphore |
| 879 | * in order to do this test safely. |
| 880 | */ |
| 881 | if (!can_use_console(cpu)) { |
| 882 | console_locked = 0; |
Peter Zijlstra | 0b5e1c5 | 2011-06-07 11:15:33 +0200 | [diff] [blame] | 883 | wake = 1; |
Linus Torvalds | 093a07e | 2008-04-15 13:09:54 -0700 | [diff] [blame] | 884 | retval = 0; |
| 885 | } |
| 886 | } |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame^] | 887 | logbuf_cpu = UINT_MAX; |
Peter Zijlstra | 0b5e1c5 | 2011-06-07 11:15:33 +0200 | [diff] [blame] | 888 | if (wake) |
| 889 | up(&console_sem); |
Thomas Gleixner | 07354eb | 2009-07-25 17:50:36 +0200 | [diff] [blame] | 890 | raw_spin_unlock(&logbuf_lock); |
Linus Torvalds | 266c2e0 | 2008-03-24 19:25:08 -0700 | [diff] [blame] | 891 | return retval; |
| 892 | } |
Ingo Molnar | 32a7600 | 2008-01-25 21:07:58 +0100 | [diff] [blame] | 893 | |
Dave Young | af91322 | 2009-09-22 16:43:33 -0700 | [diff] [blame] | 894 | int printk_delay_msec __read_mostly; |
| 895 | |
| 896 | static inline void printk_delay(void) |
| 897 | { |
| 898 | if (unlikely(printk_delay_msec)) { |
| 899 | int m = printk_delay_msec; |
| 900 | |
| 901 | while (m--) { |
| 902 | mdelay(1); |
| 903 | touch_nmi_watchdog(); |
| 904 | } |
| 905 | } |
| 906 | } |
| 907 | |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame^] | 908 | asmlinkage int vprintk_emit(int facility, int level, |
| 909 | const char *dict, size_t dictlen, |
| 910 | const char *fmt, va_list args) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | { |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame^] | 912 | static int recursion_bug; |
| 913 | static char buf[LOG_LINE_MAX]; |
| 914 | static size_t buflen; |
| 915 | static int buflevel; |
| 916 | static char textbuf[LOG_LINE_MAX]; |
| 917 | char *text = textbuf; |
| 918 | size_t textlen; |
Nick Andrew | ac60ad7 | 2008-05-12 21:21:04 +0200 | [diff] [blame] | 919 | unsigned long flags; |
Ingo Molnar | 32a7600 | 2008-01-25 21:07:58 +0100 | [diff] [blame] | 920 | int this_cpu; |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame^] | 921 | bool newline = false; |
| 922 | bool cont = false; |
| 923 | int printed_len = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | |
Randy Dunlap | bfe8df3 | 2007-10-16 01:23:46 -0700 | [diff] [blame] | 925 | boot_delay_msec(); |
Dave Young | af91322 | 2009-09-22 16:43:33 -0700 | [diff] [blame] | 926 | printk_delay(); |
Randy Dunlap | bfe8df3 | 2007-10-16 01:23:46 -0700 | [diff] [blame] | 927 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | /* This stops the holder of console_sem just where we want him */ |
Peter Zijlstra | 1a9a8ae | 2011-06-07 11:17:30 +0200 | [diff] [blame] | 929 | local_irq_save(flags); |
Ingo Molnar | 32a7600 | 2008-01-25 21:07:58 +0100 | [diff] [blame] | 930 | this_cpu = smp_processor_id(); |
| 931 | |
| 932 | /* |
| 933 | * Ouch, printk recursed into itself! |
| 934 | */ |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame^] | 935 | if (unlikely(logbuf_cpu == this_cpu)) { |
Ingo Molnar | 32a7600 | 2008-01-25 21:07:58 +0100 | [diff] [blame] | 936 | /* |
| 937 | * If a crash is occurring during printk() on this CPU, |
| 938 | * then try to get the crash message out but make sure |
| 939 | * we can't deadlock. Otherwise just return to avoid the |
| 940 | * recursion and return - but flag the recursion so that |
| 941 | * it can be printed at the next appropriate moment: |
| 942 | */ |
Peter Zijlstra | 94d24fc | 2011-06-07 11:17:30 +0200 | [diff] [blame] | 943 | if (!oops_in_progress && !lockdep_recursing(current)) { |
Tejun Heo | 3b8945e | 2008-05-12 21:21:04 +0200 | [diff] [blame] | 944 | recursion_bug = 1; |
Ingo Molnar | 32a7600 | 2008-01-25 21:07:58 +0100 | [diff] [blame] | 945 | goto out_restore_irqs; |
| 946 | } |
| 947 | zap_locks(); |
| 948 | } |
| 949 | |
Ingo Molnar | a0f1ccf | 2006-07-03 00:24:58 -0700 | [diff] [blame] | 950 | lockdep_off(); |
Thomas Gleixner | 07354eb | 2009-07-25 17:50:36 +0200 | [diff] [blame] | 951 | raw_spin_lock(&logbuf_lock); |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame^] | 952 | logbuf_cpu = this_cpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | |
Tejun Heo | 3b8945e | 2008-05-12 21:21:04 +0200 | [diff] [blame] | 954 | if (recursion_bug) { |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame^] | 955 | static const char recursion_msg[] = |
| 956 | "BUG: recent printk recursion!"; |
| 957 | |
Tejun Heo | 3b8945e | 2008-05-12 21:21:04 +0200 | [diff] [blame] | 958 | recursion_bug = 0; |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame^] | 959 | printed_len += strlen(recursion_msg); |
| 960 | /* emit KERN_CRIT message */ |
| 961 | log_store(0, 2, NULL, 0, recursion_msg, printed_len); |
Linus Torvalds | 5fd29d6 | 2009-06-16 10:57:02 -0700 | [diff] [blame] | 962 | } |
| 963 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | /* |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame^] | 965 | * The printf needs to come first; we need the syslog |
| 966 | * prefix which might be passed-in as a parameter. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | */ |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame^] | 968 | textlen = vscnprintf(text, sizeof(textbuf), fmt, args); |
Nick Andrew | ac60ad7 | 2008-05-12 21:21:04 +0200 | [diff] [blame] | 969 | |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame^] | 970 | /* mark and strip a trailing newline */ |
| 971 | if (textlen && text[textlen-1] == '\n') { |
| 972 | textlen--; |
| 973 | newline = true; |
| 974 | } |
Kay Sievers | 9d90c8d | 2011-03-13 03:19:51 +0100 | [diff] [blame] | 975 | |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame^] | 976 | /* strip syslog prefix and extract log level or flags */ |
| 977 | if (text[0] == '<' && text[1] && text[2] == '>') { |
| 978 | switch (text[1]) { |
| 979 | case '0' ... '7': |
| 980 | if (level == -1) |
| 981 | level = text[1] - '0'; |
| 982 | text += 3; |
| 983 | textlen -= 3; |
| 984 | break; |
| 985 | case 'c': /* KERN_CONT */ |
| 986 | cont = true; |
| 987 | case 'd': /* KERN_DEFAULT */ |
| 988 | text += 3; |
| 989 | textlen -= 3; |
| 990 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | } |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame^] | 992 | } |
Nick Andrew | ac60ad7 | 2008-05-12 21:21:04 +0200 | [diff] [blame] | 993 | |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame^] | 994 | if (buflen && (!cont || dict)) { |
| 995 | /* no continuation; flush existing buffer */ |
| 996 | log_store(facility, buflevel, NULL, 0, buf, buflen); |
| 997 | printed_len += buflen; |
| 998 | buflen = 0; |
| 999 | } |
| 1000 | |
| 1001 | if (buflen == 0) { |
| 1002 | /* remember level for first message in the buffer */ |
| 1003 | if (level == -1) |
| 1004 | buflevel = default_message_loglevel; |
| 1005 | else |
| 1006 | buflevel = level; |
| 1007 | } |
| 1008 | |
| 1009 | if (buflen || !newline) { |
| 1010 | /* append to existing buffer, or buffer until next message */ |
| 1011 | if (buflen + textlen > sizeof(buf)) |
| 1012 | textlen = sizeof(buf) - buflen; |
| 1013 | memcpy(buf + buflen, text, textlen); |
| 1014 | buflen += textlen; |
| 1015 | } |
| 1016 | |
| 1017 | if (newline) { |
| 1018 | /* end of line; flush buffer */ |
| 1019 | if (buflen) { |
| 1020 | log_store(facility, buflevel, |
| 1021 | dict, dictlen, buf, buflen); |
| 1022 | printed_len += buflen; |
| 1023 | buflen = 0; |
| 1024 | } else { |
| 1025 | log_store(facility, buflevel, |
| 1026 | dict, dictlen, text, textlen); |
| 1027 | printed_len += textlen; |
| 1028 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | } |
| 1030 | |
Linus Torvalds | 266c2e0 | 2008-03-24 19:25:08 -0700 | [diff] [blame] | 1031 | /* |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame^] | 1032 | * Try to acquire and then immediately release the console semaphore. |
| 1033 | * The release will print out buffers and wake up /dev/kmsg and syslog() |
| 1034 | * users. |
Linus Torvalds | 266c2e0 | 2008-03-24 19:25:08 -0700 | [diff] [blame] | 1035 | * |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame^] | 1036 | * The console_trylock_for_printk() function will release 'logbuf_lock' |
| 1037 | * regardless of whether it actually gets the console semaphore or not. |
Linus Torvalds | 266c2e0 | 2008-03-24 19:25:08 -0700 | [diff] [blame] | 1038 | */ |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1039 | if (console_trylock_for_printk(this_cpu)) |
| 1040 | console_unlock(); |
Michael Ellerman | 76a8ad2 | 2006-06-25 05:47:40 -0700 | [diff] [blame] | 1041 | |
Linus Torvalds | 266c2e0 | 2008-03-24 19:25:08 -0700 | [diff] [blame] | 1042 | lockdep_on(); |
Ingo Molnar | 32a7600 | 2008-01-25 21:07:58 +0100 | [diff] [blame] | 1043 | out_restore_irqs: |
Peter Zijlstra | 1a9a8ae | 2011-06-07 11:17:30 +0200 | [diff] [blame] | 1044 | local_irq_restore(flags); |
Michael Ellerman | 76a8ad2 | 2006-06-25 05:47:40 -0700 | [diff] [blame] | 1045 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | return printed_len; |
| 1047 | } |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame^] | 1048 | EXPORT_SYMBOL(vprintk_emit); |
| 1049 | |
| 1050 | asmlinkage int vprintk(const char *fmt, va_list args) |
| 1051 | { |
| 1052 | return vprintk_emit(0, -1, NULL, 0, fmt, args); |
| 1053 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | EXPORT_SYMBOL(vprintk); |
| 1055 | |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame^] | 1056 | asmlinkage int printk_emit(int facility, int level, |
| 1057 | const char *dict, size_t dictlen, |
| 1058 | const char *fmt, ...) |
| 1059 | { |
| 1060 | va_list args; |
| 1061 | int r; |
| 1062 | |
| 1063 | va_start(args, fmt); |
| 1064 | r = vprintk_emit(facility, level, dict, dictlen, fmt, args); |
| 1065 | va_end(args); |
| 1066 | |
| 1067 | return r; |
| 1068 | } |
| 1069 | EXPORT_SYMBOL(printk_emit); |
| 1070 | |
| 1071 | /** |
| 1072 | * printk - print a kernel message |
| 1073 | * @fmt: format string |
| 1074 | * |
| 1075 | * This is printk(). It can be called from any context. We want it to work. |
| 1076 | * |
| 1077 | * We try to grab the console_lock. If we succeed, it's easy - we log the |
| 1078 | * output and call the console drivers. If we fail to get the semaphore, we |
| 1079 | * place the output into the log buffer and return. The current holder of |
| 1080 | * the console_sem will notice the new output in console_unlock(); and will |
| 1081 | * send it to the consoles before releasing the lock. |
| 1082 | * |
| 1083 | * One effect of this deferred printing is that code which calls printk() and |
| 1084 | * then changes console_loglevel may break. This is because console_loglevel |
| 1085 | * is inspected when the actual printing occurs. |
| 1086 | * |
| 1087 | * See also: |
| 1088 | * printf(3) |
| 1089 | * |
| 1090 | * See the vsnprintf() documentation for format string extensions over C99. |
| 1091 | */ |
| 1092 | asmlinkage int printk(const char *fmt, ...) |
| 1093 | { |
| 1094 | va_list args; |
| 1095 | int r; |
| 1096 | |
| 1097 | #ifdef CONFIG_KGDB_KDB |
| 1098 | if (unlikely(kdb_trap_printk)) { |
| 1099 | va_start(args, fmt); |
| 1100 | r = vkdb_printf(fmt, args); |
| 1101 | va_end(args); |
| 1102 | return r; |
| 1103 | } |
| 1104 | #endif |
| 1105 | va_start(args, fmt); |
| 1106 | r = vprintk_emit(0, -1, NULL, 0, fmt, args); |
| 1107 | va_end(args); |
| 1108 | |
| 1109 | return r; |
| 1110 | } |
| 1111 | EXPORT_SYMBOL(printk); |
Matt Mackall | d59745c | 2005-05-01 08:59:02 -0700 | [diff] [blame] | 1112 | #else |
| 1113 | |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame^] | 1114 | static void call_console_drivers(int level, const char *text, size_t len) |
Jesper Juhl | 40dc565 | 2005-10-30 15:02:46 -0800 | [diff] [blame] | 1115 | { |
| 1116 | } |
Matt Mackall | d59745c | 2005-05-01 08:59:02 -0700 | [diff] [blame] | 1117 | |
| 1118 | #endif |
| 1119 | |
Samuel Thibault | f7511d5 | 2008-04-30 00:54:51 -0700 | [diff] [blame] | 1120 | static int __add_preferred_console(char *name, int idx, char *options, |
| 1121 | char *brl_options) |
| 1122 | { |
| 1123 | struct console_cmdline *c; |
| 1124 | int i; |
| 1125 | |
| 1126 | /* |
| 1127 | * See if this tty is not yet registered, and |
| 1128 | * if we have a slot free. |
| 1129 | */ |
| 1130 | for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++) |
| 1131 | if (strcmp(console_cmdline[i].name, name) == 0 && |
| 1132 | console_cmdline[i].index == idx) { |
| 1133 | if (!brl_options) |
| 1134 | selected_console = i; |
| 1135 | return 0; |
| 1136 | } |
| 1137 | if (i == MAX_CMDLINECONSOLES) |
| 1138 | return -E2BIG; |
| 1139 | if (!brl_options) |
| 1140 | selected_console = i; |
| 1141 | c = &console_cmdline[i]; |
| 1142 | strlcpy(c->name, name, sizeof(c->name)); |
| 1143 | c->options = options; |
| 1144 | #ifdef CONFIG_A11Y_BRAILLE_CONSOLE |
| 1145 | c->brl_options = brl_options; |
| 1146 | #endif |
| 1147 | c->index = idx; |
| 1148 | return 0; |
| 1149 | } |
John Z. Bohach | 2ea1c53 | 2006-03-24 03:18:19 -0800 | [diff] [blame] | 1150 | /* |
| 1151 | * Set up a list of consoles. Called from init/main.c |
| 1152 | */ |
| 1153 | static int __init console_setup(char *str) |
| 1154 | { |
Yinghai Lu | eaa944a | 2007-07-15 23:37:27 -0700 | [diff] [blame] | 1155 | char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for index */ |
Samuel Thibault | f7511d5 | 2008-04-30 00:54:51 -0700 | [diff] [blame] | 1156 | char *s, *options, *brl_options = NULL; |
John Z. Bohach | 2ea1c53 | 2006-03-24 03:18:19 -0800 | [diff] [blame] | 1157 | int idx; |
| 1158 | |
Samuel Thibault | f7511d5 | 2008-04-30 00:54:51 -0700 | [diff] [blame] | 1159 | #ifdef CONFIG_A11Y_BRAILLE_CONSOLE |
| 1160 | if (!memcmp(str, "brl,", 4)) { |
| 1161 | brl_options = ""; |
| 1162 | str += 4; |
| 1163 | } else if (!memcmp(str, "brl=", 4)) { |
| 1164 | brl_options = str + 4; |
| 1165 | str = strchr(brl_options, ','); |
| 1166 | if (!str) { |
| 1167 | printk(KERN_ERR "need port name after brl=\n"); |
| 1168 | return 1; |
| 1169 | } |
| 1170 | *(str++) = 0; |
| 1171 | } |
| 1172 | #endif |
| 1173 | |
John Z. Bohach | 2ea1c53 | 2006-03-24 03:18:19 -0800 | [diff] [blame] | 1174 | /* |
| 1175 | * Decode str into name, index, options. |
| 1176 | */ |
| 1177 | if (str[0] >= '0' && str[0] <= '9') { |
Yinghai Lu | eaa944a | 2007-07-15 23:37:27 -0700 | [diff] [blame] | 1178 | strcpy(buf, "ttyS"); |
| 1179 | strncpy(buf + 4, str, sizeof(buf) - 5); |
John Z. Bohach | 2ea1c53 | 2006-03-24 03:18:19 -0800 | [diff] [blame] | 1180 | } else { |
Yinghai Lu | eaa944a | 2007-07-15 23:37:27 -0700 | [diff] [blame] | 1181 | strncpy(buf, str, sizeof(buf) - 1); |
John Z. Bohach | 2ea1c53 | 2006-03-24 03:18:19 -0800 | [diff] [blame] | 1182 | } |
Yinghai Lu | eaa944a | 2007-07-15 23:37:27 -0700 | [diff] [blame] | 1183 | buf[sizeof(buf) - 1] = 0; |
John Z. Bohach | 2ea1c53 | 2006-03-24 03:18:19 -0800 | [diff] [blame] | 1184 | if ((options = strchr(str, ',')) != NULL) |
| 1185 | *(options++) = 0; |
| 1186 | #ifdef __sparc__ |
| 1187 | if (!strcmp(str, "ttya")) |
Yinghai Lu | eaa944a | 2007-07-15 23:37:27 -0700 | [diff] [blame] | 1188 | strcpy(buf, "ttyS0"); |
John Z. Bohach | 2ea1c53 | 2006-03-24 03:18:19 -0800 | [diff] [blame] | 1189 | if (!strcmp(str, "ttyb")) |
Yinghai Lu | eaa944a | 2007-07-15 23:37:27 -0700 | [diff] [blame] | 1190 | strcpy(buf, "ttyS1"); |
John Z. Bohach | 2ea1c53 | 2006-03-24 03:18:19 -0800 | [diff] [blame] | 1191 | #endif |
Yinghai Lu | eaa944a | 2007-07-15 23:37:27 -0700 | [diff] [blame] | 1192 | for (s = buf; *s; s++) |
John Z. Bohach | 2ea1c53 | 2006-03-24 03:18:19 -0800 | [diff] [blame] | 1193 | if ((*s >= '0' && *s <= '9') || *s == ',') |
| 1194 | break; |
| 1195 | idx = simple_strtoul(s, NULL, 10); |
| 1196 | *s = 0; |
| 1197 | |
Samuel Thibault | f7511d5 | 2008-04-30 00:54:51 -0700 | [diff] [blame] | 1198 | __add_preferred_console(buf, idx, options, brl_options); |
Markus Armbruster | 9e124fe | 2008-05-26 23:31:07 +0100 | [diff] [blame] | 1199 | console_set_on_cmdline = 1; |
John Z. Bohach | 2ea1c53 | 2006-03-24 03:18:19 -0800 | [diff] [blame] | 1200 | return 1; |
| 1201 | } |
| 1202 | __setup("console=", console_setup); |
| 1203 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1204 | /** |
Matt Mackall | 3c0547b | 2005-05-16 21:53:47 -0700 | [diff] [blame] | 1205 | * add_preferred_console - add a device to the list of preferred consoles. |
Martin Waitz | ddad86c | 2005-11-13 16:08:14 -0800 | [diff] [blame] | 1206 | * @name: device name |
| 1207 | * @idx: device index |
| 1208 | * @options: options for this console |
Matt Mackall | 3c0547b | 2005-05-16 21:53:47 -0700 | [diff] [blame] | 1209 | * |
| 1210 | * The last preferred console added will be used for kernel messages |
| 1211 | * and stdin/out/err for init. Normally this is used by console_setup |
| 1212 | * above to handle user-supplied console arguments; however it can also |
| 1213 | * be used by arch-specific code either to override the user or more |
| 1214 | * commonly to provide a default console (ie from PROM variables) when |
| 1215 | * the user has not supplied one. |
| 1216 | */ |
David S. Miller | fb445ee | 2007-12-29 01:19:49 -0800 | [diff] [blame] | 1217 | int add_preferred_console(char *name, int idx, char *options) |
Matt Mackall | 3c0547b | 2005-05-16 21:53:47 -0700 | [diff] [blame] | 1218 | { |
Samuel Thibault | f7511d5 | 2008-04-30 00:54:51 -0700 | [diff] [blame] | 1219 | return __add_preferred_console(name, idx, options, NULL); |
Matt Mackall | 3c0547b | 2005-05-16 21:53:47 -0700 | [diff] [blame] | 1220 | } |
| 1221 | |
Daniel Ritz | b6b1d87 | 2007-08-03 16:07:43 +0200 | [diff] [blame] | 1222 | int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options) |
Yinghai Lu | 18a8bd9 | 2007-07-15 23:37:59 -0700 | [diff] [blame] | 1223 | { |
| 1224 | struct console_cmdline *c; |
| 1225 | int i; |
| 1226 | |
| 1227 | for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++) |
| 1228 | if (strcmp(console_cmdline[i].name, name) == 0 && |
| 1229 | console_cmdline[i].index == idx) { |
| 1230 | c = &console_cmdline[i]; |
Markus Armbruster | f735295 | 2008-04-30 00:54:52 -0700 | [diff] [blame] | 1231 | strlcpy(c->name, name_new, sizeof(c->name)); |
Yinghai Lu | 18a8bd9 | 2007-07-15 23:37:59 -0700 | [diff] [blame] | 1232 | c->name[sizeof(c->name) - 1] = 0; |
| 1233 | c->options = options; |
| 1234 | c->index = idx_new; |
| 1235 | return i; |
| 1236 | } |
| 1237 | /* not found */ |
| 1238 | return -1; |
| 1239 | } |
| 1240 | |
Rusty Russell | 2329abf | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 1241 | bool console_suspend_enabled = 1; |
Andres Salomon | 8f4ce8c | 2007-10-18 03:04:50 -0700 | [diff] [blame] | 1242 | EXPORT_SYMBOL(console_suspend_enabled); |
| 1243 | |
| 1244 | static int __init console_suspend_disable(char *str) |
| 1245 | { |
| 1246 | console_suspend_enabled = 0; |
| 1247 | return 1; |
| 1248 | } |
| 1249 | __setup("no_console_suspend", console_suspend_disable); |
Yanmin Zhang | 134620f | 2011-10-31 17:11:27 -0700 | [diff] [blame] | 1250 | module_param_named(console_suspend, console_suspend_enabled, |
| 1251 | bool, S_IRUGO | S_IWUSR); |
| 1252 | MODULE_PARM_DESC(console_suspend, "suspend console during suspend" |
| 1253 | " and hibernate operations"); |
Andres Salomon | 8f4ce8c | 2007-10-18 03:04:50 -0700 | [diff] [blame] | 1254 | |
Matt Mackall | 3c0547b | 2005-05-16 21:53:47 -0700 | [diff] [blame] | 1255 | /** |
Linus Torvalds | 557240b | 2006-06-19 18:16:01 -0700 | [diff] [blame] | 1256 | * suspend_console - suspend the console subsystem |
| 1257 | * |
| 1258 | * This disables printk() while we go into suspend states |
| 1259 | */ |
| 1260 | void suspend_console(void) |
| 1261 | { |
Andres Salomon | 8f4ce8c | 2007-10-18 03:04:50 -0700 | [diff] [blame] | 1262 | if (!console_suspend_enabled) |
| 1263 | return; |
Pavel Machek | 0d63081 | 2008-07-23 21:28:32 -0700 | [diff] [blame] | 1264 | printk("Suspending console(s) (use no_console_suspend to debug)\n"); |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1265 | console_lock(); |
Linus Torvalds | 557240b | 2006-06-19 18:16:01 -0700 | [diff] [blame] | 1266 | console_suspended = 1; |
Arve Hjønnevåg | 403f307 | 2009-02-14 02:07:24 +0100 | [diff] [blame] | 1267 | up(&console_sem); |
Linus Torvalds | 557240b | 2006-06-19 18:16:01 -0700 | [diff] [blame] | 1268 | } |
| 1269 | |
| 1270 | void resume_console(void) |
| 1271 | { |
Andres Salomon | 8f4ce8c | 2007-10-18 03:04:50 -0700 | [diff] [blame] | 1272 | if (!console_suspend_enabled) |
| 1273 | return; |
Arve Hjønnevåg | 403f307 | 2009-02-14 02:07:24 +0100 | [diff] [blame] | 1274 | down(&console_sem); |
Linus Torvalds | 557240b | 2006-06-19 18:16:01 -0700 | [diff] [blame] | 1275 | console_suspended = 0; |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1276 | console_unlock(); |
Linus Torvalds | 557240b | 2006-06-19 18:16:01 -0700 | [diff] [blame] | 1277 | } |
| 1278 | |
| 1279 | /** |
Kevin Cernekee | 034260d | 2010-06-03 22:11:25 -0700 | [diff] [blame] | 1280 | * console_cpu_notify - print deferred console messages after CPU hotplug |
| 1281 | * @self: notifier struct |
| 1282 | * @action: CPU hotplug event |
| 1283 | * @hcpu: unused |
| 1284 | * |
| 1285 | * If printk() is called from a CPU that is not online yet, the messages |
| 1286 | * will be spooled but will not show up on the console. This function is |
| 1287 | * called when a new CPU comes online (or fails to come up), and ensures |
| 1288 | * that any such output gets printed. |
| 1289 | */ |
| 1290 | static int __cpuinit console_cpu_notify(struct notifier_block *self, |
| 1291 | unsigned long action, void *hcpu) |
| 1292 | { |
| 1293 | switch (action) { |
| 1294 | case CPU_ONLINE: |
| 1295 | case CPU_DEAD: |
| 1296 | case CPU_DYING: |
| 1297 | case CPU_DOWN_FAILED: |
| 1298 | case CPU_UP_CANCELED: |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1299 | console_lock(); |
| 1300 | console_unlock(); |
Kevin Cernekee | 034260d | 2010-06-03 22:11:25 -0700 | [diff] [blame] | 1301 | } |
| 1302 | return NOTIFY_OK; |
| 1303 | } |
| 1304 | |
| 1305 | /** |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1306 | * console_lock - lock the console system for exclusive use. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1307 | * |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1308 | * Acquires a lock which guarantees that the caller has |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 | * exclusive access to the console system and the console_drivers list. |
| 1310 | * |
| 1311 | * Can sleep, returns nothing. |
| 1312 | */ |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1313 | void console_lock(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | { |
Eric Sesterhenn | 8abd8e2 | 2006-04-01 01:21:17 +0200 | [diff] [blame] | 1315 | BUG_ON(in_interrupt()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1316 | down(&console_sem); |
Arve Hjønnevåg | 403f307 | 2009-02-14 02:07:24 +0100 | [diff] [blame] | 1317 | if (console_suspended) |
| 1318 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1319 | console_locked = 1; |
| 1320 | console_may_schedule = 1; |
| 1321 | } |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1322 | EXPORT_SYMBOL(console_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1323 | |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1324 | /** |
| 1325 | * console_trylock - try to lock the console system for exclusive use. |
| 1326 | * |
| 1327 | * Tried to acquire a lock which guarantees that the caller has |
| 1328 | * exclusive access to the console system and the console_drivers list. |
| 1329 | * |
| 1330 | * returns 1 on success, and 0 on failure to acquire the lock. |
| 1331 | */ |
| 1332 | int console_trylock(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1333 | { |
| 1334 | if (down_trylock(&console_sem)) |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1335 | return 0; |
Arve Hjønnevåg | 403f307 | 2009-02-14 02:07:24 +0100 | [diff] [blame] | 1336 | if (console_suspended) { |
| 1337 | up(&console_sem); |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1338 | return 0; |
Arve Hjønnevåg | 403f307 | 2009-02-14 02:07:24 +0100 | [diff] [blame] | 1339 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1340 | console_locked = 1; |
| 1341 | console_may_schedule = 0; |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1342 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1343 | } |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1344 | EXPORT_SYMBOL(console_trylock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1345 | |
| 1346 | int is_console_locked(void) |
| 1347 | { |
| 1348 | return console_locked; |
| 1349 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | |
Peter Zijlstra | 3ccf3e8 | 2012-02-27 10:47:00 +0100 | [diff] [blame] | 1351 | /* |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame^] | 1352 | * Delayed printk version, for scheduler-internal messages: |
Peter Zijlstra | 3ccf3e8 | 2012-02-27 10:47:00 +0100 | [diff] [blame] | 1353 | */ |
| 1354 | #define PRINTK_BUF_SIZE 512 |
| 1355 | |
| 1356 | #define PRINTK_PENDING_WAKEUP 0x01 |
| 1357 | #define PRINTK_PENDING_SCHED 0x02 |
| 1358 | |
Peter Zijlstra | b845b51 | 2008-08-08 21:47:09 +0200 | [diff] [blame] | 1359 | static DEFINE_PER_CPU(int, printk_pending); |
Peter Zijlstra | 3ccf3e8 | 2012-02-27 10:47:00 +0100 | [diff] [blame] | 1360 | static DEFINE_PER_CPU(char [PRINTK_BUF_SIZE], printk_sched_buf); |
Peter Zijlstra | b845b51 | 2008-08-08 21:47:09 +0200 | [diff] [blame] | 1361 | |
| 1362 | void printk_tick(void) |
| 1363 | { |
Eric Dumazet | 40dc11f | 2010-11-26 17:22:16 +0100 | [diff] [blame] | 1364 | if (__this_cpu_read(printk_pending)) { |
Peter Zijlstra | 3ccf3e8 | 2012-02-27 10:47:00 +0100 | [diff] [blame] | 1365 | int pending = __this_cpu_xchg(printk_pending, 0); |
| 1366 | if (pending & PRINTK_PENDING_SCHED) { |
| 1367 | char *buf = __get_cpu_var(printk_sched_buf); |
| 1368 | printk(KERN_WARNING "[sched_delayed] %s", buf); |
| 1369 | } |
| 1370 | if (pending & PRINTK_PENDING_WAKEUP) |
| 1371 | wake_up_interruptible(&log_wait); |
Peter Zijlstra | b845b51 | 2008-08-08 21:47:09 +0200 | [diff] [blame] | 1372 | } |
| 1373 | } |
| 1374 | |
| 1375 | int printk_needs_cpu(int cpu) |
| 1376 | { |
Eric Dumazet | 40dc11f | 2010-11-26 17:22:16 +0100 | [diff] [blame] | 1377 | if (cpu_is_offline(cpu)) |
Heiko Carstens | 61ab2544 | 2010-11-26 13:00:59 +0100 | [diff] [blame] | 1378 | printk_tick(); |
Eric Dumazet | 40dc11f | 2010-11-26 17:22:16 +0100 | [diff] [blame] | 1379 | return __this_cpu_read(printk_pending); |
Peter Zijlstra | b845b51 | 2008-08-08 21:47:09 +0200 | [diff] [blame] | 1380 | } |
| 1381 | |
Kirill Korotaev | e3e8a75 | 2007-02-10 01:46:19 -0800 | [diff] [blame] | 1382 | void wake_up_klogd(void) |
| 1383 | { |
Peter Zijlstra | b845b51 | 2008-08-08 21:47:09 +0200 | [diff] [blame] | 1384 | if (waitqueue_active(&log_wait)) |
Peter Zijlstra | 3ccf3e8 | 2012-02-27 10:47:00 +0100 | [diff] [blame] | 1385 | this_cpu_or(printk_pending, PRINTK_PENDING_WAKEUP); |
Kirill Korotaev | e3e8a75 | 2007-02-10 01:46:19 -0800 | [diff] [blame] | 1386 | } |
| 1387 | |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame^] | 1388 | /* the next printk record to write to the console */ |
| 1389 | static u64 console_seq; |
| 1390 | static u32 console_idx; |
| 1391 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1392 | /** |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1393 | * console_unlock - unlock the console system |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1394 | * |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1395 | * Releases the console_lock which the caller holds on the console system |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1396 | * and the console driver list. |
| 1397 | * |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1398 | * While the console_lock was held, console output may have been buffered |
| 1399 | * by printk(). If this is the case, console_unlock(); emits |
| 1400 | * the output prior to releasing the lock. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | * |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame^] | 1402 | * If there is output waiting, we wake it /dev/kmsg and syslog() users. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1403 | * |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1404 | * console_unlock(); may be called from any context. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1405 | */ |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1406 | void console_unlock(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1407 | { |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame^] | 1408 | static u64 seen_seq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | unsigned long flags; |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame^] | 1410 | bool wake_klogd = false; |
| 1411 | bool retry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1412 | |
Linus Torvalds | 557240b | 2006-06-19 18:16:01 -0700 | [diff] [blame] | 1413 | if (console_suspended) { |
Arve Hjønnevåg | 403f307 | 2009-02-14 02:07:24 +0100 | [diff] [blame] | 1414 | up(&console_sem); |
Linus Torvalds | 557240b | 2006-06-19 18:16:01 -0700 | [diff] [blame] | 1415 | return; |
| 1416 | } |
Antonino A. Daplas | 78944e5 | 2006-08-05 12:14:16 -0700 | [diff] [blame] | 1417 | |
| 1418 | console_may_schedule = 0; |
| 1419 | |
Peter Zijlstra | 4f2a8d3 | 2011-06-22 11:20:09 +0200 | [diff] [blame] | 1420 | again: |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame^] | 1421 | for (;;) { |
| 1422 | struct log *msg; |
| 1423 | static char text[LOG_LINE_MAX]; |
| 1424 | size_t len; |
| 1425 | int level; |
| 1426 | |
Thomas Gleixner | 07354eb | 2009-07-25 17:50:36 +0200 | [diff] [blame] | 1427 | raw_spin_lock_irqsave(&logbuf_lock, flags); |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame^] | 1428 | if (seen_seq != log_next_seq) { |
| 1429 | wake_klogd = true; |
| 1430 | seen_seq = log_next_seq; |
| 1431 | } |
| 1432 | |
| 1433 | if (console_seq < log_first_seq) { |
| 1434 | /* messages are gone, move to first one */ |
| 1435 | console_seq = log_first_seq; |
| 1436 | console_idx = log_first_idx; |
| 1437 | } |
| 1438 | |
| 1439 | if (console_seq == log_next_seq) |
| 1440 | break; |
| 1441 | |
| 1442 | msg = log_from_idx(console_idx); |
| 1443 | level = msg->level & 7; |
| 1444 | len = msg->text_len; |
| 1445 | if (len+1 >= sizeof(text)) |
| 1446 | len = sizeof(text)-1; |
| 1447 | memcpy(text, log_text(msg), len); |
| 1448 | text[len++] = '\n'; |
| 1449 | |
| 1450 | console_idx = log_next(console_idx); |
| 1451 | console_seq++; |
Thomas Gleixner | 07354eb | 2009-07-25 17:50:36 +0200 | [diff] [blame] | 1452 | raw_spin_unlock(&logbuf_lock); |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame^] | 1453 | |
Steven Rostedt | 81d68a9 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1454 | stop_critical_timings(); /* don't trace print latency */ |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame^] | 1455 | call_console_drivers(level, text, len); |
Steven Rostedt | 81d68a9 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1456 | start_critical_timings(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1457 | local_irq_restore(flags); |
| 1458 | } |
| 1459 | console_locked = 0; |
Feng Tang | fe3d8ad | 2011-03-22 16:34:21 -0700 | [diff] [blame] | 1460 | |
| 1461 | /* Release the exclusive_console once it is used */ |
| 1462 | if (unlikely(exclusive_console)) |
| 1463 | exclusive_console = NULL; |
| 1464 | |
Thomas Gleixner | 07354eb | 2009-07-25 17:50:36 +0200 | [diff] [blame] | 1465 | raw_spin_unlock(&logbuf_lock); |
Peter Zijlstra | 4f2a8d3 | 2011-06-22 11:20:09 +0200 | [diff] [blame] | 1466 | |
Peter Zijlstra | 0b5e1c5 | 2011-06-07 11:15:33 +0200 | [diff] [blame] | 1467 | up(&console_sem); |
Peter Zijlstra | 4f2a8d3 | 2011-06-22 11:20:09 +0200 | [diff] [blame] | 1468 | |
| 1469 | /* |
| 1470 | * Someone could have filled up the buffer again, so re-check if there's |
| 1471 | * something to flush. In case we cannot trylock the console_sem again, |
| 1472 | * there's a new owner and the console_unlock() from them will do the |
| 1473 | * flush, no worries. |
| 1474 | */ |
Thomas Gleixner | 07354eb | 2009-07-25 17:50:36 +0200 | [diff] [blame] | 1475 | raw_spin_lock(&logbuf_lock); |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame^] | 1476 | retry = console_seq != log_next_seq; |
Peter Zijlstra | 09dc3cf | 2011-12-08 14:34:13 -0800 | [diff] [blame] | 1477 | raw_spin_unlock_irqrestore(&logbuf_lock, flags); |
| 1478 | |
Peter Zijlstra | 4f2a8d3 | 2011-06-22 11:20:09 +0200 | [diff] [blame] | 1479 | if (retry && console_trylock()) |
| 1480 | goto again; |
| 1481 | |
Kirill Korotaev | e3e8a75 | 2007-02-10 01:46:19 -0800 | [diff] [blame] | 1482 | if (wake_klogd) |
| 1483 | wake_up_klogd(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1484 | } |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1485 | EXPORT_SYMBOL(console_unlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1486 | |
Martin Waitz | ddad86c | 2005-11-13 16:08:14 -0800 | [diff] [blame] | 1487 | /** |
| 1488 | * console_conditional_schedule - yield the CPU if required |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1489 | * |
| 1490 | * If the console code is currently allowed to sleep, and |
| 1491 | * if this CPU should yield the CPU to another task, do |
| 1492 | * so here. |
| 1493 | * |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1494 | * Must be called within console_lock();. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1495 | */ |
| 1496 | void __sched console_conditional_schedule(void) |
| 1497 | { |
| 1498 | if (console_may_schedule) |
| 1499 | cond_resched(); |
| 1500 | } |
| 1501 | EXPORT_SYMBOL(console_conditional_schedule); |
| 1502 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1503 | void console_unblank(void) |
| 1504 | { |
| 1505 | struct console *c; |
| 1506 | |
| 1507 | /* |
| 1508 | * console_unblank can no longer be called in interrupt context unless |
| 1509 | * oops_in_progress is set to 1.. |
| 1510 | */ |
| 1511 | if (oops_in_progress) { |
| 1512 | if (down_trylock(&console_sem) != 0) |
| 1513 | return; |
| 1514 | } else |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1515 | console_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1516 | |
| 1517 | console_locked = 1; |
| 1518 | console_may_schedule = 0; |
Robin Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 1519 | for_each_console(c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1520 | if ((c->flags & CON_ENABLED) && c->unblank) |
| 1521 | c->unblank(); |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1522 | console_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1523 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1524 | |
| 1525 | /* |
| 1526 | * Return the console tty driver structure and its associated index |
| 1527 | */ |
| 1528 | struct tty_driver *console_device(int *index) |
| 1529 | { |
| 1530 | struct console *c; |
| 1531 | struct tty_driver *driver = NULL; |
| 1532 | |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1533 | console_lock(); |
Robin Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 1534 | for_each_console(c) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1535 | if (!c->device) |
| 1536 | continue; |
| 1537 | driver = c->device(c, index); |
| 1538 | if (driver) |
| 1539 | break; |
| 1540 | } |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1541 | console_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1542 | return driver; |
| 1543 | } |
| 1544 | |
| 1545 | /* |
| 1546 | * Prevent further output on the passed console device so that (for example) |
| 1547 | * serial drivers can disable console output before suspending a port, and can |
| 1548 | * re-enable output afterwards. |
| 1549 | */ |
| 1550 | void console_stop(struct console *console) |
| 1551 | { |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1552 | console_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1553 | console->flags &= ~CON_ENABLED; |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1554 | console_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1555 | } |
| 1556 | EXPORT_SYMBOL(console_stop); |
| 1557 | |
| 1558 | void console_start(struct console *console) |
| 1559 | { |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1560 | console_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1561 | console->flags |= CON_ENABLED; |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1562 | console_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1563 | } |
| 1564 | EXPORT_SYMBOL(console_start); |
| 1565 | |
Fabio M. Di Nitto | 7bf6939 | 2011-03-22 16:34:20 -0700 | [diff] [blame] | 1566 | static int __read_mostly keep_bootcon; |
| 1567 | |
| 1568 | static int __init keep_bootcon_setup(char *str) |
| 1569 | { |
| 1570 | keep_bootcon = 1; |
| 1571 | printk(KERN_INFO "debug: skip boot console de-registration.\n"); |
| 1572 | |
| 1573 | return 0; |
| 1574 | } |
| 1575 | |
| 1576 | early_param("keep_bootcon", keep_bootcon_setup); |
| 1577 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1578 | /* |
| 1579 | * The console driver calls this routine during kernel initialization |
| 1580 | * to register the console printing procedure with printk() and to |
| 1581 | * print any messages that were printed by the kernel before the |
| 1582 | * console driver was initialized. |
Robin Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 1583 | * |
| 1584 | * This can happen pretty early during the boot process (because of |
| 1585 | * early_printk) - sometimes before setup_arch() completes - be careful |
| 1586 | * of what kernel features are used - they may not be initialised yet. |
| 1587 | * |
| 1588 | * There are two types of consoles - bootconsoles (early_printk) and |
| 1589 | * "real" consoles (everything which is not a bootconsole) which are |
| 1590 | * handled differently. |
| 1591 | * - Any number of bootconsoles can be registered at any time. |
| 1592 | * - As soon as a "real" console is registered, all bootconsoles |
| 1593 | * will be unregistered automatically. |
| 1594 | * - Once a "real" console is registered, any attempt to register a |
| 1595 | * bootconsoles will be rejected |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1596 | */ |
Robin Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 1597 | void register_console(struct console *newcon) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1598 | { |
Jesper Juhl | 40dc565 | 2005-10-30 15:02:46 -0800 | [diff] [blame] | 1599 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1600 | unsigned long flags; |
Robin Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 1601 | struct console *bcon = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1602 | |
Robin Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 1603 | /* |
| 1604 | * before we register a new CON_BOOT console, make sure we don't |
| 1605 | * already have a valid console |
| 1606 | */ |
| 1607 | if (console_drivers && newcon->flags & CON_BOOT) { |
| 1608 | /* find the last or real console */ |
| 1609 | for_each_console(bcon) { |
| 1610 | if (!(bcon->flags & CON_BOOT)) { |
| 1611 | printk(KERN_INFO "Too late to register bootconsole %s%d\n", |
| 1612 | newcon->name, newcon->index); |
| 1613 | return; |
| 1614 | } |
| 1615 | } |
Gerd Hoffmann | 69331af | 2007-05-08 00:26:49 -0700 | [diff] [blame] | 1616 | } |
| 1617 | |
Robin Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 1618 | if (console_drivers && console_drivers->flags & CON_BOOT) |
| 1619 | bcon = console_drivers; |
| 1620 | |
| 1621 | if (preferred_console < 0 || bcon || !console_drivers) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1622 | preferred_console = selected_console; |
| 1623 | |
Robin Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 1624 | if (newcon->early_setup) |
| 1625 | newcon->early_setup(); |
Yinghai Lu | 18a8bd9 | 2007-07-15 23:37:59 -0700 | [diff] [blame] | 1626 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1627 | /* |
| 1628 | * See if we want to use this console driver. If we |
| 1629 | * didn't select a console we take the first one |
| 1630 | * that registers here. |
| 1631 | */ |
| 1632 | if (preferred_console < 0) { |
Robin Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 1633 | if (newcon->index < 0) |
| 1634 | newcon->index = 0; |
| 1635 | if (newcon->setup == NULL || |
| 1636 | newcon->setup(newcon, NULL) == 0) { |
| 1637 | newcon->flags |= CON_ENABLED; |
| 1638 | if (newcon->device) { |
| 1639 | newcon->flags |= CON_CONSDEV; |
Jan Kiszka | cd3a1b8 | 2008-05-12 21:21:04 +0200 | [diff] [blame] | 1640 | preferred_console = 0; |
| 1641 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1642 | } |
| 1643 | } |
| 1644 | |
| 1645 | /* |
| 1646 | * See if this console matches one we selected on |
| 1647 | * the command line. |
| 1648 | */ |
Jesper Juhl | 40dc565 | 2005-10-30 15:02:46 -0800 | [diff] [blame] | 1649 | for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; |
| 1650 | i++) { |
Robin Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 1651 | if (strcmp(console_cmdline[i].name, newcon->name) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1652 | continue; |
Robin Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 1653 | if (newcon->index >= 0 && |
| 1654 | newcon->index != console_cmdline[i].index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1655 | continue; |
Robin Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 1656 | if (newcon->index < 0) |
| 1657 | newcon->index = console_cmdline[i].index; |
Samuel Thibault | f7511d5 | 2008-04-30 00:54:51 -0700 | [diff] [blame] | 1658 | #ifdef CONFIG_A11Y_BRAILLE_CONSOLE |
| 1659 | if (console_cmdline[i].brl_options) { |
Robin Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 1660 | newcon->flags |= CON_BRL; |
| 1661 | braille_register_console(newcon, |
Samuel Thibault | f7511d5 | 2008-04-30 00:54:51 -0700 | [diff] [blame] | 1662 | console_cmdline[i].index, |
| 1663 | console_cmdline[i].options, |
| 1664 | console_cmdline[i].brl_options); |
| 1665 | return; |
| 1666 | } |
| 1667 | #endif |
Robin Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 1668 | if (newcon->setup && |
| 1669 | newcon->setup(newcon, console_cmdline[i].options) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1670 | break; |
Robin Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 1671 | newcon->flags |= CON_ENABLED; |
| 1672 | newcon->index = console_cmdline[i].index; |
Greg Edwards | ab4af03 | 2005-06-23 00:09:05 -0700 | [diff] [blame] | 1673 | if (i == selected_console) { |
Robin Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 1674 | newcon->flags |= CON_CONSDEV; |
Greg Edwards | ab4af03 | 2005-06-23 00:09:05 -0700 | [diff] [blame] | 1675 | preferred_console = selected_console; |
| 1676 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1677 | break; |
| 1678 | } |
| 1679 | |
Robin Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 1680 | if (!(newcon->flags & CON_ENABLED)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1681 | return; |
| 1682 | |
Robin Getz | 8259cf4 | 2009-07-09 13:08:37 -0400 | [diff] [blame] | 1683 | /* |
| 1684 | * If we have a bootconsole, and are switching to a real console, |
| 1685 | * don't print everything out again, since when the boot console, and |
| 1686 | * the real console are the same physical device, it's annoying to |
| 1687 | * see the beginning boot messages twice |
| 1688 | */ |
| 1689 | if (bcon && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV)) |
Robin Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 1690 | newcon->flags &= ~CON_PRINTBUFFER; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1691 | |
| 1692 | /* |
| 1693 | * Put this console in the list - keep the |
| 1694 | * preferred driver at the head of the list. |
| 1695 | */ |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1696 | console_lock(); |
Robin Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 1697 | if ((newcon->flags & CON_CONSDEV) || console_drivers == NULL) { |
| 1698 | newcon->next = console_drivers; |
| 1699 | console_drivers = newcon; |
| 1700 | if (newcon->next) |
| 1701 | newcon->next->flags &= ~CON_CONSDEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1702 | } else { |
Robin Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 1703 | newcon->next = console_drivers->next; |
| 1704 | console_drivers->next = newcon; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1705 | } |
Robin Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 1706 | if (newcon->flags & CON_PRINTBUFFER) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1707 | /* |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1708 | * console_unlock(); will print out the buffered messages |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1709 | * for us. |
| 1710 | */ |
Thomas Gleixner | 07354eb | 2009-07-25 17:50:36 +0200 | [diff] [blame] | 1711 | raw_spin_lock_irqsave(&logbuf_lock, flags); |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame^] | 1712 | console_seq = syslog_seq; |
| 1713 | console_idx = syslog_idx; |
Thomas Gleixner | 07354eb | 2009-07-25 17:50:36 +0200 | [diff] [blame] | 1714 | raw_spin_unlock_irqrestore(&logbuf_lock, flags); |
Feng Tang | fe3d8ad | 2011-03-22 16:34:21 -0700 | [diff] [blame] | 1715 | /* |
| 1716 | * We're about to replay the log buffer. Only do this to the |
| 1717 | * just-registered console to avoid excessive message spam to |
| 1718 | * the already-registered consoles. |
| 1719 | */ |
| 1720 | exclusive_console = newcon; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1721 | } |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1722 | console_unlock(); |
Kay Sievers | fbc92a3 | 2010-12-01 18:51:05 +0100 | [diff] [blame] | 1723 | console_sysfs_notify(); |
Robin Getz | 8259cf4 | 2009-07-09 13:08:37 -0400 | [diff] [blame] | 1724 | |
| 1725 | /* |
| 1726 | * By unregistering the bootconsoles after we enable the real console |
| 1727 | * we get the "console xxx enabled" message on all the consoles - |
| 1728 | * boot consoles, real consoles, etc - this is to ensure that end |
| 1729 | * users know there might be something in the kernel's log buffer that |
| 1730 | * went to the bootconsole (that they do not see on the real console) |
| 1731 | */ |
Fabio M. Di Nitto | 7bf6939 | 2011-03-22 16:34:20 -0700 | [diff] [blame] | 1732 | if (bcon && |
| 1733 | ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV) && |
| 1734 | !keep_bootcon) { |
Robin Getz | 8259cf4 | 2009-07-09 13:08:37 -0400 | [diff] [blame] | 1735 | /* we need to iterate through twice, to make sure we print |
| 1736 | * everything out, before we unregister the console(s) |
| 1737 | */ |
| 1738 | printk(KERN_INFO "console [%s%d] enabled, bootconsole disabled\n", |
| 1739 | newcon->name, newcon->index); |
| 1740 | for_each_console(bcon) |
| 1741 | if (bcon->flags & CON_BOOT) |
| 1742 | unregister_console(bcon); |
| 1743 | } else { |
| 1744 | printk(KERN_INFO "%sconsole [%s%d] enabled\n", |
| 1745 | (newcon->flags & CON_BOOT) ? "boot" : "" , |
| 1746 | newcon->name, newcon->index); |
| 1747 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1748 | } |
| 1749 | EXPORT_SYMBOL(register_console); |
| 1750 | |
Jesper Juhl | 40dc565 | 2005-10-30 15:02:46 -0800 | [diff] [blame] | 1751 | int unregister_console(struct console *console) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1752 | { |
Jesper Juhl | 40dc565 | 2005-10-30 15:02:46 -0800 | [diff] [blame] | 1753 | struct console *a, *b; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1754 | int res = 1; |
| 1755 | |
Samuel Thibault | f7511d5 | 2008-04-30 00:54:51 -0700 | [diff] [blame] | 1756 | #ifdef CONFIG_A11Y_BRAILLE_CONSOLE |
| 1757 | if (console->flags & CON_BRL) |
| 1758 | return braille_unregister_console(console); |
| 1759 | #endif |
| 1760 | |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1761 | console_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1762 | if (console_drivers == console) { |
| 1763 | console_drivers=console->next; |
| 1764 | res = 0; |
Benjamin Herrenschmidt | e9b15b5 | 2005-11-23 13:37:44 -0800 | [diff] [blame] | 1765 | } else if (console_drivers) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1766 | for (a=console_drivers->next, b=console_drivers ; |
| 1767 | a; b=a, a=b->next) { |
| 1768 | if (a == console) { |
| 1769 | b->next = a->next; |
| 1770 | res = 0; |
| 1771 | break; |
Jesper Juhl | 40dc565 | 2005-10-30 15:02:46 -0800 | [diff] [blame] | 1772 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1773 | } |
| 1774 | } |
Jesper Juhl | 40dc565 | 2005-10-30 15:02:46 -0800 | [diff] [blame] | 1775 | |
Gerd Hoffmann | 69331af | 2007-05-08 00:26:49 -0700 | [diff] [blame] | 1776 | /* |
Greg Edwards | ab4af03 | 2005-06-23 00:09:05 -0700 | [diff] [blame] | 1777 | * If this isn't the last console and it has CON_CONSDEV set, we |
| 1778 | * need to set it on the next preferred console. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1779 | */ |
Gerd Hoffmann | 69331af | 2007-05-08 00:26:49 -0700 | [diff] [blame] | 1780 | if (console_drivers != NULL && console->flags & CON_CONSDEV) |
Greg Edwards | ab4af03 | 2005-06-23 00:09:05 -0700 | [diff] [blame] | 1781 | console_drivers->flags |= CON_CONSDEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1782 | |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1783 | console_unlock(); |
Kay Sievers | fbc92a3 | 2010-12-01 18:51:05 +0100 | [diff] [blame] | 1784 | console_sysfs_notify(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1785 | return res; |
| 1786 | } |
| 1787 | EXPORT_SYMBOL(unregister_console); |
Matt Mackall | d59745c | 2005-05-01 08:59:02 -0700 | [diff] [blame] | 1788 | |
Kevin Cernekee | 034260d | 2010-06-03 22:11:25 -0700 | [diff] [blame] | 1789 | static int __init printk_late_init(void) |
Robin Getz | 0c5564b | 2007-08-20 15:22:47 -0400 | [diff] [blame] | 1790 | { |
Robin Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 1791 | struct console *con; |
| 1792 | |
| 1793 | for_each_console(con) { |
Nishanth Aravamudan | 4c30c6f | 2011-08-25 15:59:11 -0700 | [diff] [blame] | 1794 | if (!keep_bootcon && con->flags & CON_BOOT) { |
Robin Getz | cb00e99 | 2007-08-21 23:14:58 -0400 | [diff] [blame] | 1795 | printk(KERN_INFO "turn off boot console %s%d\n", |
Robin Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 1796 | con->name, con->index); |
Sonic Zhang | 42c2c8c | 2009-08-06 15:58:11 -0700 | [diff] [blame] | 1797 | unregister_console(con); |
Robin Getz | cb00e99 | 2007-08-21 23:14:58 -0400 | [diff] [blame] | 1798 | } |
Robin Getz | 0c5564b | 2007-08-20 15:22:47 -0400 | [diff] [blame] | 1799 | } |
Kevin Cernekee | 034260d | 2010-06-03 22:11:25 -0700 | [diff] [blame] | 1800 | hotcpu_notifier(console_cpu_notify, 0); |
Robin Getz | 0c5564b | 2007-08-20 15:22:47 -0400 | [diff] [blame] | 1801 | return 0; |
| 1802 | } |
Kevin Cernekee | 034260d | 2010-06-03 22:11:25 -0700 | [diff] [blame] | 1803 | late_initcall(printk_late_init); |
Robin Getz | 0c5564b | 2007-08-20 15:22:47 -0400 | [diff] [blame] | 1804 | |
Joe Perches | 7ef3d2f | 2008-02-08 04:21:25 -0800 | [diff] [blame] | 1805 | #if defined CONFIG_PRINTK |
Dave Young | 717115e | 2008-07-25 01:45:58 -0700 | [diff] [blame] | 1806 | |
Peter Zijlstra | 600e145 | 2012-03-15 12:35:37 +0100 | [diff] [blame] | 1807 | int printk_sched(const char *fmt, ...) |
| 1808 | { |
| 1809 | unsigned long flags; |
| 1810 | va_list args; |
| 1811 | char *buf; |
| 1812 | int r; |
| 1813 | |
| 1814 | local_irq_save(flags); |
| 1815 | buf = __get_cpu_var(printk_sched_buf); |
| 1816 | |
| 1817 | va_start(args, fmt); |
| 1818 | r = vsnprintf(buf, PRINTK_BUF_SIZE, fmt, args); |
| 1819 | va_end(args); |
| 1820 | |
| 1821 | __this_cpu_or(printk_pending, PRINTK_PENDING_SCHED); |
| 1822 | local_irq_restore(flags); |
| 1823 | |
| 1824 | return r; |
| 1825 | } |
| 1826 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1827 | /* |
| 1828 | * printk rate limiting, lifted from the networking subsystem. |
| 1829 | * |
Uwe Kleine-König | 641de9d | 2008-07-29 22:33:38 -0700 | [diff] [blame] | 1830 | * This enforces a rate limit: not more than 10 kernel messages |
| 1831 | * every 5s to make a denial-of-service attack impossible. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1832 | */ |
Uwe Kleine-König | 641de9d | 2008-07-29 22:33:38 -0700 | [diff] [blame] | 1833 | DEFINE_RATELIMIT_STATE(printk_ratelimit_state, 5 * HZ, 10); |
| 1834 | |
Christian Borntraeger | 5c82871 | 2009-10-23 14:58:11 +0200 | [diff] [blame] | 1835 | int __printk_ratelimit(const char *func) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1836 | { |
Christian Borntraeger | 5c82871 | 2009-10-23 14:58:11 +0200 | [diff] [blame] | 1837 | return ___ratelimit(&printk_ratelimit_state, func); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1838 | } |
Christian Borntraeger | 5c82871 | 2009-10-23 14:58:11 +0200 | [diff] [blame] | 1839 | EXPORT_SYMBOL(__printk_ratelimit); |
Andrew Morton | f46c483 | 2006-11-02 22:07:16 -0800 | [diff] [blame] | 1840 | |
| 1841 | /** |
| 1842 | * printk_timed_ratelimit - caller-controlled printk ratelimiting |
| 1843 | * @caller_jiffies: pointer to caller's state |
| 1844 | * @interval_msecs: minimum interval between prints |
| 1845 | * |
| 1846 | * printk_timed_ratelimit() returns true if more than @interval_msecs |
| 1847 | * milliseconds have elapsed since the last time printk_timed_ratelimit() |
| 1848 | * returned true. |
| 1849 | */ |
| 1850 | bool printk_timed_ratelimit(unsigned long *caller_jiffies, |
| 1851 | unsigned int interval_msecs) |
| 1852 | { |
Guillaume Knispel | f2d28a2 | 2009-03-17 16:18:42 +0100 | [diff] [blame] | 1853 | if (*caller_jiffies == 0 |
| 1854 | || !time_in_range(jiffies, *caller_jiffies, |
| 1855 | *caller_jiffies |
| 1856 | + msecs_to_jiffies(interval_msecs))) { |
| 1857 | *caller_jiffies = jiffies; |
Andrew Morton | f46c483 | 2006-11-02 22:07:16 -0800 | [diff] [blame] | 1858 | return true; |
| 1859 | } |
| 1860 | return false; |
| 1861 | } |
| 1862 | EXPORT_SYMBOL(printk_timed_ratelimit); |
Simon Kagstrom | 456b565 | 2009-10-16 14:09:18 +0200 | [diff] [blame] | 1863 | |
| 1864 | static DEFINE_SPINLOCK(dump_list_lock); |
| 1865 | static LIST_HEAD(dump_list); |
| 1866 | |
| 1867 | /** |
| 1868 | * kmsg_dump_register - register a kernel log dumper. |
Randy Dunlap | 6485536 | 2009-12-17 15:27:27 -0800 | [diff] [blame] | 1869 | * @dumper: pointer to the kmsg_dumper structure |
Simon Kagstrom | 456b565 | 2009-10-16 14:09:18 +0200 | [diff] [blame] | 1870 | * |
| 1871 | * Adds a kernel log dumper to the system. The dump callback in the |
| 1872 | * structure will be called when the kernel oopses or panics and must be |
| 1873 | * set. Returns zero on success and %-EINVAL or %-EBUSY otherwise. |
| 1874 | */ |
| 1875 | int kmsg_dump_register(struct kmsg_dumper *dumper) |
| 1876 | { |
| 1877 | unsigned long flags; |
| 1878 | int err = -EBUSY; |
| 1879 | |
| 1880 | /* The dump callback needs to be set */ |
| 1881 | if (!dumper->dump) |
| 1882 | return -EINVAL; |
| 1883 | |
| 1884 | spin_lock_irqsave(&dump_list_lock, flags); |
| 1885 | /* Don't allow registering multiple times */ |
| 1886 | if (!dumper->registered) { |
| 1887 | dumper->registered = 1; |
Huang Ying | fb842b0 | 2011-01-12 16:59:43 -0800 | [diff] [blame] | 1888 | list_add_tail_rcu(&dumper->list, &dump_list); |
Simon Kagstrom | 456b565 | 2009-10-16 14:09:18 +0200 | [diff] [blame] | 1889 | err = 0; |
| 1890 | } |
| 1891 | spin_unlock_irqrestore(&dump_list_lock, flags); |
| 1892 | |
| 1893 | return err; |
| 1894 | } |
| 1895 | EXPORT_SYMBOL_GPL(kmsg_dump_register); |
| 1896 | |
| 1897 | /** |
| 1898 | * kmsg_dump_unregister - unregister a kmsg dumper. |
Randy Dunlap | 6485536 | 2009-12-17 15:27:27 -0800 | [diff] [blame] | 1899 | * @dumper: pointer to the kmsg_dumper structure |
Simon Kagstrom | 456b565 | 2009-10-16 14:09:18 +0200 | [diff] [blame] | 1900 | * |
| 1901 | * Removes a dump device from the system. Returns zero on success and |
| 1902 | * %-EINVAL otherwise. |
| 1903 | */ |
| 1904 | int kmsg_dump_unregister(struct kmsg_dumper *dumper) |
| 1905 | { |
| 1906 | unsigned long flags; |
| 1907 | int err = -EINVAL; |
| 1908 | |
| 1909 | spin_lock_irqsave(&dump_list_lock, flags); |
| 1910 | if (dumper->registered) { |
| 1911 | dumper->registered = 0; |
Huang Ying | fb842b0 | 2011-01-12 16:59:43 -0800 | [diff] [blame] | 1912 | list_del_rcu(&dumper->list); |
Simon Kagstrom | 456b565 | 2009-10-16 14:09:18 +0200 | [diff] [blame] | 1913 | err = 0; |
| 1914 | } |
| 1915 | spin_unlock_irqrestore(&dump_list_lock, flags); |
Huang Ying | fb842b0 | 2011-01-12 16:59:43 -0800 | [diff] [blame] | 1916 | synchronize_rcu(); |
Simon Kagstrom | 456b565 | 2009-10-16 14:09:18 +0200 | [diff] [blame] | 1917 | |
| 1918 | return err; |
| 1919 | } |
| 1920 | EXPORT_SYMBOL_GPL(kmsg_dump_unregister); |
| 1921 | |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame^] | 1922 | static bool always_kmsg_dump; |
| 1923 | module_param_named(always_kmsg_dump, always_kmsg_dump, bool, S_IRUGO | S_IWUSR); |
| 1924 | |
Simon Kagstrom | 456b565 | 2009-10-16 14:09:18 +0200 | [diff] [blame] | 1925 | /** |
| 1926 | * kmsg_dump - dump kernel log to kernel message dumpers. |
| 1927 | * @reason: the reason (oops, panic etc) for dumping |
| 1928 | * |
| 1929 | * Iterate through each of the dump devices and call the oops/panic |
| 1930 | * callbacks with the log buffer. |
| 1931 | */ |
| 1932 | void kmsg_dump(enum kmsg_dump_reason reason) |
| 1933 | { |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame^] | 1934 | u64 idx; |
Simon Kagstrom | 456b565 | 2009-10-16 14:09:18 +0200 | [diff] [blame] | 1935 | struct kmsg_dumper *dumper; |
| 1936 | const char *s1, *s2; |
| 1937 | unsigned long l1, l2; |
| 1938 | unsigned long flags; |
| 1939 | |
Matthew Garrett | c22ab33 | 2012-03-05 14:59:10 -0800 | [diff] [blame] | 1940 | if ((reason > KMSG_DUMP_OOPS) && !always_kmsg_dump) |
| 1941 | return; |
| 1942 | |
Simon Kagstrom | 456b565 | 2009-10-16 14:09:18 +0200 | [diff] [blame] | 1943 | /* Theoretically, the log could move on after we do this, but |
| 1944 | there's not a lot we can do about that. The new messages |
| 1945 | will overwrite the start of what we dump. */ |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame^] | 1946 | |
Thomas Gleixner | 07354eb | 2009-07-25 17:50:36 +0200 | [diff] [blame] | 1947 | raw_spin_lock_irqsave(&logbuf_lock, flags); |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame^] | 1948 | if (syslog_seq < log_first_seq) |
| 1949 | idx = syslog_idx; |
| 1950 | else |
| 1951 | idx = log_first_idx; |
Simon Kagstrom | 456b565 | 2009-10-16 14:09:18 +0200 | [diff] [blame] | 1952 | |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame^] | 1953 | if (idx > log_next_idx) { |
| 1954 | s1 = log_buf; |
| 1955 | l1 = log_next_idx; |
Simon Kagstrom | 456b565 | 2009-10-16 14:09:18 +0200 | [diff] [blame] | 1956 | |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame^] | 1957 | s2 = log_buf + idx; |
| 1958 | l2 = log_buf_len - idx; |
Simon Kagstrom | 456b565 | 2009-10-16 14:09:18 +0200 | [diff] [blame] | 1959 | } else { |
| 1960 | s1 = ""; |
| 1961 | l1 = 0; |
| 1962 | |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame^] | 1963 | s2 = log_buf + idx; |
| 1964 | l2 = log_next_idx - idx; |
Simon Kagstrom | 456b565 | 2009-10-16 14:09:18 +0200 | [diff] [blame] | 1965 | } |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame^] | 1966 | raw_spin_unlock_irqrestore(&logbuf_lock, flags); |
Simon Kagstrom | 456b565 | 2009-10-16 14:09:18 +0200 | [diff] [blame] | 1967 | |
Huang Ying | fb842b0 | 2011-01-12 16:59:43 -0800 | [diff] [blame] | 1968 | rcu_read_lock(); |
| 1969 | list_for_each_entry_rcu(dumper, &dump_list, list) |
Simon Kagstrom | 456b565 | 2009-10-16 14:09:18 +0200 | [diff] [blame] | 1970 | dumper->dump(dumper, reason, s1, l1, s2, l2); |
Huang Ying | fb842b0 | 2011-01-12 16:59:43 -0800 | [diff] [blame] | 1971 | rcu_read_unlock(); |
Simon Kagstrom | 456b565 | 2009-10-16 14:09:18 +0200 | [diff] [blame] | 1972 | } |
Joe Perches | 7ef3d2f | 2008-02-08 04:21:25 -0800 | [diff] [blame] | 1973 | #endif |