printk.c 32 KB

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