printk.c 32 KB

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