printk.c 27 KB

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