printk.c 41 KB

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