bug.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #ifndef _LINUX_BUG_H
  2. #define _LINUX_BUG_H
  3. #include <asm/bug.h>
  4. enum bug_trap_type {
  5. BUG_TRAP_TYPE_NONE = 0,
  6. BUG_TRAP_TYPE_WARN = 1,
  7. BUG_TRAP_TYPE_BUG = 2,
  8. };
  9. struct pt_regs;
  10. #ifdef __CHECKER__
  11. #define BUILD_BUG_ON_NOT_POWER_OF_2(n)
  12. #define BUILD_BUG_ON_ZERO(e) (0)
  13. #define BUILD_BUG_ON_NULL(e) ((void*)0)
  14. #define BUILD_BUG_ON(condition)
  15. #define BUILD_BUG() (0)
  16. #else /* __CHECKER__ */
  17. /* Force a compilation error if a constant expression is not a power of 2 */
  18. #define BUILD_BUG_ON_NOT_POWER_OF_2(n) \
  19. BUILD_BUG_ON((n) == 0 || (((n) & ((n) - 1)) != 0))
  20. /* Force a compilation error if condition is true, but also produce a
  21. result (of value 0 and type size_t), so the expression can be used
  22. e.g. in a structure initializer (or where-ever else comma expressions
  23. aren't permitted). */
  24. #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
  25. #define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); }))
  26. /**
  27. * BUILD_BUG_ON - break compile if a condition is true.
  28. * @condition: the condition which the compiler should know is false.
  29. *
  30. * If you have some code which relies on certain constants being equal, or
  31. * other compile-time-evaluated condition, you should use BUILD_BUG_ON to
  32. * detect if someone changes it.
  33. *
  34. * The implementation uses gcc's reluctance to create a negative array, but
  35. * gcc (as of 4.4) only emits that error for obvious cases (eg. not arguments
  36. * to inline functions). So as a fallback we use the optimizer; if it can't
  37. * prove the condition is false, it will cause a link error on the undefined
  38. * "__build_bug_on_failed". This error message can be harder to track down
  39. * though, hence the two different methods.
  40. */
  41. #ifndef __OPTIMIZE__
  42. #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
  43. #else
  44. extern int __build_bug_on_failed;
  45. #define BUILD_BUG_ON(condition) \
  46. do { \
  47. ((void)sizeof(char[1 - 2*!!(condition)])); \
  48. if (condition) __build_bug_on_failed = 1; \
  49. } while(0)
  50. #endif
  51. /**
  52. * BUILD_BUG - break compile if used.
  53. *
  54. * If you have some code that you expect the compiler to eliminate at
  55. * build time, you should use BUILD_BUG to detect if it is
  56. * unexpectedly used.
  57. */
  58. #define BUILD_BUG() \
  59. do { \
  60. extern void __build_bug_failed(void) \
  61. __linktime_error("BUILD_BUG failed"); \
  62. __build_bug_failed(); \
  63. } while (0)
  64. #endif /* __CHECKER__ */
  65. #ifdef CONFIG_GENERIC_BUG
  66. #include <asm-generic/bug.h>
  67. static inline int is_warning_bug(const struct bug_entry *bug)
  68. {
  69. return bug->flags & BUGFLAG_WARNING;
  70. }
  71. const struct bug_entry *find_bug(unsigned long bugaddr);
  72. enum bug_trap_type report_bug(unsigned long bug_addr, struct pt_regs *regs);
  73. /* These are defined by the architecture */
  74. int is_valid_bugaddr(unsigned long addr);
  75. #else /* !CONFIG_GENERIC_BUG */
  76. static inline enum bug_trap_type report_bug(unsigned long bug_addr,
  77. struct pt_regs *regs)
  78. {
  79. return BUG_TRAP_TYPE_BUG;
  80. }
  81. #endif /* CONFIG_GENERIC_BUG */
  82. #endif /* _LINUX_BUG_H */