printk.c 26 KB

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