printk.c 33 KB

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