printk.c 32 KB

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