printk.c 45 KB

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