printk.c 44 KB

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