printk.c 25 KB

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