printk.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343
  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
  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/console.h>
  23. #include <linux/init.h>
  24. #include <linux/jiffies.h>
  25. #include <linux/nmi.h>
  26. #include <linux/module.h>
  27. #include <linux/moduleparam.h>
  28. #include <linux/interrupt.h> /* For in_interrupt() */
  29. #include <linux/delay.h>
  30. #include <linux/smp.h>
  31. #include <linux/security.h>
  32. #include <linux/bootmem.h>
  33. #include <linux/syscalls.h>
  34. #include <asm/uaccess.h>
  35. /*
  36. * Architectures can override it:
  37. */
  38. void asmlinkage __attribute__((weak)) early_printk(const char *fmt, ...)
  39. {
  40. }
  41. #define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT)
  42. /* printk's without a loglevel use this.. */
  43. #define DEFAULT_MESSAGE_LOGLEVEL 4 /* KERN_WARNING */
  44. /* We show everything that is MORE important than this.. */
  45. #define MINIMUM_CONSOLE_LOGLEVEL 1 /* Minimum loglevel we let people use */
  46. #define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */
  47. DECLARE_WAIT_QUEUE_HEAD(log_wait);
  48. int console_printk[4] = {
  49. DEFAULT_CONSOLE_LOGLEVEL, /* console_loglevel */
  50. DEFAULT_MESSAGE_LOGLEVEL, /* default_message_loglevel */
  51. MINIMUM_CONSOLE_LOGLEVEL, /* minimum_console_loglevel */
  52. DEFAULT_CONSOLE_LOGLEVEL, /* default_console_loglevel */
  53. };
  54. /*
  55. * Low level drivers may need that to know if they can schedule in
  56. * their unblank() callback or not. So let's export it.
  57. */
  58. int oops_in_progress;
  59. EXPORT_SYMBOL(oops_in_progress);
  60. /*
  61. * console_sem protects the console_drivers list, and also
  62. * provides serialisation for access to the entire console
  63. * driver system.
  64. */
  65. static DECLARE_MUTEX(console_sem);
  66. static DECLARE_MUTEX(secondary_console_sem);
  67. struct console *console_drivers;
  68. EXPORT_SYMBOL_GPL(console_drivers);
  69. /*
  70. * This is used for debugging the mess that is the VT code by
  71. * keeping track if we have the console semaphore held. It's
  72. * definitely not the perfect debug tool (we don't know if _WE_
  73. * hold it are racing, but it helps tracking those weird code
  74. * path in the console code where we end up in places I want
  75. * locked without the console sempahore held
  76. */
  77. static int console_locked, console_suspended;
  78. /*
  79. * logbuf_lock protects log_buf, log_start, log_end, con_start and logged_chars
  80. * It is also used in interesting ways to provide interlocking in
  81. * release_console_sem().
  82. */
  83. static DEFINE_SPINLOCK(logbuf_lock);
  84. #define LOG_BUF_MASK (log_buf_len-1)
  85. #define LOG_BUF(idx) (log_buf[(idx) & LOG_BUF_MASK])
  86. /*
  87. * The indices into log_buf are not constrained to log_buf_len - they
  88. * must be masked before subscripting
  89. */
  90. static unsigned log_start; /* Index into log_buf: next char to be read by syslog() */
  91. static unsigned con_start; /* Index into log_buf: next char to be sent to consoles */
  92. static unsigned log_end; /* Index into log_buf: most-recently-written-char + 1 */
  93. /*
  94. * Array of consoles built from command line options (console=)
  95. */
  96. struct console_cmdline
  97. {
  98. char name[8]; /* Name of the driver */
  99. int index; /* Minor dev. to use */
  100. char *options; /* Options for the driver */
  101. #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
  102. char *brl_options; /* Options for braille driver */
  103. #endif
  104. };
  105. #define MAX_CMDLINECONSOLES 8
  106. static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
  107. static int selected_console = -1;
  108. static int preferred_console = -1;
  109. int console_set_on_cmdline;
  110. EXPORT_SYMBOL(console_set_on_cmdline);
  111. /* Flag: console code may call schedule() */
  112. static int console_may_schedule;
  113. #ifdef CONFIG_PRINTK
  114. static char __log_buf[__LOG_BUF_LEN];
  115. static char *log_buf = __log_buf;
  116. static int log_buf_len = __LOG_BUF_LEN;
  117. static unsigned logged_chars; /* Number of chars produced since last read+clear operation */
  118. static int __init log_buf_len_setup(char *str)
  119. {
  120. unsigned size = memparse(str, &str);
  121. unsigned long flags;
  122. if (size)
  123. size = roundup_pow_of_two(size);
  124. if (size > log_buf_len) {
  125. unsigned start, dest_idx, offset;
  126. char *new_log_buf;
  127. new_log_buf = alloc_bootmem(size);
  128. if (!new_log_buf) {
  129. printk(KERN_WARNING "log_buf_len: allocation failed\n");
  130. goto out;
  131. }
  132. spin_lock_irqsave(&logbuf_lock, flags);
  133. log_buf_len = size;
  134. log_buf = new_log_buf;
  135. offset = start = min(con_start, log_start);
  136. dest_idx = 0;
  137. while (start != log_end) {
  138. log_buf[dest_idx] = __log_buf[start & (__LOG_BUF_LEN - 1)];
  139. start++;
  140. dest_idx++;
  141. }
  142. log_start -= offset;
  143. con_start -= offset;
  144. log_end -= offset;
  145. spin_unlock_irqrestore(&logbuf_lock, flags);
  146. printk(KERN_NOTICE "log_buf_len: %d\n", log_buf_len);
  147. }
  148. out:
  149. return 1;
  150. }
  151. __setup("log_buf_len=", log_buf_len_setup);
  152. #ifdef CONFIG_BOOT_PRINTK_DELAY
  153. static unsigned int boot_delay; /* msecs delay after each printk during bootup */
  154. static unsigned long long printk_delay_msec; /* per msec, based on boot_delay */
  155. static int __init boot_delay_setup(char *str)
  156. {
  157. unsigned long lpj;
  158. unsigned long long loops_per_msec;
  159. lpj = preset_lpj ? preset_lpj : 1000000; /* some guess */
  160. loops_per_msec = (unsigned long long)lpj / 1000 * HZ;
  161. get_option(&str, &boot_delay);
  162. if (boot_delay > 10 * 1000)
  163. boot_delay = 0;
  164. printk_delay_msec = loops_per_msec;
  165. printk(KERN_DEBUG "boot_delay: %u, preset_lpj: %ld, lpj: %lu, "
  166. "HZ: %d, printk_delay_msec: %llu\n",
  167. boot_delay, preset_lpj, lpj, HZ, printk_delay_msec);
  168. return 1;
  169. }
  170. __setup("boot_delay=", boot_delay_setup);
  171. static void boot_delay_msec(void)
  172. {
  173. unsigned long long k;
  174. unsigned long timeout;
  175. if (boot_delay == 0 || system_state != SYSTEM_BOOTING)
  176. return;
  177. k = (unsigned long long)printk_delay_msec * boot_delay;
  178. timeout = jiffies + msecs_to_jiffies(boot_delay);
  179. while (k) {
  180. k--;
  181. cpu_relax();
  182. /*
  183. * use (volatile) jiffies to prevent
  184. * compiler reduction; loop termination via jiffies
  185. * is secondary and may or may not happen.
  186. */
  187. if (time_after(jiffies, timeout))
  188. break;
  189. touch_nmi_watchdog();
  190. }
  191. }
  192. #else
  193. static inline void boot_delay_msec(void)
  194. {
  195. }
  196. #endif
  197. /*
  198. * Return the number of unread characters in the log buffer.
  199. */
  200. static int log_buf_get_len(void)
  201. {
  202. return logged_chars;
  203. }
  204. /*
  205. * Copy a range of characters from the log buffer.
  206. */
  207. int log_buf_copy(char *dest, int idx, int len)
  208. {
  209. int ret, max;
  210. bool took_lock = false;
  211. if (!oops_in_progress) {
  212. spin_lock_irq(&logbuf_lock);
  213. took_lock = true;
  214. }
  215. max = log_buf_get_len();
  216. if (idx < 0 || idx >= max) {
  217. ret = -1;
  218. } else {
  219. if (len > max)
  220. len = max;
  221. ret = len;
  222. idx += (log_end - max);
  223. while (len-- > 0)
  224. dest[len] = LOG_BUF(idx + len);
  225. }
  226. if (took_lock)
  227. spin_unlock_irq(&logbuf_lock);
  228. return ret;
  229. }
  230. /*
  231. * Commands to do_syslog:
  232. *
  233. * 0 -- Close the log. Currently a NOP.
  234. * 1 -- Open the log. Currently a NOP.
  235. * 2 -- Read from the log.
  236. * 3 -- Read all messages remaining in the ring buffer.
  237. * 4 -- Read and clear all messages remaining in the ring buffer
  238. * 5 -- Clear ring buffer.
  239. * 6 -- Disable printk's to console
  240. * 7 -- Enable printk's to console
  241. * 8 -- Set level of messages printed to console
  242. * 9 -- Return number of unread characters in the log buffer
  243. * 10 -- Return size of the log buffer
  244. */
  245. int do_syslog(int type, char __user *buf, int len)
  246. {
  247. unsigned i, j, limit, count;
  248. int do_clear = 0;
  249. char c;
  250. int error = 0;
  251. error = security_syslog(type);
  252. if (error)
  253. return error;
  254. switch (type) {
  255. case 0: /* Close log */
  256. break;
  257. case 1: /* Open log */
  258. break;
  259. case 2: /* Read from log */
  260. error = -EINVAL;
  261. if (!buf || len < 0)
  262. goto out;
  263. error = 0;
  264. if (!len)
  265. goto out;
  266. if (!access_ok(VERIFY_WRITE, buf, len)) {
  267. error = -EFAULT;
  268. goto out;
  269. }
  270. error = wait_event_interruptible(log_wait,
  271. (log_start - log_end));
  272. if (error)
  273. goto out;
  274. i = 0;
  275. spin_lock_irq(&logbuf_lock);
  276. while (!error && (log_start != log_end) && i < len) {
  277. c = LOG_BUF(log_start);
  278. log_start++;
  279. spin_unlock_irq(&logbuf_lock);
  280. error = __put_user(c,buf);
  281. buf++;
  282. i++;
  283. cond_resched();
  284. spin_lock_irq(&logbuf_lock);
  285. }
  286. spin_unlock_irq(&logbuf_lock);
  287. if (!error)
  288. error = i;
  289. break;
  290. case 4: /* Read/clear last kernel messages */
  291. do_clear = 1;
  292. /* FALL THRU */
  293. case 3: /* Read last kernel messages */
  294. error = -EINVAL;
  295. if (!buf || len < 0)
  296. goto out;
  297. error = 0;
  298. if (!len)
  299. goto out;
  300. if (!access_ok(VERIFY_WRITE, buf, len)) {
  301. error = -EFAULT;
  302. goto out;
  303. }
  304. count = len;
  305. if (count > log_buf_len)
  306. count = log_buf_len;
  307. spin_lock_irq(&logbuf_lock);
  308. if (count > logged_chars)
  309. count = logged_chars;
  310. if (do_clear)
  311. logged_chars = 0;
  312. limit = log_end;
  313. /*
  314. * __put_user() could sleep, and while we sleep
  315. * printk() could overwrite the messages
  316. * we try to copy to user space. Therefore
  317. * the messages are copied in reverse. <manfreds>
  318. */
  319. for (i = 0; i < count && !error; i++) {
  320. j = limit-1-i;
  321. if (j + log_buf_len < log_end)
  322. break;
  323. c = LOG_BUF(j);
  324. spin_unlock_irq(&logbuf_lock);
  325. error = __put_user(c,&buf[count-1-i]);
  326. cond_resched();
  327. spin_lock_irq(&logbuf_lock);
  328. }
  329. spin_unlock_irq(&logbuf_lock);
  330. if (error)
  331. break;
  332. error = i;
  333. if (i != count) {
  334. int offset = count-error;
  335. /* buffer overflow during copy, correct user buffer. */
  336. for (i = 0; i < error; i++) {
  337. if (__get_user(c,&buf[i+offset]) ||
  338. __put_user(c,&buf[i])) {
  339. error = -EFAULT;
  340. break;
  341. }
  342. cond_resched();
  343. }
  344. }
  345. break;
  346. case 5: /* Clear ring buffer */
  347. logged_chars = 0;
  348. break;
  349. case 6: /* Disable logging to console */
  350. console_loglevel = minimum_console_loglevel;
  351. break;
  352. case 7: /* Enable logging to console */
  353. console_loglevel = default_console_loglevel;
  354. break;
  355. case 8: /* Set level of messages printed to console */
  356. error = -EINVAL;
  357. if (len < 1 || len > 8)
  358. goto out;
  359. if (len < minimum_console_loglevel)
  360. len = minimum_console_loglevel;
  361. console_loglevel = len;
  362. error = 0;
  363. break;
  364. case 9: /* Number of chars in the log buffer */
  365. error = log_end - log_start;
  366. break;
  367. case 10: /* Size of the log buffer */
  368. error = log_buf_len;
  369. break;
  370. default:
  371. error = -EINVAL;
  372. break;
  373. }
  374. out:
  375. return error;
  376. }
  377. asmlinkage long sys_syslog(int type, char __user *buf, int len)
  378. {
  379. return do_syslog(type, buf, len);
  380. }
  381. /*
  382. * Call the console drivers on a range of log_buf
  383. */
  384. static void __call_console_drivers(unsigned start, unsigned end)
  385. {
  386. struct console *con;
  387. for (con = console_drivers; con; con = con->next) {
  388. if ((con->flags & CON_ENABLED) && con->write &&
  389. (cpu_online(smp_processor_id()) ||
  390. (con->flags & CON_ANYTIME)))
  391. con->write(con, &LOG_BUF(start), end - start);
  392. }
  393. }
  394. static int __read_mostly ignore_loglevel;
  395. static int __init ignore_loglevel_setup(char *str)
  396. {
  397. ignore_loglevel = 1;
  398. printk(KERN_INFO "debug: ignoring loglevel setting.\n");
  399. return 0;
  400. }
  401. early_param("ignore_loglevel", ignore_loglevel_setup);
  402. /*
  403. * Write out chars from start to end - 1 inclusive
  404. */
  405. static void _call_console_drivers(unsigned start,
  406. unsigned end, int msg_log_level)
  407. {
  408. if ((msg_log_level < console_loglevel || ignore_loglevel) &&
  409. console_drivers && start != end) {
  410. if ((start & LOG_BUF_MASK) > (end & LOG_BUF_MASK)) {
  411. /* wrapped write */
  412. __call_console_drivers(start & LOG_BUF_MASK,
  413. log_buf_len);
  414. __call_console_drivers(0, end & LOG_BUF_MASK);
  415. } else {
  416. __call_console_drivers(start, end);
  417. }
  418. }
  419. }
  420. /*
  421. * Call the console drivers, asking them to write out
  422. * log_buf[start] to log_buf[end - 1].
  423. * The console_sem must be held.
  424. */
  425. static void call_console_drivers(unsigned start, unsigned end)
  426. {
  427. unsigned cur_index, start_print;
  428. static int msg_level = -1;
  429. BUG_ON(((int)(start - end)) > 0);
  430. cur_index = start;
  431. start_print = start;
  432. while (cur_index != end) {
  433. if (msg_level < 0 && ((end - cur_index) > 2) &&
  434. LOG_BUF(cur_index + 0) == '<' &&
  435. LOG_BUF(cur_index + 1) >= '0' &&
  436. LOG_BUF(cur_index + 1) <= '7' &&
  437. LOG_BUF(cur_index + 2) == '>') {
  438. msg_level = LOG_BUF(cur_index + 1) - '0';
  439. cur_index += 3;
  440. start_print = cur_index;
  441. }
  442. while (cur_index != end) {
  443. char c = LOG_BUF(cur_index);
  444. cur_index++;
  445. if (c == '\n') {
  446. if (msg_level < 0) {
  447. /*
  448. * printk() has already given us loglevel tags in
  449. * the buffer. This code is here in case the
  450. * log buffer has wrapped right round and scribbled
  451. * on those tags
  452. */
  453. msg_level = default_message_loglevel;
  454. }
  455. _call_console_drivers(start_print, cur_index, msg_level);
  456. msg_level = -1;
  457. start_print = cur_index;
  458. break;
  459. }
  460. }
  461. }
  462. _call_console_drivers(start_print, end, msg_level);
  463. }
  464. static void emit_log_char(char c)
  465. {
  466. LOG_BUF(log_end) = c;
  467. log_end++;
  468. if (log_end - log_start > log_buf_len)
  469. log_start = log_end - log_buf_len;
  470. if (log_end - con_start > log_buf_len)
  471. con_start = log_end - log_buf_len;
  472. if (logged_chars < log_buf_len)
  473. logged_chars++;
  474. }
  475. /*
  476. * Zap console related locks when oopsing. Only zap at most once
  477. * every 10 seconds, to leave time for slow consoles to print a
  478. * full oops.
  479. */
  480. static void zap_locks(void)
  481. {
  482. static unsigned long oops_timestamp;
  483. if (time_after_eq(jiffies, oops_timestamp) &&
  484. !time_after(jiffies, oops_timestamp + 30 * HZ))
  485. return;
  486. oops_timestamp = jiffies;
  487. /* If a crash is occurring, make sure we can't deadlock */
  488. spin_lock_init(&logbuf_lock);
  489. /* And make sure that we print immediately */
  490. init_MUTEX(&console_sem);
  491. }
  492. #if defined(CONFIG_PRINTK_TIME)
  493. static int printk_time = 1;
  494. #else
  495. static int printk_time = 0;
  496. #endif
  497. module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR);
  498. /* Check if we have any console registered that can be called early in boot. */
  499. static int have_callable_console(void)
  500. {
  501. struct console *con;
  502. for (con = console_drivers; con; con = con->next)
  503. if (con->flags & CON_ANYTIME)
  504. return 1;
  505. return 0;
  506. }
  507. /**
  508. * printk - print a kernel message
  509. * @fmt: format string
  510. *
  511. * This is printk(). It can be called from any context. We want it to work.
  512. *
  513. * We try to grab the console_sem. If we succeed, it's easy - we log the output and
  514. * call the console drivers. If we fail to get the semaphore we place the output
  515. * into the log buffer and return. The current holder of the console_sem will
  516. * notice the new output in release_console_sem() and will send it to the
  517. * consoles before releasing the semaphore.
  518. *
  519. * One effect of this deferred printing is that code which calls printk() and
  520. * then changes console_loglevel may break. This is because console_loglevel
  521. * is inspected when the actual printing occurs.
  522. *
  523. * See also:
  524. * printf(3)
  525. *
  526. * See the vsnprintf() documentation for format string extensions over C99.
  527. */
  528. asmlinkage int printk(const char *fmt, ...)
  529. {
  530. va_list args;
  531. int r;
  532. va_start(args, fmt);
  533. r = vprintk(fmt, args);
  534. va_end(args);
  535. return r;
  536. }
  537. /* cpu currently holding logbuf_lock */
  538. static volatile unsigned int printk_cpu = UINT_MAX;
  539. /*
  540. * Can we actually use the console at this time on this cpu?
  541. *
  542. * Console drivers may assume that per-cpu resources have
  543. * been allocated. So unless they're explicitly marked as
  544. * being able to cope (CON_ANYTIME) don't call them until
  545. * this CPU is officially up.
  546. */
  547. static inline int can_use_console(unsigned int cpu)
  548. {
  549. return cpu_online(cpu) || have_callable_console();
  550. }
  551. /*
  552. * Try to get console ownership to actually show the kernel
  553. * messages from a 'printk'. Return true (and with the
  554. * console_semaphore held, and 'console_locked' set) if it
  555. * is successful, false otherwise.
  556. *
  557. * This gets called with the 'logbuf_lock' spinlock held and
  558. * interrupts disabled. It should return with 'lockbuf_lock'
  559. * released but interrupts still disabled.
  560. */
  561. static int acquire_console_semaphore_for_printk(unsigned int cpu)
  562. {
  563. int retval = 0;
  564. if (!try_acquire_console_sem()) {
  565. retval = 1;
  566. /*
  567. * If we can't use the console, we need to release
  568. * the console semaphore by hand to avoid flushing
  569. * the buffer. We need to hold the console semaphore
  570. * in order to do this test safely.
  571. */
  572. if (!can_use_console(cpu)) {
  573. console_locked = 0;
  574. up(&console_sem);
  575. retval = 0;
  576. }
  577. }
  578. printk_cpu = UINT_MAX;
  579. spin_unlock(&logbuf_lock);
  580. return retval;
  581. }
  582. static const char recursion_bug_msg [] =
  583. KERN_CRIT "BUG: recent printk recursion!\n";
  584. static int recursion_bug;
  585. static int new_text_line = 1;
  586. static char printk_buf[1024];
  587. asmlinkage int vprintk(const char *fmt, va_list args)
  588. {
  589. int printed_len = 0;
  590. int current_log_level = default_message_loglevel;
  591. unsigned long flags;
  592. int this_cpu;
  593. char *p;
  594. boot_delay_msec();
  595. preempt_disable();
  596. /* This stops the holder of console_sem just where we want him */
  597. raw_local_irq_save(flags);
  598. this_cpu = smp_processor_id();
  599. /*
  600. * Ouch, printk recursed into itself!
  601. */
  602. if (unlikely(printk_cpu == this_cpu)) {
  603. /*
  604. * If a crash is occurring during printk() on this CPU,
  605. * then try to get the crash message out but make sure
  606. * we can't deadlock. Otherwise just return to avoid the
  607. * recursion and return - but flag the recursion so that
  608. * it can be printed at the next appropriate moment:
  609. */
  610. if (!oops_in_progress) {
  611. recursion_bug = 1;
  612. goto out_restore_irqs;
  613. }
  614. zap_locks();
  615. }
  616. lockdep_off();
  617. spin_lock(&logbuf_lock);
  618. printk_cpu = this_cpu;
  619. if (recursion_bug) {
  620. recursion_bug = 0;
  621. strcpy(printk_buf, recursion_bug_msg);
  622. printed_len = sizeof(recursion_bug_msg);
  623. }
  624. /* Emit the output into the temporary buffer */
  625. printed_len += vscnprintf(printk_buf + printed_len,
  626. sizeof(printk_buf) - printed_len, fmt, args);
  627. /*
  628. * Copy the output into log_buf. If the caller didn't provide
  629. * appropriate log level tags, we insert them here
  630. */
  631. for (p = printk_buf; *p; p++) {
  632. if (new_text_line) {
  633. /* If a token, set current_log_level and skip over */
  634. if (p[0] == '<' && p[1] >= '0' && p[1] <= '7' &&
  635. p[2] == '>') {
  636. current_log_level = p[1] - '0';
  637. p += 3;
  638. printed_len -= 3;
  639. }
  640. /* Always output the token */
  641. emit_log_char('<');
  642. emit_log_char(current_log_level + '0');
  643. emit_log_char('>');
  644. printed_len += 3;
  645. new_text_line = 0;
  646. if (printk_time) {
  647. /* Follow the token with the time */
  648. char tbuf[50], *tp;
  649. unsigned tlen;
  650. unsigned long long t;
  651. unsigned long nanosec_rem;
  652. t = cpu_clock(printk_cpu);
  653. nanosec_rem = do_div(t, 1000000000);
  654. tlen = sprintf(tbuf, "[%5lu.%06lu] ",
  655. (unsigned long) t,
  656. nanosec_rem / 1000);
  657. for (tp = tbuf; tp < tbuf + tlen; tp++)
  658. emit_log_char(*tp);
  659. printed_len += tlen;
  660. }
  661. if (!*p)
  662. break;
  663. }
  664. emit_log_char(*p);
  665. if (*p == '\n')
  666. new_text_line = 1;
  667. }
  668. /*
  669. * Try to acquire and then immediately release the
  670. * console semaphore. The release will do all the
  671. * actual magic (print out buffers, wake up klogd,
  672. * etc).
  673. *
  674. * The acquire_console_semaphore_for_printk() function
  675. * will release 'logbuf_lock' regardless of whether it
  676. * actually gets the semaphore or not.
  677. */
  678. if (acquire_console_semaphore_for_printk(this_cpu))
  679. release_console_sem();
  680. lockdep_on();
  681. out_restore_irqs:
  682. raw_local_irq_restore(flags);
  683. preempt_enable();
  684. return printed_len;
  685. }
  686. EXPORT_SYMBOL(printk);
  687. EXPORT_SYMBOL(vprintk);
  688. #else
  689. asmlinkage long sys_syslog(int type, char __user *buf, int len)
  690. {
  691. return -ENOSYS;
  692. }
  693. static void call_console_drivers(unsigned start, unsigned end)
  694. {
  695. }
  696. #endif
  697. static int __add_preferred_console(char *name, int idx, char *options,
  698. char *brl_options)
  699. {
  700. struct console_cmdline *c;
  701. int i;
  702. /*
  703. * See if this tty is not yet registered, and
  704. * if we have a slot free.
  705. */
  706. for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
  707. if (strcmp(console_cmdline[i].name, name) == 0 &&
  708. console_cmdline[i].index == idx) {
  709. if (!brl_options)
  710. selected_console = i;
  711. return 0;
  712. }
  713. if (i == MAX_CMDLINECONSOLES)
  714. return -E2BIG;
  715. if (!brl_options)
  716. selected_console = i;
  717. c = &console_cmdline[i];
  718. strlcpy(c->name, name, sizeof(c->name));
  719. c->options = options;
  720. #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
  721. c->brl_options = brl_options;
  722. #endif
  723. c->index = idx;
  724. return 0;
  725. }
  726. /*
  727. * Set up a list of consoles. Called from init/main.c
  728. */
  729. static int __init console_setup(char *str)
  730. {
  731. char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for index */
  732. char *s, *options, *brl_options = NULL;
  733. int idx;
  734. #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
  735. if (!memcmp(str, "brl,", 4)) {
  736. brl_options = "";
  737. str += 4;
  738. } else if (!memcmp(str, "brl=", 4)) {
  739. brl_options = str + 4;
  740. str = strchr(brl_options, ',');
  741. if (!str) {
  742. printk(KERN_ERR "need port name after brl=\n");
  743. return 1;
  744. }
  745. *(str++) = 0;
  746. }
  747. #endif
  748. /*
  749. * Decode str into name, index, options.
  750. */
  751. if (str[0] >= '0' && str[0] <= '9') {
  752. strcpy(buf, "ttyS");
  753. strncpy(buf + 4, str, sizeof(buf) - 5);
  754. } else {
  755. strncpy(buf, str, sizeof(buf) - 1);
  756. }
  757. buf[sizeof(buf) - 1] = 0;
  758. if ((options = strchr(str, ',')) != NULL)
  759. *(options++) = 0;
  760. #ifdef __sparc__
  761. if (!strcmp(str, "ttya"))
  762. strcpy(buf, "ttyS0");
  763. if (!strcmp(str, "ttyb"))
  764. strcpy(buf, "ttyS1");
  765. #endif
  766. for (s = buf; *s; s++)
  767. if ((*s >= '0' && *s <= '9') || *s == ',')
  768. break;
  769. idx = simple_strtoul(s, NULL, 10);
  770. *s = 0;
  771. __add_preferred_console(buf, idx, options, brl_options);
  772. console_set_on_cmdline = 1;
  773. return 1;
  774. }
  775. __setup("console=", console_setup);
  776. /**
  777. * add_preferred_console - add a device to the list of preferred consoles.
  778. * @name: device name
  779. * @idx: device index
  780. * @options: options for this console
  781. *
  782. * The last preferred console added will be used for kernel messages
  783. * and stdin/out/err for init. Normally this is used by console_setup
  784. * above to handle user-supplied console arguments; however it can also
  785. * be used by arch-specific code either to override the user or more
  786. * commonly to provide a default console (ie from PROM variables) when
  787. * the user has not supplied one.
  788. */
  789. int add_preferred_console(char *name, int idx, char *options)
  790. {
  791. return __add_preferred_console(name, idx, options, NULL);
  792. }
  793. int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options)
  794. {
  795. struct console_cmdline *c;
  796. int i;
  797. for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
  798. if (strcmp(console_cmdline[i].name, name) == 0 &&
  799. console_cmdline[i].index == idx) {
  800. c = &console_cmdline[i];
  801. strlcpy(c->name, name_new, sizeof(c->name));
  802. c->name[sizeof(c->name) - 1] = 0;
  803. c->options = options;
  804. c->index = idx_new;
  805. return i;
  806. }
  807. /* not found */
  808. return -1;
  809. }
  810. int console_suspend_enabled = 1;
  811. EXPORT_SYMBOL(console_suspend_enabled);
  812. static int __init console_suspend_disable(char *str)
  813. {
  814. console_suspend_enabled = 0;
  815. return 1;
  816. }
  817. __setup("no_console_suspend", console_suspend_disable);
  818. /**
  819. * suspend_console - suspend the console subsystem
  820. *
  821. * This disables printk() while we go into suspend states
  822. */
  823. void suspend_console(void)
  824. {
  825. if (!console_suspend_enabled)
  826. return;
  827. printk("Suspending console(s) (use no_console_suspend to debug)\n");
  828. acquire_console_sem();
  829. console_suspended = 1;
  830. }
  831. void resume_console(void)
  832. {
  833. if (!console_suspend_enabled)
  834. return;
  835. console_suspended = 0;
  836. release_console_sem();
  837. }
  838. /**
  839. * acquire_console_sem - lock the console system for exclusive use.
  840. *
  841. * Acquires a semaphore which guarantees that the caller has
  842. * exclusive access to the console system and the console_drivers list.
  843. *
  844. * Can sleep, returns nothing.
  845. */
  846. void acquire_console_sem(void)
  847. {
  848. BUG_ON(in_interrupt());
  849. if (console_suspended) {
  850. down(&secondary_console_sem);
  851. return;
  852. }
  853. down(&console_sem);
  854. console_locked = 1;
  855. console_may_schedule = 1;
  856. }
  857. EXPORT_SYMBOL(acquire_console_sem);
  858. int try_acquire_console_sem(void)
  859. {
  860. if (down_trylock(&console_sem))
  861. return -1;
  862. console_locked = 1;
  863. console_may_schedule = 0;
  864. return 0;
  865. }
  866. EXPORT_SYMBOL(try_acquire_console_sem);
  867. int is_console_locked(void)
  868. {
  869. return console_locked;
  870. }
  871. static DEFINE_PER_CPU(int, printk_pending);
  872. void printk_tick(void)
  873. {
  874. if (__get_cpu_var(printk_pending)) {
  875. __get_cpu_var(printk_pending) = 0;
  876. wake_up_interruptible(&log_wait);
  877. }
  878. }
  879. int printk_needs_cpu(int cpu)
  880. {
  881. return per_cpu(printk_pending, cpu);
  882. }
  883. void wake_up_klogd(void)
  884. {
  885. if (waitqueue_active(&log_wait))
  886. __raw_get_cpu_var(printk_pending) = 1;
  887. }
  888. /**
  889. * release_console_sem - unlock the console system
  890. *
  891. * Releases the semaphore which the caller holds on the console system
  892. * and the console driver list.
  893. *
  894. * While the semaphore was held, console output may have been buffered
  895. * by printk(). If this is the case, release_console_sem() emits
  896. * the output prior to releasing the semaphore.
  897. *
  898. * If there is output waiting for klogd, we wake it up.
  899. *
  900. * release_console_sem() may be called from any context.
  901. */
  902. void release_console_sem(void)
  903. {
  904. unsigned long flags;
  905. unsigned _con_start, _log_end;
  906. unsigned wake_klogd = 0;
  907. if (console_suspended) {
  908. up(&secondary_console_sem);
  909. return;
  910. }
  911. console_may_schedule = 0;
  912. for ( ; ; ) {
  913. spin_lock_irqsave(&logbuf_lock, flags);
  914. wake_klogd |= log_start - log_end;
  915. if (con_start == log_end)
  916. break; /* Nothing to print */
  917. _con_start = con_start;
  918. _log_end = log_end;
  919. con_start = log_end; /* Flush */
  920. spin_unlock(&logbuf_lock);
  921. stop_critical_timings(); /* don't trace print latency */
  922. call_console_drivers(_con_start, _log_end);
  923. start_critical_timings();
  924. local_irq_restore(flags);
  925. }
  926. console_locked = 0;
  927. up(&console_sem);
  928. spin_unlock_irqrestore(&logbuf_lock, flags);
  929. if (wake_klogd)
  930. wake_up_klogd();
  931. }
  932. EXPORT_SYMBOL(release_console_sem);
  933. /**
  934. * console_conditional_schedule - yield the CPU if required
  935. *
  936. * If the console code is currently allowed to sleep, and
  937. * if this CPU should yield the CPU to another task, do
  938. * so here.
  939. *
  940. * Must be called within acquire_console_sem().
  941. */
  942. void __sched console_conditional_schedule(void)
  943. {
  944. if (console_may_schedule)
  945. cond_resched();
  946. }
  947. EXPORT_SYMBOL(console_conditional_schedule);
  948. void console_print(const char *s)
  949. {
  950. printk(KERN_EMERG "%s", s);
  951. }
  952. EXPORT_SYMBOL(console_print);
  953. void console_unblank(void)
  954. {
  955. struct console *c;
  956. /*
  957. * console_unblank can no longer be called in interrupt context unless
  958. * oops_in_progress is set to 1..
  959. */
  960. if (oops_in_progress) {
  961. if (down_trylock(&console_sem) != 0)
  962. return;
  963. } else
  964. acquire_console_sem();
  965. console_locked = 1;
  966. console_may_schedule = 0;
  967. for (c = console_drivers; c != NULL; c = c->next)
  968. if ((c->flags & CON_ENABLED) && c->unblank)
  969. c->unblank();
  970. release_console_sem();
  971. }
  972. /*
  973. * Return the console tty driver structure and its associated index
  974. */
  975. struct tty_driver *console_device(int *index)
  976. {
  977. struct console *c;
  978. struct tty_driver *driver = NULL;
  979. acquire_console_sem();
  980. for (c = console_drivers; c != NULL; c = c->next) {
  981. if (!c->device)
  982. continue;
  983. driver = c->device(c, index);
  984. if (driver)
  985. break;
  986. }
  987. release_console_sem();
  988. return driver;
  989. }
  990. /*
  991. * Prevent further output on the passed console device so that (for example)
  992. * serial drivers can disable console output before suspending a port, and can
  993. * re-enable output afterwards.
  994. */
  995. void console_stop(struct console *console)
  996. {
  997. acquire_console_sem();
  998. console->flags &= ~CON_ENABLED;
  999. release_console_sem();
  1000. }
  1001. EXPORT_SYMBOL(console_stop);
  1002. void console_start(struct console *console)
  1003. {
  1004. acquire_console_sem();
  1005. console->flags |= CON_ENABLED;
  1006. release_console_sem();
  1007. }
  1008. EXPORT_SYMBOL(console_start);
  1009. /*
  1010. * The console driver calls this routine during kernel initialization
  1011. * to register the console printing procedure with printk() and to
  1012. * print any messages that were printed by the kernel before the
  1013. * console driver was initialized.
  1014. */
  1015. void register_console(struct console *console)
  1016. {
  1017. int i;
  1018. unsigned long flags;
  1019. struct console *bootconsole = NULL;
  1020. if (console_drivers) {
  1021. if (console->flags & CON_BOOT)
  1022. return;
  1023. if (console_drivers->flags & CON_BOOT)
  1024. bootconsole = console_drivers;
  1025. }
  1026. if (preferred_console < 0 || bootconsole || !console_drivers)
  1027. preferred_console = selected_console;
  1028. if (console->early_setup)
  1029. console->early_setup();
  1030. /*
  1031. * See if we want to use this console driver. If we
  1032. * didn't select a console we take the first one
  1033. * that registers here.
  1034. */
  1035. if (preferred_console < 0) {
  1036. if (console->index < 0)
  1037. console->index = 0;
  1038. if (console->setup == NULL ||
  1039. console->setup(console, NULL) == 0) {
  1040. console->flags |= CON_ENABLED;
  1041. if (console->device) {
  1042. console->flags |= CON_CONSDEV;
  1043. preferred_console = 0;
  1044. }
  1045. }
  1046. }
  1047. /*
  1048. * See if this console matches one we selected on
  1049. * the command line.
  1050. */
  1051. for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0];
  1052. i++) {
  1053. if (strcmp(console_cmdline[i].name, console->name) != 0)
  1054. continue;
  1055. if (console->index >= 0 &&
  1056. console->index != console_cmdline[i].index)
  1057. continue;
  1058. if (console->index < 0)
  1059. console->index = console_cmdline[i].index;
  1060. #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
  1061. if (console_cmdline[i].brl_options) {
  1062. console->flags |= CON_BRL;
  1063. braille_register_console(console,
  1064. console_cmdline[i].index,
  1065. console_cmdline[i].options,
  1066. console_cmdline[i].brl_options);
  1067. return;
  1068. }
  1069. #endif
  1070. if (console->setup &&
  1071. console->setup(console, console_cmdline[i].options) != 0)
  1072. break;
  1073. console->flags |= CON_ENABLED;
  1074. console->index = console_cmdline[i].index;
  1075. if (i == selected_console) {
  1076. console->flags |= CON_CONSDEV;
  1077. preferred_console = selected_console;
  1078. }
  1079. break;
  1080. }
  1081. if (!(console->flags & CON_ENABLED))
  1082. return;
  1083. if (bootconsole && (console->flags & CON_CONSDEV)) {
  1084. printk(KERN_INFO "console handover: boot [%s%d] -> real [%s%d]\n",
  1085. bootconsole->name, bootconsole->index,
  1086. console->name, console->index);
  1087. unregister_console(bootconsole);
  1088. console->flags &= ~CON_PRINTBUFFER;
  1089. } else {
  1090. printk(KERN_INFO "console [%s%d] enabled\n",
  1091. console->name, console->index);
  1092. }
  1093. /*
  1094. * Put this console in the list - keep the
  1095. * preferred driver at the head of the list.
  1096. */
  1097. acquire_console_sem();
  1098. if ((console->flags & CON_CONSDEV) || console_drivers == NULL) {
  1099. console->next = console_drivers;
  1100. console_drivers = console;
  1101. if (console->next)
  1102. console->next->flags &= ~CON_CONSDEV;
  1103. } else {
  1104. console->next = console_drivers->next;
  1105. console_drivers->next = console;
  1106. }
  1107. if (console->flags & CON_PRINTBUFFER) {
  1108. /*
  1109. * release_console_sem() will print out the buffered messages
  1110. * for us.
  1111. */
  1112. spin_lock_irqsave(&logbuf_lock, flags);
  1113. con_start = log_start;
  1114. spin_unlock_irqrestore(&logbuf_lock, flags);
  1115. }
  1116. release_console_sem();
  1117. }
  1118. EXPORT_SYMBOL(register_console);
  1119. int unregister_console(struct console *console)
  1120. {
  1121. struct console *a, *b;
  1122. int res = 1;
  1123. #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
  1124. if (console->flags & CON_BRL)
  1125. return braille_unregister_console(console);
  1126. #endif
  1127. acquire_console_sem();
  1128. if (console_drivers == console) {
  1129. console_drivers=console->next;
  1130. res = 0;
  1131. } else if (console_drivers) {
  1132. for (a=console_drivers->next, b=console_drivers ;
  1133. a; b=a, a=b->next) {
  1134. if (a == console) {
  1135. b->next = a->next;
  1136. res = 0;
  1137. break;
  1138. }
  1139. }
  1140. }
  1141. /*
  1142. * If this isn't the last console and it has CON_CONSDEV set, we
  1143. * need to set it on the next preferred console.
  1144. */
  1145. if (console_drivers != NULL && console->flags & CON_CONSDEV)
  1146. console_drivers->flags |= CON_CONSDEV;
  1147. release_console_sem();
  1148. return res;
  1149. }
  1150. EXPORT_SYMBOL(unregister_console);
  1151. static int __init disable_boot_consoles(void)
  1152. {
  1153. if (console_drivers != NULL) {
  1154. if (console_drivers->flags & CON_BOOT) {
  1155. printk(KERN_INFO "turn off boot console %s%d\n",
  1156. console_drivers->name, console_drivers->index);
  1157. return unregister_console(console_drivers);
  1158. }
  1159. }
  1160. return 0;
  1161. }
  1162. late_initcall(disable_boot_consoles);
  1163. #if defined CONFIG_PRINTK
  1164. /*
  1165. * printk rate limiting, lifted from the networking subsystem.
  1166. *
  1167. * This enforces a rate limit: not more than 10 kernel messages
  1168. * every 5s to make a denial-of-service attack impossible.
  1169. */
  1170. DEFINE_RATELIMIT_STATE(printk_ratelimit_state, 5 * HZ, 10);
  1171. int printk_ratelimit(void)
  1172. {
  1173. return __ratelimit(&printk_ratelimit_state);
  1174. }
  1175. EXPORT_SYMBOL(printk_ratelimit);
  1176. /**
  1177. * printk_timed_ratelimit - caller-controlled printk ratelimiting
  1178. * @caller_jiffies: pointer to caller's state
  1179. * @interval_msecs: minimum interval between prints
  1180. *
  1181. * printk_timed_ratelimit() returns true if more than @interval_msecs
  1182. * milliseconds have elapsed since the last time printk_timed_ratelimit()
  1183. * returned true.
  1184. */
  1185. bool printk_timed_ratelimit(unsigned long *caller_jiffies,
  1186. unsigned int interval_msecs)
  1187. {
  1188. if (*caller_jiffies == 0 || time_after(jiffies, *caller_jiffies)) {
  1189. *caller_jiffies = jiffies + msecs_to_jiffies(interval_msecs);
  1190. return true;
  1191. }
  1192. return false;
  1193. }
  1194. EXPORT_SYMBOL(printk_timed_ratelimit);
  1195. #endif