printk.c 34 KB

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