printk.c 32 KB

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