printk.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324
  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. /*
  612. * Copy the output into log_buf. If the caller didn't provide
  613. * appropriate log level tags, we insert them here
  614. */
  615. for (p = printk_buf; *p; p++) {
  616. if (new_text_line) {
  617. /* If a token, set current_log_level and skip over */
  618. if (p[0] == '<' && p[1] >= '0' && p[1] <= '7' &&
  619. p[2] == '>') {
  620. current_log_level = p[1] - '0';
  621. p += 3;
  622. printed_len -= 3;
  623. }
  624. /* Always output the token */
  625. emit_log_char('<');
  626. emit_log_char(current_log_level + '0');
  627. emit_log_char('>');
  628. printed_len += 3;
  629. new_text_line = 0;
  630. if (printk_time) {
  631. /* Follow the token with the time */
  632. char tbuf[50], *tp;
  633. unsigned tlen;
  634. unsigned long long t;
  635. unsigned long nanosec_rem;
  636. t = cpu_clock(printk_cpu);
  637. nanosec_rem = do_div(t, 1000000000);
  638. tlen = sprintf(tbuf, "[%5lu.%06lu] ",
  639. (unsigned long) t,
  640. nanosec_rem / 1000);
  641. for (tp = tbuf; tp < tbuf + tlen; tp++)
  642. emit_log_char(*tp);
  643. printed_len += tlen;
  644. }
  645. if (!*p)
  646. break;
  647. }
  648. emit_log_char(*p);
  649. if (*p == '\n')
  650. new_text_line = 1;
  651. }
  652. /*
  653. * Try to acquire and then immediately release the
  654. * console semaphore. The release will do all the
  655. * actual magic (print out buffers, wake up klogd,
  656. * etc).
  657. *
  658. * The acquire_console_semaphore_for_printk() function
  659. * will release 'logbuf_lock' regardless of whether it
  660. * actually gets the semaphore or not.
  661. */
  662. if (acquire_console_semaphore_for_printk(this_cpu))
  663. release_console_sem();
  664. lockdep_on();
  665. out_restore_irqs:
  666. raw_local_irq_restore(flags);
  667. preempt_enable();
  668. return printed_len;
  669. }
  670. EXPORT_SYMBOL(printk);
  671. EXPORT_SYMBOL(vprintk);
  672. #else
  673. static void call_console_drivers(unsigned start, unsigned end)
  674. {
  675. }
  676. #endif
  677. static int __add_preferred_console(char *name, int idx, char *options,
  678. char *brl_options)
  679. {
  680. struct console_cmdline *c;
  681. int i;
  682. /*
  683. * See if this tty is not yet registered, and
  684. * if we have a slot free.
  685. */
  686. for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
  687. if (strcmp(console_cmdline[i].name, name) == 0 &&
  688. console_cmdline[i].index == idx) {
  689. if (!brl_options)
  690. selected_console = i;
  691. return 0;
  692. }
  693. if (i == MAX_CMDLINECONSOLES)
  694. return -E2BIG;
  695. if (!brl_options)
  696. selected_console = i;
  697. c = &console_cmdline[i];
  698. strlcpy(c->name, name, sizeof(c->name));
  699. c->options = options;
  700. #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
  701. c->brl_options = brl_options;
  702. #endif
  703. c->index = idx;
  704. return 0;
  705. }
  706. /*
  707. * Set up a list of consoles. Called from init/main.c
  708. */
  709. static int __init console_setup(char *str)
  710. {
  711. char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for index */
  712. char *s, *options, *brl_options = NULL;
  713. int idx;
  714. #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
  715. if (!memcmp(str, "brl,", 4)) {
  716. brl_options = "";
  717. str += 4;
  718. } else if (!memcmp(str, "brl=", 4)) {
  719. brl_options = str + 4;
  720. str = strchr(brl_options, ',');
  721. if (!str) {
  722. printk(KERN_ERR "need port name after brl=\n");
  723. return 1;
  724. }
  725. *(str++) = 0;
  726. }
  727. #endif
  728. /*
  729. * Decode str into name, index, options.
  730. */
  731. if (str[0] >= '0' && str[0] <= '9') {
  732. strcpy(buf, "ttyS");
  733. strncpy(buf + 4, str, sizeof(buf) - 5);
  734. } else {
  735. strncpy(buf, str, sizeof(buf) - 1);
  736. }
  737. buf[sizeof(buf) - 1] = 0;
  738. if ((options = strchr(str, ',')) != NULL)
  739. *(options++) = 0;
  740. #ifdef __sparc__
  741. if (!strcmp(str, "ttya"))
  742. strcpy(buf, "ttyS0");
  743. if (!strcmp(str, "ttyb"))
  744. strcpy(buf, "ttyS1");
  745. #endif
  746. for (s = buf; *s; s++)
  747. if ((*s >= '0' && *s <= '9') || *s == ',')
  748. break;
  749. idx = simple_strtoul(s, NULL, 10);
  750. *s = 0;
  751. __add_preferred_console(buf, idx, options, brl_options);
  752. console_set_on_cmdline = 1;
  753. return 1;
  754. }
  755. __setup("console=", console_setup);
  756. /**
  757. * add_preferred_console - add a device to the list of preferred consoles.
  758. * @name: device name
  759. * @idx: device index
  760. * @options: options for this console
  761. *
  762. * The last preferred console added will be used for kernel messages
  763. * and stdin/out/err for init. Normally this is used by console_setup
  764. * above to handle user-supplied console arguments; however it can also
  765. * be used by arch-specific code either to override the user or more
  766. * commonly to provide a default console (ie from PROM variables) when
  767. * the user has not supplied one.
  768. */
  769. int add_preferred_console(char *name, int idx, char *options)
  770. {
  771. return __add_preferred_console(name, idx, options, NULL);
  772. }
  773. int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options)
  774. {
  775. struct console_cmdline *c;
  776. int i;
  777. for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
  778. if (strcmp(console_cmdline[i].name, name) == 0 &&
  779. console_cmdline[i].index == idx) {
  780. c = &console_cmdline[i];
  781. strlcpy(c->name, name_new, sizeof(c->name));
  782. c->name[sizeof(c->name) - 1] = 0;
  783. c->options = options;
  784. c->index = idx_new;
  785. return i;
  786. }
  787. /* not found */
  788. return -1;
  789. }
  790. int console_suspend_enabled = 1;
  791. EXPORT_SYMBOL(console_suspend_enabled);
  792. static int __init console_suspend_disable(char *str)
  793. {
  794. console_suspend_enabled = 0;
  795. return 1;
  796. }
  797. __setup("no_console_suspend", console_suspend_disable);
  798. /**
  799. * suspend_console - suspend the console subsystem
  800. *
  801. * This disables printk() while we go into suspend states
  802. */
  803. void suspend_console(void)
  804. {
  805. if (!console_suspend_enabled)
  806. return;
  807. printk("Suspending console(s) (use no_console_suspend to debug)\n");
  808. acquire_console_sem();
  809. console_suspended = 1;
  810. up(&console_sem);
  811. }
  812. void resume_console(void)
  813. {
  814. if (!console_suspend_enabled)
  815. return;
  816. down(&console_sem);
  817. console_suspended = 0;
  818. release_console_sem();
  819. }
  820. /**
  821. * acquire_console_sem - lock the console system for exclusive use.
  822. *
  823. * Acquires a semaphore which guarantees that the caller has
  824. * exclusive access to the console system and the console_drivers list.
  825. *
  826. * Can sleep, returns nothing.
  827. */
  828. void acquire_console_sem(void)
  829. {
  830. BUG_ON(in_interrupt());
  831. down(&console_sem);
  832. if (console_suspended)
  833. return;
  834. console_locked = 1;
  835. console_may_schedule = 1;
  836. }
  837. EXPORT_SYMBOL(acquire_console_sem);
  838. int try_acquire_console_sem(void)
  839. {
  840. if (down_trylock(&console_sem))
  841. return -1;
  842. if (console_suspended) {
  843. up(&console_sem);
  844. return -1;
  845. }
  846. console_locked = 1;
  847. console_may_schedule = 0;
  848. return 0;
  849. }
  850. EXPORT_SYMBOL(try_acquire_console_sem);
  851. int is_console_locked(void)
  852. {
  853. return console_locked;
  854. }
  855. static DEFINE_PER_CPU(int, printk_pending);
  856. void printk_tick(void)
  857. {
  858. if (__get_cpu_var(printk_pending)) {
  859. __get_cpu_var(printk_pending) = 0;
  860. wake_up_interruptible(&log_wait);
  861. }
  862. }
  863. int printk_needs_cpu(int cpu)
  864. {
  865. return per_cpu(printk_pending, cpu);
  866. }
  867. void wake_up_klogd(void)
  868. {
  869. if (waitqueue_active(&log_wait))
  870. __raw_get_cpu_var(printk_pending) = 1;
  871. }
  872. /**
  873. * release_console_sem - unlock the console system
  874. *
  875. * Releases the semaphore which the caller holds on the console system
  876. * and the console driver list.
  877. *
  878. * While the semaphore was held, console output may have been buffered
  879. * by printk(). If this is the case, release_console_sem() emits
  880. * the output prior to releasing the semaphore.
  881. *
  882. * If there is output waiting for klogd, we wake it up.
  883. *
  884. * release_console_sem() may be called from any context.
  885. */
  886. void release_console_sem(void)
  887. {
  888. unsigned long flags;
  889. unsigned _con_start, _log_end;
  890. unsigned wake_klogd = 0;
  891. if (console_suspended) {
  892. up(&console_sem);
  893. return;
  894. }
  895. console_may_schedule = 0;
  896. for ( ; ; ) {
  897. spin_lock_irqsave(&logbuf_lock, flags);
  898. wake_klogd |= log_start - log_end;
  899. if (con_start == log_end)
  900. break; /* Nothing to print */
  901. _con_start = con_start;
  902. _log_end = log_end;
  903. con_start = log_end; /* Flush */
  904. spin_unlock(&logbuf_lock);
  905. stop_critical_timings(); /* don't trace print latency */
  906. call_console_drivers(_con_start, _log_end);
  907. start_critical_timings();
  908. local_irq_restore(flags);
  909. }
  910. console_locked = 0;
  911. up(&console_sem);
  912. spin_unlock_irqrestore(&logbuf_lock, flags);
  913. if (wake_klogd)
  914. wake_up_klogd();
  915. }
  916. EXPORT_SYMBOL(release_console_sem);
  917. /**
  918. * console_conditional_schedule - yield the CPU if required
  919. *
  920. * If the console code is currently allowed to sleep, and
  921. * if this CPU should yield the CPU to another task, do
  922. * so here.
  923. *
  924. * Must be called within acquire_console_sem().
  925. */
  926. void __sched console_conditional_schedule(void)
  927. {
  928. if (console_may_schedule)
  929. cond_resched();
  930. }
  931. EXPORT_SYMBOL(console_conditional_schedule);
  932. void console_print(const char *s)
  933. {
  934. printk(KERN_EMERG "%s", s);
  935. }
  936. EXPORT_SYMBOL(console_print);
  937. void console_unblank(void)
  938. {
  939. struct console *c;
  940. /*
  941. * console_unblank can no longer be called in interrupt context unless
  942. * oops_in_progress is set to 1..
  943. */
  944. if (oops_in_progress) {
  945. if (down_trylock(&console_sem) != 0)
  946. return;
  947. } else
  948. acquire_console_sem();
  949. console_locked = 1;
  950. console_may_schedule = 0;
  951. for (c = console_drivers; c != NULL; c = c->next)
  952. if ((c->flags & CON_ENABLED) && c->unblank)
  953. c->unblank();
  954. release_console_sem();
  955. }
  956. /*
  957. * Return the console tty driver structure and its associated index
  958. */
  959. struct tty_driver *console_device(int *index)
  960. {
  961. struct console *c;
  962. struct tty_driver *driver = NULL;
  963. acquire_console_sem();
  964. for (c = console_drivers; c != NULL; c = c->next) {
  965. if (!c->device)
  966. continue;
  967. driver = c->device(c, index);
  968. if (driver)
  969. break;
  970. }
  971. release_console_sem();
  972. return driver;
  973. }
  974. /*
  975. * Prevent further output on the passed console device so that (for example)
  976. * serial drivers can disable console output before suspending a port, and can
  977. * re-enable output afterwards.
  978. */
  979. void console_stop(struct console *console)
  980. {
  981. acquire_console_sem();
  982. console->flags &= ~CON_ENABLED;
  983. release_console_sem();
  984. }
  985. EXPORT_SYMBOL(console_stop);
  986. void console_start(struct console *console)
  987. {
  988. acquire_console_sem();
  989. console->flags |= CON_ENABLED;
  990. release_console_sem();
  991. }
  992. EXPORT_SYMBOL(console_start);
  993. /*
  994. * The console driver calls this routine during kernel initialization
  995. * to register the console printing procedure with printk() and to
  996. * print any messages that were printed by the kernel before the
  997. * console driver was initialized.
  998. */
  999. void register_console(struct console *console)
  1000. {
  1001. int i;
  1002. unsigned long flags;
  1003. struct console *bootconsole = NULL;
  1004. if (console_drivers) {
  1005. if (console->flags & CON_BOOT)
  1006. return;
  1007. if (console_drivers->flags & CON_BOOT)
  1008. bootconsole = console_drivers;
  1009. }
  1010. if (preferred_console < 0 || bootconsole || !console_drivers)
  1011. preferred_console = selected_console;
  1012. if (console->early_setup)
  1013. console->early_setup();
  1014. /*
  1015. * See if we want to use this console driver. If we
  1016. * didn't select a console we take the first one
  1017. * that registers here.
  1018. */
  1019. if (preferred_console < 0) {
  1020. if (console->index < 0)
  1021. console->index = 0;
  1022. if (console->setup == NULL ||
  1023. console->setup(console, NULL) == 0) {
  1024. console->flags |= CON_ENABLED;
  1025. if (console->device) {
  1026. console->flags |= CON_CONSDEV;
  1027. preferred_console = 0;
  1028. }
  1029. }
  1030. }
  1031. /*
  1032. * See if this console matches one we selected on
  1033. * the command line.
  1034. */
  1035. for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0];
  1036. i++) {
  1037. if (strcmp(console_cmdline[i].name, console->name) != 0)
  1038. continue;
  1039. if (console->index >= 0 &&
  1040. console->index != console_cmdline[i].index)
  1041. continue;
  1042. if (console->index < 0)
  1043. console->index = console_cmdline[i].index;
  1044. #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
  1045. if (console_cmdline[i].brl_options) {
  1046. console->flags |= CON_BRL;
  1047. braille_register_console(console,
  1048. console_cmdline[i].index,
  1049. console_cmdline[i].options,
  1050. console_cmdline[i].brl_options);
  1051. return;
  1052. }
  1053. #endif
  1054. if (console->setup &&
  1055. console->setup(console, console_cmdline[i].options) != 0)
  1056. break;
  1057. console->flags |= CON_ENABLED;
  1058. console->index = console_cmdline[i].index;
  1059. if (i == selected_console) {
  1060. console->flags |= CON_CONSDEV;
  1061. preferred_console = selected_console;
  1062. }
  1063. break;
  1064. }
  1065. if (!(console->flags & CON_ENABLED))
  1066. return;
  1067. if (bootconsole && (console->flags & CON_CONSDEV)) {
  1068. printk(KERN_INFO "console handover: boot [%s%d] -> real [%s%d]\n",
  1069. bootconsole->name, bootconsole->index,
  1070. console->name, console->index);
  1071. unregister_console(bootconsole);
  1072. console->flags &= ~CON_PRINTBUFFER;
  1073. } else {
  1074. printk(KERN_INFO "console [%s%d] enabled\n",
  1075. console->name, console->index);
  1076. }
  1077. /*
  1078. * Put this console in the list - keep the
  1079. * preferred driver at the head of the list.
  1080. */
  1081. acquire_console_sem();
  1082. if ((console->flags & CON_CONSDEV) || console_drivers == NULL) {
  1083. console->next = console_drivers;
  1084. console_drivers = console;
  1085. if (console->next)
  1086. console->next->flags &= ~CON_CONSDEV;
  1087. } else {
  1088. console->next = console_drivers->next;
  1089. console_drivers->next = console;
  1090. }
  1091. if (console->flags & CON_PRINTBUFFER) {
  1092. /*
  1093. * release_console_sem() will print out the buffered messages
  1094. * for us.
  1095. */
  1096. spin_lock_irqsave(&logbuf_lock, flags);
  1097. con_start = log_start;
  1098. spin_unlock_irqrestore(&logbuf_lock, flags);
  1099. }
  1100. release_console_sem();
  1101. }
  1102. EXPORT_SYMBOL(register_console);
  1103. int unregister_console(struct console *console)
  1104. {
  1105. struct console *a, *b;
  1106. int res = 1;
  1107. #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
  1108. if (console->flags & CON_BRL)
  1109. return braille_unregister_console(console);
  1110. #endif
  1111. acquire_console_sem();
  1112. if (console_drivers == console) {
  1113. console_drivers=console->next;
  1114. res = 0;
  1115. } else if (console_drivers) {
  1116. for (a=console_drivers->next, b=console_drivers ;
  1117. a; b=a, a=b->next) {
  1118. if (a == console) {
  1119. b->next = a->next;
  1120. res = 0;
  1121. break;
  1122. }
  1123. }
  1124. }
  1125. /*
  1126. * If this isn't the last console and it has CON_CONSDEV set, we
  1127. * need to set it on the next preferred console.
  1128. */
  1129. if (console_drivers != NULL && console->flags & CON_CONSDEV)
  1130. console_drivers->flags |= CON_CONSDEV;
  1131. release_console_sem();
  1132. return res;
  1133. }
  1134. EXPORT_SYMBOL(unregister_console);
  1135. static int __init disable_boot_consoles(void)
  1136. {
  1137. if (console_drivers != NULL) {
  1138. if (console_drivers->flags & CON_BOOT) {
  1139. printk(KERN_INFO "turn off boot console %s%d\n",
  1140. console_drivers->name, console_drivers->index);
  1141. return unregister_console(console_drivers);
  1142. }
  1143. }
  1144. return 0;
  1145. }
  1146. late_initcall(disable_boot_consoles);
  1147. #if defined CONFIG_PRINTK
  1148. /*
  1149. * printk rate limiting, lifted from the networking subsystem.
  1150. *
  1151. * This enforces a rate limit: not more than 10 kernel messages
  1152. * every 5s to make a denial-of-service attack impossible.
  1153. */
  1154. DEFINE_RATELIMIT_STATE(printk_ratelimit_state, 5 * HZ, 10);
  1155. int printk_ratelimit(void)
  1156. {
  1157. return __ratelimit(&printk_ratelimit_state);
  1158. }
  1159. EXPORT_SYMBOL(printk_ratelimit);
  1160. /**
  1161. * printk_timed_ratelimit - caller-controlled printk ratelimiting
  1162. * @caller_jiffies: pointer to caller's state
  1163. * @interval_msecs: minimum interval between prints
  1164. *
  1165. * printk_timed_ratelimit() returns true if more than @interval_msecs
  1166. * milliseconds have elapsed since the last time printk_timed_ratelimit()
  1167. * returned true.
  1168. */
  1169. bool printk_timed_ratelimit(unsigned long *caller_jiffies,
  1170. unsigned int interval_msecs)
  1171. {
  1172. if (*caller_jiffies == 0
  1173. || !time_in_range(jiffies, *caller_jiffies,
  1174. *caller_jiffies
  1175. + msecs_to_jiffies(interval_msecs))) {
  1176. *caller_jiffies = jiffies;
  1177. return true;
  1178. }
  1179. return false;
  1180. }
  1181. EXPORT_SYMBOL(printk_timed_ratelimit);
  1182. #endif