printk.c 43 KB

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