printk.c 33 KB

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