preempt_mask.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 could in theory be the same as the number of
  13. * interrupts in the system, but we run all interrupt handlers with
  14. * interrupts disabled, so we cannot have nesting interrupts. Though
  15. * there are a few palaeontologic drivers which reenable interrupts in
  16. * the handler, so we need more than one bit here.
  17. *
  18. * PREEMPT_MASK: 0x000000ff
  19. * SOFTIRQ_MASK: 0x0000ff00
  20. * HARDIRQ_MASK: 0x000f0000
  21. * NMI_MASK: 0x00100000
  22. * PREEMPT_ACTIVE: 0x00200000
  23. */
  24. #define PREEMPT_BITS 8
  25. #define SOFTIRQ_BITS 8
  26. #define HARDIRQ_BITS 4
  27. #define NMI_BITS 1
  28. #define PREEMPT_SHIFT 0
  29. #define SOFTIRQ_SHIFT (PREEMPT_SHIFT + PREEMPT_BITS)
  30. #define HARDIRQ_SHIFT (SOFTIRQ_SHIFT + SOFTIRQ_BITS)
  31. #define NMI_SHIFT (HARDIRQ_SHIFT + HARDIRQ_BITS)
  32. #define __IRQ_MASK(x) ((1UL << (x))-1)
  33. #define PREEMPT_MASK (__IRQ_MASK(PREEMPT_BITS) << PREEMPT_SHIFT)
  34. #define SOFTIRQ_MASK (__IRQ_MASK(SOFTIRQ_BITS) << SOFTIRQ_SHIFT)
  35. #define HARDIRQ_MASK (__IRQ_MASK(HARDIRQ_BITS) << HARDIRQ_SHIFT)
  36. #define NMI_MASK (__IRQ_MASK(NMI_BITS) << NMI_SHIFT)
  37. #define PREEMPT_OFFSET (1UL << PREEMPT_SHIFT)
  38. #define SOFTIRQ_OFFSET (1UL << SOFTIRQ_SHIFT)
  39. #define HARDIRQ_OFFSET (1UL << HARDIRQ_SHIFT)
  40. #define NMI_OFFSET (1UL << NMI_SHIFT)
  41. #define SOFTIRQ_DISABLE_OFFSET (2 * SOFTIRQ_OFFSET)
  42. #define PREEMPT_ACTIVE_BITS 1
  43. #define PREEMPT_ACTIVE_SHIFT (NMI_SHIFT + NMI_BITS)
  44. #define PREEMPT_ACTIVE (__IRQ_MASK(PREEMPT_ACTIVE_BITS) << PREEMPT_ACTIVE_SHIFT)
  45. #define hardirq_count() (preempt_count() & HARDIRQ_MASK)
  46. #define softirq_count() (preempt_count() & SOFTIRQ_MASK)
  47. #define irq_count() (preempt_count() & (HARDIRQ_MASK | SOFTIRQ_MASK \
  48. | NMI_MASK))
  49. /*
  50. * Are we doing bottom half or hardware interrupt processing?
  51. * Are we in a softirq context? Interrupt context?
  52. * in_softirq - Are we currently processing softirq or have bh disabled?
  53. * in_serving_softirq - Are we currently processing softirq?
  54. */
  55. #define in_irq() (hardirq_count())
  56. #define in_softirq() (softirq_count())
  57. #define in_interrupt() (irq_count())
  58. #define in_serving_softirq() (softirq_count() & SOFTIRQ_OFFSET)
  59. /*
  60. * Are we in NMI context?
  61. */
  62. #define in_nmi() (preempt_count() & NMI_MASK)
  63. #if defined(CONFIG_PREEMPT_COUNT)
  64. # define PREEMPT_CHECK_OFFSET 1
  65. #else
  66. # define PREEMPT_CHECK_OFFSET 0
  67. #endif
  68. /*
  69. * Are we running in atomic context? WARNING: this macro cannot
  70. * always detect atomic context; in particular, it cannot know about
  71. * held spinlocks in non-preemptible kernels. Thus it should not be
  72. * used in the general case to determine whether sleeping is possible.
  73. * Do not use in_atomic() in driver code.
  74. */
  75. #define in_atomic() ((preempt_count() & ~PREEMPT_ACTIVE) != 0)
  76. /*
  77. * Check whether we were atomic before we did preempt_disable():
  78. * (used by the scheduler, *after* releasing the kernel lock)
  79. */
  80. #define in_atomic_preempt_off() \
  81. ((preempt_count() & ~PREEMPT_ACTIVE) != PREEMPT_CHECK_OFFSET)
  82. #ifdef CONFIG_PREEMPT_COUNT
  83. # define preemptible() (preempt_count() == 0 && !irqs_disabled())
  84. #else
  85. # define preemptible() 0
  86. #endif
  87. #endif /* LINUX_PREEMPT_MASK_H */