printk.c 33 KB

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