printk.c 26 KB

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