]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - include/linux/printk.h
printk/nmi: generic solution for safe printk in NMI
[mirror_ubuntu-artful-kernel.git] / include / linux / printk.h
1 #ifndef __KERNEL_PRINTK__
2 #define __KERNEL_PRINTK__
3
4 #include <stdarg.h>
5 #include <linux/init.h>
6 #include <linux/kern_levels.h>
7 #include <linux/linkage.h>
8 #include <linux/cache.h>
9
10 extern const char linux_banner[];
11 extern const char linux_proc_banner[];
12
13 static inline int printk_get_level(const char *buffer)
14 {
15 if (buffer[0] == KERN_SOH_ASCII && buffer[1]) {
16 switch (buffer[1]) {
17 case '0' ... '7':
18 case 'd': /* KERN_DEFAULT */
19 return buffer[1];
20 }
21 }
22 return 0;
23 }
24
25 static inline const char *printk_skip_level(const char *buffer)
26 {
27 if (printk_get_level(buffer))
28 return buffer + 2;
29
30 return buffer;
31 }
32
33 #define CONSOLE_EXT_LOG_MAX 8192
34
35 /* printk's without a loglevel use this.. */
36 #define MESSAGE_LOGLEVEL_DEFAULT CONFIG_MESSAGE_LOGLEVEL_DEFAULT
37
38 /* We show everything that is MORE important than this.. */
39 #define CONSOLE_LOGLEVEL_SILENT 0 /* Mum's the word */
40 #define CONSOLE_LOGLEVEL_MIN 1 /* Minimum loglevel we let people use */
41 #define CONSOLE_LOGLEVEL_QUIET 4 /* Shhh ..., when booted with "quiet" */
42 #define CONSOLE_LOGLEVEL_DEFAULT 7 /* anything MORE serious than KERN_DEBUG */
43 #define CONSOLE_LOGLEVEL_DEBUG 10 /* issue debug messages */
44 #define CONSOLE_LOGLEVEL_MOTORMOUTH 15 /* You can't shut this one up */
45
46 extern int console_printk[];
47
48 #define console_loglevel (console_printk[0])
49 #define default_message_loglevel (console_printk[1])
50 #define minimum_console_loglevel (console_printk[2])
51 #define default_console_loglevel (console_printk[3])
52
53 static inline void console_silent(void)
54 {
55 console_loglevel = CONSOLE_LOGLEVEL_SILENT;
56 }
57
58 static inline void console_verbose(void)
59 {
60 if (console_loglevel)
61 console_loglevel = CONSOLE_LOGLEVEL_MOTORMOUTH;
62 }
63
64 struct va_format {
65 const char *fmt;
66 va_list *va;
67 };
68
69 /*
70 * FW_BUG
71 * Add this to a message where you are sure the firmware is buggy or behaves
72 * really stupid or out of spec. Be aware that the responsible BIOS developer
73 * should be able to fix this issue or at least get a concrete idea of the
74 * problem by reading your message without the need of looking at the kernel
75 * code.
76 *
77 * Use it for definite and high priority BIOS bugs.
78 *
79 * FW_WARN
80 * Use it for not that clear (e.g. could the kernel messed up things already?)
81 * and medium priority BIOS bugs.
82 *
83 * FW_INFO
84 * Use this one if you want to tell the user or vendor about something
85 * suspicious, but generally harmless related to the firmware.
86 *
87 * Use it for information or very low priority BIOS bugs.
88 */
89 #define FW_BUG "[Firmware Bug]: "
90 #define FW_WARN "[Firmware Warn]: "
91 #define FW_INFO "[Firmware Info]: "
92
93 /*
94 * HW_ERR
95 * Add this to a message for hardware errors, so that user can report
96 * it to hardware vendor instead of LKML or software vendor.
97 */
98 #define HW_ERR "[Hardware Error]: "
99
100 /*
101 * DEPRECATED
102 * Add this to a message whenever you want to warn user space about the use
103 * of a deprecated aspect of an API so they can stop using it
104 */
105 #define DEPRECATED "[Deprecated]: "
106
107 /*
108 * Dummy printk for disabled debugging statements to use whilst maintaining
109 * gcc's format checking.
110 */
111 #define no_printk(fmt, ...) \
112 do { \
113 if (0) \
114 printk(fmt, ##__VA_ARGS__); \
115 } while (0)
116
117 #ifdef CONFIG_EARLY_PRINTK
118 extern asmlinkage __printf(1, 2)
119 void early_printk(const char *fmt, ...);
120 #else
121 static inline __printf(1, 2) __cold
122 void early_printk(const char *s, ...) { }
123 #endif
124
125 #ifdef CONFIG_PRINTK_NMI
126 extern void printk_nmi_init(void);
127 extern void printk_nmi_enter(void);
128 extern void printk_nmi_exit(void);
129 extern void printk_nmi_flush(void);
130 #else
131 static inline void printk_nmi_init(void) { }
132 static inline void printk_nmi_enter(void) { }
133 static inline void printk_nmi_exit(void) { }
134 static inline void printk_nmi_flush(void) { }
135 #endif /* PRINTK_NMI */
136
137 #ifdef CONFIG_PRINTK
138 asmlinkage __printf(5, 0)
139 int vprintk_emit(int facility, int level,
140 const char *dict, size_t dictlen,
141 const char *fmt, va_list args);
142
143 asmlinkage __printf(1, 0)
144 int vprintk(const char *fmt, va_list args);
145
146 asmlinkage __printf(5, 6) __cold
147 int printk_emit(int facility, int level,
148 const char *dict, size_t dictlen,
149 const char *fmt, ...);
150
151 asmlinkage __printf(1, 2) __cold
152 int printk(const char *fmt, ...);
153
154 /*
155 * Special printk facility for scheduler/timekeeping use only, _DO_NOT_USE_ !
156 */
157 __printf(1, 2) __cold int printk_deferred(const char *fmt, ...);
158
159 /*
160 * Please don't use printk_ratelimit(), because it shares ratelimiting state
161 * with all other unrelated printk_ratelimit() callsites. Instead use
162 * printk_ratelimited() or plain old __ratelimit().
163 */
164 extern int __printk_ratelimit(const char *func);
165 #define printk_ratelimit() __printk_ratelimit(__func__)
166 extern bool printk_timed_ratelimit(unsigned long *caller_jiffies,
167 unsigned int interval_msec);
168
169 extern int printk_delay_msec;
170 extern int dmesg_restrict;
171 extern int kptr_restrict;
172
173 extern void wake_up_klogd(void);
174
175 char *log_buf_addr_get(void);
176 u32 log_buf_len_get(void);
177 void log_buf_kexec_setup(void);
178 void __init setup_log_buf(int early);
179 __printf(1, 2) void dump_stack_set_arch_desc(const char *fmt, ...);
180 void dump_stack_print_info(const char *log_lvl);
181 void show_regs_print_info(const char *log_lvl);
182 #else
183 static inline __printf(1, 0)
184 int vprintk(const char *s, va_list args)
185 {
186 return 0;
187 }
188 static inline __printf(1, 2) __cold
189 int printk(const char *s, ...)
190 {
191 return 0;
192 }
193 static inline __printf(1, 2) __cold
194 int printk_deferred(const char *s, ...)
195 {
196 return 0;
197 }
198 static inline int printk_ratelimit(void)
199 {
200 return 0;
201 }
202 static inline bool printk_timed_ratelimit(unsigned long *caller_jiffies,
203 unsigned int interval_msec)
204 {
205 return false;
206 }
207
208 static inline void wake_up_klogd(void)
209 {
210 }
211
212 static inline char *log_buf_addr_get(void)
213 {
214 return NULL;
215 }
216
217 static inline u32 log_buf_len_get(void)
218 {
219 return 0;
220 }
221
222 static inline void log_buf_kexec_setup(void)
223 {
224 }
225
226 static inline void setup_log_buf(int early)
227 {
228 }
229
230 static inline __printf(1, 2) void dump_stack_set_arch_desc(const char *fmt, ...)
231 {
232 }
233
234 static inline void dump_stack_print_info(const char *log_lvl)
235 {
236 }
237
238 static inline void show_regs_print_info(const char *log_lvl)
239 {
240 }
241 #endif
242
243 extern asmlinkage void dump_stack(void) __cold;
244
245 #ifndef pr_fmt
246 #define pr_fmt(fmt) fmt
247 #endif
248
249 /*
250 * These can be used to print at the various log levels.
251 * All of these will print unconditionally, although note that pr_debug()
252 * and other debug macros are compiled out unless either DEBUG is defined
253 * or CONFIG_DYNAMIC_DEBUG is set.
254 */
255 #define pr_emerg(fmt, ...) \
256 printk(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
257 #define pr_alert(fmt, ...) \
258 printk(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
259 #define pr_crit(fmt, ...) \
260 printk(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
261 #define pr_err(fmt, ...) \
262 printk(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
263 #define pr_warning(fmt, ...) \
264 printk(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
265 #define pr_warn pr_warning
266 #define pr_notice(fmt, ...) \
267 printk(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
268 #define pr_info(fmt, ...) \
269 printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
270 /*
271 * Like KERN_CONT, pr_cont() should only be used when continuing
272 * a line with no newline ('\n') enclosed. Otherwise it defaults
273 * back to KERN_DEFAULT.
274 */
275 #define pr_cont(fmt, ...) \
276 printk(KERN_CONT fmt, ##__VA_ARGS__)
277
278 /* pr_devel() should produce zero code unless DEBUG is defined */
279 #ifdef DEBUG
280 #define pr_devel(fmt, ...) \
281 printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
282 #else
283 #define pr_devel(fmt, ...) \
284 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
285 #endif
286
287 #include <linux/dynamic_debug.h>
288
289 /* If you are writing a driver, please use dev_dbg instead */
290 #if defined(CONFIG_DYNAMIC_DEBUG)
291 /* dynamic_pr_debug() uses pr_fmt() internally so we don't need it here */
292 #define pr_debug(fmt, ...) \
293 dynamic_pr_debug(fmt, ##__VA_ARGS__)
294 #elif defined(DEBUG)
295 #define pr_debug(fmt, ...) \
296 printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
297 #else
298 #define pr_debug(fmt, ...) \
299 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
300 #endif
301
302 /*
303 * Print a one-time message (analogous to WARN_ONCE() et al):
304 */
305
306 #ifdef CONFIG_PRINTK
307 #define printk_once(fmt, ...) \
308 ({ \
309 static bool __print_once __read_mostly; \
310 \
311 if (!__print_once) { \
312 __print_once = true; \
313 printk(fmt, ##__VA_ARGS__); \
314 } \
315 })
316 #define printk_deferred_once(fmt, ...) \
317 ({ \
318 static bool __print_once __read_mostly; \
319 \
320 if (!__print_once) { \
321 __print_once = true; \
322 printk_deferred(fmt, ##__VA_ARGS__); \
323 } \
324 })
325 #else
326 #define printk_once(fmt, ...) \
327 no_printk(fmt, ##__VA_ARGS__)
328 #define printk_deferred_once(fmt, ...) \
329 no_printk(fmt, ##__VA_ARGS__)
330 #endif
331
332 #define pr_emerg_once(fmt, ...) \
333 printk_once(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
334 #define pr_alert_once(fmt, ...) \
335 printk_once(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
336 #define pr_crit_once(fmt, ...) \
337 printk_once(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
338 #define pr_err_once(fmt, ...) \
339 printk_once(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
340 #define pr_warn_once(fmt, ...) \
341 printk_once(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
342 #define pr_notice_once(fmt, ...) \
343 printk_once(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
344 #define pr_info_once(fmt, ...) \
345 printk_once(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
346 #define pr_cont_once(fmt, ...) \
347 printk_once(KERN_CONT pr_fmt(fmt), ##__VA_ARGS__)
348
349 #if defined(DEBUG)
350 #define pr_devel_once(fmt, ...) \
351 printk_once(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
352 #else
353 #define pr_devel_once(fmt, ...) \
354 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
355 #endif
356
357 /* If you are writing a driver, please use dev_dbg instead */
358 #if defined(DEBUG)
359 #define pr_debug_once(fmt, ...) \
360 printk_once(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
361 #else
362 #define pr_debug_once(fmt, ...) \
363 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
364 #endif
365
366 /*
367 * ratelimited messages with local ratelimit_state,
368 * no local ratelimit_state used in the !PRINTK case
369 */
370 #ifdef CONFIG_PRINTK
371 #define printk_ratelimited(fmt, ...) \
372 ({ \
373 static DEFINE_RATELIMIT_STATE(_rs, \
374 DEFAULT_RATELIMIT_INTERVAL, \
375 DEFAULT_RATELIMIT_BURST); \
376 \
377 if (__ratelimit(&_rs)) \
378 printk(fmt, ##__VA_ARGS__); \
379 })
380 #else
381 #define printk_ratelimited(fmt, ...) \
382 no_printk(fmt, ##__VA_ARGS__)
383 #endif
384
385 #define pr_emerg_ratelimited(fmt, ...) \
386 printk_ratelimited(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
387 #define pr_alert_ratelimited(fmt, ...) \
388 printk_ratelimited(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
389 #define pr_crit_ratelimited(fmt, ...) \
390 printk_ratelimited(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
391 #define pr_err_ratelimited(fmt, ...) \
392 printk_ratelimited(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
393 #define pr_warn_ratelimited(fmt, ...) \
394 printk_ratelimited(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
395 #define pr_notice_ratelimited(fmt, ...) \
396 printk_ratelimited(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
397 #define pr_info_ratelimited(fmt, ...) \
398 printk_ratelimited(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
399 /* no pr_cont_ratelimited, don't do that... */
400
401 #if defined(DEBUG)
402 #define pr_devel_ratelimited(fmt, ...) \
403 printk_ratelimited(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
404 #else
405 #define pr_devel_ratelimited(fmt, ...) \
406 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
407 #endif
408
409 /* If you are writing a driver, please use dev_dbg instead */
410 #if defined(CONFIG_DYNAMIC_DEBUG)
411 /* descriptor check is first to prevent flooding with "callbacks suppressed" */
412 #define pr_debug_ratelimited(fmt, ...) \
413 do { \
414 static DEFINE_RATELIMIT_STATE(_rs, \
415 DEFAULT_RATELIMIT_INTERVAL, \
416 DEFAULT_RATELIMIT_BURST); \
417 DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, pr_fmt(fmt)); \
418 if (unlikely(descriptor.flags & _DPRINTK_FLAGS_PRINT) && \
419 __ratelimit(&_rs)) \
420 __dynamic_pr_debug(&descriptor, pr_fmt(fmt), ##__VA_ARGS__); \
421 } while (0)
422 #elif defined(DEBUG)
423 #define pr_debug_ratelimited(fmt, ...) \
424 printk_ratelimited(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
425 #else
426 #define pr_debug_ratelimited(fmt, ...) \
427 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
428 #endif
429
430 extern const struct file_operations kmsg_fops;
431
432 enum {
433 DUMP_PREFIX_NONE,
434 DUMP_PREFIX_ADDRESS,
435 DUMP_PREFIX_OFFSET
436 };
437 extern int hex_dump_to_buffer(const void *buf, size_t len, int rowsize,
438 int groupsize, char *linebuf, size_t linebuflen,
439 bool ascii);
440 #ifdef CONFIG_PRINTK
441 extern void print_hex_dump(const char *level, const char *prefix_str,
442 int prefix_type, int rowsize, int groupsize,
443 const void *buf, size_t len, bool ascii);
444 #if defined(CONFIG_DYNAMIC_DEBUG)
445 #define print_hex_dump_bytes(prefix_str, prefix_type, buf, len) \
446 dynamic_hex_dump(prefix_str, prefix_type, 16, 1, buf, len, true)
447 #else
448 extern void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
449 const void *buf, size_t len);
450 #endif /* defined(CONFIG_DYNAMIC_DEBUG) */
451 #else
452 static inline void print_hex_dump(const char *level, const char *prefix_str,
453 int prefix_type, int rowsize, int groupsize,
454 const void *buf, size_t len, bool ascii)
455 {
456 }
457 static inline void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
458 const void *buf, size_t len)
459 {
460 }
461
462 #endif
463
464 #if defined(CONFIG_DYNAMIC_DEBUG)
465 #define print_hex_dump_debug(prefix_str, prefix_type, rowsize, \
466 groupsize, buf, len, ascii) \
467 dynamic_hex_dump(prefix_str, prefix_type, rowsize, \
468 groupsize, buf, len, ascii)
469 #elif defined(DEBUG)
470 #define print_hex_dump_debug(prefix_str, prefix_type, rowsize, \
471 groupsize, buf, len, ascii) \
472 print_hex_dump(KERN_DEBUG, prefix_str, prefix_type, rowsize, \
473 groupsize, buf, len, ascii)
474 #else
475 static inline void print_hex_dump_debug(const char *prefix_str, int prefix_type,
476 int rowsize, int groupsize,
477 const void *buf, size_t len, bool ascii)
478 {
479 }
480 #endif
481
482 #endif