printk.c 33 KB

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