preempt_mask.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #ifndef LINUX_PREEMPT_MASK_H
  2. #define LINUX_PREEMPT_MASK_H
  3. #include <linux/preempt.h>
  4. #include <asm/hardirq.h>
  5. /*
  6. * We put the hardirq and softirq counter into the preemption
  7. * counter. The bitmask has the following meaning:
  8. *
  9. * - bits 0-7 are the preemption count (max preemption depth: 256)
  10. * - bits 8-15 are the softirq count (max # of softirqs: 256)
  11. *
  12. * The hardirq count can in theory reach the same as NR_IRQS.
  13. * In reality, the number of nested IRQS is limited to the stack
  14. * size as well. For archs with over 1000 IRQS it is not practical
  15. * to expect that they will all nest. We give a max of 10 bits for
  16. * hardirq nesting. An arch may choose to give less than 10 bits.
  17. * m68k expects it to be 8.
  18. *
  19. * - bits 16-25 are the hardirq count (max # of nested hardirqs: 1024)
  20. * - bit 26 is the NMI_MASK
  21. * - bit 27 is the PREEMPT_ACTIVE flag
  22. *
  23. * PREEMPT_MASK: 0x000000ff
  24. * SOFTIRQ_MASK: 0x0000ff00
  25. * HARDIRQ_MASK: 0x03ff0000
  26. * NMI_MASK: 0x04000000
  27. */
  28. #define PREEMPT_BITS 8
  29. #define SOFTIRQ_BITS 8
  30. #define NMI_BITS 1
  31. #define MAX_HARDIRQ_BITS 10
  32. #ifndef HARDIRQ_BITS
  33. # define HARDIRQ_BITS MAX_HARDIRQ_BITS
  34. #endif
  35. #if HARDIRQ_BITS > MAX_HARDIRQ_BITS
  36. #error HARDIRQ_BITS too high!
  37. #endif
  38. #define PREEMPT_SHIFT 0
  39. #define SOFTIRQ_SHIFT (PREEMPT_SHIFT + PREEMPT_BITS)
  40. #define HARDIRQ_SHIFT (SOFTIRQ_SHIFT + SOFTIRQ_BITS)
  41. #define NMI_SHIFT (HARDIRQ_SHIFT + HARDIRQ_BITS)
  42. #define __IRQ_MASK(x) ((1UL << (x))-1)
  43. #define PREEMPT_MASK (__IRQ_MASK(PREEMPT_BITS) << PREEMPT_SHIFT)
  44. #define SOFTIRQ_MASK (__IRQ_MASK(SOFTIRQ_BITS) << SOFTIRQ_SHIFT)
  45. #define HARDIRQ_MASK (__IRQ_MASK(HARDIRQ_BITS) << HARDIRQ_SHIFT)
  46. #define NMI_MASK (__IRQ_MASK(NMI_BITS) << NMI_SHIFT)
  47. #define PREEMPT_OFFSET (1UL << PREEMPT_SHIFT)
  48. #define SOFTIRQ_OFFSET (1UL << SOFTIRQ_SHIFT)
  49. #define HARDIRQ_OFFSET (1UL << HARDIRQ_SHIFT)
  50. #define NMI_OFFSET (1UL << NMI_SHIFT)
  51. #define SOFTIRQ_DISABLE_OFFSET (2 * SOFTIRQ_OFFSET)
  52. #ifndef PREEMPT_ACTIVE
  53. #define PREEMPT_ACTIVE_BITS 1
  54. #define PREEMPT_ACTIVE_SHIFT (NMI_SHIFT + NMI_BITS)
  55. #define PREEMPT_ACTIVE (__IRQ_MASK(PREEMPT_ACTIVE_BITS) << PREEMPT_ACTIVE_SHIFT)
  56. #endif
  57. #if PREEMPT_ACTIVE < (1 << (NMI_SHIFT + NMI_BITS))
  58. #error PREEMPT_ACTIVE is too low!
  59. #endif
  60. #define hardirq_count() (preempt_count() & HARDIRQ_MASK)
  61. #define softirq_count() (preempt_count() & SOFTIRQ_MASK)
  62. #define irq_count() (preempt_count() & (HARDIRQ_MASK | SOFTIRQ_MASK \
  63. | NMI_MASK))
  64. /*
  65. * Are we doing bottom half or hardware interrupt processing?
  66. * Are we in a softirq context? Interrupt context?
  67. * in_softirq - Are we currently processing softirq or have bh disabled?
  68. * in_serving_softirq - Are we currently processing softirq?
  69. */
  70. #define in_irq() (hardirq_count())
  71. #define in_softirq() (softirq_count())
  72. #define in_interrupt() (irq_count())
  73. #define in_serving_softirq() (softirq_count() & SOFTIRQ_OFFSET)
  74. /*
  75. * Are we in NMI context?
  76. */
  77. #define in_nmi() (preempt_count() & NMI_MASK)
  78. #if defined(CONFIG_PREEMPT_COUNT)
  79. # define PREEMPT_CHECK_OFFSET 1
  80. #else
  81. # define PREEMPT_CHECK_OFFSET 0
  82. #endif
  83. /*
  84. * Are we running in atomic context? WARNING: this macro cannot
  85. * always detect atomic context; in particular, it cannot know about
  86. * held spinlocks in non-preemptible kernels. Thus it should not be
  87. * used in the general case to determine whether sleeping is possible.
  88. * Do not use in_atomic() in driver code.
  89. */
  90. #define in_atomic() ((preempt_count() & ~PREEMPT_ACTIVE) != 0)
  91. /*
  92. * Check whether we were atomic before we did preempt_disable():
  93. * (used by the scheduler, *after* releasing the kernel lock)
  94. */
  95. #define in_atomic_preempt_off() \
  96. ((preempt_count() & ~PREEMPT_ACTIVE) != PREEMPT_CHECK_OFFSET)
  97. #ifdef CONFIG_PREEMPT_COUNT
  98. # define preemptible() (preempt_count() == 0 && !irqs_disabled())
  99. #else
  100. # define preemptible() 0
  101. #endif
  102. #endif /* LINUX_PREEMPT_MASK_H */