printk.c 44 KB

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