irqflags.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #ifndef _X86_IRQFLAGS_H_
  2. #define _X86_IRQFLAGS_H_
  3. #include <asm/processor-flags.h>
  4. #ifndef __ASSEMBLY__
  5. /*
  6. * Interrupt control:
  7. */
  8. static inline unsigned long native_save_fl(void)
  9. {
  10. unsigned long flags;
  11. __asm__ __volatile__(
  12. "# __raw_save_flags\n\t"
  13. "pushf ; pop %0"
  14. : "=g" (flags)
  15. : /* no input */
  16. : "memory"
  17. );
  18. return flags;
  19. }
  20. static inline void native_restore_fl(unsigned long flags)
  21. {
  22. __asm__ __volatile__(
  23. "push %0 ; popf"
  24. : /* no output */
  25. :"g" (flags)
  26. :"memory", "cc"
  27. );
  28. }
  29. static inline void native_irq_disable(void)
  30. {
  31. asm volatile("cli": : :"memory");
  32. }
  33. static inline void native_irq_enable(void)
  34. {
  35. asm volatile("sti": : :"memory");
  36. }
  37. static inline void native_safe_halt(void)
  38. {
  39. asm volatile("sti; hlt": : :"memory");
  40. }
  41. static inline void native_halt(void)
  42. {
  43. asm volatile("hlt": : :"memory");
  44. }
  45. #endif
  46. #ifdef CONFIG_PARAVIRT
  47. #include <asm/paravirt.h>
  48. #else
  49. #ifndef __ASSEMBLY__
  50. static inline unsigned long __raw_local_save_flags(void)
  51. {
  52. return native_save_fl();
  53. }
  54. static inline void raw_local_irq_restore(unsigned long flags)
  55. {
  56. native_restore_fl(flags);
  57. }
  58. static inline void raw_local_irq_disable(void)
  59. {
  60. native_irq_disable();
  61. }
  62. static inline void raw_local_irq_enable(void)
  63. {
  64. native_irq_enable();
  65. }
  66. /*
  67. * Used in the idle loop; sti takes one instruction cycle
  68. * to complete:
  69. */
  70. static inline void raw_safe_halt(void)
  71. {
  72. native_safe_halt();
  73. }
  74. /*
  75. * Used when interrupts are already enabled or to
  76. * shutdown the processor:
  77. */
  78. static inline void halt(void)
  79. {
  80. native_halt();
  81. }
  82. /*
  83. * For spinlocks, etc:
  84. */
  85. static inline unsigned long __raw_local_irq_save(void)
  86. {
  87. unsigned long flags = __raw_local_save_flags();
  88. raw_local_irq_disable();
  89. return flags;
  90. }
  91. #else
  92. #define ENABLE_INTERRUPTS(x) sti
  93. #define DISABLE_INTERRUPTS(x) cli
  94. #ifdef CONFIG_X86_64
  95. #define INTERRUPT_RETURN iretq
  96. #define ENABLE_INTERRUPTS_SYSCALL_RET \
  97. movq %gs:pda_oldrsp, %rsp; \
  98. swapgs; \
  99. sysretq;
  100. #else
  101. #define INTERRUPT_RETURN iret
  102. #define ENABLE_INTERRUPTS_SYSCALL_RET sti; sysexit
  103. #define GET_CR0_INTO_EAX movl %cr0, %eax
  104. #endif
  105. #endif /* __ASSEMBLY__ */
  106. #endif /* CONFIG_PARAVIRT */
  107. #ifndef __ASSEMBLY__
  108. #define raw_local_save_flags(flags) \
  109. do { (flags) = __raw_local_save_flags(); } while (0)
  110. #define raw_local_irq_save(flags) \
  111. do { (flags) = __raw_local_irq_save(); } while (0)
  112. static inline int raw_irqs_disabled_flags(unsigned long flags)
  113. {
  114. return !(flags & X86_EFLAGS_IF);
  115. }
  116. static inline int raw_irqs_disabled(void)
  117. {
  118. unsigned long flags = __raw_local_save_flags();
  119. return raw_irqs_disabled_flags(flags);
  120. }
  121. /*
  122. * makes the traced hardirq state match with the machine state
  123. *
  124. * should be a rarely used function, only in places where its
  125. * otherwise impossible to know the irq state, like in traps.
  126. */
  127. static inline void trace_hardirqs_fixup_flags(unsigned long flags)
  128. {
  129. if (raw_irqs_disabled_flags(flags))
  130. trace_hardirqs_off();
  131. else
  132. trace_hardirqs_on();
  133. }
  134. static inline void trace_hardirqs_fixup(void)
  135. {
  136. unsigned long flags = __raw_local_save_flags();
  137. trace_hardirqs_fixup_flags(flags);
  138. }
  139. #else
  140. #ifdef CONFIG_X86_64
  141. /*
  142. * Currently paravirt can't handle swapgs nicely when we
  143. * don't have a stack we can rely on (such as a user space
  144. * stack). So we either find a way around these or just fault
  145. * and emulate if a guest tries to call swapgs directly.
  146. *
  147. * Either way, this is a good way to document that we don't
  148. * have a reliable stack. x86_64 only.
  149. */
  150. #define SWAPGS_UNSAFE_STACK swapgs
  151. #define ARCH_TRACE_IRQS_ON call trace_hardirqs_on_thunk
  152. #define ARCH_TRACE_IRQS_OFF call trace_hardirqs_off_thunk
  153. #define ARCH_LOCKDEP_SYS_EXIT call lockdep_sys_exit_thunk
  154. #define ARCH_LOCKDEP_SYS_EXIT_IRQ \
  155. TRACE_IRQS_ON; \
  156. sti; \
  157. SAVE_REST; \
  158. LOCKDEP_SYS_EXIT; \
  159. RESTORE_REST; \
  160. cli; \
  161. TRACE_IRQS_OFF;
  162. #else
  163. #define ARCH_TRACE_IRQS_ON \
  164. pushl %eax; \
  165. pushl %ecx; \
  166. pushl %edx; \
  167. call trace_hardirqs_on; \
  168. popl %edx; \
  169. popl %ecx; \
  170. popl %eax;
  171. #define ARCH_TRACE_IRQS_OFF \
  172. pushl %eax; \
  173. pushl %ecx; \
  174. pushl %edx; \
  175. call trace_hardirqs_off; \
  176. popl %edx; \
  177. popl %ecx; \
  178. popl %eax;
  179. #define ARCH_LOCKDEP_SYS_EXIT \
  180. pushl %eax; \
  181. pushl %ecx; \
  182. pushl %edx; \
  183. call lockdep_sys_exit; \
  184. popl %edx; \
  185. popl %ecx; \
  186. popl %eax;
  187. #define ARCH_LOCKDEP_SYS_EXIT_IRQ
  188. #endif
  189. #ifdef CONFIG_TRACE_IRQFLAGS
  190. # define TRACE_IRQS_ON ARCH_TRACE_IRQS_ON
  191. # define TRACE_IRQS_OFF ARCH_TRACE_IRQS_OFF
  192. #else
  193. # define TRACE_IRQS_ON
  194. # define TRACE_IRQS_OFF
  195. #endif
  196. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  197. # define LOCKDEP_SYS_EXIT ARCH_LOCKDEP_SYS_EXIT
  198. # define LOCKDEP_SYS_EXIT_IRQ ARCH_LOCKDEP_SYS_EXIT_IRQ
  199. # else
  200. # define LOCKDEP_SYS_EXIT
  201. # define LOCKDEP_SYS_EXIT_IRQ
  202. # endif
  203. #endif /* __ASSEMBLY__ */
  204. #endif