printk.c 43 KB

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