printk.c 42 KB

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