ia64/linux-2.6.18-xen.hg

annotate kernel/printk.c @ 912:dd42cdb0ab89

[IA64] Build blktap2 driver by default in x86 builds.

add CONFIG_XEN_BLKDEV_TAP2=y to buildconfigs/linux-defconfig_xen_ia64.

Signed-off-by: Isaku Yamahata <yamahata@valinux.co.jp>
author Isaku Yamahata <yamahata@valinux.co.jp>
date Mon Jun 29 12:09:16 2009 +0900 (2009-06-29)
parents 831230e53067
children
rev   line source
ian@0 1 /*
ian@0 2 * linux/kernel/printk.c
ian@0 3 *
ian@0 4 * Copyright (C) 1991, 1992 Linus Torvalds
ian@0 5 *
ian@0 6 * Modified to make sys_syslog() more flexible: added commands to
ian@0 7 * return the last 4k of kernel messages, regardless of whether
ian@0 8 * they've been read or not. Added option to suppress kernel printk's
ian@0 9 * to the console. Added hook for sending the console messages
ian@0 10 * elsewhere, in preparation for a serial line console (someday).
ian@0 11 * Ted Ts'o, 2/11/93.
ian@0 12 * Modified for sysctl support, 1/8/97, Chris Horn.
ian@0 13 * Fixed SMP synchronization, 08/08/99, Manfred Spraul
ian@0 14 * manfred@colorfullife.com
ian@0 15 * Rewrote bits to get rid of console_lock
ian@0 16 * 01Mar01 Andrew Morton <andrewm@uow.edu.au>
ian@0 17 */
ian@0 18
ian@0 19 #include <linux/kernel.h>
ian@0 20 #include <linux/mm.h>
ian@0 21 #include <linux/tty.h>
ian@0 22 #include <linux/tty_driver.h>
ian@0 23 #include <linux/smp_lock.h>
ian@0 24 #include <linux/console.h>
ian@0 25 #include <linux/init.h>
ian@0 26 #include <linux/module.h>
ian@0 27 #include <linux/moduleparam.h>
ian@0 28 #include <linux/interrupt.h> /* For in_interrupt() */
ian@0 29 #include <linux/delay.h>
ian@0 30 #include <linux/smp.h>
ian@0 31 #include <linux/security.h>
ian@0 32 #include <linux/bootmem.h>
ian@0 33 #include <linux/syscalls.h>
ian@0 34
ian@0 35 #include <asm/uaccess.h>
ian@0 36
ian@0 37 #define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT)
ian@0 38
ian@0 39 /* printk's without a loglevel use this.. */
ian@0 40 #define DEFAULT_MESSAGE_LOGLEVEL 4 /* KERN_WARNING */
ian@0 41
ian@0 42 /* We show everything that is MORE important than this.. */
ian@0 43 #define MINIMUM_CONSOLE_LOGLEVEL 1 /* Minimum loglevel we let people use */
ian@0 44 #define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */
ian@0 45
ian@0 46 DECLARE_WAIT_QUEUE_HEAD(log_wait);
ian@0 47
ian@0 48 int console_printk[4] = {
ian@0 49 DEFAULT_CONSOLE_LOGLEVEL, /* console_loglevel */
ian@0 50 DEFAULT_MESSAGE_LOGLEVEL, /* default_message_loglevel */
ian@0 51 MINIMUM_CONSOLE_LOGLEVEL, /* minimum_console_loglevel */
ian@0 52 DEFAULT_CONSOLE_LOGLEVEL, /* default_console_loglevel */
ian@0 53 };
ian@0 54
ian@0 55 EXPORT_UNUSED_SYMBOL(console_printk); /* June 2006 */
ian@0 56
ian@0 57 /*
ian@0 58 * Low lever drivers may need that to know if they can schedule in
ian@0 59 * their unblank() callback or not. So let's export it.
ian@0 60 */
ian@0 61 int oops_in_progress;
ian@0 62 EXPORT_SYMBOL(oops_in_progress);
ian@0 63
ian@0 64 /*
ian@0 65 * console_sem protects the console_drivers list, and also
ian@0 66 * provides serialisation for access to the entire console
ian@0 67 * driver system.
ian@0 68 */
ian@0 69 static DECLARE_MUTEX(console_sem);
ian@0 70 static DECLARE_MUTEX(secondary_console_sem);
ian@0 71 struct console *console_drivers;
ian@0 72 /*
ian@0 73 * This is used for debugging the mess that is the VT code by
ian@0 74 * keeping track if we have the console semaphore held. It's
ian@0 75 * definitely not the perfect debug tool (we don't know if _WE_
ian@0 76 * hold it are racing, but it helps tracking those weird code
ian@0 77 * path in the console code where we end up in places I want
ian@0 78 * locked without the console sempahore held
ian@0 79 */
ian@0 80 static int console_locked, console_suspended;
ian@0 81
ian@0 82 /*
ian@0 83 * logbuf_lock protects log_buf, log_start, log_end, con_start and logged_chars
ian@0 84 * It is also used in interesting ways to provide interlocking in
ian@0 85 * release_console_sem().
ian@0 86 */
ian@0 87 static DEFINE_SPINLOCK(logbuf_lock);
ian@0 88
ian@0 89 #define LOG_BUF_MASK (log_buf_len-1)
ian@0 90 #define LOG_BUF(idx) (log_buf[(idx) & LOG_BUF_MASK])
ian@0 91
ian@0 92 /*
ian@0 93 * The indices into log_buf are not constrained to log_buf_len - they
ian@0 94 * must be masked before subscripting
ian@0 95 */
ian@0 96 static unsigned long log_start; /* Index into log_buf: next char to be read by syslog() */
ian@0 97 static unsigned long con_start; /* Index into log_buf: next char to be sent to consoles */
ian@0 98 static unsigned long log_end; /* Index into log_buf: most-recently-written-char + 1 */
ian@0 99
ian@0 100 /*
ian@0 101 * Array of consoles built from command line options (console=)
ian@0 102 */
ian@0 103 struct console_cmdline
ian@0 104 {
ian@0 105 char name[8]; /* Name of the driver */
ian@0 106 int index; /* Minor dev. to use */
ian@0 107 char *options; /* Options for the driver */
ian@0 108 };
ian@0 109
ian@0 110 #define MAX_CMDLINECONSOLES 8
ian@0 111
ian@0 112 static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
ian@0 113 static int selected_console = -1;
ian@0 114 static int preferred_console = -1;
ian@0 115
ian@0 116 /* Flag: console code may call schedule() */
ian@0 117 static int console_may_schedule;
ian@0 118
ian@0 119 #ifdef CONFIG_PRINTK
ian@0 120
ian@0 121 static char __log_buf[__LOG_BUF_LEN];
ian@0 122 static char *log_buf = __log_buf;
ian@0 123 static int log_buf_len = __LOG_BUF_LEN;
ian@0 124 static unsigned long logged_chars; /* Number of chars produced since last read+clear operation */
ian@0 125
ian@0 126 static int __init log_buf_len_setup(char *str)
ian@0 127 {
ian@0 128 unsigned long size = memparse(str, &str);
ian@0 129 unsigned long flags;
ian@0 130
ian@0 131 if (size)
ian@0 132 size = roundup_pow_of_two(size);
ian@0 133 if (size > log_buf_len) {
ian@0 134 unsigned long start, dest_idx, offset;
ian@0 135 char *new_log_buf;
ian@0 136
ian@0 137 new_log_buf = alloc_bootmem(size);
ian@0 138 if (!new_log_buf) {
ian@0 139 printk(KERN_WARNING "log_buf_len: allocation failed\n");
ian@0 140 goto out;
ian@0 141 }
ian@0 142
ian@0 143 spin_lock_irqsave(&logbuf_lock, flags);
ian@0 144 log_buf_len = size;
ian@0 145 log_buf = new_log_buf;
ian@0 146
ian@0 147 offset = start = min(con_start, log_start);
ian@0 148 dest_idx = 0;
ian@0 149 while (start != log_end) {
ian@0 150 log_buf[dest_idx] = __log_buf[start & (__LOG_BUF_LEN - 1)];
ian@0 151 start++;
ian@0 152 dest_idx++;
ian@0 153 }
ian@0 154 log_start -= offset;
ian@0 155 con_start -= offset;
ian@0 156 log_end -= offset;
ian@0 157 spin_unlock_irqrestore(&logbuf_lock, flags);
ian@0 158
ian@0 159 printk(KERN_NOTICE "log_buf_len: %d\n", log_buf_len);
ian@0 160 }
ian@0 161 out:
ian@0 162 return 1;
ian@0 163 }
ian@0 164
ian@0 165 __setup("log_buf_len=", log_buf_len_setup);
ian@0 166
ian@0 167 /*
ian@0 168 * Commands to do_syslog:
ian@0 169 *
ian@0 170 * 0 -- Close the log. Currently a NOP.
ian@0 171 * 1 -- Open the log. Currently a NOP.
ian@0 172 * 2 -- Read from the log.
ian@0 173 * 3 -- Read all messages remaining in the ring buffer.
ian@0 174 * 4 -- Read and clear all messages remaining in the ring buffer
ian@0 175 * 5 -- Clear ring buffer.
ian@0 176 * 6 -- Disable printk's to console
ian@0 177 * 7 -- Enable printk's to console
ian@0 178 * 8 -- Set level of messages printed to console
ian@0 179 * 9 -- Return number of unread characters in the log buffer
ian@0 180 * 10 -- Return size of the log buffer
ian@0 181 */
ian@0 182 int do_syslog(int type, char __user *buf, int len)
ian@0 183 {
ian@0 184 unsigned long i, j, limit, count;
ian@0 185 int do_clear = 0;
ian@0 186 char c;
ian@0 187 int error = 0;
ian@0 188
ian@0 189 error = security_syslog(type);
ian@0 190 if (error)
ian@0 191 return error;
ian@0 192
ian@0 193 switch (type) {
ian@0 194 case 0: /* Close log */
ian@0 195 break;
ian@0 196 case 1: /* Open log */
ian@0 197 break;
ian@0 198 case 2: /* Read from log */
ian@0 199 error = -EINVAL;
ian@0 200 if (!buf || len < 0)
ian@0 201 goto out;
ian@0 202 error = 0;
ian@0 203 if (!len)
ian@0 204 goto out;
ian@0 205 if (!access_ok(VERIFY_WRITE, buf, len)) {
ian@0 206 error = -EFAULT;
ian@0 207 goto out;
ian@0 208 }
ian@0 209 error = wait_event_interruptible(log_wait,
ian@0 210 (log_start - log_end));
ian@0 211 if (error)
ian@0 212 goto out;
ian@0 213 i = 0;
ian@0 214 spin_lock_irq(&logbuf_lock);
ian@0 215 while (!error && (log_start != log_end) && i < len) {
ian@0 216 c = LOG_BUF(log_start);
ian@0 217 log_start++;
ian@0 218 spin_unlock_irq(&logbuf_lock);
ian@0 219 error = __put_user(c,buf);
ian@0 220 buf++;
ian@0 221 i++;
ian@0 222 cond_resched();
ian@0 223 spin_lock_irq(&logbuf_lock);
ian@0 224 }
ian@0 225 spin_unlock_irq(&logbuf_lock);
ian@0 226 if (!error)
ian@0 227 error = i;
ian@0 228 break;
ian@0 229 case 4: /* Read/clear last kernel messages */
ian@0 230 do_clear = 1;
ian@0 231 /* FALL THRU */
ian@0 232 case 3: /* Read last kernel messages */
ian@0 233 error = -EINVAL;
ian@0 234 if (!buf || len < 0)
ian@0 235 goto out;
ian@0 236 error = 0;
ian@0 237 if (!len)
ian@0 238 goto out;
ian@0 239 if (!access_ok(VERIFY_WRITE, buf, len)) {
ian@0 240 error = -EFAULT;
ian@0 241 goto out;
ian@0 242 }
ian@0 243 count = len;
ian@0 244 if (count > log_buf_len)
ian@0 245 count = log_buf_len;
ian@0 246 spin_lock_irq(&logbuf_lock);
ian@0 247 if (count > logged_chars)
ian@0 248 count = logged_chars;
ian@0 249 if (do_clear)
ian@0 250 logged_chars = 0;
ian@0 251 limit = log_end;
ian@0 252 /*
ian@0 253 * __put_user() could sleep, and while we sleep
ian@0 254 * printk() could overwrite the messages
ian@0 255 * we try to copy to user space. Therefore
ian@0 256 * the messages are copied in reverse. <manfreds>
ian@0 257 */
ian@0 258 for (i = 0; i < count && !error; i++) {
ian@0 259 j = limit-1-i;
ian@0 260 if (j + log_buf_len < log_end)
ian@0 261 break;
ian@0 262 c = LOG_BUF(j);
ian@0 263 spin_unlock_irq(&logbuf_lock);
ian@0 264 error = __put_user(c,&buf[count-1-i]);
ian@0 265 cond_resched();
ian@0 266 spin_lock_irq(&logbuf_lock);
ian@0 267 }
ian@0 268 spin_unlock_irq(&logbuf_lock);
ian@0 269 if (error)
ian@0 270 break;
ian@0 271 error = i;
ian@0 272 if (i != count) {
ian@0 273 int offset = count-error;
ian@0 274 /* buffer overflow during copy, correct user buffer. */
ian@0 275 for (i = 0; i < error; i++) {
ian@0 276 if (__get_user(c,&buf[i+offset]) ||
ian@0 277 __put_user(c,&buf[i])) {
ian@0 278 error = -EFAULT;
ian@0 279 break;
ian@0 280 }
ian@0 281 cond_resched();
ian@0 282 }
ian@0 283 }
ian@0 284 break;
ian@0 285 case 5: /* Clear ring buffer */
ian@0 286 logged_chars = 0;
ian@0 287 break;
ian@0 288 case 6: /* Disable logging to console */
ian@0 289 console_loglevel = minimum_console_loglevel;
ian@0 290 break;
ian@0 291 case 7: /* Enable logging to console */
ian@0 292 console_loglevel = default_console_loglevel;
ian@0 293 break;
ian@0 294 case 8: /* Set level of messages printed to console */
ian@0 295 error = -EINVAL;
ian@0 296 if (len < 1 || len > 8)
ian@0 297 goto out;
ian@0 298 if (len < minimum_console_loglevel)
ian@0 299 len = minimum_console_loglevel;
ian@0 300 console_loglevel = len;
ian@0 301 error = 0;
ian@0 302 break;
ian@0 303 case 9: /* Number of chars in the log buffer */
ian@0 304 error = log_end - log_start;
ian@0 305 break;
ian@0 306 case 10: /* Size of the log buffer */
ian@0 307 error = log_buf_len;
ian@0 308 break;
ian@0 309 default:
ian@0 310 error = -EINVAL;
ian@0 311 break;
ian@0 312 }
ian@0 313 out:
ian@0 314 return error;
ian@0 315 }
ian@0 316
ian@0 317 asmlinkage long sys_syslog(int type, char __user *buf, int len)
ian@0 318 {
ian@0 319 return do_syslog(type, buf, len);
ian@0 320 }
ian@0 321
ian@0 322 /*
ian@0 323 * Call the console drivers on a range of log_buf
ian@0 324 */
ian@0 325 static void __call_console_drivers(unsigned long start, unsigned long end)
ian@0 326 {
ian@0 327 struct console *con;
ian@0 328
ian@0 329 for (con = console_drivers; con; con = con->next) {
ian@0 330 if ((con->flags & CON_ENABLED) && con->write &&
ian@0 331 (cpu_online(smp_processor_id()) ||
ian@0 332 (con->flags & CON_ANYTIME)))
ian@0 333 con->write(con, &LOG_BUF(start), end - start);
ian@0 334 }
ian@0 335 }
ian@0 336
ian@0 337 /*
ian@0 338 * Write out chars from start to end - 1 inclusive
ian@0 339 */
ian@0 340 static void _call_console_drivers(unsigned long start,
ian@0 341 unsigned long end, int msg_log_level)
ian@0 342 {
ian@0 343 if (msg_log_level < console_loglevel &&
ian@0 344 console_drivers && start != end) {
ian@0 345 if ((start & LOG_BUF_MASK) > (end & LOG_BUF_MASK)) {
ian@0 346 /* wrapped write */
ian@0 347 __call_console_drivers(start & LOG_BUF_MASK,
ian@0 348 log_buf_len);
ian@0 349 __call_console_drivers(0, end & LOG_BUF_MASK);
ian@0 350 } else {
ian@0 351 __call_console_drivers(start, end);
ian@0 352 }
ian@0 353 }
ian@0 354 }
ian@0 355
ian@0 356 /*
ian@0 357 * Call the console drivers, asking them to write out
ian@0 358 * log_buf[start] to log_buf[end - 1].
ian@0 359 * The console_sem must be held.
ian@0 360 */
ian@0 361 static void call_console_drivers(unsigned long start, unsigned long end)
ian@0 362 {
ian@0 363 unsigned long cur_index, start_print;
ian@0 364 static int msg_level = -1;
ian@0 365
ian@0 366 BUG_ON(((long)(start - end)) > 0);
ian@0 367
ian@0 368 cur_index = start;
ian@0 369 start_print = start;
ian@0 370 while (cur_index != end) {
ian@0 371 if (msg_level < 0 && ((end - cur_index) > 2) &&
ian@0 372 LOG_BUF(cur_index + 0) == '<' &&
ian@0 373 LOG_BUF(cur_index + 1) >= '0' &&
ian@0 374 LOG_BUF(cur_index + 1) <= '7' &&
ian@0 375 LOG_BUF(cur_index + 2) == '>') {
ian@0 376 msg_level = LOG_BUF(cur_index + 1) - '0';
ian@0 377 cur_index += 3;
ian@0 378 start_print = cur_index;
ian@0 379 }
ian@0 380 while (cur_index != end) {
ian@0 381 char c = LOG_BUF(cur_index);
ian@0 382
ian@0 383 cur_index++;
ian@0 384 if (c == '\n') {
ian@0 385 if (msg_level < 0) {
ian@0 386 /*
ian@0 387 * printk() has already given us loglevel tags in
ian@0 388 * the buffer. This code is here in case the
ian@0 389 * log buffer has wrapped right round and scribbled
ian@0 390 * on those tags
ian@0 391 */
ian@0 392 msg_level = default_message_loglevel;
ian@0 393 }
ian@0 394 _call_console_drivers(start_print, cur_index, msg_level);
ian@0 395 msg_level = -1;
ian@0 396 start_print = cur_index;
ian@0 397 break;
ian@0 398 }
ian@0 399 }
ian@0 400 }
ian@0 401 _call_console_drivers(start_print, end, msg_level);
ian@0 402 }
ian@0 403
ian@0 404 static void emit_log_char(char c)
ian@0 405 {
ian@0 406 LOG_BUF(log_end) = c;
ian@0 407 log_end++;
ian@0 408 if (log_end - log_start > log_buf_len)
ian@0 409 log_start = log_end - log_buf_len;
ian@0 410 if (log_end - con_start > log_buf_len)
ian@0 411 con_start = log_end - log_buf_len;
ian@0 412 if (logged_chars < log_buf_len)
ian@0 413 logged_chars++;
ian@0 414 }
ian@0 415
ian@0 416 /*
ian@0 417 * Zap console related locks when oopsing. Only zap at most once
ian@0 418 * every 10 seconds, to leave time for slow consoles to print a
ian@0 419 * full oops.
ian@0 420 */
ian@0 421 static void zap_locks(void)
ian@0 422 {
ian@0 423 static unsigned long oops_timestamp;
ian@0 424
ian@0 425 if (time_after_eq(jiffies, oops_timestamp) &&
ian@0 426 !time_after(jiffies, oops_timestamp + 30 * HZ))
ian@0 427 return;
ian@0 428
ian@0 429 oops_timestamp = jiffies;
ian@0 430
ian@0 431 /* If a crash is occurring, make sure we can't deadlock */
ian@0 432 spin_lock_init(&logbuf_lock);
ian@0 433 /* And make sure that we print immediately */
ian@0 434 init_MUTEX(&console_sem);
ian@0 435 }
ian@0 436
ian@0 437 #if defined(CONFIG_PRINTK_TIME)
ian@0 438 static int printk_time = 1;
ian@0 439 #else
ian@0 440 static int printk_time = 0;
ian@0 441 #endif
ian@0 442 module_param(printk_time, int, S_IRUGO | S_IWUSR);
ian@0 443
ian@0 444 static int __init printk_time_setup(char *str)
ian@0 445 {
ian@0 446 if (*str)
ian@0 447 return 0;
ian@0 448 printk_time = 1;
ian@0 449 return 1;
ian@0 450 }
ian@0 451
ian@0 452 __setup("time", printk_time_setup);
ian@0 453
ian@0 454 __attribute__((weak)) unsigned long long printk_clock(void)
ian@0 455 {
ian@0 456 return sched_clock();
ian@0 457 }
ian@0 458
ian@0 459 /* Check if we have any console registered that can be called early in boot. */
ian@0 460 static int have_callable_console(void)
ian@0 461 {
ian@0 462 struct console *con;
ian@0 463
ian@0 464 for (con = console_drivers; con; con = con->next)
ian@0 465 if (con->flags & CON_ANYTIME)
ian@0 466 return 1;
ian@0 467
ian@0 468 return 0;
ian@0 469 }
ian@0 470
ian@0 471 /**
ian@0 472 * printk - print a kernel message
ian@0 473 * @fmt: format string
ian@0 474 *
ian@0 475 * This is printk. It can be called from any context. We want it to work.
ian@0 476 *
ian@0 477 * We try to grab the console_sem. If we succeed, it's easy - we log the output and
ian@0 478 * call the console drivers. If we fail to get the semaphore we place the output
ian@0 479 * into the log buffer and return. The current holder of the console_sem will
ian@0 480 * notice the new output in release_console_sem() and will send it to the
ian@0 481 * consoles before releasing the semaphore.
ian@0 482 *
ian@0 483 * One effect of this deferred printing is that code which calls printk() and
ian@0 484 * then changes console_loglevel may break. This is because console_loglevel
ian@0 485 * is inspected when the actual printing occurs.
ian@0 486 *
ian@0 487 * See also:
ian@0 488 * printf(3)
ian@0 489 */
ian@0 490
ian@0 491 asmlinkage int printk(const char *fmt, ...)
ian@0 492 {
ian@0 493 va_list args;
ian@0 494 int r;
ian@0 495
ian@0 496 va_start(args, fmt);
ian@0 497 r = vprintk(fmt, args);
ian@0 498 va_end(args);
ian@0 499
ian@0 500 return r;
ian@0 501 }
ian@0 502
ian@0 503 /* cpu currently holding logbuf_lock */
ian@0 504 static volatile unsigned int printk_cpu = UINT_MAX;
ian@0 505
ian@0 506 asmlinkage int vprintk(const char *fmt, va_list args)
ian@0 507 {
ian@0 508 unsigned long flags;
ian@0 509 int printed_len;
ian@0 510 char *p;
ian@0 511 static char printk_buf[1024];
ian@0 512 static int log_level_unknown = 1;
ian@0 513
ian@0 514 preempt_disable();
ian@0 515 if (unlikely(oops_in_progress) && printk_cpu == smp_processor_id())
ian@0 516 /* If a crash is occurring during printk() on this CPU,
ian@0 517 * make sure we can't deadlock */
ian@0 518 zap_locks();
ian@0 519
ian@0 520 /* This stops the holder of console_sem just where we want him */
ian@0 521 local_irq_save(flags);
ian@0 522 lockdep_off();
ian@0 523 spin_lock(&logbuf_lock);
ian@0 524 printk_cpu = smp_processor_id();
ian@0 525
ian@0 526 /* Emit the output into the temporary buffer */
ian@0 527 printed_len = vscnprintf(printk_buf, sizeof(printk_buf), fmt, args);
ian@0 528
ian@0 529 /*
ian@0 530 * Copy the output into log_buf. If the caller didn't provide
ian@0 531 * appropriate log level tags, we insert them here
ian@0 532 */
ian@0 533 for (p = printk_buf; *p; p++) {
ian@0 534 if (log_level_unknown) {
ian@0 535 /* log_level_unknown signals the start of a new line */
ian@0 536 if (printk_time) {
ian@0 537 int loglev_char;
ian@0 538 char tbuf[50], *tp;
ian@0 539 unsigned tlen;
ian@0 540 unsigned long long t;
ian@0 541 unsigned long nanosec_rem;
ian@0 542
ian@0 543 /*
ian@0 544 * force the log level token to be
ian@0 545 * before the time output.
ian@0 546 */
ian@0 547 if (p[0] == '<' && p[1] >='0' &&
ian@0 548 p[1] <= '7' && p[2] == '>') {
ian@0 549 loglev_char = p[1];
ian@0 550 p += 3;
ian@0 551 printed_len -= 3;
ian@0 552 } else {
ian@0 553 loglev_char = default_message_loglevel
ian@0 554 + '0';
ian@0 555 }
ian@0 556 t = printk_clock();
ian@0 557 nanosec_rem = do_div(t, 1000000000);
ian@0 558 tlen = sprintf(tbuf,
ian@0 559 "<%c>[%5lu.%06lu] ",
ian@0 560 loglev_char,
ian@0 561 (unsigned long)t,
ian@0 562 nanosec_rem/1000);
ian@0 563
ian@0 564 for (tp = tbuf; tp < tbuf + tlen; tp++)
ian@0 565 emit_log_char(*tp);
ian@0 566 printed_len += tlen;
ian@0 567 } else {
ian@0 568 if (p[0] != '<' || p[1] < '0' ||
ian@0 569 p[1] > '7' || p[2] != '>') {
ian@0 570 emit_log_char('<');
ian@0 571 emit_log_char(default_message_loglevel
ian@0 572 + '0');
ian@0 573 emit_log_char('>');
ian@0 574 printed_len += 3;
ian@0 575 }
ian@0 576 }
ian@0 577 log_level_unknown = 0;
ian@0 578 if (!*p)
ian@0 579 break;
ian@0 580 }
ian@0 581 emit_log_char(*p);
ian@0 582 if (*p == '\n')
ian@0 583 log_level_unknown = 1;
ian@0 584 }
ian@0 585
ian@0 586 if (!down_trylock(&console_sem)) {
ian@0 587 /*
ian@0 588 * We own the drivers. We can drop the spinlock and
ian@0 589 * let release_console_sem() print the text, maybe ...
ian@0 590 */
ian@0 591 console_locked = 1;
ian@0 592 printk_cpu = UINT_MAX;
ian@0 593 spin_unlock(&logbuf_lock);
ian@0 594
ian@0 595 /*
ian@0 596 * Console drivers may assume that per-cpu resources have
ian@0 597 * been allocated. So unless they're explicitly marked as
ian@0 598 * being able to cope (CON_ANYTIME) don't call them until
ian@0 599 * this CPU is officially up.
ian@0 600 */
ian@0 601 if (cpu_online(smp_processor_id()) || have_callable_console()) {
ian@0 602 console_may_schedule = 0;
ian@0 603 release_console_sem();
ian@0 604 } else {
ian@0 605 /* Release by hand to avoid flushing the buffer. */
ian@0 606 console_locked = 0;
ian@0 607 up(&console_sem);
ian@0 608 }
ian@0 609 lockdep_on();
ian@0 610 local_irq_restore(flags);
ian@0 611 } else {
ian@0 612 /*
ian@0 613 * Someone else owns the drivers. We drop the spinlock, which
ian@0 614 * allows the semaphore holder to proceed and to call the
ian@0 615 * console drivers with the output which we just produced.
ian@0 616 */
ian@0 617 printk_cpu = UINT_MAX;
ian@0 618 spin_unlock(&logbuf_lock);
ian@0 619 lockdep_on();
ian@0 620 local_irq_restore(flags);
ian@0 621 }
ian@0 622
ian@0 623 preempt_enable();
ian@0 624 return printed_len;
ian@0 625 }
ian@0 626 EXPORT_SYMBOL(printk);
ian@0 627 EXPORT_SYMBOL(vprintk);
ian@0 628
ian@0 629 #else
ian@0 630
ian@0 631 asmlinkage long sys_syslog(int type, char __user *buf, int len)
ian@0 632 {
ian@0 633 return 0;
ian@0 634 }
ian@0 635
ian@0 636 int do_syslog(int type, char __user *buf, int len)
ian@0 637 {
ian@0 638 return 0;
ian@0 639 }
ian@0 640
ian@0 641 static void call_console_drivers(unsigned long start, unsigned long end)
ian@0 642 {
ian@0 643 }
ian@0 644
ian@0 645 #endif
ian@0 646
ian@0 647 /*
ian@0 648 * Set up a list of consoles. Called from init/main.c
ian@0 649 */
ian@0 650 static int __init console_setup(char *str)
ian@0 651 {
ian@0 652 char name[sizeof(console_cmdline[0].name)];
ian@0 653 char *s, *options;
ian@0 654 int idx;
ian@0 655
ian@0 656 /*
ian@0 657 * Decode str into name, index, options.
ian@0 658 */
ian@0 659 if (str[0] >= '0' && str[0] <= '9') {
ian@0 660 strcpy(name, "ttyS");
ian@0 661 strncpy(name + 4, str, sizeof(name) - 5);
ian@0 662 } else {
ian@0 663 strncpy(name, str, sizeof(name) - 1);
ian@0 664 }
ian@0 665 name[sizeof(name) - 1] = 0;
ian@0 666 if ((options = strchr(str, ',')) != NULL)
ian@0 667 *(options++) = 0;
ian@0 668 #ifdef __sparc__
ian@0 669 if (!strcmp(str, "ttya"))
ian@0 670 strcpy(name, "ttyS0");
ian@0 671 if (!strcmp(str, "ttyb"))
ian@0 672 strcpy(name, "ttyS1");
ian@0 673 #endif
ian@0 674 for (s = name; *s; s++)
ian@0 675 if ((*s >= '0' && *s <= '9') || *s == ',')
ian@0 676 break;
ian@0 677 idx = simple_strtoul(s, NULL, 10);
ian@0 678 *s = 0;
ian@0 679
ian@0 680 add_preferred_console(name, idx, options);
ian@0 681 return 1;
ian@0 682 }
ian@0 683 __setup("console=", console_setup);
ian@0 684
ian@0 685 /**
ian@0 686 * add_preferred_console - add a device to the list of preferred consoles.
ian@0 687 * @name: device name
ian@0 688 * @idx: device index
ian@0 689 * @options: options for this console
ian@0 690 *
ian@0 691 * The last preferred console added will be used for kernel messages
ian@0 692 * and stdin/out/err for init. Normally this is used by console_setup
ian@0 693 * above to handle user-supplied console arguments; however it can also
ian@0 694 * be used by arch-specific code either to override the user or more
ian@0 695 * commonly to provide a default console (ie from PROM variables) when
ian@0 696 * the user has not supplied one.
ian@0 697 */
ian@0 698 int __init add_preferred_console(char *name, int idx, char *options)
ian@0 699 {
ian@0 700 struct console_cmdline *c;
ian@0 701 int i;
ian@0 702
ian@0 703 /*
ian@0 704 * See if this tty is not yet registered, and
ian@0 705 * if we have a slot free.
ian@0 706 */
ian@0 707 for(i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
ian@0 708 if (strcmp(console_cmdline[i].name, name) == 0 &&
ian@0 709 console_cmdline[i].index == idx) {
ian@0 710 selected_console = i;
ian@0 711 return 0;
ian@0 712 }
ian@0 713 if (i == MAX_CMDLINECONSOLES)
ian@0 714 return -E2BIG;
ian@0 715 selected_console = i;
ian@0 716 c = &console_cmdline[i];
ian@0 717 memcpy(c->name, name, sizeof(c->name));
ian@0 718 c->name[sizeof(c->name) - 1] = 0;
ian@0 719 c->options = options;
ian@0 720 c->index = idx;
ian@0 721 return 0;
ian@0 722 }
ian@0 723
ian@0 724 /**
ian@0 725 * suspend_console - suspend the console subsystem
ian@0 726 *
ian@0 727 * This disables printk() while we go into suspend states
ian@0 728 */
ian@0 729 void suspend_console(void)
ian@0 730 {
ian@0 731 acquire_console_sem();
ian@0 732 console_suspended = 1;
ian@0 733 }
ian@0 734
ian@0 735 void resume_console(void)
ian@0 736 {
ian@0 737 console_suspended = 0;
ian@0 738 release_console_sem();
ian@0 739 }
ian@0 740
ian@0 741 /**
ian@0 742 * acquire_console_sem - lock the console system for exclusive use.
ian@0 743 *
ian@0 744 * Acquires a semaphore which guarantees that the caller has
ian@0 745 * exclusive access to the console system and the console_drivers list.
ian@0 746 *
ian@0 747 * Can sleep, returns nothing.
ian@0 748 */
ian@0 749 void acquire_console_sem(void)
ian@0 750 {
ian@0 751 BUG_ON(in_interrupt());
ian@0 752 if (console_suspended) {
ian@0 753 down(&secondary_console_sem);
ian@0 754 return;
ian@0 755 }
ian@0 756 down(&console_sem);
ian@0 757 console_locked = 1;
ian@0 758 console_may_schedule = 1;
ian@0 759 }
ian@0 760 EXPORT_SYMBOL(acquire_console_sem);
ian@0 761
ian@0 762 int try_acquire_console_sem(void)
ian@0 763 {
ian@0 764 if (down_trylock(&console_sem))
ian@0 765 return -1;
ian@0 766 console_locked = 1;
ian@0 767 console_may_schedule = 0;
ian@0 768 return 0;
ian@0 769 }
ian@0 770 EXPORT_SYMBOL(try_acquire_console_sem);
ian@0 771
ian@0 772 int is_console_locked(void)
ian@0 773 {
ian@0 774 return console_locked;
ian@0 775 }
ian@0 776 EXPORT_UNUSED_SYMBOL(is_console_locked); /* June 2006 */
ian@0 777
ian@0 778 /**
ian@0 779 * release_console_sem - unlock the console system
ian@0 780 *
ian@0 781 * Releases the semaphore which the caller holds on the console system
ian@0 782 * and the console driver list.
ian@0 783 *
ian@0 784 * While the semaphore was held, console output may have been buffered
ian@0 785 * by printk(). If this is the case, release_console_sem() emits
ian@0 786 * the output prior to releasing the semaphore.
ian@0 787 *
ian@0 788 * If there is output waiting for klogd, we wake it up.
ian@0 789 *
ian@0 790 * release_console_sem() may be called from any context.
ian@0 791 */
ian@0 792 void release_console_sem(void)
ian@0 793 {
ian@0 794 unsigned long flags;
ian@0 795 unsigned long _con_start, _log_end;
ian@0 796 unsigned long wake_klogd = 0;
ian@0 797
ian@0 798 if (console_suspended) {
ian@0 799 up(&secondary_console_sem);
ian@0 800 return;
ian@0 801 }
ian@0 802
ian@0 803 console_may_schedule = 0;
ian@0 804
ian@0 805 for ( ; ; ) {
ian@0 806 spin_lock_irqsave(&logbuf_lock, flags);
ian@0 807 wake_klogd |= log_start - log_end;
ian@0 808 if (con_start == log_end)
ian@0 809 break; /* Nothing to print */
ian@0 810 _con_start = con_start;
ian@0 811 _log_end = log_end;
ian@0 812 con_start = log_end; /* Flush */
ian@0 813 spin_unlock(&logbuf_lock);
ian@0 814 call_console_drivers(_con_start, _log_end);
ian@0 815 local_irq_restore(flags);
ian@0 816 }
ian@0 817 console_locked = 0;
ian@0 818 up(&console_sem);
ian@0 819 spin_unlock_irqrestore(&logbuf_lock, flags);
ian@0 820 if (wake_klogd && !oops_in_progress && waitqueue_active(&log_wait)) {
ian@0 821 /*
ian@0 822 * If we printk from within the lock dependency code,
ian@0 823 * from within the scheduler code, then do not lock
ian@0 824 * up due to self-recursion:
ian@0 825 */
ian@0 826 if (!lockdep_internal())
ian@0 827 wake_up_interruptible(&log_wait);
ian@0 828 }
ian@0 829 }
ian@0 830 EXPORT_SYMBOL(release_console_sem);
ian@0 831
ian@0 832 /**
ian@0 833 * console_conditional_schedule - yield the CPU if required
ian@0 834 *
ian@0 835 * If the console code is currently allowed to sleep, and
ian@0 836 * if this CPU should yield the CPU to another task, do
ian@0 837 * so here.
ian@0 838 *
ian@0 839 * Must be called within acquire_console_sem().
ian@0 840 */
ian@0 841 void __sched console_conditional_schedule(void)
ian@0 842 {
ian@0 843 if (console_may_schedule)
ian@0 844 cond_resched();
ian@0 845 }
ian@0 846 EXPORT_SYMBOL(console_conditional_schedule);
ian@0 847
ian@0 848 void console_print(const char *s)
ian@0 849 {
ian@0 850 printk(KERN_EMERG "%s", s);
ian@0 851 }
ian@0 852 EXPORT_SYMBOL(console_print);
ian@0 853
ian@0 854 void console_unblank(void)
ian@0 855 {
ian@0 856 struct console *c;
ian@0 857
ian@0 858 /*
ian@0 859 * console_unblank can no longer be called in interrupt context unless
ian@0 860 * oops_in_progress is set to 1..
ian@0 861 */
ian@0 862 if (oops_in_progress) {
ian@0 863 if (down_trylock(&console_sem) != 0)
ian@0 864 return;
ian@0 865 } else
ian@0 866 acquire_console_sem();
ian@0 867
ian@0 868 console_locked = 1;
ian@0 869 console_may_schedule = 0;
ian@0 870 for (c = console_drivers; c != NULL; c = c->next)
ian@0 871 if ((c->flags & CON_ENABLED) && c->unblank)
ian@0 872 c->unblank();
ian@0 873 release_console_sem();
ian@0 874 }
ian@0 875
ian@0 876 /*
ian@0 877 * Return the console tty driver structure and its associated index
ian@0 878 */
ian@0 879 struct tty_driver *console_device(int *index)
ian@0 880 {
ian@0 881 struct console *c;
ian@0 882 struct tty_driver *driver = NULL;
ian@0 883
ian@0 884 acquire_console_sem();
ian@0 885 for (c = console_drivers; c != NULL; c = c->next) {
ian@0 886 if (!c->device)
ian@0 887 continue;
ian@0 888 driver = c->device(c, index);
ian@0 889 if (driver)
ian@0 890 break;
ian@0 891 }
ian@0 892 release_console_sem();
ian@0 893 return driver;
ian@0 894 }
ian@0 895
ian@0 896 /*
ian@0 897 * Prevent further output on the passed console device so that (for example)
ian@0 898 * serial drivers can disable console output before suspending a port, and can
ian@0 899 * re-enable output afterwards.
ian@0 900 */
ian@0 901 void console_stop(struct console *console)
ian@0 902 {
ian@0 903 acquire_console_sem();
ian@0 904 console->flags &= ~CON_ENABLED;
ian@0 905 release_console_sem();
ian@0 906 }
ian@0 907 EXPORT_SYMBOL(console_stop);
ian@0 908
ian@0 909 void console_start(struct console *console)
ian@0 910 {
ian@0 911 acquire_console_sem();
ian@0 912 console->flags |= CON_ENABLED;
ian@0 913 release_console_sem();
ian@0 914 }
ian@0 915 EXPORT_SYMBOL(console_start);
ian@0 916
ian@0 917 /*
ian@0 918 * The console driver calls this routine during kernel initialization
ian@0 919 * to register the console printing procedure with printk() and to
ian@0 920 * print any messages that were printed by the kernel before the
ian@0 921 * console driver was initialized.
ian@0 922 */
ian@0 923 void register_console(struct console *console)
ian@0 924 {
ian@0 925 int i;
ian@0 926 unsigned long flags;
ian@0 927
ian@0 928 if (preferred_console < 0)
ian@0 929 preferred_console = selected_console;
ian@0 930
ian@0 931 /*
ian@0 932 * See if we want to use this console driver. If we
ian@0 933 * didn't select a console we take the first one
ian@0 934 * that registers here.
ian@0 935 */
ian@0 936 if (preferred_console < 0) {
ian@0 937 if (console->index < 0)
ian@0 938 console->index = 0;
ian@0 939 if (console->setup == NULL ||
ian@0 940 console->setup(console, NULL) == 0) {
ian@0 941 console->flags |= CON_ENABLED | CON_CONSDEV;
ian@0 942 preferred_console = 0;
ian@0 943 }
ian@0 944 }
ian@0 945
ian@0 946 /*
ian@0 947 * See if this console matches one we selected on
ian@0 948 * the command line.
ian@0 949 */
ian@0 950 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0];
ian@0 951 i++) {
ian@0 952 if (strcmp(console_cmdline[i].name, console->name) != 0)
ian@0 953 continue;
ian@0 954 if (console->index >= 0 &&
ian@0 955 console->index != console_cmdline[i].index)
ian@0 956 continue;
ian@0 957 if (console->index < 0)
ian@0 958 console->index = console_cmdline[i].index;
ian@0 959 if (console->setup &&
ian@0 960 console->setup(console, console_cmdline[i].options) != 0)
ian@0 961 break;
ian@0 962 console->flags |= CON_ENABLED;
ian@0 963 console->index = console_cmdline[i].index;
ian@0 964 if (i == selected_console) {
ian@0 965 console->flags |= CON_CONSDEV;
ian@0 966 preferred_console = selected_console;
ian@0 967 }
ian@0 968 break;
ian@0 969 }
ian@0 970
ian@0 971 if (!(console->flags & CON_ENABLED))
ian@0 972 return;
ian@0 973
ian@0 974 if (console_drivers && (console_drivers->flags & CON_BOOT)) {
ian@0 975 unregister_console(console_drivers);
ian@0 976 console->flags &= ~CON_PRINTBUFFER;
ian@0 977 }
ian@0 978
ian@0 979 /*
ian@0 980 * Put this console in the list - keep the
ian@0 981 * preferred driver at the head of the list.
ian@0 982 */
ian@0 983 acquire_console_sem();
ian@0 984 if ((console->flags & CON_CONSDEV) || console_drivers == NULL) {
ian@0 985 console->next = console_drivers;
ian@0 986 console_drivers = console;
ian@0 987 if (console->next)
ian@0 988 console->next->flags &= ~CON_CONSDEV;
ian@0 989 } else {
ian@0 990 console->next = console_drivers->next;
ian@0 991 console_drivers->next = console;
ian@0 992 }
ian@0 993 if (console->flags & CON_PRINTBUFFER) {
ian@0 994 /*
ian@0 995 * release_console_sem() will print out the buffered messages
ian@0 996 * for us.
ian@0 997 */
ian@0 998 spin_lock_irqsave(&logbuf_lock, flags);
ian@0 999 con_start = log_start;
ian@0 1000 spin_unlock_irqrestore(&logbuf_lock, flags);
ian@0 1001 }
ian@0 1002 release_console_sem();
ian@0 1003 }
ian@0 1004 EXPORT_SYMBOL(register_console);
ian@0 1005
ian@0 1006 int unregister_console(struct console *console)
ian@0 1007 {
ian@0 1008 struct console *a, *b;
ian@0 1009 int res = 1;
ian@0 1010
ian@0 1011 acquire_console_sem();
ian@0 1012 if (console_drivers == console) {
ian@0 1013 console_drivers=console->next;
ian@0 1014 res = 0;
ian@0 1015 } else if (console_drivers) {
ian@0 1016 for (a=console_drivers->next, b=console_drivers ;
ian@0 1017 a; b=a, a=b->next) {
ian@0 1018 if (a == console) {
ian@0 1019 b->next = a->next;
ian@0 1020 res = 0;
ian@0 1021 break;
ian@0 1022 }
ian@0 1023 }
ian@0 1024 }
ian@0 1025
ian@0 1026 /* If last console is removed, we re-enable picking the first
ian@0 1027 * one that gets registered. Without that, pmac early boot console
ian@0 1028 * would prevent fbcon from taking over.
ian@0 1029 *
ian@0 1030 * If this isn't the last console and it has CON_CONSDEV set, we
ian@0 1031 * need to set it on the next preferred console.
ian@0 1032 */
ian@0 1033 if (console_drivers == NULL)
ian@0 1034 preferred_console = selected_console;
ian@0 1035 else if (console->flags & CON_CONSDEV)
ian@0 1036 console_drivers->flags |= CON_CONSDEV;
ian@0 1037
ian@0 1038 release_console_sem();
ian@0 1039 return res;
ian@0 1040 }
ian@0 1041 EXPORT_SYMBOL(unregister_console);
ian@0 1042
ian@0 1043 /**
ian@0 1044 * tty_write_message - write a message to a certain tty, not just the console.
ian@0 1045 * @tty: the destination tty_struct
ian@0 1046 * @msg: the message to write
ian@0 1047 *
ian@0 1048 * This is used for messages that need to be redirected to a specific tty.
ian@0 1049 * We don't put it into the syslog queue right now maybe in the future if
ian@0 1050 * really needed.
ian@0 1051 */
ian@0 1052 void tty_write_message(struct tty_struct *tty, char *msg)
ian@0 1053 {
ian@0 1054 if (tty && tty->driver->write)
ian@0 1055 tty->driver->write(tty, msg, strlen(msg));
ian@0 1056 return;
ian@0 1057 }
ian@0 1058
ian@0 1059 /*
ian@0 1060 * printk rate limiting, lifted from the networking subsystem.
ian@0 1061 *
ian@0 1062 * This enforces a rate limit: not more than one kernel message
ian@0 1063 * every printk_ratelimit_jiffies to make a denial-of-service
ian@0 1064 * attack impossible.
ian@0 1065 */
ian@0 1066 int __printk_ratelimit(int ratelimit_jiffies, int ratelimit_burst)
ian@0 1067 {
ian@0 1068 static DEFINE_SPINLOCK(ratelimit_lock);
ian@0 1069 static unsigned long toks = 10 * 5 * HZ;
ian@0 1070 static unsigned long last_msg;
ian@0 1071 static int missed;
ian@0 1072 unsigned long flags;
ian@0 1073 unsigned long now = jiffies;
ian@0 1074
ian@0 1075 spin_lock_irqsave(&ratelimit_lock, flags);
ian@0 1076 toks += now - last_msg;
ian@0 1077 last_msg = now;
ian@0 1078 if (toks > (ratelimit_burst * ratelimit_jiffies))
ian@0 1079 toks = ratelimit_burst * ratelimit_jiffies;
ian@0 1080 if (toks >= ratelimit_jiffies) {
ian@0 1081 int lost = missed;
ian@0 1082
ian@0 1083 missed = 0;
ian@0 1084 toks -= ratelimit_jiffies;
ian@0 1085 spin_unlock_irqrestore(&ratelimit_lock, flags);
ian@0 1086 if (lost)
ian@0 1087 printk(KERN_WARNING "printk: %d messages suppressed.\n", lost);
ian@0 1088 return 1;
ian@0 1089 }
ian@0 1090 missed++;
ian@0 1091 spin_unlock_irqrestore(&ratelimit_lock, flags);
ian@0 1092 return 0;
ian@0 1093 }
ian@0 1094 EXPORT_SYMBOL(__printk_ratelimit);
ian@0 1095
ian@0 1096 /* minimum time in jiffies between messages */
ian@0 1097 int printk_ratelimit_jiffies = 5 * HZ;
ian@0 1098
ian@0 1099 /* number of messages we send before ratelimiting */
ian@0 1100 int printk_ratelimit_burst = 10;
ian@0 1101
ian@0 1102 int printk_ratelimit(void)
ian@0 1103 {
ian@0 1104 return __printk_ratelimit(printk_ratelimit_jiffies,
ian@0 1105 printk_ratelimit_burst);
ian@0 1106 }
ian@0 1107 EXPORT_SYMBOL(printk_ratelimit);