printk.c 25 KB

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