printk.c 29 KB

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