printk.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324
  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. /* Check if we have any console registered that can be called early in boot. */
  504. static int have_callable_console(void)
  505. {
  506. struct console *con;
  507. for (con = console_drivers; con; con = con->next)
  508. if (con->flags & CON_ANYTIME)
  509. return 1;
  510. return 0;
  511. }
  512. /**
  513. * printk - print a kernel message
  514. * @fmt: format string
  515. *
  516. * This is printk(). It can be called from any context. We want it to work.
  517. * Be aware of the fact that if oops_in_progress is not set, we might try to
  518. * wake klogd up which could deadlock on runqueue lock if printk() is called
  519. * from scheduler code.
  520. *
  521. * We try to grab the console_sem. If we succeed, it's easy - we log the output and
  522. * call the console drivers. If we fail to get the semaphore we place the output
  523. * into the log buffer and return. The current holder of the console_sem will
  524. * notice the new output in release_console_sem() and will send it to the
  525. * consoles before releasing the semaphore.
  526. *
  527. * One effect of this deferred printing is that code which calls printk() and
  528. * then changes console_loglevel may break. This is because console_loglevel
  529. * is inspected when the actual printing occurs.
  530. *
  531. * See also:
  532. * printf(3)
  533. */
  534. asmlinkage int printk(const char *fmt, ...)
  535. {
  536. va_list args;
  537. int r;
  538. va_start(args, fmt);
  539. r = vprintk(fmt, args);
  540. va_end(args);
  541. return r;
  542. }
  543. /* cpu currently holding logbuf_lock */
  544. static volatile unsigned int printk_cpu = UINT_MAX;
  545. const char printk_recursion_bug_msg [] =
  546. KERN_CRIT "BUG: recent printk recursion!\n";
  547. static int printk_recursion_bug;
  548. asmlinkage int vprintk(const char *fmt, va_list args)
  549. {
  550. static int log_level_unknown = 1;
  551. static char printk_buf[1024];
  552. unsigned long flags;
  553. int printed_len = 0;
  554. int this_cpu;
  555. char *p;
  556. boot_delay_msec();
  557. preempt_disable();
  558. /* This stops the holder of console_sem just where we want him */
  559. raw_local_irq_save(flags);
  560. this_cpu = smp_processor_id();
  561. /*
  562. * Ouch, printk recursed into itself!
  563. */
  564. if (unlikely(printk_cpu == this_cpu)) {
  565. /*
  566. * If a crash is occurring during printk() on this CPU,
  567. * then try to get the crash message out but make sure
  568. * we can't deadlock. Otherwise just return to avoid the
  569. * recursion and return - but flag the recursion so that
  570. * it can be printed at the next appropriate moment:
  571. */
  572. if (!oops_in_progress) {
  573. printk_recursion_bug = 1;
  574. goto out_restore_irqs;
  575. }
  576. zap_locks();
  577. }
  578. lockdep_off();
  579. spin_lock(&logbuf_lock);
  580. printk_cpu = this_cpu;
  581. if (printk_recursion_bug) {
  582. printk_recursion_bug = 0;
  583. strcpy(printk_buf, printk_recursion_bug_msg);
  584. printed_len = sizeof(printk_recursion_bug_msg);
  585. }
  586. /* Emit the output into the temporary buffer */
  587. printed_len += vscnprintf(printk_buf + printed_len,
  588. sizeof(printk_buf) - printed_len, fmt, args);
  589. /*
  590. * Copy the output into log_buf. If the caller didn't provide
  591. * appropriate log level tags, we insert them here
  592. */
  593. for (p = printk_buf; *p; p++) {
  594. if (log_level_unknown) {
  595. /* log_level_unknown signals the start of a new line */
  596. if (printk_time) {
  597. int loglev_char;
  598. char tbuf[50], *tp;
  599. unsigned tlen;
  600. unsigned long long t;
  601. unsigned long nanosec_rem;
  602. /*
  603. * force the log level token to be
  604. * before the time output.
  605. */
  606. if (p[0] == '<' && p[1] >='0' &&
  607. p[1] <= '7' && p[2] == '>') {
  608. loglev_char = p[1];
  609. p += 3;
  610. printed_len -= 3;
  611. } else {
  612. loglev_char = default_message_loglevel
  613. + '0';
  614. }
  615. t = cpu_clock(printk_cpu);
  616. nanosec_rem = do_div(t, 1000000000);
  617. tlen = sprintf(tbuf,
  618. "<%c>[%5lu.%06lu] ",
  619. loglev_char,
  620. (unsigned long)t,
  621. nanosec_rem/1000);
  622. for (tp = tbuf; tp < tbuf + tlen; tp++)
  623. emit_log_char(*tp);
  624. printed_len += tlen;
  625. } else {
  626. if (p[0] != '<' || p[1] < '0' ||
  627. p[1] > '7' || p[2] != '>') {
  628. emit_log_char('<');
  629. emit_log_char(default_message_loglevel
  630. + '0');
  631. emit_log_char('>');
  632. printed_len += 3;
  633. }
  634. }
  635. log_level_unknown = 0;
  636. if (!*p)
  637. break;
  638. }
  639. emit_log_char(*p);
  640. if (*p == '\n')
  641. log_level_unknown = 1;
  642. }
  643. if (!down_trylock(&console_sem)) {
  644. /*
  645. * We own the drivers. We can drop the spinlock and
  646. * let release_console_sem() print the text, maybe ...
  647. */
  648. console_locked = 1;
  649. printk_cpu = UINT_MAX;
  650. spin_unlock(&logbuf_lock);
  651. /*
  652. * Console drivers may assume that per-cpu resources have
  653. * been allocated. So unless they're explicitly marked as
  654. * being able to cope (CON_ANYTIME) don't call them until
  655. * this CPU is officially up.
  656. */
  657. if (cpu_online(smp_processor_id()) || have_callable_console()) {
  658. console_may_schedule = 0;
  659. release_console_sem();
  660. } else {
  661. /* Release by hand to avoid flushing the buffer. */
  662. console_locked = 0;
  663. up(&console_sem);
  664. }
  665. lockdep_on();
  666. raw_local_irq_restore(flags);
  667. } else {
  668. /*
  669. * Someone else owns the drivers. We drop the spinlock, which
  670. * allows the semaphore holder to proceed and to call the
  671. * console drivers with the output which we just produced.
  672. */
  673. printk_cpu = UINT_MAX;
  674. spin_unlock(&logbuf_lock);
  675. lockdep_on();
  676. out_restore_irqs:
  677. raw_local_irq_restore(flags);
  678. }
  679. preempt_enable();
  680. return printed_len;
  681. }
  682. EXPORT_SYMBOL(printk);
  683. EXPORT_SYMBOL(vprintk);
  684. #else
  685. asmlinkage long sys_syslog(int type, char __user *buf, int len)
  686. {
  687. return -ENOSYS;
  688. }
  689. static void call_console_drivers(unsigned start, unsigned end)
  690. {
  691. }
  692. #endif
  693. /*
  694. * Set up a list of consoles. Called from init/main.c
  695. */
  696. static int __init console_setup(char *str)
  697. {
  698. char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for index */
  699. char *s, *options;
  700. int idx;
  701. /*
  702. * Decode str into name, index, options.
  703. */
  704. if (str[0] >= '0' && str[0] <= '9') {
  705. strcpy(buf, "ttyS");
  706. strncpy(buf + 4, str, sizeof(buf) - 5);
  707. } else {
  708. strncpy(buf, str, sizeof(buf) - 1);
  709. }
  710. buf[sizeof(buf) - 1] = 0;
  711. if ((options = strchr(str, ',')) != NULL)
  712. *(options++) = 0;
  713. #ifdef __sparc__
  714. if (!strcmp(str, "ttya"))
  715. strcpy(buf, "ttyS0");
  716. if (!strcmp(str, "ttyb"))
  717. strcpy(buf, "ttyS1");
  718. #endif
  719. for (s = buf; *s; s++)
  720. if ((*s >= '0' && *s <= '9') || *s == ',')
  721. break;
  722. idx = simple_strtoul(s, NULL, 10);
  723. *s = 0;
  724. add_preferred_console(buf, idx, options);
  725. return 1;
  726. }
  727. __setup("console=", console_setup);
  728. /**
  729. * add_preferred_console - add a device to the list of preferred consoles.
  730. * @name: device name
  731. * @idx: device index
  732. * @options: options for this console
  733. *
  734. * The last preferred console added will be used for kernel messages
  735. * and stdin/out/err for init. Normally this is used by console_setup
  736. * above to handle user-supplied console arguments; however it can also
  737. * be used by arch-specific code either to override the user or more
  738. * commonly to provide a default console (ie from PROM variables) when
  739. * the user has not supplied one.
  740. */
  741. int add_preferred_console(char *name, int idx, char *options)
  742. {
  743. struct console_cmdline *c;
  744. int i;
  745. /*
  746. * See if this tty is not yet registered, and
  747. * if we have a slot free.
  748. */
  749. for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
  750. if (strcmp(console_cmdline[i].name, name) == 0 &&
  751. console_cmdline[i].index == idx) {
  752. selected_console = i;
  753. return 0;
  754. }
  755. if (i == MAX_CMDLINECONSOLES)
  756. return -E2BIG;
  757. selected_console = i;
  758. c = &console_cmdline[i];
  759. memcpy(c->name, name, sizeof(c->name));
  760. c->name[sizeof(c->name) - 1] = 0;
  761. c->options = options;
  762. c->index = idx;
  763. return 0;
  764. }
  765. int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options)
  766. {
  767. struct console_cmdline *c;
  768. int i;
  769. for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
  770. if (strcmp(console_cmdline[i].name, name) == 0 &&
  771. console_cmdline[i].index == idx) {
  772. c = &console_cmdline[i];
  773. memcpy(c->name, name_new, sizeof(c->name));
  774. c->name[sizeof(c->name) - 1] = 0;
  775. c->options = options;
  776. c->index = idx_new;
  777. return i;
  778. }
  779. /* not found */
  780. return -1;
  781. }
  782. int console_suspend_enabled = 1;
  783. EXPORT_SYMBOL(console_suspend_enabled);
  784. static int __init console_suspend_disable(char *str)
  785. {
  786. console_suspend_enabled = 0;
  787. return 1;
  788. }
  789. __setup("no_console_suspend", console_suspend_disable);
  790. /**
  791. * suspend_console - suspend the console subsystem
  792. *
  793. * This disables printk() while we go into suspend states
  794. */
  795. void suspend_console(void)
  796. {
  797. if (!console_suspend_enabled)
  798. return;
  799. printk("Suspending console(s)\n");
  800. acquire_console_sem();
  801. console_suspended = 1;
  802. }
  803. void resume_console(void)
  804. {
  805. if (!console_suspend_enabled)
  806. return;
  807. console_suspended = 0;
  808. release_console_sem();
  809. }
  810. /**
  811. * acquire_console_sem - lock the console system for exclusive use.
  812. *
  813. * Acquires a semaphore which guarantees that the caller has
  814. * exclusive access to the console system and the console_drivers list.
  815. *
  816. * Can sleep, returns nothing.
  817. */
  818. void acquire_console_sem(void)
  819. {
  820. BUG_ON(in_interrupt());
  821. if (console_suspended) {
  822. down(&secondary_console_sem);
  823. return;
  824. }
  825. down(&console_sem);
  826. console_locked = 1;
  827. console_may_schedule = 1;
  828. }
  829. EXPORT_SYMBOL(acquire_console_sem);
  830. int try_acquire_console_sem(void)
  831. {
  832. if (down_trylock(&console_sem))
  833. return -1;
  834. console_locked = 1;
  835. console_may_schedule = 0;
  836. return 0;
  837. }
  838. EXPORT_SYMBOL(try_acquire_console_sem);
  839. int is_console_locked(void)
  840. {
  841. return console_locked;
  842. }
  843. void wake_up_klogd(void)
  844. {
  845. if (!oops_in_progress && waitqueue_active(&log_wait))
  846. wake_up_interruptible(&log_wait);
  847. }
  848. /**
  849. * release_console_sem - unlock the console system
  850. *
  851. * Releases the semaphore which the caller holds on the console system
  852. * and the console driver list.
  853. *
  854. * While the semaphore was held, console output may have been buffered
  855. * by printk(). If this is the case, release_console_sem() emits
  856. * the output prior to releasing the semaphore.
  857. *
  858. * If there is output waiting for klogd, we wake it up.
  859. *
  860. * release_console_sem() may be called from any context.
  861. */
  862. void release_console_sem(void)
  863. {
  864. unsigned long flags;
  865. unsigned _con_start, _log_end;
  866. unsigned wake_klogd = 0;
  867. if (console_suspended) {
  868. up(&secondary_console_sem);
  869. return;
  870. }
  871. console_may_schedule = 0;
  872. for ( ; ; ) {
  873. spin_lock_irqsave(&logbuf_lock, flags);
  874. wake_klogd |= log_start - log_end;
  875. if (con_start == log_end)
  876. break; /* Nothing to print */
  877. _con_start = con_start;
  878. _log_end = log_end;
  879. con_start = log_end; /* Flush */
  880. spin_unlock(&logbuf_lock);
  881. call_console_drivers(_con_start, _log_end);
  882. local_irq_restore(flags);
  883. }
  884. console_locked = 0;
  885. up(&console_sem);
  886. spin_unlock_irqrestore(&logbuf_lock, flags);
  887. if (wake_klogd)
  888. wake_up_klogd();
  889. }
  890. EXPORT_SYMBOL(release_console_sem);
  891. /**
  892. * console_conditional_schedule - yield the CPU if required
  893. *
  894. * If the console code is currently allowed to sleep, and
  895. * if this CPU should yield the CPU to another task, do
  896. * so here.
  897. *
  898. * Must be called within acquire_console_sem().
  899. */
  900. void __sched console_conditional_schedule(void)
  901. {
  902. if (console_may_schedule)
  903. cond_resched();
  904. }
  905. EXPORT_SYMBOL(console_conditional_schedule);
  906. void console_print(const char *s)
  907. {
  908. printk(KERN_EMERG "%s", s);
  909. }
  910. EXPORT_SYMBOL(console_print);
  911. void console_unblank(void)
  912. {
  913. struct console *c;
  914. /*
  915. * console_unblank can no longer be called in interrupt context unless
  916. * oops_in_progress is set to 1..
  917. */
  918. if (oops_in_progress) {
  919. if (down_trylock(&console_sem) != 0)
  920. return;
  921. } else
  922. acquire_console_sem();
  923. console_locked = 1;
  924. console_may_schedule = 0;
  925. for (c = console_drivers; c != NULL; c = c->next)
  926. if ((c->flags & CON_ENABLED) && c->unblank)
  927. c->unblank();
  928. release_console_sem();
  929. }
  930. /*
  931. * Return the console tty driver structure and its associated index
  932. */
  933. struct tty_driver *console_device(int *index)
  934. {
  935. struct console *c;
  936. struct tty_driver *driver = NULL;
  937. acquire_console_sem();
  938. for (c = console_drivers; c != NULL; c = c->next) {
  939. if (!c->device)
  940. continue;
  941. driver = c->device(c, index);
  942. if (driver)
  943. break;
  944. }
  945. release_console_sem();
  946. return driver;
  947. }
  948. /*
  949. * Prevent further output on the passed console device so that (for example)
  950. * serial drivers can disable console output before suspending a port, and can
  951. * re-enable output afterwards.
  952. */
  953. void console_stop(struct console *console)
  954. {
  955. acquire_console_sem();
  956. console->flags &= ~CON_ENABLED;
  957. release_console_sem();
  958. }
  959. EXPORT_SYMBOL(console_stop);
  960. void console_start(struct console *console)
  961. {
  962. acquire_console_sem();
  963. console->flags |= CON_ENABLED;
  964. release_console_sem();
  965. }
  966. EXPORT_SYMBOL(console_start);
  967. /*
  968. * The console driver calls this routine during kernel initialization
  969. * to register the console printing procedure with printk() and to
  970. * print any messages that were printed by the kernel before the
  971. * console driver was initialized.
  972. */
  973. void register_console(struct console *console)
  974. {
  975. int i;
  976. unsigned long flags;
  977. struct console *bootconsole = NULL;
  978. if (console_drivers) {
  979. if (console->flags & CON_BOOT)
  980. return;
  981. if (console_drivers->flags & CON_BOOT)
  982. bootconsole = console_drivers;
  983. }
  984. if (preferred_console < 0 || bootconsole || !console_drivers)
  985. preferred_console = selected_console;
  986. if (console->early_setup)
  987. console->early_setup();
  988. /*
  989. * See if we want to use this console driver. If we
  990. * didn't select a console we take the first one
  991. * that registers here.
  992. */
  993. if (preferred_console < 0) {
  994. if (console->index < 0)
  995. console->index = 0;
  996. if (console->setup == NULL ||
  997. console->setup(console, NULL) == 0) {
  998. console->flags |= CON_ENABLED | CON_CONSDEV;
  999. preferred_console = 0;
  1000. }
  1001. }
  1002. /*
  1003. * See if this console matches one we selected on
  1004. * the command line.
  1005. */
  1006. for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0];
  1007. i++) {
  1008. if (strcmp(console_cmdline[i].name, console->name) != 0)
  1009. continue;
  1010. if (console->index >= 0 &&
  1011. console->index != console_cmdline[i].index)
  1012. continue;
  1013. if (console->index < 0)
  1014. console->index = console_cmdline[i].index;
  1015. if (console->setup &&
  1016. console->setup(console, console_cmdline[i].options) != 0)
  1017. break;
  1018. console->flags |= CON_ENABLED;
  1019. console->index = console_cmdline[i].index;
  1020. if (i == selected_console) {
  1021. console->flags |= CON_CONSDEV;
  1022. preferred_console = selected_console;
  1023. }
  1024. break;
  1025. }
  1026. if (!(console->flags & CON_ENABLED))
  1027. return;
  1028. if (bootconsole && (console->flags & CON_CONSDEV)) {
  1029. printk(KERN_INFO "console handover: boot [%s%d] -> real [%s%d]\n",
  1030. bootconsole->name, bootconsole->index,
  1031. console->name, console->index);
  1032. unregister_console(bootconsole);
  1033. console->flags &= ~CON_PRINTBUFFER;
  1034. } else {
  1035. printk(KERN_INFO "console [%s%d] enabled\n",
  1036. console->name, console->index);
  1037. }
  1038. /*
  1039. * Put this console in the list - keep the
  1040. * preferred driver at the head of the list.
  1041. */
  1042. acquire_console_sem();
  1043. if ((console->flags & CON_CONSDEV) || console_drivers == NULL) {
  1044. console->next = console_drivers;
  1045. console_drivers = console;
  1046. if (console->next)
  1047. console->next->flags &= ~CON_CONSDEV;
  1048. } else {
  1049. console->next = console_drivers->next;
  1050. console_drivers->next = console;
  1051. }
  1052. if (console->flags & CON_PRINTBUFFER) {
  1053. /*
  1054. * release_console_sem() will print out the buffered messages
  1055. * for us.
  1056. */
  1057. spin_lock_irqsave(&logbuf_lock, flags);
  1058. con_start = log_start;
  1059. spin_unlock_irqrestore(&logbuf_lock, flags);
  1060. }
  1061. release_console_sem();
  1062. }
  1063. EXPORT_SYMBOL(register_console);
  1064. int unregister_console(struct console *console)
  1065. {
  1066. struct console *a, *b;
  1067. int res = 1;
  1068. acquire_console_sem();
  1069. if (console_drivers == console) {
  1070. console_drivers=console->next;
  1071. res = 0;
  1072. } else if (console_drivers) {
  1073. for (a=console_drivers->next, b=console_drivers ;
  1074. a; b=a, a=b->next) {
  1075. if (a == console) {
  1076. b->next = a->next;
  1077. res = 0;
  1078. break;
  1079. }
  1080. }
  1081. }
  1082. /*
  1083. * If this isn't the last console and it has CON_CONSDEV set, we
  1084. * need to set it on the next preferred console.
  1085. */
  1086. if (console_drivers != NULL && console->flags & CON_CONSDEV)
  1087. console_drivers->flags |= CON_CONSDEV;
  1088. release_console_sem();
  1089. return res;
  1090. }
  1091. EXPORT_SYMBOL(unregister_console);
  1092. static int __init disable_boot_consoles(void)
  1093. {
  1094. if (console_drivers != NULL) {
  1095. if (console_drivers->flags & CON_BOOT) {
  1096. printk(KERN_INFO "turn off boot console %s%d\n",
  1097. console_drivers->name, console_drivers->index);
  1098. return unregister_console(console_drivers);
  1099. }
  1100. }
  1101. return 0;
  1102. }
  1103. late_initcall(disable_boot_consoles);
  1104. /**
  1105. * tty_write_message - write a message to a certain tty, not just the console.
  1106. * @tty: the destination tty_struct
  1107. * @msg: the message to write
  1108. *
  1109. * This is used for messages that need to be redirected to a specific tty.
  1110. * We don't put it into the syslog queue right now maybe in the future if
  1111. * really needed.
  1112. */
  1113. void tty_write_message(struct tty_struct *tty, char *msg)
  1114. {
  1115. if (tty && tty->driver->write)
  1116. tty->driver->write(tty, msg, strlen(msg));
  1117. return;
  1118. }
  1119. #if defined CONFIG_PRINTK
  1120. /*
  1121. * printk rate limiting, lifted from the networking subsystem.
  1122. *
  1123. * This enforces a rate limit: not more than one kernel message
  1124. * every printk_ratelimit_jiffies to make a denial-of-service
  1125. * attack impossible.
  1126. */
  1127. int __printk_ratelimit(int ratelimit_jiffies, int ratelimit_burst)
  1128. {
  1129. static DEFINE_SPINLOCK(ratelimit_lock);
  1130. static unsigned toks = 10 * 5 * HZ;
  1131. static unsigned long last_msg;
  1132. static int missed;
  1133. unsigned long flags;
  1134. unsigned long now = jiffies;
  1135. spin_lock_irqsave(&ratelimit_lock, flags);
  1136. toks += now - last_msg;
  1137. last_msg = now;
  1138. if (toks > (ratelimit_burst * ratelimit_jiffies))
  1139. toks = ratelimit_burst * ratelimit_jiffies;
  1140. if (toks >= ratelimit_jiffies) {
  1141. int lost = missed;
  1142. missed = 0;
  1143. toks -= ratelimit_jiffies;
  1144. spin_unlock_irqrestore(&ratelimit_lock, flags);
  1145. if (lost)
  1146. printk(KERN_WARNING "printk: %d messages suppressed.\n", lost);
  1147. return 1;
  1148. }
  1149. missed++;
  1150. spin_unlock_irqrestore(&ratelimit_lock, flags);
  1151. return 0;
  1152. }
  1153. EXPORT_SYMBOL(__printk_ratelimit);
  1154. /* minimum time in jiffies between messages */
  1155. int printk_ratelimit_jiffies = 5 * HZ;
  1156. /* number of messages we send before ratelimiting */
  1157. int printk_ratelimit_burst = 10;
  1158. int printk_ratelimit(void)
  1159. {
  1160. return __printk_ratelimit(printk_ratelimit_jiffies,
  1161. printk_ratelimit_burst);
  1162. }
  1163. EXPORT_SYMBOL(printk_ratelimit);
  1164. /**
  1165. * printk_timed_ratelimit - caller-controlled printk ratelimiting
  1166. * @caller_jiffies: pointer to caller's state
  1167. * @interval_msecs: minimum interval between prints
  1168. *
  1169. * printk_timed_ratelimit() returns true if more than @interval_msecs
  1170. * milliseconds have elapsed since the last time printk_timed_ratelimit()
  1171. * returned true.
  1172. */
  1173. bool printk_timed_ratelimit(unsigned long *caller_jiffies,
  1174. unsigned int interval_msecs)
  1175. {
  1176. if (*caller_jiffies == 0 || time_after(jiffies, *caller_jiffies)) {
  1177. *caller_jiffies = jiffies + msecs_to_jiffies(interval_msecs);
  1178. return true;
  1179. }
  1180. return false;
  1181. }
  1182. EXPORT_SYMBOL(printk_timed_ratelimit);
  1183. #endif