printk.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242
  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 <andrewm@uow.edu.au>
  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/jiffies.h>
  35. #include <asm/uaccess.h>
  36. #define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT)
  37. /* printk's without a loglevel use this.. */
  38. #define DEFAULT_MESSAGE_LOGLEVEL 4 /* KERN_WARNING */
  39. /* We show everything that is MORE important than this.. */
  40. #define MINIMUM_CONSOLE_LOGLEVEL 1 /* Minimum loglevel we let people use */
  41. #define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */
  42. DECLARE_WAIT_QUEUE_HEAD(log_wait);
  43. int console_printk[4] = {
  44. DEFAULT_CONSOLE_LOGLEVEL, /* console_loglevel */
  45. DEFAULT_MESSAGE_LOGLEVEL, /* default_message_loglevel */
  46. MINIMUM_CONSOLE_LOGLEVEL, /* minimum_console_loglevel */
  47. DEFAULT_CONSOLE_LOGLEVEL, /* default_console_loglevel */
  48. };
  49. /*
  50. * Low level drivers may need that to know if they can schedule in
  51. * their unblank() callback or not. So let's export it.
  52. */
  53. int oops_in_progress;
  54. EXPORT_SYMBOL(oops_in_progress);
  55. /*
  56. * console_sem protects the console_drivers list, and also
  57. * provides serialisation for access to the entire console
  58. * driver system.
  59. */
  60. static DECLARE_MUTEX(console_sem);
  61. static DECLARE_MUTEX(secondary_console_sem);
  62. struct console *console_drivers;
  63. /*
  64. * This is used for debugging the mess that is the VT code by
  65. * keeping track if we have the console semaphore held. It's
  66. * definitely not the perfect debug tool (we don't know if _WE_
  67. * hold it are racing, but it helps tracking those weird code
  68. * path in the console code where we end up in places I want
  69. * locked without the console sempahore held
  70. */
  71. static int console_locked, console_suspended;
  72. /*
  73. * logbuf_lock protects log_buf, log_start, log_end, con_start and logged_chars
  74. * It is also used in interesting ways to provide interlocking in
  75. * release_console_sem().
  76. */
  77. static DEFINE_SPINLOCK(logbuf_lock);
  78. #define LOG_BUF_MASK (log_buf_len-1)
  79. #define LOG_BUF(idx) (log_buf[(idx) & LOG_BUF_MASK])
  80. /*
  81. * The indices into log_buf are not constrained to log_buf_len - they
  82. * must be masked before subscripting
  83. */
  84. static unsigned long log_start; /* Index into log_buf: next char to be read by syslog() */
  85. static unsigned long con_start; /* Index into log_buf: next char to be sent to consoles */
  86. static unsigned long log_end; /* Index into log_buf: most-recently-written-char + 1 */
  87. /*
  88. * Array of consoles built from command line options (console=)
  89. */
  90. struct console_cmdline
  91. {
  92. char name[8]; /* Name of the driver */
  93. int index; /* Minor dev. to use */
  94. char *options; /* Options for the driver */
  95. };
  96. #define MAX_CMDLINECONSOLES 8
  97. static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
  98. static int selected_console = -1;
  99. static int preferred_console = -1;
  100. /* Flag: console code may call schedule() */
  101. static int console_may_schedule;
  102. #ifdef CONFIG_PRINTK
  103. static char __log_buf[__LOG_BUF_LEN];
  104. static char *log_buf = __log_buf;
  105. static int log_buf_len = __LOG_BUF_LEN;
  106. static unsigned long logged_chars; /* Number of chars produced since last read+clear operation */
  107. static int __init log_buf_len_setup(char *str)
  108. {
  109. unsigned long size = memparse(str, &str);
  110. unsigned long flags;
  111. if (size)
  112. size = roundup_pow_of_two(size);
  113. if (size > log_buf_len) {
  114. unsigned long start, dest_idx, offset;
  115. char *new_log_buf;
  116. new_log_buf = alloc_bootmem(size);
  117. if (!new_log_buf) {
  118. printk(KERN_WARNING "log_buf_len: allocation failed\n");
  119. goto out;
  120. }
  121. spin_lock_irqsave(&logbuf_lock, flags);
  122. log_buf_len = size;
  123. log_buf = new_log_buf;
  124. offset = start = min(con_start, log_start);
  125. dest_idx = 0;
  126. while (start != log_end) {
  127. log_buf[dest_idx] = __log_buf[start & (__LOG_BUF_LEN - 1)];
  128. start++;
  129. dest_idx++;
  130. }
  131. log_start -= offset;
  132. con_start -= offset;
  133. log_end -= offset;
  134. spin_unlock_irqrestore(&logbuf_lock, flags);
  135. printk(KERN_NOTICE "log_buf_len: %d\n", log_buf_len);
  136. }
  137. out:
  138. return 1;
  139. }
  140. __setup("log_buf_len=", log_buf_len_setup);
  141. #ifdef CONFIG_BOOT_PRINTK_DELAY
  142. static unsigned int boot_delay; /* msecs delay after each printk during bootup */
  143. static unsigned long long printk_delay_msec; /* per msec, based on boot_delay */
  144. static int __init boot_delay_setup(char *str)
  145. {
  146. unsigned long lpj;
  147. unsigned long long loops_per_msec;
  148. lpj = preset_lpj ? preset_lpj : 1000000; /* some guess */
  149. loops_per_msec = (unsigned long long)lpj / 1000 * HZ;
  150. get_option(&str, &boot_delay);
  151. if (boot_delay > 10 * 1000)
  152. boot_delay = 0;
  153. printk_delay_msec = loops_per_msec;
  154. printk(KERN_DEBUG "boot_delay: %u, preset_lpj: %ld, lpj: %lu, "
  155. "HZ: %d, printk_delay_msec: %llu\n",
  156. boot_delay, preset_lpj, lpj, HZ, printk_delay_msec);
  157. return 1;
  158. }
  159. __setup("boot_delay=", boot_delay_setup);
  160. static void boot_delay_msec(void)
  161. {
  162. unsigned long long k;
  163. unsigned long timeout;
  164. if (boot_delay == 0 || system_state != SYSTEM_BOOTING)
  165. return;
  166. k = (unsigned long long)printk_delay_msec * boot_delay;
  167. timeout = jiffies + msecs_to_jiffies(boot_delay);
  168. while (k) {
  169. k--;
  170. cpu_relax();
  171. /*
  172. * use (volatile) jiffies to prevent
  173. * compiler reduction; loop termination via jiffies
  174. * is secondary and may or may not happen.
  175. */
  176. if (time_after(jiffies, timeout))
  177. break;
  178. touch_nmi_watchdog();
  179. }
  180. }
  181. #else
  182. static inline void boot_delay_msec(void)
  183. {
  184. }
  185. #endif
  186. /*
  187. * Commands to do_syslog:
  188. *
  189. * 0 -- Close the log. Currently a NOP.
  190. * 1 -- Open the log. Currently a NOP.
  191. * 2 -- Read from the log.
  192. * 3 -- Read all messages remaining in the ring buffer.
  193. * 4 -- Read and clear all messages remaining in the ring buffer
  194. * 5 -- Clear ring buffer.
  195. * 6 -- Disable printk's to console
  196. * 7 -- Enable printk's to console
  197. * 8 -- Set level of messages printed to console
  198. * 9 -- Return number of unread characters in the log buffer
  199. * 10 -- Return size of the log buffer
  200. */
  201. int do_syslog(int type, char __user *buf, int len)
  202. {
  203. unsigned long i, j, limit, count;
  204. int do_clear = 0;
  205. char c;
  206. int error = 0;
  207. error = security_syslog(type);
  208. if (error)
  209. return error;
  210. switch (type) {
  211. case 0: /* Close log */
  212. break;
  213. case 1: /* Open log */
  214. break;
  215. case 2: /* Read from log */
  216. error = -EINVAL;
  217. if (!buf || len < 0)
  218. goto out;
  219. error = 0;
  220. if (!len)
  221. goto out;
  222. if (!access_ok(VERIFY_WRITE, buf, len)) {
  223. error = -EFAULT;
  224. goto out;
  225. }
  226. error = wait_event_interruptible(log_wait,
  227. (log_start - log_end));
  228. if (error)
  229. goto out;
  230. i = 0;
  231. spin_lock_irq(&logbuf_lock);
  232. while (!error && (log_start != log_end) && i < len) {
  233. c = LOG_BUF(log_start);
  234. log_start++;
  235. spin_unlock_irq(&logbuf_lock);
  236. error = __put_user(c,buf);
  237. buf++;
  238. i++;
  239. cond_resched();
  240. spin_lock_irq(&logbuf_lock);
  241. }
  242. spin_unlock_irq(&logbuf_lock);
  243. if (!error)
  244. error = i;
  245. break;
  246. case 4: /* Read/clear last kernel messages */
  247. do_clear = 1;
  248. /* FALL THRU */
  249. case 3: /* Read last kernel messages */
  250. error = -EINVAL;
  251. if (!buf || len < 0)
  252. goto out;
  253. error = 0;
  254. if (!len)
  255. goto out;
  256. if (!access_ok(VERIFY_WRITE, buf, len)) {
  257. error = -EFAULT;
  258. goto out;
  259. }
  260. count = len;
  261. if (count > log_buf_len)
  262. count = log_buf_len;
  263. spin_lock_irq(&logbuf_lock);
  264. if (count > logged_chars)
  265. count = logged_chars;
  266. if (do_clear)
  267. logged_chars = 0;
  268. limit = log_end;
  269. /*
  270. * __put_user() could sleep, and while we sleep
  271. * printk() could overwrite the messages
  272. * we try to copy to user space. Therefore
  273. * the messages are copied in reverse. <manfreds>
  274. */
  275. for (i = 0; i < count && !error; i++) {
  276. j = limit-1-i;
  277. if (j + log_buf_len < log_end)
  278. break;
  279. c = LOG_BUF(j);
  280. spin_unlock_irq(&logbuf_lock);
  281. error = __put_user(c,&buf[count-1-i]);
  282. cond_resched();
  283. spin_lock_irq(&logbuf_lock);
  284. }
  285. spin_unlock_irq(&logbuf_lock);
  286. if (error)
  287. break;
  288. error = i;
  289. if (i != count) {
  290. int offset = count-error;
  291. /* buffer overflow during copy, correct user buffer. */
  292. for (i = 0; i < error; i++) {
  293. if (__get_user(c,&buf[i+offset]) ||
  294. __put_user(c,&buf[i])) {
  295. error = -EFAULT;
  296. break;
  297. }
  298. cond_resched();
  299. }
  300. }
  301. break;
  302. case 5: /* Clear ring buffer */
  303. logged_chars = 0;
  304. break;
  305. case 6: /* Disable logging to console */
  306. console_loglevel = minimum_console_loglevel;
  307. break;
  308. case 7: /* Enable logging to console */
  309. console_loglevel = default_console_loglevel;
  310. break;
  311. case 8: /* Set level of messages printed to console */
  312. error = -EINVAL;
  313. if (len < 1 || len > 8)
  314. goto out;
  315. if (len < minimum_console_loglevel)
  316. len = minimum_console_loglevel;
  317. console_loglevel = len;
  318. error = 0;
  319. break;
  320. case 9: /* Number of chars in the log buffer */
  321. error = log_end - log_start;
  322. break;
  323. case 10: /* Size of the log buffer */
  324. error = log_buf_len;
  325. break;
  326. default:
  327. error = -EINVAL;
  328. break;
  329. }
  330. out:
  331. return error;
  332. }
  333. asmlinkage long sys_syslog(int type, char __user *buf, int len)
  334. {
  335. return do_syslog(type, buf, len);
  336. }
  337. /*
  338. * Call the console drivers on a range of log_buf
  339. */
  340. static void __call_console_drivers(unsigned long start, unsigned long end)
  341. {
  342. struct console *con;
  343. for (con = console_drivers; con; con = con->next) {
  344. if ((con->flags & CON_ENABLED) && con->write &&
  345. (cpu_online(smp_processor_id()) ||
  346. (con->flags & CON_ANYTIME)))
  347. con->write(con, &LOG_BUF(start), end - start);
  348. }
  349. }
  350. static int __read_mostly ignore_loglevel;
  351. static int __init ignore_loglevel_setup(char *str)
  352. {
  353. ignore_loglevel = 1;
  354. printk(KERN_INFO "debug: ignoring loglevel setting.\n");
  355. return 1;
  356. }
  357. __setup("ignore_loglevel", ignore_loglevel_setup);
  358. /*
  359. * Write out chars from start to end - 1 inclusive
  360. */
  361. static void _call_console_drivers(unsigned long start,
  362. unsigned long end, int msg_log_level)
  363. {
  364. if ((msg_log_level < console_loglevel || ignore_loglevel) &&
  365. console_drivers && start != end) {
  366. if ((start & LOG_BUF_MASK) > (end & LOG_BUF_MASK)) {
  367. /* wrapped write */
  368. __call_console_drivers(start & LOG_BUF_MASK,
  369. log_buf_len);
  370. __call_console_drivers(0, end & LOG_BUF_MASK);
  371. } else {
  372. __call_console_drivers(start, end);
  373. }
  374. }
  375. }
  376. /*
  377. * Call the console drivers, asking them to write out
  378. * log_buf[start] to log_buf[end - 1].
  379. * The console_sem must be held.
  380. */
  381. static void call_console_drivers(unsigned long start, unsigned long end)
  382. {
  383. unsigned long cur_index, start_print;
  384. static int msg_level = -1;
  385. BUG_ON(((long)(start - end)) > 0);
  386. cur_index = start;
  387. start_print = start;
  388. while (cur_index != end) {
  389. if (msg_level < 0 && ((end - cur_index) > 2) &&
  390. LOG_BUF(cur_index + 0) == '<' &&
  391. LOG_BUF(cur_index + 1) >= '0' &&
  392. LOG_BUF(cur_index + 1) <= '7' &&
  393. LOG_BUF(cur_index + 2) == '>') {
  394. msg_level = LOG_BUF(cur_index + 1) - '0';
  395. cur_index += 3;
  396. start_print = cur_index;
  397. }
  398. while (cur_index != end) {
  399. char c = LOG_BUF(cur_index);
  400. cur_index++;
  401. if (c == '\n') {
  402. if (msg_level < 0) {
  403. /*
  404. * printk() has already given us loglevel tags in
  405. * the buffer. This code is here in case the
  406. * log buffer has wrapped right round and scribbled
  407. * on those tags
  408. */
  409. msg_level = default_message_loglevel;
  410. }
  411. _call_console_drivers(start_print, cur_index, msg_level);
  412. msg_level = -1;
  413. start_print = cur_index;
  414. break;
  415. }
  416. }
  417. }
  418. _call_console_drivers(start_print, end, msg_level);
  419. }
  420. static void emit_log_char(char c)
  421. {
  422. LOG_BUF(log_end) = c;
  423. log_end++;
  424. if (log_end - log_start > log_buf_len)
  425. log_start = log_end - log_buf_len;
  426. if (log_end - con_start > log_buf_len)
  427. con_start = log_end - log_buf_len;
  428. if (logged_chars < log_buf_len)
  429. logged_chars++;
  430. }
  431. /*
  432. * Zap console related locks when oopsing. Only zap at most once
  433. * every 10 seconds, to leave time for slow consoles to print a
  434. * full oops.
  435. */
  436. static void zap_locks(void)
  437. {
  438. static unsigned long oops_timestamp;
  439. if (time_after_eq(jiffies, oops_timestamp) &&
  440. !time_after(jiffies, oops_timestamp + 30 * HZ))
  441. return;
  442. oops_timestamp = jiffies;
  443. /* If a crash is occurring, make sure we can't deadlock */
  444. spin_lock_init(&logbuf_lock);
  445. /* And make sure that we print immediately */
  446. init_MUTEX(&console_sem);
  447. }
  448. #if defined(CONFIG_PRINTK_TIME)
  449. static int printk_time = 1;
  450. #else
  451. static int printk_time = 0;
  452. #endif
  453. module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR);
  454. static int __init printk_time_setup(char *str)
  455. {
  456. if (*str)
  457. return 0;
  458. printk_time = 1;
  459. printk(KERN_NOTICE "The 'time' option is deprecated and "
  460. "is scheduled for removal in early 2008\n");
  461. printk(KERN_NOTICE "Use 'printk.time=<value>' instead\n");
  462. return 1;
  463. }
  464. __setup("time", printk_time_setup);
  465. __attribute__((weak)) unsigned long long printk_clock(void)
  466. {
  467. return sched_clock();
  468. }
  469. /* Check if we have any console registered that can be called early in boot. */
  470. static int have_callable_console(void)
  471. {
  472. struct console *con;
  473. for (con = console_drivers; con; con = con->next)
  474. if (con->flags & CON_ANYTIME)
  475. return 1;
  476. return 0;
  477. }
  478. /**
  479. * printk - print a kernel message
  480. * @fmt: format string
  481. *
  482. * This is printk(). It can be called from any context. We want it to work.
  483. * Be aware of the fact that if oops_in_progress is not set, we might try to
  484. * wake klogd up which could deadlock on runqueue lock if printk() is called
  485. * from scheduler code.
  486. *
  487. * We try to grab the console_sem. If we succeed, it's easy - we log the output and
  488. * call the console drivers. If we fail to get the semaphore we place the output
  489. * into the log buffer and return. The current holder of the console_sem will
  490. * notice the new output in release_console_sem() and will send it to the
  491. * consoles before releasing the semaphore.
  492. *
  493. * One effect of this deferred printing is that code which calls printk() and
  494. * then changes console_loglevel may break. This is because console_loglevel
  495. * is inspected when the actual printing occurs.
  496. *
  497. * See also:
  498. * printf(3)
  499. */
  500. asmlinkage int printk(const char *fmt, ...)
  501. {
  502. va_list args;
  503. int r;
  504. va_start(args, fmt);
  505. r = vprintk(fmt, args);
  506. va_end(args);
  507. return r;
  508. }
  509. /* cpu currently holding logbuf_lock */
  510. static volatile unsigned int printk_cpu = UINT_MAX;
  511. asmlinkage int vprintk(const char *fmt, va_list args)
  512. {
  513. unsigned long flags;
  514. int printed_len;
  515. char *p;
  516. static char printk_buf[1024];
  517. static int log_level_unknown = 1;
  518. boot_delay_msec();
  519. preempt_disable();
  520. if (unlikely(oops_in_progress) && printk_cpu == smp_processor_id())
  521. /* If a crash is occurring during printk() on this CPU,
  522. * make sure we can't deadlock */
  523. zap_locks();
  524. /* This stops the holder of console_sem just where we want him */
  525. raw_local_irq_save(flags);
  526. lockdep_off();
  527. spin_lock(&logbuf_lock);
  528. printk_cpu = smp_processor_id();
  529. /* Emit the output into the temporary buffer */
  530. printed_len = vscnprintf(printk_buf, sizeof(printk_buf), fmt, args);
  531. /*
  532. * Copy the output into log_buf. If the caller didn't provide
  533. * appropriate log level tags, we insert them here
  534. */
  535. for (p = printk_buf; *p; p++) {
  536. if (log_level_unknown) {
  537. /* log_level_unknown signals the start of a new line */
  538. if (printk_time) {
  539. int loglev_char;
  540. char tbuf[50], *tp;
  541. unsigned tlen;
  542. unsigned long long t;
  543. unsigned long nanosec_rem;
  544. /*
  545. * force the log level token to be
  546. * before the time output.
  547. */
  548. if (p[0] == '<' && p[1] >='0' &&
  549. p[1] <= '7' && p[2] == '>') {
  550. loglev_char = p[1];
  551. p += 3;
  552. printed_len -= 3;
  553. } else {
  554. loglev_char = default_message_loglevel
  555. + '0';
  556. }
  557. t = printk_clock();
  558. nanosec_rem = do_div(t, 1000000000);
  559. tlen = sprintf(tbuf,
  560. "<%c>[%5lu.%06lu] ",
  561. loglev_char,
  562. (unsigned long)t,
  563. nanosec_rem/1000);
  564. for (tp = tbuf; tp < tbuf + tlen; tp++)
  565. emit_log_char(*tp);
  566. printed_len += tlen;
  567. } else {
  568. if (p[0] != '<' || p[1] < '0' ||
  569. p[1] > '7' || p[2] != '>') {
  570. emit_log_char('<');
  571. emit_log_char(default_message_loglevel
  572. + '0');
  573. emit_log_char('>');
  574. printed_len += 3;
  575. }
  576. }
  577. log_level_unknown = 0;
  578. if (!*p)
  579. break;
  580. }
  581. emit_log_char(*p);
  582. if (*p == '\n')
  583. log_level_unknown = 1;
  584. }
  585. if (!down_trylock(&console_sem)) {
  586. /*
  587. * We own the drivers. We can drop the spinlock and
  588. * let release_console_sem() print the text, maybe ...
  589. */
  590. console_locked = 1;
  591. printk_cpu = UINT_MAX;
  592. spin_unlock(&logbuf_lock);
  593. /*
  594. * Console drivers may assume that per-cpu resources have
  595. * been allocated. So unless they're explicitly marked as
  596. * being able to cope (CON_ANYTIME) don't call them until
  597. * this CPU is officially up.
  598. */
  599. if (cpu_online(smp_processor_id()) || have_callable_console()) {
  600. console_may_schedule = 0;
  601. release_console_sem();
  602. } else {
  603. /* Release by hand to avoid flushing the buffer. */
  604. console_locked = 0;
  605. up(&console_sem);
  606. }
  607. lockdep_on();
  608. raw_local_irq_restore(flags);
  609. } else {
  610. /*
  611. * Someone else owns the drivers. We drop the spinlock, which
  612. * allows the semaphore holder to proceed and to call the
  613. * console drivers with the output which we just produced.
  614. */
  615. printk_cpu = UINT_MAX;
  616. spin_unlock(&logbuf_lock);
  617. lockdep_on();
  618. raw_local_irq_restore(flags);
  619. }
  620. preempt_enable();
  621. return printed_len;
  622. }
  623. EXPORT_SYMBOL(printk);
  624. EXPORT_SYMBOL(vprintk);
  625. #else
  626. asmlinkage long sys_syslog(int type, char __user *buf, int len)
  627. {
  628. return -ENOSYS;
  629. }
  630. static void call_console_drivers(unsigned long start, unsigned long end)
  631. {
  632. }
  633. #endif
  634. /*
  635. * Set up a list of consoles. Called from init/main.c
  636. */
  637. static int __init console_setup(char *str)
  638. {
  639. char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for index */
  640. char *s, *options;
  641. int idx;
  642. /*
  643. * Decode str into name, index, options.
  644. */
  645. if (str[0] >= '0' && str[0] <= '9') {
  646. strcpy(buf, "ttyS");
  647. strncpy(buf + 4, str, sizeof(buf) - 5);
  648. } else {
  649. strncpy(buf, str, sizeof(buf) - 1);
  650. }
  651. buf[sizeof(buf) - 1] = 0;
  652. if ((options = strchr(str, ',')) != NULL)
  653. *(options++) = 0;
  654. #ifdef __sparc__
  655. if (!strcmp(str, "ttya"))
  656. strcpy(buf, "ttyS0");
  657. if (!strcmp(str, "ttyb"))
  658. strcpy(buf, "ttyS1");
  659. #endif
  660. for (s = buf; *s; s++)
  661. if ((*s >= '0' && *s <= '9') || *s == ',')
  662. break;
  663. idx = simple_strtoul(s, NULL, 10);
  664. *s = 0;
  665. add_preferred_console(buf, idx, options);
  666. return 1;
  667. }
  668. __setup("console=", console_setup);
  669. /**
  670. * add_preferred_console - add a device to the list of preferred consoles.
  671. * @name: device name
  672. * @idx: device index
  673. * @options: options for this console
  674. *
  675. * The last preferred console added will be used for kernel messages
  676. * and stdin/out/err for init. Normally this is used by console_setup
  677. * above to handle user-supplied console arguments; however it can also
  678. * be used by arch-specific code either to override the user or more
  679. * commonly to provide a default console (ie from PROM variables) when
  680. * the user has not supplied one.
  681. */
  682. int __init add_preferred_console(char *name, int idx, char *options)
  683. {
  684. struct console_cmdline *c;
  685. int i;
  686. /*
  687. * See if this tty is not yet registered, and
  688. * if we have a slot free.
  689. */
  690. for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
  691. if (strcmp(console_cmdline[i].name, name) == 0 &&
  692. console_cmdline[i].index == idx) {
  693. selected_console = i;
  694. return 0;
  695. }
  696. if (i == MAX_CMDLINECONSOLES)
  697. return -E2BIG;
  698. selected_console = i;
  699. c = &console_cmdline[i];
  700. memcpy(c->name, name, sizeof(c->name));
  701. c->name[sizeof(c->name) - 1] = 0;
  702. c->options = options;
  703. c->index = idx;
  704. return 0;
  705. }
  706. int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options)
  707. {
  708. struct console_cmdline *c;
  709. int i;
  710. for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
  711. if (strcmp(console_cmdline[i].name, name) == 0 &&
  712. console_cmdline[i].index == idx) {
  713. c = &console_cmdline[i];
  714. memcpy(c->name, name_new, sizeof(c->name));
  715. c->name[sizeof(c->name) - 1] = 0;
  716. c->options = options;
  717. c->index = idx_new;
  718. return i;
  719. }
  720. /* not found */
  721. return -1;
  722. }
  723. #ifndef CONFIG_DISABLE_CONSOLE_SUSPEND
  724. /**
  725. * suspend_console - suspend the console subsystem
  726. *
  727. * This disables printk() while we go into suspend states
  728. */
  729. void suspend_console(void)
  730. {
  731. printk("Suspending console(s)\n");
  732. acquire_console_sem();
  733. console_suspended = 1;
  734. }
  735. void resume_console(void)
  736. {
  737. console_suspended = 0;
  738. release_console_sem();
  739. }
  740. #endif /* CONFIG_DISABLE_CONSOLE_SUSPEND */
  741. /**
  742. * acquire_console_sem - lock the console system for exclusive use.
  743. *
  744. * Acquires a semaphore which guarantees that the caller has
  745. * exclusive access to the console system and the console_drivers list.
  746. *
  747. * Can sleep, returns nothing.
  748. */
  749. void acquire_console_sem(void)
  750. {
  751. BUG_ON(in_interrupt());
  752. if (console_suspended) {
  753. down(&secondary_console_sem);
  754. return;
  755. }
  756. down(&console_sem);
  757. console_locked = 1;
  758. console_may_schedule = 1;
  759. }
  760. EXPORT_SYMBOL(acquire_console_sem);
  761. int try_acquire_console_sem(void)
  762. {
  763. if (down_trylock(&console_sem))
  764. return -1;
  765. console_locked = 1;
  766. console_may_schedule = 0;
  767. return 0;
  768. }
  769. EXPORT_SYMBOL(try_acquire_console_sem);
  770. int is_console_locked(void)
  771. {
  772. return console_locked;
  773. }
  774. void wake_up_klogd(void)
  775. {
  776. if (!oops_in_progress && waitqueue_active(&log_wait))
  777. wake_up_interruptible(&log_wait);
  778. }
  779. /**
  780. * release_console_sem - unlock the console system
  781. *
  782. * Releases the semaphore which the caller holds on the console system
  783. * and the console driver list.
  784. *
  785. * While the semaphore was held, console output may have been buffered
  786. * by printk(). If this is the case, release_console_sem() emits
  787. * the output prior to releasing the semaphore.
  788. *
  789. * If there is output waiting for klogd, we wake it up.
  790. *
  791. * release_console_sem() may be called from any context.
  792. */
  793. void release_console_sem(void)
  794. {
  795. unsigned long flags;
  796. unsigned long _con_start, _log_end;
  797. unsigned long wake_klogd = 0;
  798. if (console_suspended) {
  799. up(&secondary_console_sem);
  800. return;
  801. }
  802. console_may_schedule = 0;
  803. for ( ; ; ) {
  804. spin_lock_irqsave(&logbuf_lock, flags);
  805. wake_klogd |= log_start - log_end;
  806. if (con_start == log_end)
  807. break; /* Nothing to print */
  808. _con_start = con_start;
  809. _log_end = log_end;
  810. con_start = log_end; /* Flush */
  811. spin_unlock(&logbuf_lock);
  812. call_console_drivers(_con_start, _log_end);
  813. local_irq_restore(flags);
  814. }
  815. console_locked = 0;
  816. up(&console_sem);
  817. spin_unlock_irqrestore(&logbuf_lock, flags);
  818. if (wake_klogd)
  819. wake_up_klogd();
  820. }
  821. EXPORT_SYMBOL(release_console_sem);
  822. /**
  823. * console_conditional_schedule - yield the CPU if required
  824. *
  825. * If the console code is currently allowed to sleep, and
  826. * if this CPU should yield the CPU to another task, do
  827. * so here.
  828. *
  829. * Must be called within acquire_console_sem().
  830. */
  831. void __sched console_conditional_schedule(void)
  832. {
  833. if (console_may_schedule)
  834. cond_resched();
  835. }
  836. EXPORT_SYMBOL(console_conditional_schedule);
  837. void console_print(const char *s)
  838. {
  839. printk(KERN_EMERG "%s", s);
  840. }
  841. EXPORT_SYMBOL(console_print);
  842. void console_unblank(void)
  843. {
  844. struct console *c;
  845. /*
  846. * console_unblank can no longer be called in interrupt context unless
  847. * oops_in_progress is set to 1..
  848. */
  849. if (oops_in_progress) {
  850. if (down_trylock(&console_sem) != 0)
  851. return;
  852. } else
  853. acquire_console_sem();
  854. console_locked = 1;
  855. console_may_schedule = 0;
  856. for (c = console_drivers; c != NULL; c = c->next)
  857. if ((c->flags & CON_ENABLED) && c->unblank)
  858. c->unblank();
  859. release_console_sem();
  860. }
  861. /*
  862. * Return the console tty driver structure and its associated index
  863. */
  864. struct tty_driver *console_device(int *index)
  865. {
  866. struct console *c;
  867. struct tty_driver *driver = NULL;
  868. acquire_console_sem();
  869. for (c = console_drivers; c != NULL; c = c->next) {
  870. if (!c->device)
  871. continue;
  872. driver = c->device(c, index);
  873. if (driver)
  874. break;
  875. }
  876. release_console_sem();
  877. return driver;
  878. }
  879. /*
  880. * Prevent further output on the passed console device so that (for example)
  881. * serial drivers can disable console output before suspending a port, and can
  882. * re-enable output afterwards.
  883. */
  884. void console_stop(struct console *console)
  885. {
  886. acquire_console_sem();
  887. console->flags &= ~CON_ENABLED;
  888. release_console_sem();
  889. }
  890. EXPORT_SYMBOL(console_stop);
  891. void console_start(struct console *console)
  892. {
  893. acquire_console_sem();
  894. console->flags |= CON_ENABLED;
  895. release_console_sem();
  896. }
  897. EXPORT_SYMBOL(console_start);
  898. /*
  899. * The console driver calls this routine during kernel initialization
  900. * to register the console printing procedure with printk() and to
  901. * print any messages that were printed by the kernel before the
  902. * console driver was initialized.
  903. */
  904. void register_console(struct console *console)
  905. {
  906. int i;
  907. unsigned long flags;
  908. struct console *bootconsole = NULL;
  909. if (console_drivers) {
  910. if (console->flags & CON_BOOT)
  911. return;
  912. if (console_drivers->flags & CON_BOOT)
  913. bootconsole = console_drivers;
  914. }
  915. if (preferred_console < 0 || bootconsole || !console_drivers)
  916. preferred_console = selected_console;
  917. if (console->early_setup)
  918. console->early_setup();
  919. /*
  920. * See if we want to use this console driver. If we
  921. * didn't select a console we take the first one
  922. * that registers here.
  923. */
  924. if (preferred_console < 0) {
  925. if (console->index < 0)
  926. console->index = 0;
  927. if (console->setup == NULL ||
  928. console->setup(console, NULL) == 0) {
  929. console->flags |= CON_ENABLED | CON_CONSDEV;
  930. preferred_console = 0;
  931. }
  932. }
  933. /*
  934. * See if this console matches one we selected on
  935. * the command line.
  936. */
  937. for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0];
  938. i++) {
  939. if (strcmp(console_cmdline[i].name, console->name) != 0)
  940. continue;
  941. if (console->index >= 0 &&
  942. console->index != console_cmdline[i].index)
  943. continue;
  944. if (console->index < 0)
  945. console->index = console_cmdline[i].index;
  946. if (console->setup &&
  947. console->setup(console, console_cmdline[i].options) != 0)
  948. break;
  949. console->flags |= CON_ENABLED;
  950. console->index = console_cmdline[i].index;
  951. if (i == selected_console) {
  952. console->flags |= CON_CONSDEV;
  953. preferred_console = selected_console;
  954. }
  955. break;
  956. }
  957. if (!(console->flags & CON_ENABLED))
  958. return;
  959. if (bootconsole && (console->flags & CON_CONSDEV)) {
  960. printk(KERN_INFO "console handover: boot [%s%d] -> real [%s%d]\n",
  961. bootconsole->name, bootconsole->index,
  962. console->name, console->index);
  963. unregister_console(bootconsole);
  964. console->flags &= ~CON_PRINTBUFFER;
  965. } else {
  966. printk(KERN_INFO "console [%s%d] enabled\n",
  967. console->name, console->index);
  968. }
  969. /*
  970. * Put this console in the list - keep the
  971. * preferred driver at the head of the list.
  972. */
  973. acquire_console_sem();
  974. if ((console->flags & CON_CONSDEV) || console_drivers == NULL) {
  975. console->next = console_drivers;
  976. console_drivers = console;
  977. if (console->next)
  978. console->next->flags &= ~CON_CONSDEV;
  979. } else {
  980. console->next = console_drivers->next;
  981. console_drivers->next = console;
  982. }
  983. if (console->flags & CON_PRINTBUFFER) {
  984. /*
  985. * release_console_sem() will print out the buffered messages
  986. * for us.
  987. */
  988. spin_lock_irqsave(&logbuf_lock, flags);
  989. con_start = log_start;
  990. spin_unlock_irqrestore(&logbuf_lock, flags);
  991. }
  992. release_console_sem();
  993. }
  994. EXPORT_SYMBOL(register_console);
  995. int unregister_console(struct console *console)
  996. {
  997. struct console *a, *b;
  998. int res = 1;
  999. acquire_console_sem();
  1000. if (console_drivers == console) {
  1001. console_drivers=console->next;
  1002. res = 0;
  1003. } else if (console_drivers) {
  1004. for (a=console_drivers->next, b=console_drivers ;
  1005. a; b=a, a=b->next) {
  1006. if (a == console) {
  1007. b->next = a->next;
  1008. res = 0;
  1009. break;
  1010. }
  1011. }
  1012. }
  1013. /*
  1014. * If this isn't the last console and it has CON_CONSDEV set, we
  1015. * need to set it on the next preferred console.
  1016. */
  1017. if (console_drivers != NULL && console->flags & CON_CONSDEV)
  1018. console_drivers->flags |= CON_CONSDEV;
  1019. release_console_sem();
  1020. return res;
  1021. }
  1022. EXPORT_SYMBOL(unregister_console);
  1023. static int __init disable_boot_consoles(void)
  1024. {
  1025. if (console_drivers != NULL) {
  1026. if (console_drivers->flags & CON_BOOT) {
  1027. printk(KERN_INFO "turn off boot console %s%d\n",
  1028. console_drivers->name, console_drivers->index);
  1029. return unregister_console(console_drivers);
  1030. }
  1031. }
  1032. return 0;
  1033. }
  1034. late_initcall(disable_boot_consoles);
  1035. /**
  1036. * tty_write_message - write a message to a certain tty, not just the console.
  1037. * @tty: the destination tty_struct
  1038. * @msg: the message to write
  1039. *
  1040. * This is used for messages that need to be redirected to a specific tty.
  1041. * We don't put it into the syslog queue right now maybe in the future if
  1042. * really needed.
  1043. */
  1044. void tty_write_message(struct tty_struct *tty, char *msg)
  1045. {
  1046. if (tty && tty->driver->write)
  1047. tty->driver->write(tty, msg, strlen(msg));
  1048. return;
  1049. }
  1050. /*
  1051. * printk rate limiting, lifted from the networking subsystem.
  1052. *
  1053. * This enforces a rate limit: not more than one kernel message
  1054. * every printk_ratelimit_jiffies to make a denial-of-service
  1055. * attack impossible.
  1056. */
  1057. int __printk_ratelimit(int ratelimit_jiffies, int ratelimit_burst)
  1058. {
  1059. static DEFINE_SPINLOCK(ratelimit_lock);
  1060. static unsigned long toks = 10 * 5 * HZ;
  1061. static unsigned long last_msg;
  1062. static int missed;
  1063. unsigned long flags;
  1064. unsigned long now = jiffies;
  1065. spin_lock_irqsave(&ratelimit_lock, flags);
  1066. toks += now - last_msg;
  1067. last_msg = now;
  1068. if (toks > (ratelimit_burst * ratelimit_jiffies))
  1069. toks = ratelimit_burst * ratelimit_jiffies;
  1070. if (toks >= ratelimit_jiffies) {
  1071. int lost = missed;
  1072. missed = 0;
  1073. toks -= ratelimit_jiffies;
  1074. spin_unlock_irqrestore(&ratelimit_lock, flags);
  1075. if (lost)
  1076. printk(KERN_WARNING "printk: %d messages suppressed.\n", lost);
  1077. return 1;
  1078. }
  1079. missed++;
  1080. spin_unlock_irqrestore(&ratelimit_lock, flags);
  1081. return 0;
  1082. }
  1083. EXPORT_SYMBOL(__printk_ratelimit);
  1084. /* minimum time in jiffies between messages */
  1085. int printk_ratelimit_jiffies = 5 * HZ;
  1086. /* number of messages we send before ratelimiting */
  1087. int printk_ratelimit_burst = 10;
  1088. int printk_ratelimit(void)
  1089. {
  1090. return __printk_ratelimit(printk_ratelimit_jiffies,
  1091. printk_ratelimit_burst);
  1092. }
  1093. EXPORT_SYMBOL(printk_ratelimit);
  1094. /**
  1095. * printk_timed_ratelimit - caller-controlled printk ratelimiting
  1096. * @caller_jiffies: pointer to caller's state
  1097. * @interval_msecs: minimum interval between prints
  1098. *
  1099. * printk_timed_ratelimit() returns true if more than @interval_msecs
  1100. * milliseconds have elapsed since the last time printk_timed_ratelimit()
  1101. * returned true.
  1102. */
  1103. bool printk_timed_ratelimit(unsigned long *caller_jiffies,
  1104. unsigned int interval_msecs)
  1105. {
  1106. if (*caller_jiffies == 0 || time_after(jiffies, *caller_jiffies)) {
  1107. *caller_jiffies = jiffies + msecs_to_jiffies(interval_msecs);
  1108. return true;
  1109. }
  1110. return false;
  1111. }
  1112. EXPORT_SYMBOL(printk_timed_ratelimit);