printk.c 39 KB

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