printk.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691
  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
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/mm.h>
  20. #include <linux/tty.h>
  21. #include <linux/tty_driver.h>
  22. #include <linux/console.h>
  23. #include <linux/init.h>
  24. #include <linux/jiffies.h>
  25. #include <linux/nmi.h>
  26. #include <linux/module.h>
  27. #include <linux/moduleparam.h>
  28. #include <linux/interrupt.h> /* For in_interrupt() */
  29. #include <linux/delay.h>
  30. #include <linux/smp.h>
  31. #include <linux/security.h>
  32. #include <linux/bootmem.h>
  33. #include <linux/syscalls.h>
  34. #include <linux/kexec.h>
  35. #include <linux/kdb.h>
  36. #include <linux/ratelimit.h>
  37. #include <linux/kmsg_dump.h>
  38. #include <linux/syslog.h>
  39. #include <linux/cpu.h>
  40. #include <linux/notifier.h>
  41. #include <linux/rculist.h>
  42. #include <asm/uaccess.h>
  43. /*
  44. * Architectures can override it:
  45. */
  46. void asmlinkage __attribute__((weak)) early_printk(const char *fmt, ...)
  47. {
  48. }
  49. #define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT)
  50. /* printk's without a loglevel use this.. */
  51. #define DEFAULT_MESSAGE_LOGLEVEL 4 /* KERN_WARNING */
  52. /* We show everything that is MORE important than this.. */
  53. #define MINIMUM_CONSOLE_LOGLEVEL 1 /* Minimum loglevel we let people use */
  54. #define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */
  55. DECLARE_WAIT_QUEUE_HEAD(log_wait);
  56. int console_printk[4] = {
  57. DEFAULT_CONSOLE_LOGLEVEL, /* console_loglevel */
  58. DEFAULT_MESSAGE_LOGLEVEL, /* default_message_loglevel */
  59. MINIMUM_CONSOLE_LOGLEVEL, /* minimum_console_loglevel */
  60. DEFAULT_CONSOLE_LOGLEVEL, /* default_console_loglevel */
  61. };
  62. /*
  63. * Low level drivers may need that to know if they can schedule in
  64. * their unblank() callback or not. So let's export it.
  65. */
  66. int oops_in_progress;
  67. EXPORT_SYMBOL(oops_in_progress);
  68. /*
  69. * console_sem protects the console_drivers list, and also
  70. * provides serialisation for access to the entire console
  71. * driver system.
  72. */
  73. static DEFINE_SEMAPHORE(console_sem);
  74. struct console *console_drivers;
  75. EXPORT_SYMBOL_GPL(console_drivers);
  76. /*
  77. * This is used for debugging the mess that is the VT code by
  78. * keeping track if we have the console semaphore held. It's
  79. * definitely not the perfect debug tool (we don't know if _WE_
  80. * hold it are racing, but it helps tracking those weird code
  81. * path in the console code where we end up in places I want
  82. * locked without the console sempahore held
  83. */
  84. static int console_locked, console_suspended;
  85. /*
  86. * logbuf_lock protects log_buf, log_start, log_end, con_start and logged_chars
  87. * It is also used in interesting ways to provide interlocking in
  88. * console_unlock();.
  89. */
  90. static DEFINE_SPINLOCK(logbuf_lock);
  91. #define LOG_BUF_MASK (log_buf_len-1)
  92. #define LOG_BUF(idx) (log_buf[(idx) & LOG_BUF_MASK])
  93. /*
  94. * The indices into log_buf are not constrained to log_buf_len - they
  95. * must be masked before subscripting
  96. */
  97. static unsigned log_start; /* Index into log_buf: next char to be read by syslog() */
  98. static unsigned con_start; /* Index into log_buf: next char to be sent to consoles */
  99. static unsigned log_end; /* Index into log_buf: most-recently-written-char + 1 */
  100. /*
  101. * Array of consoles built from command line options (console=)
  102. */
  103. struct console_cmdline
  104. {
  105. char name[8]; /* Name of the driver */
  106. int index; /* Minor dev. to use */
  107. char *options; /* Options for the driver */
  108. #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
  109. char *brl_options; /* Options for braille driver */
  110. #endif
  111. };
  112. #define MAX_CMDLINECONSOLES 8
  113. static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
  114. static int selected_console = -1;
  115. static int preferred_console = -1;
  116. int console_set_on_cmdline;
  117. EXPORT_SYMBOL(console_set_on_cmdline);
  118. /* Flag: console code may call schedule() */
  119. static int console_may_schedule;
  120. #ifdef CONFIG_PRINTK
  121. static char __log_buf[__LOG_BUF_LEN];
  122. static char *log_buf = __log_buf;
  123. static int log_buf_len = __LOG_BUF_LEN;
  124. static unsigned logged_chars; /* Number of chars produced since last read+clear operation */
  125. static int saved_console_loglevel = -1;
  126. #ifdef CONFIG_KEXEC
  127. /*
  128. * This appends the listed symbols to /proc/vmcoreinfo
  129. *
  130. * /proc/vmcoreinfo is used by various utiilties, like crash and makedumpfile to
  131. * obtain access to symbols that are otherwise very difficult to locate. These
  132. * symbols are specifically used so that utilities can access and extract the
  133. * dmesg log from a vmcore file after a crash.
  134. */
  135. void log_buf_kexec_setup(void)
  136. {
  137. VMCOREINFO_SYMBOL(log_buf);
  138. VMCOREINFO_SYMBOL(log_end);
  139. VMCOREINFO_SYMBOL(log_buf_len);
  140. VMCOREINFO_SYMBOL(logged_chars);
  141. }
  142. #endif
  143. static int __init log_buf_len_setup(char *str)
  144. {
  145. unsigned size = memparse(str, &str);
  146. unsigned long flags;
  147. if (size)
  148. size = roundup_pow_of_two(size);
  149. if (size > log_buf_len) {
  150. unsigned start, dest_idx, offset;
  151. char *new_log_buf;
  152. new_log_buf = alloc_bootmem(size);
  153. if (!new_log_buf) {
  154. printk(KERN_WARNING "log_buf_len: allocation failed\n");
  155. goto out;
  156. }
  157. spin_lock_irqsave(&logbuf_lock, flags);
  158. log_buf_len = size;
  159. log_buf = new_log_buf;
  160. offset = start = min(con_start, log_start);
  161. dest_idx = 0;
  162. while (start != log_end) {
  163. log_buf[dest_idx] = __log_buf[start & (__LOG_BUF_LEN - 1)];
  164. start++;
  165. dest_idx++;
  166. }
  167. log_start -= offset;
  168. con_start -= offset;
  169. log_end -= offset;
  170. spin_unlock_irqrestore(&logbuf_lock, flags);
  171. printk(KERN_NOTICE "log_buf_len: %d\n", log_buf_len);
  172. }
  173. out:
  174. return 1;
  175. }
  176. __setup("log_buf_len=", log_buf_len_setup);
  177. #ifdef CONFIG_BOOT_PRINTK_DELAY
  178. static int boot_delay; /* msecs delay after each printk during bootup */
  179. static unsigned long long loops_per_msec; /* based on boot_delay */
  180. static int __init boot_delay_setup(char *str)
  181. {
  182. unsigned long lpj;
  183. lpj = preset_lpj ? preset_lpj : 1000000; /* some guess */
  184. loops_per_msec = (unsigned long long)lpj / 1000 * HZ;
  185. get_option(&str, &boot_delay);
  186. if (boot_delay > 10 * 1000)
  187. boot_delay = 0;
  188. pr_debug("boot_delay: %u, preset_lpj: %ld, lpj: %lu, "
  189. "HZ: %d, loops_per_msec: %llu\n",
  190. boot_delay, preset_lpj, lpj, HZ, loops_per_msec);
  191. return 1;
  192. }
  193. __setup("boot_delay=", boot_delay_setup);
  194. static void boot_delay_msec(void)
  195. {
  196. unsigned long long k;
  197. unsigned long timeout;
  198. if (boot_delay == 0 || system_state != SYSTEM_BOOTING)
  199. return;
  200. k = (unsigned long long)loops_per_msec * boot_delay;
  201. timeout = jiffies + msecs_to_jiffies(boot_delay);
  202. while (k) {
  203. k--;
  204. cpu_relax();
  205. /*
  206. * use (volatile) jiffies to prevent
  207. * compiler reduction; loop termination via jiffies
  208. * is secondary and may or may not happen.
  209. */
  210. if (time_after(jiffies, timeout))
  211. break;
  212. touch_nmi_watchdog();
  213. }
  214. }
  215. #else
  216. static inline void boot_delay_msec(void)
  217. {
  218. }
  219. #endif
  220. #ifdef CONFIG_SECURITY_DMESG_RESTRICT
  221. int dmesg_restrict = 1;
  222. #else
  223. int dmesg_restrict;
  224. #endif
  225. static int syslog_action_restricted(int type)
  226. {
  227. if (dmesg_restrict)
  228. return 1;
  229. /* Unless restricted, we allow "read all" and "get buffer size" for everybody */
  230. return type != SYSLOG_ACTION_READ_ALL && type != SYSLOG_ACTION_SIZE_BUFFER;
  231. }
  232. static int check_syslog_permissions(int type, bool from_file)
  233. {
  234. /*
  235. * If this is from /proc/kmsg and we've already opened it, then we've
  236. * already done the capabilities checks at open time.
  237. */
  238. if (from_file && type != SYSLOG_ACTION_OPEN)
  239. return 0;
  240. if (syslog_action_restricted(type)) {
  241. if (capable(CAP_SYSLOG))
  242. return 0;
  243. /* For historical reasons, accept CAP_SYS_ADMIN too, with a warning */
  244. if (capable(CAP_SYS_ADMIN)) {
  245. WARN_ONCE(1, "Attempt to access syslog with CAP_SYS_ADMIN "
  246. "but no CAP_SYSLOG (deprecated).\n");
  247. return 0;
  248. }
  249. return -EPERM;
  250. }
  251. return 0;
  252. }
  253. int do_syslog(int type, char __user *buf, int len, bool from_file)
  254. {
  255. unsigned i, j, limit, count;
  256. int do_clear = 0;
  257. char c;
  258. int error;
  259. error = check_syslog_permissions(type, from_file);
  260. if (error)
  261. goto out;
  262. error = security_syslog(type);
  263. if (error)
  264. return error;
  265. switch (type) {
  266. case SYSLOG_ACTION_CLOSE: /* Close log */
  267. break;
  268. case SYSLOG_ACTION_OPEN: /* Open log */
  269. break;
  270. case SYSLOG_ACTION_READ: /* Read from log */
  271. error = -EINVAL;
  272. if (!buf || len < 0)
  273. goto out;
  274. error = 0;
  275. if (!len)
  276. goto out;
  277. if (!access_ok(VERIFY_WRITE, buf, len)) {
  278. error = -EFAULT;
  279. goto out;
  280. }
  281. error = wait_event_interruptible(log_wait,
  282. (log_start - log_end));
  283. if (error)
  284. goto out;
  285. i = 0;
  286. spin_lock_irq(&logbuf_lock);
  287. while (!error && (log_start != log_end) && i < len) {
  288. c = LOG_BUF(log_start);
  289. log_start++;
  290. spin_unlock_irq(&logbuf_lock);
  291. error = __put_user(c,buf);
  292. buf++;
  293. i++;
  294. cond_resched();
  295. spin_lock_irq(&logbuf_lock);
  296. }
  297. spin_unlock_irq(&logbuf_lock);
  298. if (!error)
  299. error = i;
  300. break;
  301. /* Read/clear last kernel messages */
  302. case SYSLOG_ACTION_READ_CLEAR:
  303. do_clear = 1;
  304. /* FALL THRU */
  305. /* Read last kernel messages */
  306. case SYSLOG_ACTION_READ_ALL:
  307. error = -EINVAL;
  308. if (!buf || len < 0)
  309. goto out;
  310. error = 0;
  311. if (!len)
  312. goto out;
  313. if (!access_ok(VERIFY_WRITE, buf, len)) {
  314. error = -EFAULT;
  315. goto out;
  316. }
  317. count = len;
  318. if (count > log_buf_len)
  319. count = log_buf_len;
  320. spin_lock_irq(&logbuf_lock);
  321. if (count > logged_chars)
  322. count = logged_chars;
  323. if (do_clear)
  324. logged_chars = 0;
  325. limit = log_end;
  326. /*
  327. * __put_user() could sleep, and while we sleep
  328. * printk() could overwrite the messages
  329. * we try to copy to user space. Therefore
  330. * the messages are copied in reverse. <manfreds>
  331. */
  332. for (i = 0; i < count && !error; i++) {
  333. j = limit-1-i;
  334. if (j + log_buf_len < log_end)
  335. break;
  336. c = LOG_BUF(j);
  337. spin_unlock_irq(&logbuf_lock);
  338. error = __put_user(c,&buf[count-1-i]);
  339. cond_resched();
  340. spin_lock_irq(&logbuf_lock);
  341. }
  342. spin_unlock_irq(&logbuf_lock);
  343. if (error)
  344. break;
  345. error = i;
  346. if (i != count) {
  347. int offset = count-error;
  348. /* buffer overflow during copy, correct user buffer. */
  349. for (i = 0; i < error; i++) {
  350. if (__get_user(c,&buf[i+offset]) ||
  351. __put_user(c,&buf[i])) {
  352. error = -EFAULT;
  353. break;
  354. }
  355. cond_resched();
  356. }
  357. }
  358. break;
  359. /* Clear ring buffer */
  360. case SYSLOG_ACTION_CLEAR:
  361. logged_chars = 0;
  362. break;
  363. /* Disable logging to console */
  364. case SYSLOG_ACTION_CONSOLE_OFF:
  365. if (saved_console_loglevel == -1)
  366. saved_console_loglevel = console_loglevel;
  367. console_loglevel = minimum_console_loglevel;
  368. break;
  369. /* Enable logging to console */
  370. case SYSLOG_ACTION_CONSOLE_ON:
  371. if (saved_console_loglevel != -1) {
  372. console_loglevel = saved_console_loglevel;
  373. saved_console_loglevel = -1;
  374. }
  375. break;
  376. /* Set level of messages printed to console */
  377. case SYSLOG_ACTION_CONSOLE_LEVEL:
  378. error = -EINVAL;
  379. if (len < 1 || len > 8)
  380. goto out;
  381. if (len < minimum_console_loglevel)
  382. len = minimum_console_loglevel;
  383. console_loglevel = len;
  384. /* Implicitly re-enable logging to console */
  385. saved_console_loglevel = -1;
  386. error = 0;
  387. break;
  388. /* Number of chars in the log buffer */
  389. case SYSLOG_ACTION_SIZE_UNREAD:
  390. error = log_end - log_start;
  391. break;
  392. /* Size of the log buffer */
  393. case SYSLOG_ACTION_SIZE_BUFFER:
  394. error = log_buf_len;
  395. break;
  396. default:
  397. error = -EINVAL;
  398. break;
  399. }
  400. out:
  401. return error;
  402. }
  403. SYSCALL_DEFINE3(syslog, int, type, char __user *, buf, int, len)
  404. {
  405. return do_syslog(type, buf, len, SYSLOG_FROM_CALL);
  406. }
  407. #ifdef CONFIG_KGDB_KDB
  408. /* kdb dmesg command needs access to the syslog buffer. do_syslog()
  409. * uses locks so it cannot be used during debugging. Just tell kdb
  410. * where the start and end of the physical and logical logs are. This
  411. * is equivalent to do_syslog(3).
  412. */
  413. void kdb_syslog_data(char *syslog_data[4])
  414. {
  415. syslog_data[0] = log_buf;
  416. syslog_data[1] = log_buf + log_buf_len;
  417. syslog_data[2] = log_buf + log_end -
  418. (logged_chars < log_buf_len ? logged_chars : log_buf_len);
  419. syslog_data[3] = log_buf + log_end;
  420. }
  421. #endif /* CONFIG_KGDB_KDB */
  422. /*
  423. * Call the console drivers on a range of log_buf
  424. */
  425. static void __call_console_drivers(unsigned start, unsigned end)
  426. {
  427. struct console *con;
  428. for_each_console(con) {
  429. if ((con->flags & CON_ENABLED) && con->write &&
  430. (cpu_online(smp_processor_id()) ||
  431. (con->flags & CON_ANYTIME)))
  432. con->write(con, &LOG_BUF(start), end - start);
  433. }
  434. }
  435. static int __read_mostly ignore_loglevel;
  436. static int __init ignore_loglevel_setup(char *str)
  437. {
  438. ignore_loglevel = 1;
  439. printk(KERN_INFO "debug: ignoring loglevel setting.\n");
  440. return 0;
  441. }
  442. early_param("ignore_loglevel", ignore_loglevel_setup);
  443. /*
  444. * Write out chars from start to end - 1 inclusive
  445. */
  446. static void _call_console_drivers(unsigned start,
  447. unsigned end, int msg_log_level)
  448. {
  449. if ((msg_log_level < console_loglevel || ignore_loglevel) &&
  450. console_drivers && start != end) {
  451. if ((start & LOG_BUF_MASK) > (end & LOG_BUF_MASK)) {
  452. /* wrapped write */
  453. __call_console_drivers(start & LOG_BUF_MASK,
  454. log_buf_len);
  455. __call_console_drivers(0, end & LOG_BUF_MASK);
  456. } else {
  457. __call_console_drivers(start, end);
  458. }
  459. }
  460. }
  461. /*
  462. * Parse the syslog header <[0-9]*>. The decimal value represents 32bit, the
  463. * lower 3 bit are the log level, the rest are the log facility. In case
  464. * userspace passes usual userspace syslog messages to /dev/kmsg or
  465. * /dev/ttyprintk, the log prefix might contain the facility. Printk needs
  466. * to extract the correct log level for in-kernel processing, and not mangle
  467. * the original value.
  468. *
  469. * If a prefix is found, the length of the prefix is returned. If 'level' is
  470. * passed, it will be filled in with the log level without a possible facility
  471. * value. If 'special' is passed, the special printk prefix chars are accepted
  472. * and returned. If no valid header is found, 0 is returned and the passed
  473. * variables are not touched.
  474. */
  475. static size_t log_prefix(const char *p, unsigned int *level, char *special)
  476. {
  477. unsigned int lev = 0;
  478. char sp = '\0';
  479. size_t len;
  480. if (p[0] != '<' || !p[1])
  481. return 0;
  482. if (p[2] == '>') {
  483. /* usual single digit level number or special char */
  484. switch (p[1]) {
  485. case '0' ... '7':
  486. lev = p[1] - '0';
  487. break;
  488. case 'c': /* KERN_CONT */
  489. case 'd': /* KERN_DEFAULT */
  490. sp = p[1];
  491. break;
  492. default:
  493. return 0;
  494. }
  495. len = 3;
  496. } else {
  497. /* multi digit including the level and facility number */
  498. char *endp = NULL;
  499. if (p[1] < '0' && p[1] > '9')
  500. return 0;
  501. lev = (simple_strtoul(&p[1], &endp, 10) & 7);
  502. if (endp == NULL || endp[0] != '>')
  503. return 0;
  504. len = (endp + 1) - p;
  505. }
  506. /* do not accept special char if not asked for */
  507. if (sp && !special)
  508. return 0;
  509. if (special) {
  510. *special = sp;
  511. /* return special char, do not touch level */
  512. if (sp)
  513. return len;
  514. }
  515. if (level)
  516. *level = lev;
  517. return len;
  518. }
  519. /*
  520. * Call the console drivers, asking them to write out
  521. * log_buf[start] to log_buf[end - 1].
  522. * The console_lock must be held.
  523. */
  524. static void call_console_drivers(unsigned start, unsigned end)
  525. {
  526. unsigned cur_index, start_print;
  527. static int msg_level = -1;
  528. BUG_ON(((int)(start - end)) > 0);
  529. cur_index = start;
  530. start_print = start;
  531. while (cur_index != end) {
  532. if (msg_level < 0 && ((end - cur_index) > 2)) {
  533. /* strip log prefix */
  534. cur_index += log_prefix(&LOG_BUF(cur_index), &msg_level, NULL);
  535. start_print = cur_index;
  536. }
  537. while (cur_index != end) {
  538. char c = LOG_BUF(cur_index);
  539. cur_index++;
  540. if (c == '\n') {
  541. if (msg_level < 0) {
  542. /*
  543. * printk() has already given us loglevel tags in
  544. * the buffer. This code is here in case the
  545. * log buffer has wrapped right round and scribbled
  546. * on those tags
  547. */
  548. msg_level = default_message_loglevel;
  549. }
  550. _call_console_drivers(start_print, cur_index, msg_level);
  551. msg_level = -1;
  552. start_print = cur_index;
  553. break;
  554. }
  555. }
  556. }
  557. _call_console_drivers(start_print, end, msg_level);
  558. }
  559. static void emit_log_char(char c)
  560. {
  561. LOG_BUF(log_end) = c;
  562. log_end++;
  563. if (log_end - log_start > log_buf_len)
  564. log_start = log_end - log_buf_len;
  565. if (log_end - con_start > log_buf_len)
  566. con_start = log_end - log_buf_len;
  567. if (logged_chars < log_buf_len)
  568. logged_chars++;
  569. }
  570. /*
  571. * Zap console related locks when oopsing. Only zap at most once
  572. * every 10 seconds, to leave time for slow consoles to print a
  573. * full oops.
  574. */
  575. static void zap_locks(void)
  576. {
  577. static unsigned long oops_timestamp;
  578. if (time_after_eq(jiffies, oops_timestamp) &&
  579. !time_after(jiffies, oops_timestamp + 30 * HZ))
  580. return;
  581. oops_timestamp = jiffies;
  582. /* If a crash is occurring, make sure we can't deadlock */
  583. spin_lock_init(&logbuf_lock);
  584. /* And make sure that we print immediately */
  585. sema_init(&console_sem, 1);
  586. }
  587. #if defined(CONFIG_PRINTK_TIME)
  588. static int printk_time = 1;
  589. #else
  590. static int printk_time = 0;
  591. #endif
  592. module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR);
  593. /* Check if we have any console registered that can be called early in boot. */
  594. static int have_callable_console(void)
  595. {
  596. struct console *con;
  597. for_each_console(con)
  598. if (con->flags & CON_ANYTIME)
  599. return 1;
  600. return 0;
  601. }
  602. /**
  603. * printk - print a kernel message
  604. * @fmt: format string
  605. *
  606. * This is printk(). It can be called from any context. We want it to work.
  607. *
  608. * We try to grab the console_lock. If we succeed, it's easy - we log the output and
  609. * call the console drivers. If we fail to get the semaphore we place the output
  610. * into the log buffer and return. The current holder of the console_sem will
  611. * notice the new output in console_unlock(); and will send it to the
  612. * consoles before releasing the lock.
  613. *
  614. * One effect of this deferred printing is that code which calls printk() and
  615. * then changes console_loglevel may break. This is because console_loglevel
  616. * is inspected when the actual printing occurs.
  617. *
  618. * See also:
  619. * printf(3)
  620. *
  621. * See the vsnprintf() documentation for format string extensions over C99.
  622. */
  623. asmlinkage int printk(const char *fmt, ...)
  624. {
  625. va_list args;
  626. int r;
  627. #ifdef CONFIG_KGDB_KDB
  628. if (unlikely(kdb_trap_printk)) {
  629. va_start(args, fmt);
  630. r = vkdb_printf(fmt, args);
  631. va_end(args);
  632. return r;
  633. }
  634. #endif
  635. va_start(args, fmt);
  636. r = vprintk(fmt, args);
  637. va_end(args);
  638. return r;
  639. }
  640. /* cpu currently holding logbuf_lock */
  641. static volatile unsigned int printk_cpu = UINT_MAX;
  642. /*
  643. * Can we actually use the console at this time on this cpu?
  644. *
  645. * Console drivers may assume that per-cpu resources have
  646. * been allocated. So unless they're explicitly marked as
  647. * being able to cope (CON_ANYTIME) don't call them until
  648. * this CPU is officially up.
  649. */
  650. static inline int can_use_console(unsigned int cpu)
  651. {
  652. return cpu_online(cpu) || have_callable_console();
  653. }
  654. /*
  655. * Try to get console ownership to actually show the kernel
  656. * messages from a 'printk'. Return true (and with the
  657. * console_lock held, and 'console_locked' set) if it
  658. * is successful, false otherwise.
  659. *
  660. * This gets called with the 'logbuf_lock' spinlock held and
  661. * interrupts disabled. It should return with 'lockbuf_lock'
  662. * released but interrupts still disabled.
  663. */
  664. static int console_trylock_for_printk(unsigned int cpu)
  665. __releases(&logbuf_lock)
  666. {
  667. int retval = 0;
  668. if (console_trylock()) {
  669. retval = 1;
  670. /*
  671. * If we can't use the console, we need to release
  672. * the console semaphore by hand to avoid flushing
  673. * the buffer. We need to hold the console semaphore
  674. * in order to do this test safely.
  675. */
  676. if (!can_use_console(cpu)) {
  677. console_locked = 0;
  678. up(&console_sem);
  679. retval = 0;
  680. }
  681. }
  682. printk_cpu = UINT_MAX;
  683. spin_unlock(&logbuf_lock);
  684. return retval;
  685. }
  686. static const char recursion_bug_msg [] =
  687. KERN_CRIT "BUG: recent printk recursion!\n";
  688. static int recursion_bug;
  689. static int new_text_line = 1;
  690. static char printk_buf[1024];
  691. int printk_delay_msec __read_mostly;
  692. static inline void printk_delay(void)
  693. {
  694. if (unlikely(printk_delay_msec)) {
  695. int m = printk_delay_msec;
  696. while (m--) {
  697. mdelay(1);
  698. touch_nmi_watchdog();
  699. }
  700. }
  701. }
  702. asmlinkage int vprintk(const char *fmt, va_list args)
  703. {
  704. int printed_len = 0;
  705. int current_log_level = default_message_loglevel;
  706. unsigned long flags;
  707. int this_cpu;
  708. char *p;
  709. size_t plen;
  710. char special;
  711. boot_delay_msec();
  712. printk_delay();
  713. preempt_disable();
  714. /* This stops the holder of console_sem just where we want him */
  715. raw_local_irq_save(flags);
  716. this_cpu = smp_processor_id();
  717. /*
  718. * Ouch, printk recursed into itself!
  719. */
  720. if (unlikely(printk_cpu == this_cpu)) {
  721. /*
  722. * If a crash is occurring during printk() on this CPU,
  723. * then try to get the crash message out but make sure
  724. * we can't deadlock. Otherwise just return to avoid the
  725. * recursion and return - but flag the recursion so that
  726. * it can be printed at the next appropriate moment:
  727. */
  728. if (!oops_in_progress) {
  729. recursion_bug = 1;
  730. goto out_restore_irqs;
  731. }
  732. zap_locks();
  733. }
  734. lockdep_off();
  735. spin_lock(&logbuf_lock);
  736. printk_cpu = this_cpu;
  737. if (recursion_bug) {
  738. recursion_bug = 0;
  739. strcpy(printk_buf, recursion_bug_msg);
  740. printed_len = strlen(recursion_bug_msg);
  741. }
  742. /* Emit the output into the temporary buffer */
  743. printed_len += vscnprintf(printk_buf + printed_len,
  744. sizeof(printk_buf) - printed_len, fmt, args);
  745. p = printk_buf;
  746. /* Read log level and handle special printk prefix */
  747. plen = log_prefix(p, &current_log_level, &special);
  748. if (plen) {
  749. p += plen;
  750. switch (special) {
  751. case 'c': /* Strip <c> KERN_CONT, continue line */
  752. plen = 0;
  753. break;
  754. case 'd': /* Strip <d> KERN_DEFAULT, start new line */
  755. plen = 0;
  756. default:
  757. if (!new_text_line) {
  758. emit_log_char('\n');
  759. new_text_line = 1;
  760. }
  761. }
  762. }
  763. /*
  764. * Copy the output into log_buf. If the caller didn't provide
  765. * the appropriate log prefix, we insert them here
  766. */
  767. for (; *p; p++) {
  768. if (new_text_line) {
  769. new_text_line = 0;
  770. if (plen) {
  771. /* Copy original log prefix */
  772. int i;
  773. for (i = 0; i < plen; i++)
  774. emit_log_char(printk_buf[i]);
  775. printed_len += plen;
  776. } else {
  777. /* Add log prefix */
  778. emit_log_char('<');
  779. emit_log_char(current_log_level + '0');
  780. emit_log_char('>');
  781. printed_len += 3;
  782. }
  783. if (printk_time) {
  784. /* Add the current time stamp */
  785. char tbuf[50], *tp;
  786. unsigned tlen;
  787. unsigned long long t;
  788. unsigned long nanosec_rem;
  789. t = cpu_clock(printk_cpu);
  790. nanosec_rem = do_div(t, 1000000000);
  791. tlen = sprintf(tbuf, "[%5lu.%06lu] ",
  792. (unsigned long) t,
  793. nanosec_rem / 1000);
  794. for (tp = tbuf; tp < tbuf + tlen; tp++)
  795. emit_log_char(*tp);
  796. printed_len += tlen;
  797. }
  798. if (!*p)
  799. break;
  800. }
  801. emit_log_char(*p);
  802. if (*p == '\n')
  803. new_text_line = 1;
  804. }
  805. /*
  806. * Try to acquire and then immediately release the
  807. * console semaphore. The release will do all the
  808. * actual magic (print out buffers, wake up klogd,
  809. * etc).
  810. *
  811. * The console_trylock_for_printk() function
  812. * will release 'logbuf_lock' regardless of whether it
  813. * actually gets the semaphore or not.
  814. */
  815. if (console_trylock_for_printk(this_cpu))
  816. console_unlock();
  817. lockdep_on();
  818. out_restore_irqs:
  819. raw_local_irq_restore(flags);
  820. preempt_enable();
  821. return printed_len;
  822. }
  823. EXPORT_SYMBOL(printk);
  824. EXPORT_SYMBOL(vprintk);
  825. #else
  826. static void call_console_drivers(unsigned start, unsigned end)
  827. {
  828. }
  829. #endif
  830. static int __add_preferred_console(char *name, int idx, char *options,
  831. char *brl_options)
  832. {
  833. struct console_cmdline *c;
  834. int i;
  835. /*
  836. * See if this tty is not yet registered, and
  837. * if we have a slot free.
  838. */
  839. for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
  840. if (strcmp(console_cmdline[i].name, name) == 0 &&
  841. console_cmdline[i].index == idx) {
  842. if (!brl_options)
  843. selected_console = i;
  844. return 0;
  845. }
  846. if (i == MAX_CMDLINECONSOLES)
  847. return -E2BIG;
  848. if (!brl_options)
  849. selected_console = i;
  850. c = &console_cmdline[i];
  851. strlcpy(c->name, name, sizeof(c->name));
  852. c->options = options;
  853. #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
  854. c->brl_options = brl_options;
  855. #endif
  856. c->index = idx;
  857. return 0;
  858. }
  859. /*
  860. * Set up a list of consoles. Called from init/main.c
  861. */
  862. static int __init console_setup(char *str)
  863. {
  864. char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for index */
  865. char *s, *options, *brl_options = NULL;
  866. int idx;
  867. #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
  868. if (!memcmp(str, "brl,", 4)) {
  869. brl_options = "";
  870. str += 4;
  871. } else if (!memcmp(str, "brl=", 4)) {
  872. brl_options = str + 4;
  873. str = strchr(brl_options, ',');
  874. if (!str) {
  875. printk(KERN_ERR "need port name after brl=\n");
  876. return 1;
  877. }
  878. *(str++) = 0;
  879. }
  880. #endif
  881. /*
  882. * Decode str into name, index, options.
  883. */
  884. if (str[0] >= '0' && str[0] <= '9') {
  885. strcpy(buf, "ttyS");
  886. strncpy(buf + 4, str, sizeof(buf) - 5);
  887. } else {
  888. strncpy(buf, str, sizeof(buf) - 1);
  889. }
  890. buf[sizeof(buf) - 1] = 0;
  891. if ((options = strchr(str, ',')) != NULL)
  892. *(options++) = 0;
  893. #ifdef __sparc__
  894. if (!strcmp(str, "ttya"))
  895. strcpy(buf, "ttyS0");
  896. if (!strcmp(str, "ttyb"))
  897. strcpy(buf, "ttyS1");
  898. #endif
  899. for (s = buf; *s; s++)
  900. if ((*s >= '0' && *s <= '9') || *s == ',')
  901. break;
  902. idx = simple_strtoul(s, NULL, 10);
  903. *s = 0;
  904. __add_preferred_console(buf, idx, options, brl_options);
  905. console_set_on_cmdline = 1;
  906. return 1;
  907. }
  908. __setup("console=", console_setup);
  909. /**
  910. * add_preferred_console - add a device to the list of preferred consoles.
  911. * @name: device name
  912. * @idx: device index
  913. * @options: options for this console
  914. *
  915. * The last preferred console added will be used for kernel messages
  916. * and stdin/out/err for init. Normally this is used by console_setup
  917. * above to handle user-supplied console arguments; however it can also
  918. * be used by arch-specific code either to override the user or more
  919. * commonly to provide a default console (ie from PROM variables) when
  920. * the user has not supplied one.
  921. */
  922. int add_preferred_console(char *name, int idx, char *options)
  923. {
  924. return __add_preferred_console(name, idx, options, NULL);
  925. }
  926. int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options)
  927. {
  928. struct console_cmdline *c;
  929. int i;
  930. for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
  931. if (strcmp(console_cmdline[i].name, name) == 0 &&
  932. console_cmdline[i].index == idx) {
  933. c = &console_cmdline[i];
  934. strlcpy(c->name, name_new, sizeof(c->name));
  935. c->name[sizeof(c->name) - 1] = 0;
  936. c->options = options;
  937. c->index = idx_new;
  938. return i;
  939. }
  940. /* not found */
  941. return -1;
  942. }
  943. int console_suspend_enabled = 1;
  944. EXPORT_SYMBOL(console_suspend_enabled);
  945. static int __init console_suspend_disable(char *str)
  946. {
  947. console_suspend_enabled = 0;
  948. return 1;
  949. }
  950. __setup("no_console_suspend", console_suspend_disable);
  951. /**
  952. * suspend_console - suspend the console subsystem
  953. *
  954. * This disables printk() while we go into suspend states
  955. */
  956. void suspend_console(void)
  957. {
  958. if (!console_suspend_enabled)
  959. return;
  960. printk("Suspending console(s) (use no_console_suspend to debug)\n");
  961. console_lock();
  962. console_suspended = 1;
  963. up(&console_sem);
  964. }
  965. void resume_console(void)
  966. {
  967. if (!console_suspend_enabled)
  968. return;
  969. down(&console_sem);
  970. console_suspended = 0;
  971. console_unlock();
  972. }
  973. /**
  974. * console_cpu_notify - print deferred console messages after CPU hotplug
  975. * @self: notifier struct
  976. * @action: CPU hotplug event
  977. * @hcpu: unused
  978. *
  979. * If printk() is called from a CPU that is not online yet, the messages
  980. * will be spooled but will not show up on the console. This function is
  981. * called when a new CPU comes online (or fails to come up), and ensures
  982. * that any such output gets printed.
  983. */
  984. static int __cpuinit console_cpu_notify(struct notifier_block *self,
  985. unsigned long action, void *hcpu)
  986. {
  987. switch (action) {
  988. case CPU_ONLINE:
  989. case CPU_DEAD:
  990. case CPU_DYING:
  991. case CPU_DOWN_FAILED:
  992. case CPU_UP_CANCELED:
  993. console_lock();
  994. console_unlock();
  995. }
  996. return NOTIFY_OK;
  997. }
  998. /**
  999. * console_lock - lock the console system for exclusive use.
  1000. *
  1001. * Acquires a lock which guarantees that the caller has
  1002. * exclusive access to the console system and the console_drivers list.
  1003. *
  1004. * Can sleep, returns nothing.
  1005. */
  1006. void console_lock(void)
  1007. {
  1008. BUG_ON(in_interrupt());
  1009. down(&console_sem);
  1010. if (console_suspended)
  1011. return;
  1012. console_locked = 1;
  1013. console_may_schedule = 1;
  1014. }
  1015. EXPORT_SYMBOL(console_lock);
  1016. /**
  1017. * console_trylock - try to lock the console system for exclusive use.
  1018. *
  1019. * Tried to acquire a lock which guarantees that the caller has
  1020. * exclusive access to the console system and the console_drivers list.
  1021. *
  1022. * returns 1 on success, and 0 on failure to acquire the lock.
  1023. */
  1024. int console_trylock(void)
  1025. {
  1026. if (down_trylock(&console_sem))
  1027. return 0;
  1028. if (console_suspended) {
  1029. up(&console_sem);
  1030. return 0;
  1031. }
  1032. console_locked = 1;
  1033. console_may_schedule = 0;
  1034. return 1;
  1035. }
  1036. EXPORT_SYMBOL(console_trylock);
  1037. int is_console_locked(void)
  1038. {
  1039. return console_locked;
  1040. }
  1041. static DEFINE_PER_CPU(int, printk_pending);
  1042. void printk_tick(void)
  1043. {
  1044. if (__this_cpu_read(printk_pending)) {
  1045. __this_cpu_write(printk_pending, 0);
  1046. wake_up_interruptible(&log_wait);
  1047. }
  1048. }
  1049. int printk_needs_cpu(int cpu)
  1050. {
  1051. if (cpu_is_offline(cpu))
  1052. printk_tick();
  1053. return __this_cpu_read(printk_pending);
  1054. }
  1055. void wake_up_klogd(void)
  1056. {
  1057. if (waitqueue_active(&log_wait))
  1058. this_cpu_write(printk_pending, 1);
  1059. }
  1060. /**
  1061. * console_unlock - unlock the console system
  1062. *
  1063. * Releases the console_lock which the caller holds on the console system
  1064. * and the console driver list.
  1065. *
  1066. * While the console_lock was held, console output may have been buffered
  1067. * by printk(). If this is the case, console_unlock(); emits
  1068. * the output prior to releasing the lock.
  1069. *
  1070. * If there is output waiting for klogd, we wake it up.
  1071. *
  1072. * console_unlock(); may be called from any context.
  1073. */
  1074. void console_unlock(void)
  1075. {
  1076. unsigned long flags;
  1077. unsigned _con_start, _log_end;
  1078. unsigned wake_klogd = 0;
  1079. if (console_suspended) {
  1080. up(&console_sem);
  1081. return;
  1082. }
  1083. console_may_schedule = 0;
  1084. for ( ; ; ) {
  1085. spin_lock_irqsave(&logbuf_lock, flags);
  1086. wake_klogd |= log_start - log_end;
  1087. if (con_start == log_end)
  1088. break; /* Nothing to print */
  1089. _con_start = con_start;
  1090. _log_end = log_end;
  1091. con_start = log_end; /* Flush */
  1092. spin_unlock(&logbuf_lock);
  1093. stop_critical_timings(); /* don't trace print latency */
  1094. call_console_drivers(_con_start, _log_end);
  1095. start_critical_timings();
  1096. local_irq_restore(flags);
  1097. }
  1098. console_locked = 0;
  1099. up(&console_sem);
  1100. spin_unlock_irqrestore(&logbuf_lock, flags);
  1101. if (wake_klogd)
  1102. wake_up_klogd();
  1103. }
  1104. EXPORT_SYMBOL(console_unlock);
  1105. /**
  1106. * console_conditional_schedule - yield the CPU if required
  1107. *
  1108. * If the console code is currently allowed to sleep, and
  1109. * if this CPU should yield the CPU to another task, do
  1110. * so here.
  1111. *
  1112. * Must be called within console_lock();.
  1113. */
  1114. void __sched console_conditional_schedule(void)
  1115. {
  1116. if (console_may_schedule)
  1117. cond_resched();
  1118. }
  1119. EXPORT_SYMBOL(console_conditional_schedule);
  1120. void console_unblank(void)
  1121. {
  1122. struct console *c;
  1123. /*
  1124. * console_unblank can no longer be called in interrupt context unless
  1125. * oops_in_progress is set to 1..
  1126. */
  1127. if (oops_in_progress) {
  1128. if (down_trylock(&console_sem) != 0)
  1129. return;
  1130. } else
  1131. console_lock();
  1132. console_locked = 1;
  1133. console_may_schedule = 0;
  1134. for_each_console(c)
  1135. if ((c->flags & CON_ENABLED) && c->unblank)
  1136. c->unblank();
  1137. console_unlock();
  1138. }
  1139. /*
  1140. * Return the console tty driver structure and its associated index
  1141. */
  1142. struct tty_driver *console_device(int *index)
  1143. {
  1144. struct console *c;
  1145. struct tty_driver *driver = NULL;
  1146. console_lock();
  1147. for_each_console(c) {
  1148. if (!c->device)
  1149. continue;
  1150. driver = c->device(c, index);
  1151. if (driver)
  1152. break;
  1153. }
  1154. console_unlock();
  1155. return driver;
  1156. }
  1157. /*
  1158. * Prevent further output on the passed console device so that (for example)
  1159. * serial drivers can disable console output before suspending a port, and can
  1160. * re-enable output afterwards.
  1161. */
  1162. void console_stop(struct console *console)
  1163. {
  1164. console_lock();
  1165. console->flags &= ~CON_ENABLED;
  1166. console_unlock();
  1167. }
  1168. EXPORT_SYMBOL(console_stop);
  1169. void console_start(struct console *console)
  1170. {
  1171. console_lock();
  1172. console->flags |= CON_ENABLED;
  1173. console_unlock();
  1174. }
  1175. EXPORT_SYMBOL(console_start);
  1176. static int __read_mostly keep_bootcon;
  1177. static int __init keep_bootcon_setup(char *str)
  1178. {
  1179. keep_bootcon = 1;
  1180. printk(KERN_INFO "debug: skip boot console de-registration.\n");
  1181. return 0;
  1182. }
  1183. early_param("keep_bootcon", keep_bootcon_setup);
  1184. /*
  1185. * The console driver calls this routine during kernel initialization
  1186. * to register the console printing procedure with printk() and to
  1187. * print any messages that were printed by the kernel before the
  1188. * console driver was initialized.
  1189. *
  1190. * This can happen pretty early during the boot process (because of
  1191. * early_printk) - sometimes before setup_arch() completes - be careful
  1192. * of what kernel features are used - they may not be initialised yet.
  1193. *
  1194. * There are two types of consoles - bootconsoles (early_printk) and
  1195. * "real" consoles (everything which is not a bootconsole) which are
  1196. * handled differently.
  1197. * - Any number of bootconsoles can be registered at any time.
  1198. * - As soon as a "real" console is registered, all bootconsoles
  1199. * will be unregistered automatically.
  1200. * - Once a "real" console is registered, any attempt to register a
  1201. * bootconsoles will be rejected
  1202. */
  1203. void register_console(struct console *newcon)
  1204. {
  1205. int i;
  1206. unsigned long flags;
  1207. struct console *bcon = NULL;
  1208. /*
  1209. * before we register a new CON_BOOT console, make sure we don't
  1210. * already have a valid console
  1211. */
  1212. if (console_drivers && newcon->flags & CON_BOOT) {
  1213. /* find the last or real console */
  1214. for_each_console(bcon) {
  1215. if (!(bcon->flags & CON_BOOT)) {
  1216. printk(KERN_INFO "Too late to register bootconsole %s%d\n",
  1217. newcon->name, newcon->index);
  1218. return;
  1219. }
  1220. }
  1221. }
  1222. if (console_drivers && console_drivers->flags & CON_BOOT)
  1223. bcon = console_drivers;
  1224. if (preferred_console < 0 || bcon || !console_drivers)
  1225. preferred_console = selected_console;
  1226. if (newcon->early_setup)
  1227. newcon->early_setup();
  1228. /*
  1229. * See if we want to use this console driver. If we
  1230. * didn't select a console we take the first one
  1231. * that registers here.
  1232. */
  1233. if (preferred_console < 0) {
  1234. if (newcon->index < 0)
  1235. newcon->index = 0;
  1236. if (newcon->setup == NULL ||
  1237. newcon->setup(newcon, NULL) == 0) {
  1238. newcon->flags |= CON_ENABLED;
  1239. if (newcon->device) {
  1240. newcon->flags |= CON_CONSDEV;
  1241. preferred_console = 0;
  1242. }
  1243. }
  1244. }
  1245. /*
  1246. * See if this console matches one we selected on
  1247. * the command line.
  1248. */
  1249. for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0];
  1250. i++) {
  1251. if (strcmp(console_cmdline[i].name, newcon->name) != 0)
  1252. continue;
  1253. if (newcon->index >= 0 &&
  1254. newcon->index != console_cmdline[i].index)
  1255. continue;
  1256. if (newcon->index < 0)
  1257. newcon->index = console_cmdline[i].index;
  1258. #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
  1259. if (console_cmdline[i].brl_options) {
  1260. newcon->flags |= CON_BRL;
  1261. braille_register_console(newcon,
  1262. console_cmdline[i].index,
  1263. console_cmdline[i].options,
  1264. console_cmdline[i].brl_options);
  1265. return;
  1266. }
  1267. #endif
  1268. if (newcon->setup &&
  1269. newcon->setup(newcon, console_cmdline[i].options) != 0)
  1270. break;
  1271. newcon->flags |= CON_ENABLED;
  1272. newcon->index = console_cmdline[i].index;
  1273. if (i == selected_console) {
  1274. newcon->flags |= CON_CONSDEV;
  1275. preferred_console = selected_console;
  1276. }
  1277. break;
  1278. }
  1279. if (!(newcon->flags & CON_ENABLED))
  1280. return;
  1281. /*
  1282. * If we have a bootconsole, and are switching to a real console,
  1283. * don't print everything out again, since when the boot console, and
  1284. * the real console are the same physical device, it's annoying to
  1285. * see the beginning boot messages twice
  1286. */
  1287. if (bcon && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV))
  1288. newcon->flags &= ~CON_PRINTBUFFER;
  1289. /*
  1290. * Put this console in the list - keep the
  1291. * preferred driver at the head of the list.
  1292. */
  1293. console_lock();
  1294. if ((newcon->flags & CON_CONSDEV) || console_drivers == NULL) {
  1295. newcon->next = console_drivers;
  1296. console_drivers = newcon;
  1297. if (newcon->next)
  1298. newcon->next->flags &= ~CON_CONSDEV;
  1299. } else {
  1300. newcon->next = console_drivers->next;
  1301. console_drivers->next = newcon;
  1302. }
  1303. if (newcon->flags & CON_PRINTBUFFER) {
  1304. /*
  1305. * console_unlock(); will print out the buffered messages
  1306. * for us.
  1307. */
  1308. spin_lock_irqsave(&logbuf_lock, flags);
  1309. con_start = log_start;
  1310. spin_unlock_irqrestore(&logbuf_lock, flags);
  1311. }
  1312. console_unlock();
  1313. console_sysfs_notify();
  1314. /*
  1315. * By unregistering the bootconsoles after we enable the real console
  1316. * we get the "console xxx enabled" message on all the consoles -
  1317. * boot consoles, real consoles, etc - this is to ensure that end
  1318. * users know there might be something in the kernel's log buffer that
  1319. * went to the bootconsole (that they do not see on the real console)
  1320. */
  1321. if (bcon &&
  1322. ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV) &&
  1323. !keep_bootcon) {
  1324. /* we need to iterate through twice, to make sure we print
  1325. * everything out, before we unregister the console(s)
  1326. */
  1327. printk(KERN_INFO "console [%s%d] enabled, bootconsole disabled\n",
  1328. newcon->name, newcon->index);
  1329. for_each_console(bcon)
  1330. if (bcon->flags & CON_BOOT)
  1331. unregister_console(bcon);
  1332. } else {
  1333. printk(KERN_INFO "%sconsole [%s%d] enabled\n",
  1334. (newcon->flags & CON_BOOT) ? "boot" : "" ,
  1335. newcon->name, newcon->index);
  1336. }
  1337. }
  1338. EXPORT_SYMBOL(register_console);
  1339. int unregister_console(struct console *console)
  1340. {
  1341. struct console *a, *b;
  1342. int res = 1;
  1343. #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
  1344. if (console->flags & CON_BRL)
  1345. return braille_unregister_console(console);
  1346. #endif
  1347. console_lock();
  1348. if (console_drivers == console) {
  1349. console_drivers=console->next;
  1350. res = 0;
  1351. } else if (console_drivers) {
  1352. for (a=console_drivers->next, b=console_drivers ;
  1353. a; b=a, a=b->next) {
  1354. if (a == console) {
  1355. b->next = a->next;
  1356. res = 0;
  1357. break;
  1358. }
  1359. }
  1360. }
  1361. /*
  1362. * If this isn't the last console and it has CON_CONSDEV set, we
  1363. * need to set it on the next preferred console.
  1364. */
  1365. if (console_drivers != NULL && console->flags & CON_CONSDEV)
  1366. console_drivers->flags |= CON_CONSDEV;
  1367. console_unlock();
  1368. console_sysfs_notify();
  1369. return res;
  1370. }
  1371. EXPORT_SYMBOL(unregister_console);
  1372. static int __init printk_late_init(void)
  1373. {
  1374. struct console *con;
  1375. for_each_console(con) {
  1376. if (con->flags & CON_BOOT) {
  1377. printk(KERN_INFO "turn off boot console %s%d\n",
  1378. con->name, con->index);
  1379. unregister_console(con);
  1380. }
  1381. }
  1382. hotcpu_notifier(console_cpu_notify, 0);
  1383. return 0;
  1384. }
  1385. late_initcall(printk_late_init);
  1386. #if defined CONFIG_PRINTK
  1387. /*
  1388. * printk rate limiting, lifted from the networking subsystem.
  1389. *
  1390. * This enforces a rate limit: not more than 10 kernel messages
  1391. * every 5s to make a denial-of-service attack impossible.
  1392. */
  1393. DEFINE_RATELIMIT_STATE(printk_ratelimit_state, 5 * HZ, 10);
  1394. int __printk_ratelimit(const char *func)
  1395. {
  1396. return ___ratelimit(&printk_ratelimit_state, func);
  1397. }
  1398. EXPORT_SYMBOL(__printk_ratelimit);
  1399. /**
  1400. * printk_timed_ratelimit - caller-controlled printk ratelimiting
  1401. * @caller_jiffies: pointer to caller's state
  1402. * @interval_msecs: minimum interval between prints
  1403. *
  1404. * printk_timed_ratelimit() returns true if more than @interval_msecs
  1405. * milliseconds have elapsed since the last time printk_timed_ratelimit()
  1406. * returned true.
  1407. */
  1408. bool printk_timed_ratelimit(unsigned long *caller_jiffies,
  1409. unsigned int interval_msecs)
  1410. {
  1411. if (*caller_jiffies == 0
  1412. || !time_in_range(jiffies, *caller_jiffies,
  1413. *caller_jiffies
  1414. + msecs_to_jiffies(interval_msecs))) {
  1415. *caller_jiffies = jiffies;
  1416. return true;
  1417. }
  1418. return false;
  1419. }
  1420. EXPORT_SYMBOL(printk_timed_ratelimit);
  1421. static DEFINE_SPINLOCK(dump_list_lock);
  1422. static LIST_HEAD(dump_list);
  1423. /**
  1424. * kmsg_dump_register - register a kernel log dumper.
  1425. * @dumper: pointer to the kmsg_dumper structure
  1426. *
  1427. * Adds a kernel log dumper to the system. The dump callback in the
  1428. * structure will be called when the kernel oopses or panics and must be
  1429. * set. Returns zero on success and %-EINVAL or %-EBUSY otherwise.
  1430. */
  1431. int kmsg_dump_register(struct kmsg_dumper *dumper)
  1432. {
  1433. unsigned long flags;
  1434. int err = -EBUSY;
  1435. /* The dump callback needs to be set */
  1436. if (!dumper->dump)
  1437. return -EINVAL;
  1438. spin_lock_irqsave(&dump_list_lock, flags);
  1439. /* Don't allow registering multiple times */
  1440. if (!dumper->registered) {
  1441. dumper->registered = 1;
  1442. list_add_tail_rcu(&dumper->list, &dump_list);
  1443. err = 0;
  1444. }
  1445. spin_unlock_irqrestore(&dump_list_lock, flags);
  1446. return err;
  1447. }
  1448. EXPORT_SYMBOL_GPL(kmsg_dump_register);
  1449. /**
  1450. * kmsg_dump_unregister - unregister a kmsg dumper.
  1451. * @dumper: pointer to the kmsg_dumper structure
  1452. *
  1453. * Removes a dump device from the system. Returns zero on success and
  1454. * %-EINVAL otherwise.
  1455. */
  1456. int kmsg_dump_unregister(struct kmsg_dumper *dumper)
  1457. {
  1458. unsigned long flags;
  1459. int err = -EINVAL;
  1460. spin_lock_irqsave(&dump_list_lock, flags);
  1461. if (dumper->registered) {
  1462. dumper->registered = 0;
  1463. list_del_rcu(&dumper->list);
  1464. err = 0;
  1465. }
  1466. spin_unlock_irqrestore(&dump_list_lock, flags);
  1467. synchronize_rcu();
  1468. return err;
  1469. }
  1470. EXPORT_SYMBOL_GPL(kmsg_dump_unregister);
  1471. /**
  1472. * kmsg_dump - dump kernel log to kernel message dumpers.
  1473. * @reason: the reason (oops, panic etc) for dumping
  1474. *
  1475. * Iterate through each of the dump devices and call the oops/panic
  1476. * callbacks with the log buffer.
  1477. */
  1478. void kmsg_dump(enum kmsg_dump_reason reason)
  1479. {
  1480. unsigned long end;
  1481. unsigned chars;
  1482. struct kmsg_dumper *dumper;
  1483. const char *s1, *s2;
  1484. unsigned long l1, l2;
  1485. unsigned long flags;
  1486. /* Theoretically, the log could move on after we do this, but
  1487. there's not a lot we can do about that. The new messages
  1488. will overwrite the start of what we dump. */
  1489. spin_lock_irqsave(&logbuf_lock, flags);
  1490. end = log_end & LOG_BUF_MASK;
  1491. chars = logged_chars;
  1492. spin_unlock_irqrestore(&logbuf_lock, flags);
  1493. if (chars > end) {
  1494. s1 = log_buf + log_buf_len - chars + end;
  1495. l1 = chars - end;
  1496. s2 = log_buf;
  1497. l2 = end;
  1498. } else {
  1499. s1 = "";
  1500. l1 = 0;
  1501. s2 = log_buf + end - chars;
  1502. l2 = chars;
  1503. }
  1504. rcu_read_lock();
  1505. list_for_each_entry_rcu(dumper, &dump_list, list)
  1506. dumper->dump(dumper, reason, s1, l1, s2, l2);
  1507. rcu_read_unlock();
  1508. }
  1509. #endif