printk.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  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/interrupt.h> /* For in_interrupt() */
  27. #include <linux/config.h>
  28. #include <linux/delay.h>
  29. #include <linux/smp.h>
  30. #include <linux/security.h>
  31. #include <linux/bootmem.h>
  32. #include <linux/syscalls.h>
  33. #include <asm/uaccess.h>
  34. #define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT)
  35. /* printk's without a loglevel use this.. */
  36. #define DEFAULT_MESSAGE_LOGLEVEL 4 /* KERN_WARNING */
  37. /* We show everything that is MORE important than this.. */
  38. #define MINIMUM_CONSOLE_LOGLEVEL 1 /* Minimum loglevel we let people use */
  39. #define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */
  40. DECLARE_WAIT_QUEUE_HEAD(log_wait);
  41. int console_printk[4] = {
  42. DEFAULT_CONSOLE_LOGLEVEL, /* console_loglevel */
  43. DEFAULT_MESSAGE_LOGLEVEL, /* default_message_loglevel */
  44. MINIMUM_CONSOLE_LOGLEVEL, /* minimum_console_loglevel */
  45. DEFAULT_CONSOLE_LOGLEVEL, /* default_console_loglevel */
  46. };
  47. EXPORT_SYMBOL(console_printk);
  48. /*
  49. * Low lever drivers may need that to know if they can schedule in
  50. * their unblank() callback or not. So let's export it.
  51. */
  52. int oops_in_progress;
  53. EXPORT_SYMBOL(oops_in_progress);
  54. /*
  55. * console_sem protects the console_drivers list, and also
  56. * provides serialisation for access to the entire console
  57. * driver system.
  58. */
  59. static DECLARE_MUTEX(console_sem);
  60. struct console *console_drivers;
  61. /*
  62. * This is used for debugging the mess that is the VT code by
  63. * keeping track if we have the console semaphore held. It's
  64. * definitely not the perfect debug tool (we don't know if _WE_
  65. * hold it are racing, but it helps tracking those weird code
  66. * path in the console code where we end up in places I want
  67. * locked without the console sempahore held
  68. */
  69. static int console_locked;
  70. /*
  71. * logbuf_lock protects log_buf, log_start, log_end, con_start and logged_chars
  72. * It is also used in interesting ways to provide interlocking in
  73. * release_console_sem().
  74. */
  75. static DEFINE_SPINLOCK(logbuf_lock);
  76. #define LOG_BUF_MASK (log_buf_len-1)
  77. #define LOG_BUF(idx) (log_buf[(idx) & LOG_BUF_MASK])
  78. /*
  79. * The indices into log_buf are not constrained to log_buf_len - they
  80. * must be masked before subscripting
  81. */
  82. static unsigned long log_start; /* Index into log_buf: next char to be read by syslog() */
  83. static unsigned long con_start; /* Index into log_buf: next char to be sent to consoles */
  84. static unsigned long log_end; /* Index into log_buf: most-recently-written-char + 1 */
  85. /*
  86. * Array of consoles built from command line options (console=)
  87. */
  88. struct console_cmdline
  89. {
  90. char name[8]; /* Name of the driver */
  91. int index; /* Minor dev. to use */
  92. char *options; /* Options for the driver */
  93. };
  94. #define MAX_CMDLINECONSOLES 8
  95. static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
  96. static int selected_console = -1;
  97. static int preferred_console = -1;
  98. /* Flag: console code may call schedule() */
  99. static int console_may_schedule;
  100. #ifdef CONFIG_PRINTK
  101. static char __log_buf[__LOG_BUF_LEN];
  102. static char *log_buf = __log_buf;
  103. static int log_buf_len = __LOG_BUF_LEN;
  104. static unsigned long logged_chars; /* Number of chars produced since last read+clear operation */
  105. 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. con->write(con, &LOG_BUF(start), end - start);
  299. }
  300. }
  301. /*
  302. * Write out chars from start to end - 1 inclusive
  303. */
  304. static void _call_console_drivers(unsigned long start,
  305. unsigned long end, int msg_log_level)
  306. {
  307. if (msg_log_level < console_loglevel &&
  308. console_drivers && start != end) {
  309. if ((start & LOG_BUF_MASK) > (end & LOG_BUF_MASK)) {
  310. /* wrapped write */
  311. __call_console_drivers(start & LOG_BUF_MASK,
  312. log_buf_len);
  313. __call_console_drivers(0, end & LOG_BUF_MASK);
  314. } else {
  315. __call_console_drivers(start, end);
  316. }
  317. }
  318. }
  319. /*
  320. * Call the console drivers, asking them to write out
  321. * log_buf[start] to log_buf[end - 1].
  322. * The console_sem must be held.
  323. */
  324. static void call_console_drivers(unsigned long start, unsigned long end)
  325. {
  326. unsigned long cur_index, start_print;
  327. static int msg_level = -1;
  328. if (((long)(start - end)) > 0)
  329. BUG();
  330. cur_index = start;
  331. start_print = start;
  332. while (cur_index != end) {
  333. if (msg_level < 0 && ((end - cur_index) > 2) &&
  334. LOG_BUF(cur_index + 0) == '<' &&
  335. LOG_BUF(cur_index + 1) >= '0' &&
  336. LOG_BUF(cur_index + 1) <= '7' &&
  337. LOG_BUF(cur_index + 2) == '>') {
  338. msg_level = LOG_BUF(cur_index + 1) - '0';
  339. cur_index += 3;
  340. start_print = cur_index;
  341. }
  342. while (cur_index != end) {
  343. char c = LOG_BUF(cur_index);
  344. cur_index++;
  345. if (c == '\n') {
  346. if (msg_level < 0) {
  347. /*
  348. * printk() has already given us loglevel tags in
  349. * the buffer. This code is here in case the
  350. * log buffer has wrapped right round and scribbled
  351. * on those tags
  352. */
  353. msg_level = default_message_loglevel;
  354. }
  355. _call_console_drivers(start_print, cur_index, msg_level);
  356. msg_level = -1;
  357. start_print = cur_index;
  358. break;
  359. }
  360. }
  361. }
  362. _call_console_drivers(start_print, end, msg_level);
  363. }
  364. static void emit_log_char(char c)
  365. {
  366. LOG_BUF(log_end) = c;
  367. log_end++;
  368. if (log_end - log_start > log_buf_len)
  369. log_start = log_end - log_buf_len;
  370. if (log_end - con_start > log_buf_len)
  371. con_start = log_end - log_buf_len;
  372. if (logged_chars < log_buf_len)
  373. logged_chars++;
  374. }
  375. /*
  376. * Zap console related locks when oopsing. Only zap at most once
  377. * every 10 seconds, to leave time for slow consoles to print a
  378. * full oops.
  379. */
  380. static void zap_locks(void)
  381. {
  382. static unsigned long oops_timestamp;
  383. if (time_after_eq(jiffies, oops_timestamp) &&
  384. !time_after(jiffies, oops_timestamp + 30 * HZ))
  385. return;
  386. oops_timestamp = jiffies;
  387. /* If a crash is occurring, make sure we can't deadlock */
  388. spin_lock_init(&logbuf_lock);
  389. /* And make sure that we print immediately */
  390. init_MUTEX(&console_sem);
  391. }
  392. #if defined(CONFIG_PRINTK_TIME)
  393. static int printk_time = 1;
  394. #else
  395. static int printk_time = 0;
  396. #endif
  397. static int __init printk_time_setup(char *str)
  398. {
  399. if (*str)
  400. return 0;
  401. printk_time = 1;
  402. return 1;
  403. }
  404. __setup("time", printk_time_setup);
  405. __attribute__((weak)) unsigned long long printk_clock(void)
  406. {
  407. return sched_clock();
  408. }
  409. /**
  410. * printk - print a kernel message
  411. * @fmt: format string
  412. *
  413. * This is printk. It can be called from any context. We want it to work.
  414. *
  415. * We try to grab the console_sem. If we succeed, it's easy - we log the output and
  416. * call the console drivers. If we fail to get the semaphore we place the output
  417. * into the log buffer and return. The current holder of the console_sem will
  418. * notice the new output in release_console_sem() and will send it to the
  419. * consoles before releasing the semaphore.
  420. *
  421. * One effect of this deferred printing is that code which calls printk() and
  422. * then changes console_loglevel may break. This is because console_loglevel
  423. * is inspected when the actual printing occurs.
  424. *
  425. * See also:
  426. * printf(3)
  427. */
  428. asmlinkage int printk(const char *fmt, ...)
  429. {
  430. va_list args;
  431. int r;
  432. va_start(args, fmt);
  433. r = vprintk(fmt, args);
  434. va_end(args);
  435. return r;
  436. }
  437. /* cpu currently holding logbuf_lock */
  438. static volatile unsigned int printk_cpu = UINT_MAX;
  439. asmlinkage int vprintk(const char *fmt, va_list args)
  440. {
  441. unsigned long flags;
  442. int printed_len;
  443. char *p;
  444. static char printk_buf[1024];
  445. static int log_level_unknown = 1;
  446. preempt_disable();
  447. if (unlikely(oops_in_progress) && printk_cpu == smp_processor_id())
  448. /* If a crash is occurring during printk() on this CPU,
  449. * make sure we can't deadlock */
  450. zap_locks();
  451. /* This stops the holder of console_sem just where we want him */
  452. spin_lock_irqsave(&logbuf_lock, flags);
  453. printk_cpu = smp_processor_id();
  454. /* Emit the output into the temporary buffer */
  455. printed_len = vscnprintf(printk_buf, sizeof(printk_buf), fmt, args);
  456. /*
  457. * Copy the output into log_buf. If the caller didn't provide
  458. * appropriate log level tags, we insert them here
  459. */
  460. for (p = printk_buf; *p; p++) {
  461. if (log_level_unknown) {
  462. /* log_level_unknown signals the start of a new line */
  463. if (printk_time) {
  464. int loglev_char;
  465. char tbuf[50], *tp;
  466. unsigned tlen;
  467. unsigned long long t;
  468. unsigned long nanosec_rem;
  469. /*
  470. * force the log level token to be
  471. * before the time output.
  472. */
  473. if (p[0] == '<' && p[1] >='0' &&
  474. p[1] <= '7' && p[2] == '>') {
  475. loglev_char = p[1];
  476. p += 3;
  477. printed_len -= 3;
  478. } else {
  479. loglev_char = default_message_loglevel
  480. + '0';
  481. }
  482. t = printk_clock();
  483. nanosec_rem = do_div(t, 1000000000);
  484. tlen = sprintf(tbuf,
  485. "<%c>[%5lu.%06lu] ",
  486. loglev_char,
  487. (unsigned long)t,
  488. nanosec_rem/1000);
  489. for (tp = tbuf; tp < tbuf + tlen; tp++)
  490. emit_log_char(*tp);
  491. printed_len += tlen;
  492. } else {
  493. if (p[0] != '<' || p[1] < '0' ||
  494. p[1] > '7' || p[2] != '>') {
  495. emit_log_char('<');
  496. emit_log_char(default_message_loglevel
  497. + '0');
  498. emit_log_char('>');
  499. printed_len += 3;
  500. }
  501. }
  502. log_level_unknown = 0;
  503. if (!*p)
  504. break;
  505. }
  506. emit_log_char(*p);
  507. if (*p == '\n')
  508. log_level_unknown = 1;
  509. }
  510. if (!cpu_online(smp_processor_id())) {
  511. /*
  512. * Some console drivers may assume that per-cpu resources have
  513. * been allocated. So don't allow them to be called by this
  514. * CPU until it is officially up. We shouldn't be calling into
  515. * random console drivers on a CPU which doesn't exist yet..
  516. */
  517. printk_cpu = UINT_MAX;
  518. spin_unlock_irqrestore(&logbuf_lock, flags);
  519. goto out;
  520. }
  521. if (!down_trylock(&console_sem)) {
  522. console_locked = 1;
  523. /*
  524. * We own the drivers. We can drop the spinlock and let
  525. * release_console_sem() print the text
  526. */
  527. printk_cpu = UINT_MAX;
  528. spin_unlock_irqrestore(&logbuf_lock, flags);
  529. console_may_schedule = 0;
  530. release_console_sem();
  531. } else {
  532. /*
  533. * Someone else owns the drivers. We drop the spinlock, which
  534. * allows the semaphore holder to proceed and to call the
  535. * console drivers with the output which we just produced.
  536. */
  537. printk_cpu = UINT_MAX;
  538. spin_unlock_irqrestore(&logbuf_lock, flags);
  539. }
  540. out:
  541. preempt_enable();
  542. return printed_len;
  543. }
  544. EXPORT_SYMBOL(printk);
  545. EXPORT_SYMBOL(vprintk);
  546. #else
  547. asmlinkage long sys_syslog(int type, char __user *buf, int len)
  548. {
  549. return 0;
  550. }
  551. int do_syslog(int type, char __user *buf, int len)
  552. {
  553. return 0;
  554. }
  555. static void call_console_drivers(unsigned long start, unsigned long end)
  556. {
  557. }
  558. #endif
  559. /*
  560. * Set up a list of consoles. Called from init/main.c
  561. */
  562. static int __init console_setup(char *str)
  563. {
  564. char name[sizeof(console_cmdline[0].name)];
  565. char *s, *options;
  566. int idx;
  567. /*
  568. * Decode str into name, index, options.
  569. */
  570. if (str[0] >= '0' && str[0] <= '9') {
  571. strcpy(name, "ttyS");
  572. strncpy(name + 4, str, sizeof(name) - 5);
  573. } else {
  574. strncpy(name, str, sizeof(name) - 1);
  575. }
  576. name[sizeof(name) - 1] = 0;
  577. if ((options = strchr(str, ',')) != NULL)
  578. *(options++) = 0;
  579. #ifdef __sparc__
  580. if (!strcmp(str, "ttya"))
  581. strcpy(name, "ttyS0");
  582. if (!strcmp(str, "ttyb"))
  583. strcpy(name, "ttyS1");
  584. #endif
  585. for (s = name; *s; s++)
  586. if ((*s >= '0' && *s <= '9') || *s == ',')
  587. break;
  588. idx = simple_strtoul(s, NULL, 10);
  589. *s = 0;
  590. add_preferred_console(name, idx, options);
  591. return 1;
  592. }
  593. __setup("console=", console_setup);
  594. /**
  595. * add_preferred_console - add a device to the list of preferred consoles.
  596. * @name: device name
  597. * @idx: device index
  598. * @options: options for this console
  599. *
  600. * The last preferred console added will be used for kernel messages
  601. * and stdin/out/err for init. Normally this is used by console_setup
  602. * above to handle user-supplied console arguments; however it can also
  603. * be used by arch-specific code either to override the user or more
  604. * commonly to provide a default console (ie from PROM variables) when
  605. * the user has not supplied one.
  606. */
  607. int __init add_preferred_console(char *name, int idx, char *options)
  608. {
  609. struct console_cmdline *c;
  610. int i;
  611. /*
  612. * See if this tty is not yet registered, and
  613. * if we have a slot free.
  614. */
  615. for(i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
  616. if (strcmp(console_cmdline[i].name, name) == 0 &&
  617. console_cmdline[i].index == idx) {
  618. selected_console = i;
  619. return 0;
  620. }
  621. if (i == MAX_CMDLINECONSOLES)
  622. return -E2BIG;
  623. selected_console = i;
  624. c = &console_cmdline[i];
  625. memcpy(c->name, name, sizeof(c->name));
  626. c->name[sizeof(c->name) - 1] = 0;
  627. c->options = options;
  628. c->index = idx;
  629. return 0;
  630. }
  631. /**
  632. * acquire_console_sem - lock the console system for exclusive use.
  633. *
  634. * Acquires a semaphore which guarantees that the caller has
  635. * exclusive access to the console system and the console_drivers list.
  636. *
  637. * Can sleep, returns nothing.
  638. */
  639. void acquire_console_sem(void)
  640. {
  641. if (in_interrupt())
  642. BUG();
  643. down(&console_sem);
  644. console_locked = 1;
  645. console_may_schedule = 1;
  646. }
  647. EXPORT_SYMBOL(acquire_console_sem);
  648. int try_acquire_console_sem(void)
  649. {
  650. if (down_trylock(&console_sem))
  651. return -1;
  652. console_locked = 1;
  653. console_may_schedule = 0;
  654. return 0;
  655. }
  656. EXPORT_SYMBOL(try_acquire_console_sem);
  657. int is_console_locked(void)
  658. {
  659. return console_locked;
  660. }
  661. EXPORT_SYMBOL(is_console_locked);
  662. /**
  663. * release_console_sem - unlock the console system
  664. *
  665. * Releases the semaphore which the caller holds on the console system
  666. * and the console driver list.
  667. *
  668. * While the semaphore was held, console output may have been buffered
  669. * by printk(). If this is the case, release_console_sem() emits
  670. * the output prior to releasing the semaphore.
  671. *
  672. * If there is output waiting for klogd, we wake it up.
  673. *
  674. * release_console_sem() may be called from any context.
  675. */
  676. void release_console_sem(void)
  677. {
  678. unsigned long flags;
  679. unsigned long _con_start, _log_end;
  680. unsigned long wake_klogd = 0;
  681. for ( ; ; ) {
  682. spin_lock_irqsave(&logbuf_lock, flags);
  683. wake_klogd |= log_start - log_end;
  684. if (con_start == log_end)
  685. break; /* Nothing to print */
  686. _con_start = con_start;
  687. _log_end = log_end;
  688. con_start = log_end; /* Flush */
  689. spin_unlock(&logbuf_lock);
  690. call_console_drivers(_con_start, _log_end);
  691. local_irq_restore(flags);
  692. }
  693. console_locked = 0;
  694. console_may_schedule = 0;
  695. up(&console_sem);
  696. spin_unlock_irqrestore(&logbuf_lock, flags);
  697. if (wake_klogd && !oops_in_progress && waitqueue_active(&log_wait))
  698. wake_up_interruptible(&log_wait);
  699. }
  700. EXPORT_SYMBOL(release_console_sem);
  701. /**
  702. * console_conditional_schedule - yield the CPU if required
  703. *
  704. * If the console code is currently allowed to sleep, and
  705. * if this CPU should yield the CPU to another task, do
  706. * so here.
  707. *
  708. * Must be called within acquire_console_sem().
  709. */
  710. void __sched console_conditional_schedule(void)
  711. {
  712. if (console_may_schedule)
  713. cond_resched();
  714. }
  715. EXPORT_SYMBOL(console_conditional_schedule);
  716. void console_print(const char *s)
  717. {
  718. printk(KERN_EMERG "%s", s);
  719. }
  720. EXPORT_SYMBOL(console_print);
  721. void console_unblank(void)
  722. {
  723. struct console *c;
  724. /*
  725. * console_unblank can no longer be called in interrupt context unless
  726. * oops_in_progress is set to 1..
  727. */
  728. if (oops_in_progress) {
  729. if (down_trylock(&console_sem) != 0)
  730. return;
  731. } else
  732. acquire_console_sem();
  733. console_locked = 1;
  734. console_may_schedule = 0;
  735. for (c = console_drivers; c != NULL; c = c->next)
  736. if ((c->flags & CON_ENABLED) && c->unblank)
  737. c->unblank();
  738. release_console_sem();
  739. }
  740. /*
  741. * Return the console tty driver structure and its associated index
  742. */
  743. struct tty_driver *console_device(int *index)
  744. {
  745. struct console *c;
  746. struct tty_driver *driver = NULL;
  747. acquire_console_sem();
  748. for (c = console_drivers; c != NULL; c = c->next) {
  749. if (!c->device)
  750. continue;
  751. driver = c->device(c, index);
  752. if (driver)
  753. break;
  754. }
  755. release_console_sem();
  756. return driver;
  757. }
  758. /*
  759. * Prevent further output on the passed console device so that (for example)
  760. * serial drivers can disable console output before suspending a port, and can
  761. * re-enable output afterwards.
  762. */
  763. void console_stop(struct console *console)
  764. {
  765. acquire_console_sem();
  766. console->flags &= ~CON_ENABLED;
  767. release_console_sem();
  768. }
  769. EXPORT_SYMBOL(console_stop);
  770. void console_start(struct console *console)
  771. {
  772. acquire_console_sem();
  773. console->flags |= CON_ENABLED;
  774. release_console_sem();
  775. }
  776. EXPORT_SYMBOL(console_start);
  777. /*
  778. * The console driver calls this routine during kernel initialization
  779. * to register the console printing procedure with printk() and to
  780. * print any messages that were printed by the kernel before the
  781. * console driver was initialized.
  782. */
  783. void register_console(struct console *console)
  784. {
  785. int i;
  786. unsigned long flags;
  787. if (preferred_console < 0)
  788. preferred_console = selected_console;
  789. /*
  790. * See if we want to use this console driver. If we
  791. * didn't select a console we take the first one
  792. * that registers here.
  793. */
  794. if (preferred_console < 0) {
  795. if (console->index < 0)
  796. console->index = 0;
  797. if (console->setup == NULL ||
  798. console->setup(console, NULL) == 0) {
  799. console->flags |= CON_ENABLED | CON_CONSDEV;
  800. preferred_console = 0;
  801. }
  802. }
  803. /*
  804. * See if this console matches one we selected on
  805. * the command line.
  806. */
  807. for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0];
  808. i++) {
  809. if (strcmp(console_cmdline[i].name, console->name) != 0)
  810. continue;
  811. if (console->index >= 0 &&
  812. console->index != console_cmdline[i].index)
  813. continue;
  814. if (console->index < 0)
  815. console->index = console_cmdline[i].index;
  816. if (console->setup &&
  817. console->setup(console, console_cmdline[i].options) != 0)
  818. break;
  819. console->flags |= CON_ENABLED;
  820. console->index = console_cmdline[i].index;
  821. if (i == selected_console) {
  822. console->flags |= CON_CONSDEV;
  823. preferred_console = selected_console;
  824. }
  825. break;
  826. }
  827. if (!(console->flags & CON_ENABLED))
  828. return;
  829. if (console_drivers && (console_drivers->flags & CON_BOOT)) {
  830. unregister_console(console_drivers);
  831. console->flags &= ~CON_PRINTBUFFER;
  832. }
  833. /*
  834. * Put this console in the list - keep the
  835. * preferred driver at the head of the list.
  836. */
  837. acquire_console_sem();
  838. if ((console->flags & CON_CONSDEV) || console_drivers == NULL) {
  839. console->next = console_drivers;
  840. console_drivers = console;
  841. if (console->next)
  842. console->next->flags &= ~CON_CONSDEV;
  843. } else {
  844. console->next = console_drivers->next;
  845. console_drivers->next = console;
  846. }
  847. if (console->flags & CON_PRINTBUFFER) {
  848. /*
  849. * release_console_sem() will print out the buffered messages
  850. * for us.
  851. */
  852. spin_lock_irqsave(&logbuf_lock, flags);
  853. con_start = log_start;
  854. spin_unlock_irqrestore(&logbuf_lock, flags);
  855. }
  856. release_console_sem();
  857. }
  858. EXPORT_SYMBOL(register_console);
  859. int unregister_console(struct console *console)
  860. {
  861. struct console *a, *b;
  862. int res = 1;
  863. acquire_console_sem();
  864. if (console_drivers == console) {
  865. console_drivers=console->next;
  866. res = 0;
  867. } else if (console_drivers) {
  868. for (a=console_drivers->next, b=console_drivers ;
  869. a; b=a, a=b->next) {
  870. if (a == console) {
  871. b->next = a->next;
  872. res = 0;
  873. break;
  874. }
  875. }
  876. }
  877. /* If last console is removed, we re-enable picking the first
  878. * one that gets registered. Without that, pmac early boot console
  879. * would prevent fbcon from taking over.
  880. *
  881. * If this isn't the last console and it has CON_CONSDEV set, we
  882. * need to set it on the next preferred console.
  883. */
  884. if (console_drivers == NULL)
  885. preferred_console = selected_console;
  886. else if (console->flags & CON_CONSDEV)
  887. console_drivers->flags |= CON_CONSDEV;
  888. release_console_sem();
  889. return res;
  890. }
  891. EXPORT_SYMBOL(unregister_console);
  892. /**
  893. * tty_write_message - write a message to a certain tty, not just the console.
  894. * @tty: the destination tty_struct
  895. * @msg: the message to write
  896. *
  897. * This is used for messages that need to be redirected to a specific tty.
  898. * We don't put it into the syslog queue right now maybe in the future if
  899. * really needed.
  900. */
  901. void tty_write_message(struct tty_struct *tty, char *msg)
  902. {
  903. if (tty && tty->driver->write)
  904. tty->driver->write(tty, msg, strlen(msg));
  905. return;
  906. }
  907. /*
  908. * printk rate limiting, lifted from the networking subsystem.
  909. *
  910. * This enforces a rate limit: not more than one kernel message
  911. * every printk_ratelimit_jiffies to make a denial-of-service
  912. * attack impossible.
  913. */
  914. int __printk_ratelimit(int ratelimit_jiffies, int ratelimit_burst)
  915. {
  916. static DEFINE_SPINLOCK(ratelimit_lock);
  917. static unsigned long toks = 10 * 5 * HZ;
  918. static unsigned long last_msg;
  919. static int missed;
  920. unsigned long flags;
  921. unsigned long now = jiffies;
  922. spin_lock_irqsave(&ratelimit_lock, flags);
  923. toks += now - last_msg;
  924. last_msg = now;
  925. if (toks > (ratelimit_burst * ratelimit_jiffies))
  926. toks = ratelimit_burst * ratelimit_jiffies;
  927. if (toks >= ratelimit_jiffies) {
  928. int lost = missed;
  929. missed = 0;
  930. toks -= ratelimit_jiffies;
  931. spin_unlock_irqrestore(&ratelimit_lock, flags);
  932. if (lost)
  933. printk(KERN_WARNING "printk: %d messages suppressed.\n", lost);
  934. return 1;
  935. }
  936. missed++;
  937. spin_unlock_irqrestore(&ratelimit_lock, flags);
  938. return 0;
  939. }
  940. EXPORT_SYMBOL(__printk_ratelimit);
  941. /* minimum time in jiffies between messages */
  942. int printk_ratelimit_jiffies = 5 * HZ;
  943. /* number of messages we send before ratelimiting */
  944. int printk_ratelimit_burst = 10;
  945. int printk_ratelimit(void)
  946. {
  947. return __printk_ratelimit(printk_ratelimit_jiffies,
  948. printk_ratelimit_burst);
  949. }
  950. EXPORT_SYMBOL(printk_ratelimit);