printk.c 28 KB

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