printk.c 32 KB

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