printk.c 33 KB

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