printk.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  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/config.h>
  29. #include <linux/delay.h>
  30. #include <linux/smp.h>
  31. #include <linux/security.h>
  32. #include <linux/bootmem.h>
  33. #include <linux/syscalls.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_SYMBOL(console_printk);
  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. spin_lock_irqsave(&logbuf_lock, flags);
  466. printk_cpu = smp_processor_id();
  467. /* Emit the output into the temporary buffer */
  468. printed_len = vscnprintf(printk_buf, sizeof(printk_buf), fmt, args);
  469. /*
  470. * Copy the output into log_buf. If the caller didn't provide
  471. * appropriate log level tags, we insert them here
  472. */
  473. for (p = printk_buf; *p; p++) {
  474. if (log_level_unknown) {
  475. /* log_level_unknown signals the start of a new line */
  476. if (printk_time) {
  477. int loglev_char;
  478. char tbuf[50], *tp;
  479. unsigned tlen;
  480. unsigned long long t;
  481. unsigned long nanosec_rem;
  482. /*
  483. * force the log level token to be
  484. * before the time output.
  485. */
  486. if (p[0] == '<' && p[1] >='0' &&
  487. p[1] <= '7' && p[2] == '>') {
  488. loglev_char = p[1];
  489. p += 3;
  490. printed_len -= 3;
  491. } else {
  492. loglev_char = default_message_loglevel
  493. + '0';
  494. }
  495. t = printk_clock();
  496. nanosec_rem = do_div(t, 1000000000);
  497. tlen = sprintf(tbuf,
  498. "<%c>[%5lu.%06lu] ",
  499. loglev_char,
  500. (unsigned long)t,
  501. nanosec_rem/1000);
  502. for (tp = tbuf; tp < tbuf + tlen; tp++)
  503. emit_log_char(*tp);
  504. printed_len += tlen;
  505. } else {
  506. if (p[0] != '<' || p[1] < '0' ||
  507. p[1] > '7' || p[2] != '>') {
  508. emit_log_char('<');
  509. emit_log_char(default_message_loglevel
  510. + '0');
  511. emit_log_char('>');
  512. printed_len += 3;
  513. }
  514. }
  515. log_level_unknown = 0;
  516. if (!*p)
  517. break;
  518. }
  519. emit_log_char(*p);
  520. if (*p == '\n')
  521. log_level_unknown = 1;
  522. }
  523. if (!down_trylock(&console_sem)) {
  524. /*
  525. * We own the drivers. We can drop the spinlock and
  526. * let release_console_sem() print the text, maybe ...
  527. */
  528. console_locked = 1;
  529. printk_cpu = UINT_MAX;
  530. spin_unlock_irqrestore(&logbuf_lock, flags);
  531. /*
  532. * Console drivers may assume that per-cpu resources have
  533. * been allocated. So unless they're explicitly marked as
  534. * being able to cope (CON_ANYTIME) don't call them until
  535. * this CPU is officially up.
  536. */
  537. if (cpu_online(smp_processor_id()) || have_callable_console()) {
  538. console_may_schedule = 0;
  539. release_console_sem();
  540. } else {
  541. /* Release by hand to avoid flushing the buffer. */
  542. console_locked = 0;
  543. up(&console_sem);
  544. }
  545. } else {
  546. /*
  547. * Someone else owns the drivers. We drop the spinlock, which
  548. * allows the semaphore holder to proceed and to call the
  549. * console drivers with the output which we just produced.
  550. */
  551. printk_cpu = UINT_MAX;
  552. spin_unlock_irqrestore(&logbuf_lock, flags);
  553. }
  554. preempt_enable();
  555. return printed_len;
  556. }
  557. EXPORT_SYMBOL(printk);
  558. EXPORT_SYMBOL(vprintk);
  559. #else
  560. asmlinkage long sys_syslog(int type, char __user *buf, int len)
  561. {
  562. return 0;
  563. }
  564. int do_syslog(int type, char __user *buf, int len)
  565. {
  566. return 0;
  567. }
  568. static void call_console_drivers(unsigned long start, unsigned long end)
  569. {
  570. }
  571. #endif
  572. /*
  573. * Set up a list of consoles. Called from init/main.c
  574. */
  575. static int __init console_setup(char *str)
  576. {
  577. char name[sizeof(console_cmdline[0].name)];
  578. char *s, *options;
  579. int idx;
  580. /*
  581. * Decode str into name, index, options.
  582. */
  583. if (str[0] >= '0' && str[0] <= '9') {
  584. strcpy(name, "ttyS");
  585. strncpy(name + 4, str, sizeof(name) - 5);
  586. } else {
  587. strncpy(name, str, sizeof(name) - 1);
  588. }
  589. name[sizeof(name) - 1] = 0;
  590. if ((options = strchr(str, ',')) != NULL)
  591. *(options++) = 0;
  592. #ifdef __sparc__
  593. if (!strcmp(str, "ttya"))
  594. strcpy(name, "ttyS0");
  595. if (!strcmp(str, "ttyb"))
  596. strcpy(name, "ttyS1");
  597. #endif
  598. for (s = name; *s; s++)
  599. if ((*s >= '0' && *s <= '9') || *s == ',')
  600. break;
  601. idx = simple_strtoul(s, NULL, 10);
  602. *s = 0;
  603. add_preferred_console(name, idx, options);
  604. return 1;
  605. }
  606. __setup("console=", console_setup);
  607. /**
  608. * add_preferred_console - add a device to the list of preferred consoles.
  609. * @name: device name
  610. * @idx: device index
  611. * @options: options for this console
  612. *
  613. * The last preferred console added will be used for kernel messages
  614. * and stdin/out/err for init. Normally this is used by console_setup
  615. * above to handle user-supplied console arguments; however it can also
  616. * be used by arch-specific code either to override the user or more
  617. * commonly to provide a default console (ie from PROM variables) when
  618. * the user has not supplied one.
  619. */
  620. int __init add_preferred_console(char *name, int idx, char *options)
  621. {
  622. struct console_cmdline *c;
  623. int i;
  624. /*
  625. * See if this tty is not yet registered, and
  626. * if we have a slot free.
  627. */
  628. for(i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
  629. if (strcmp(console_cmdline[i].name, name) == 0 &&
  630. console_cmdline[i].index == idx) {
  631. selected_console = i;
  632. return 0;
  633. }
  634. if (i == MAX_CMDLINECONSOLES)
  635. return -E2BIG;
  636. selected_console = i;
  637. c = &console_cmdline[i];
  638. memcpy(c->name, name, sizeof(c->name));
  639. c->name[sizeof(c->name) - 1] = 0;
  640. c->options = options;
  641. c->index = idx;
  642. return 0;
  643. }
  644. /**
  645. * suspend_console - suspend the console subsystem
  646. *
  647. * This disables printk() while we go into suspend states
  648. */
  649. void suspend_console(void)
  650. {
  651. acquire_console_sem();
  652. console_suspended = 1;
  653. }
  654. void resume_console(void)
  655. {
  656. console_suspended = 0;
  657. release_console_sem();
  658. }
  659. /**
  660. * acquire_console_sem - lock the console system for exclusive use.
  661. *
  662. * Acquires a semaphore which guarantees that the caller has
  663. * exclusive access to the console system and the console_drivers list.
  664. *
  665. * Can sleep, returns nothing.
  666. */
  667. void acquire_console_sem(void)
  668. {
  669. BUG_ON(in_interrupt());
  670. if (console_suspended) {
  671. down(&secondary_console_sem);
  672. return;
  673. }
  674. down(&console_sem);
  675. console_locked = 1;
  676. console_may_schedule = 1;
  677. }
  678. EXPORT_SYMBOL(acquire_console_sem);
  679. int try_acquire_console_sem(void)
  680. {
  681. if (down_trylock(&console_sem))
  682. return -1;
  683. console_locked = 1;
  684. console_may_schedule = 0;
  685. return 0;
  686. }
  687. EXPORT_SYMBOL(try_acquire_console_sem);
  688. int is_console_locked(void)
  689. {
  690. return console_locked;
  691. }
  692. EXPORT_SYMBOL(is_console_locked);
  693. /**
  694. * release_console_sem - unlock the console system
  695. *
  696. * Releases the semaphore which the caller holds on the console system
  697. * and the console driver list.
  698. *
  699. * While the semaphore was held, console output may have been buffered
  700. * by printk(). If this is the case, release_console_sem() emits
  701. * the output prior to releasing the semaphore.
  702. *
  703. * If there is output waiting for klogd, we wake it up.
  704. *
  705. * release_console_sem() may be called from any context.
  706. */
  707. void release_console_sem(void)
  708. {
  709. unsigned long flags;
  710. unsigned long _con_start, _log_end;
  711. unsigned long wake_klogd = 0;
  712. if (console_suspended) {
  713. up(&secondary_console_sem);
  714. return;
  715. }
  716. for ( ; ; ) {
  717. spin_lock_irqsave(&logbuf_lock, flags);
  718. wake_klogd |= log_start - log_end;
  719. if (con_start == log_end)
  720. break; /* Nothing to print */
  721. _con_start = con_start;
  722. _log_end = log_end;
  723. con_start = log_end; /* Flush */
  724. spin_unlock(&logbuf_lock);
  725. call_console_drivers(_con_start, _log_end);
  726. local_irq_restore(flags);
  727. }
  728. console_locked = 0;
  729. console_may_schedule = 0;
  730. up(&console_sem);
  731. spin_unlock_irqrestore(&logbuf_lock, flags);
  732. if (wake_klogd && !oops_in_progress && waitqueue_active(&log_wait))
  733. wake_up_interruptible(&log_wait);
  734. }
  735. EXPORT_SYMBOL(release_console_sem);
  736. /**
  737. * console_conditional_schedule - yield the CPU if required
  738. *
  739. * If the console code is currently allowed to sleep, and
  740. * if this CPU should yield the CPU to another task, do
  741. * so here.
  742. *
  743. * Must be called within acquire_console_sem().
  744. */
  745. void __sched console_conditional_schedule(void)
  746. {
  747. if (console_may_schedule)
  748. cond_resched();
  749. }
  750. EXPORT_SYMBOL(console_conditional_schedule);
  751. void console_print(const char *s)
  752. {
  753. printk(KERN_EMERG "%s", s);
  754. }
  755. EXPORT_SYMBOL(console_print);
  756. void console_unblank(void)
  757. {
  758. struct console *c;
  759. /*
  760. * console_unblank can no longer be called in interrupt context unless
  761. * oops_in_progress is set to 1..
  762. */
  763. if (oops_in_progress) {
  764. if (down_trylock(&console_sem) != 0)
  765. return;
  766. } else
  767. acquire_console_sem();
  768. console_locked = 1;
  769. console_may_schedule = 0;
  770. for (c = console_drivers; c != NULL; c = c->next)
  771. if ((c->flags & CON_ENABLED) && c->unblank)
  772. c->unblank();
  773. release_console_sem();
  774. }
  775. /*
  776. * Return the console tty driver structure and its associated index
  777. */
  778. struct tty_driver *console_device(int *index)
  779. {
  780. struct console *c;
  781. struct tty_driver *driver = NULL;
  782. acquire_console_sem();
  783. for (c = console_drivers; c != NULL; c = c->next) {
  784. if (!c->device)
  785. continue;
  786. driver = c->device(c, index);
  787. if (driver)
  788. break;
  789. }
  790. release_console_sem();
  791. return driver;
  792. }
  793. /*
  794. * Prevent further output on the passed console device so that (for example)
  795. * serial drivers can disable console output before suspending a port, and can
  796. * re-enable output afterwards.
  797. */
  798. void console_stop(struct console *console)
  799. {
  800. acquire_console_sem();
  801. console->flags &= ~CON_ENABLED;
  802. release_console_sem();
  803. }
  804. EXPORT_SYMBOL(console_stop);
  805. void console_start(struct console *console)
  806. {
  807. acquire_console_sem();
  808. console->flags |= CON_ENABLED;
  809. release_console_sem();
  810. }
  811. EXPORT_SYMBOL(console_start);
  812. /*
  813. * The console driver calls this routine during kernel initialization
  814. * to register the console printing procedure with printk() and to
  815. * print any messages that were printed by the kernel before the
  816. * console driver was initialized.
  817. */
  818. void register_console(struct console *console)
  819. {
  820. int i;
  821. unsigned long flags;
  822. if (preferred_console < 0)
  823. preferred_console = selected_console;
  824. /*
  825. * See if we want to use this console driver. If we
  826. * didn't select a console we take the first one
  827. * that registers here.
  828. */
  829. if (preferred_console < 0) {
  830. if (console->index < 0)
  831. console->index = 0;
  832. if (console->setup == NULL ||
  833. console->setup(console, NULL) == 0) {
  834. console->flags |= CON_ENABLED | CON_CONSDEV;
  835. preferred_console = 0;
  836. }
  837. }
  838. /*
  839. * See if this console matches one we selected on
  840. * the command line.
  841. */
  842. for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0];
  843. i++) {
  844. if (strcmp(console_cmdline[i].name, console->name) != 0)
  845. continue;
  846. if (console->index >= 0 &&
  847. console->index != console_cmdline[i].index)
  848. continue;
  849. if (console->index < 0)
  850. console->index = console_cmdline[i].index;
  851. if (console->setup &&
  852. console->setup(console, console_cmdline[i].options) != 0)
  853. break;
  854. console->flags |= CON_ENABLED;
  855. console->index = console_cmdline[i].index;
  856. if (i == selected_console) {
  857. console->flags |= CON_CONSDEV;
  858. preferred_console = selected_console;
  859. }
  860. break;
  861. }
  862. if (!(console->flags & CON_ENABLED))
  863. return;
  864. if (console_drivers && (console_drivers->flags & CON_BOOT)) {
  865. unregister_console(console_drivers);
  866. console->flags &= ~CON_PRINTBUFFER;
  867. }
  868. /*
  869. * Put this console in the list - keep the
  870. * preferred driver at the head of the list.
  871. */
  872. acquire_console_sem();
  873. if ((console->flags & CON_CONSDEV) || console_drivers == NULL) {
  874. console->next = console_drivers;
  875. console_drivers = console;
  876. if (console->next)
  877. console->next->flags &= ~CON_CONSDEV;
  878. } else {
  879. console->next = console_drivers->next;
  880. console_drivers->next = console;
  881. }
  882. if (console->flags & CON_PRINTBUFFER) {
  883. /*
  884. * release_console_sem() will print out the buffered messages
  885. * for us.
  886. */
  887. spin_lock_irqsave(&logbuf_lock, flags);
  888. con_start = log_start;
  889. spin_unlock_irqrestore(&logbuf_lock, flags);
  890. }
  891. release_console_sem();
  892. }
  893. EXPORT_SYMBOL(register_console);
  894. int unregister_console(struct console *console)
  895. {
  896. struct console *a, *b;
  897. int res = 1;
  898. acquire_console_sem();
  899. if (console_drivers == console) {
  900. console_drivers=console->next;
  901. res = 0;
  902. } else if (console_drivers) {
  903. for (a=console_drivers->next, b=console_drivers ;
  904. a; b=a, a=b->next) {
  905. if (a == console) {
  906. b->next = a->next;
  907. res = 0;
  908. break;
  909. }
  910. }
  911. }
  912. /* If last console is removed, we re-enable picking the first
  913. * one that gets registered. Without that, pmac early boot console
  914. * would prevent fbcon from taking over.
  915. *
  916. * If this isn't the last console and it has CON_CONSDEV set, we
  917. * need to set it on the next preferred console.
  918. */
  919. if (console_drivers == NULL)
  920. preferred_console = selected_console;
  921. else if (console->flags & CON_CONSDEV)
  922. console_drivers->flags |= CON_CONSDEV;
  923. release_console_sem();
  924. return res;
  925. }
  926. EXPORT_SYMBOL(unregister_console);
  927. /**
  928. * tty_write_message - write a message to a certain tty, not just the console.
  929. * @tty: the destination tty_struct
  930. * @msg: the message to write
  931. *
  932. * This is used for messages that need to be redirected to a specific tty.
  933. * We don't put it into the syslog queue right now maybe in the future if
  934. * really needed.
  935. */
  936. void tty_write_message(struct tty_struct *tty, char *msg)
  937. {
  938. if (tty && tty->driver->write)
  939. tty->driver->write(tty, msg, strlen(msg));
  940. return;
  941. }
  942. /*
  943. * printk rate limiting, lifted from the networking subsystem.
  944. *
  945. * This enforces a rate limit: not more than one kernel message
  946. * every printk_ratelimit_jiffies to make a denial-of-service
  947. * attack impossible.
  948. */
  949. int __printk_ratelimit(int ratelimit_jiffies, int ratelimit_burst)
  950. {
  951. static DEFINE_SPINLOCK(ratelimit_lock);
  952. static unsigned long toks = 10 * 5 * HZ;
  953. static unsigned long last_msg;
  954. static int missed;
  955. unsigned long flags;
  956. unsigned long now = jiffies;
  957. spin_lock_irqsave(&ratelimit_lock, flags);
  958. toks += now - last_msg;
  959. last_msg = now;
  960. if (toks > (ratelimit_burst * ratelimit_jiffies))
  961. toks = ratelimit_burst * ratelimit_jiffies;
  962. if (toks >= ratelimit_jiffies) {
  963. int lost = missed;
  964. missed = 0;
  965. toks -= ratelimit_jiffies;
  966. spin_unlock_irqrestore(&ratelimit_lock, flags);
  967. if (lost)
  968. printk(KERN_WARNING "printk: %d messages suppressed.\n", lost);
  969. return 1;
  970. }
  971. missed++;
  972. spin_unlock_irqrestore(&ratelimit_lock, flags);
  973. return 0;
  974. }
  975. EXPORT_SYMBOL(__printk_ratelimit);
  976. /* minimum time in jiffies between messages */
  977. int printk_ratelimit_jiffies = 5 * HZ;
  978. /* number of messages we send before ratelimiting */
  979. int printk_ratelimit_burst = 10;
  980. int printk_ratelimit(void)
  981. {
  982. return __printk_ratelimit(printk_ratelimit_jiffies,
  983. printk_ratelimit_burst);
  984. }
  985. EXPORT_SYMBOL(printk_ratelimit);