printk.c 28 KB

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