printk.c 33 KB

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