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