printk.c 53 KB

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