printk.c 33 KB

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