blob: 0c8f4281b15145f1ebaedcdd56fd496457f726c2 [file] [log] [blame]
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +01001#ifndef _X86_IRQFLAGS_H_
2#define _X86_IRQFLAGS_H_
3
4#include <asm/processor-flags.h>
5
6#ifndef __ASSEMBLY__
Chris Metcalf6727ad92016-10-07 17:02:55 -07007
Thomas Gleixner2394f592019-02-18 23:04:01 +01008#include <asm/nospec-branch.h>
9
Chris Metcalf6727ad92016-10-07 17:02:55 -070010/* Provide __cpuidle; we can't safely include <linux/cpu.h> */
11#define __cpuidle __attribute__((__section__(".cpuidle.text")))
12
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +010013/*
14 * Interrupt control:
15 */
16
Nick Desaulniers329d8152018-08-03 10:05:50 -070017/* Declaration required for gcc < 4.9 to prevent -Werror=missing-prototypes */
18extern inline unsigned long native_save_fl(void);
Nick Desaulniers1919f3f2018-06-21 09:23:24 -070019extern inline unsigned long native_save_fl(void)
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +010020{
21 unsigned long flags;
22
H. Peter Anvinf1f029c2009-08-03 16:33:40 -070023 /*
H. Peter Anvinab94fcf2009-08-25 16:47:16 -070024 * "=rm" is safe here, because "pop" adjusts the stack before
25 * it evaluates its effective address -- this is part of the
26 * documented behavior of the "pop" instruction.
H. Peter Anvinf1f029c2009-08-03 16:33:40 -070027 */
Joe Perchescf7f7192008-03-23 01:02:30 -070028 asm volatile("# __raw_save_flags\n\t"
29 "pushf ; pop %0"
H. Peter Anvinab94fcf2009-08-25 16:47:16 -070030 : "=rm" (flags)
Joe Perchescf7f7192008-03-23 01:02:30 -070031 : /* no input */
32 : "memory");
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +010033
34 return flags;
35}
36
Nick Desaulniersc1482462018-08-27 14:40:09 -070037extern inline void native_restore_fl(unsigned long flags);
38extern inline void native_restore_fl(unsigned long flags)
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +010039{
Joe Perchescf7f7192008-03-23 01:02:30 -070040 asm volatile("push %0 ; popf"
41 : /* no output */
42 :"g" (flags)
43 :"memory", "cc");
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +010044}
45
46static inline void native_irq_disable(void)
47{
48 asm volatile("cli": : :"memory");
49}
50
51static inline void native_irq_enable(void)
52{
53 asm volatile("sti": : :"memory");
54}
55
Chris Metcalf6727ad92016-10-07 17:02:55 -070056static inline __cpuidle void native_safe_halt(void)
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +010057{
Thomas Gleixner2394f592019-02-18 23:04:01 +010058 mds_idle_clear_cpu_buffers();
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +010059 asm volatile("sti; hlt": : :"memory");
60}
61
Chris Metcalf6727ad92016-10-07 17:02:55 -070062static inline __cpuidle void native_halt(void)
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +010063{
Thomas Gleixner2394f592019-02-18 23:04:01 +010064 mds_idle_clear_cpu_buffers();
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +010065 asm volatile("hlt": : :"memory");
66}
67
68#endif
69
70#ifdef CONFIG_PARAVIRT
71#include <asm/paravirt.h>
Thomas Gleixner96a388d2007-10-11 11:20:03 +020072#else
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +010073#ifndef __ASSEMBLY__
Steven Rostedte08fbb72011-07-01 23:04:36 -040074#include <linux/types.h>
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +010075
Steven Rostedte08fbb72011-07-01 23:04:36 -040076static inline notrace unsigned long arch_local_save_flags(void)
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +010077{
78 return native_save_fl();
79}
80
Steven Rostedte08fbb72011-07-01 23:04:36 -040081static inline notrace void arch_local_irq_restore(unsigned long flags)
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +010082{
83 native_restore_fl(flags);
84}
85
Steven Rostedte08fbb72011-07-01 23:04:36 -040086static inline notrace void arch_local_irq_disable(void)
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +010087{
88 native_irq_disable();
89}
90
Steven Rostedte08fbb72011-07-01 23:04:36 -040091static inline notrace void arch_local_irq_enable(void)
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +010092{
93 native_irq_enable();
94}
95
96/*
97 * Used in the idle loop; sti takes one instruction cycle
98 * to complete:
99 */
Chris Metcalf6727ad92016-10-07 17:02:55 -0700100static inline __cpuidle void arch_safe_halt(void)
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +0100101{
102 native_safe_halt();
103}
104
105/*
106 * Used when interrupts are already enabled or to
107 * shutdown the processor:
108 */
Chris Metcalf6727ad92016-10-07 17:02:55 -0700109static inline __cpuidle void halt(void)
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +0100110{
111 native_halt();
112}
113
114/*
115 * For spinlocks, etc:
116 */
Steven Rostedte08fbb72011-07-01 23:04:36 -0400117static inline notrace unsigned long arch_local_irq_save(void)
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +0100118{
David Howellsdf9ee292010-10-07 14:08:55 +0100119 unsigned long flags = arch_local_save_flags();
120 arch_local_irq_disable();
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +0100121 return flags;
122}
123#else
124
125#define ENABLE_INTERRUPTS(x) sti
126#define DISABLE_INTERRUPTS(x) cli
127
128#ifdef CONFIG_X86_64
Jeremy Fitzhardingedf366e92008-06-27 12:04:03 -0700129#define SWAPGS swapgs
130/*
131 * Currently paravirt can't handle swapgs nicely when we
132 * don't have a stack we can rely on (such as a user space
133 * stack). So we either find a way around these or just fault
134 * and emulate if a guest tries to call swapgs directly.
135 *
136 * Either way, this is a good way to document that we don't
137 * have a reliable stack. x86_64 only.
138 */
Jeremy Fitzhardingea00394f2008-06-25 00:19:30 -0400139#define SWAPGS_UNSAFE_STACK swapgs
Jeremy Fitzhardingedf366e92008-06-27 12:04:03 -0700140
141#define PARAVIRT_ADJUST_EXCEPTION_FRAME /* */
142
Andy Lutomirski7209a752014-07-23 08:34:11 -0700143#define INTERRUPT_RETURN jmp native_iret
Jeremy Fitzhardinge2be29982008-06-25 00:19:28 -0400144#define USERGS_SYSRET64 \
145 swapgs; \
146 sysretq;
147#define USERGS_SYSRET32 \
148 swapgs; \
149 sysretl
Jeremy Fitzhardinge2be29982008-06-25 00:19:28 -0400150
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +0100151#else
152#define INTERRUPT_RETURN iret
Jeremy Fitzhardinged75cd22f2008-06-25 00:19:26 -0400153#define ENABLE_INTERRUPTS_SYSEXIT sti; sysexit
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +0100154#define GET_CR0_INTO_EAX movl %cr0, %eax
155#endif
156
157
158#endif /* __ASSEMBLY__ */
159#endif /* CONFIG_PARAVIRT */
160
161#ifndef __ASSEMBLY__
David Howellsdf9ee292010-10-07 14:08:55 +0100162static inline int arch_irqs_disabled_flags(unsigned long flags)
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +0100163{
164 return !(flags & X86_EFLAGS_IF);
165}
166
David Howellsdf9ee292010-10-07 14:08:55 +0100167static inline int arch_irqs_disabled(void)
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +0100168{
David Howellsdf9ee292010-10-07 14:08:55 +0100169 unsigned long flags = arch_local_save_flags();
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +0100170
David Howellsdf9ee292010-10-07 14:08:55 +0100171 return arch_irqs_disabled_flags(flags);
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +0100172}
Denys Vlasenko40e2ec62015-03-25 21:14:26 +0100173#endif /* !__ASSEMBLY__ */
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +0100174
Denys Vlasenko40e2ec62015-03-25 21:14:26 +0100175#ifdef __ASSEMBLY__
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +0100176#ifdef CONFIG_TRACE_IRQFLAGS
Steven Rostedt81d68a92008-05-12 21:20:42 +0200177# define TRACE_IRQS_ON call trace_hardirqs_on_thunk;
178# define TRACE_IRQS_OFF call trace_hardirqs_off_thunk;
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +0100179#else
180# define TRACE_IRQS_ON
181# define TRACE_IRQS_OFF
182#endif
183#ifdef CONFIG_DEBUG_LOCK_ALLOC
Denys Vlasenko40e2ec62015-03-25 21:14:26 +0100184# ifdef CONFIG_X86_64
Denys Vlasenko7dc7cc02015-03-25 21:14:27 +0100185# define LOCKDEP_SYS_EXIT call lockdep_sys_exit_thunk
186# define LOCKDEP_SYS_EXIT_IRQ \
Denys Vlasenko40e2ec62015-03-25 21:14:26 +0100187 TRACE_IRQS_ON; \
188 sti; \
Denys Vlasenko7dc7cc02015-03-25 21:14:27 +0100189 call lockdep_sys_exit_thunk; \
Denys Vlasenko40e2ec62015-03-25 21:14:26 +0100190 cli; \
191 TRACE_IRQS_OFF;
192# else
Denys Vlasenko7dc7cc02015-03-25 21:14:27 +0100193# define LOCKDEP_SYS_EXIT \
Denys Vlasenko40e2ec62015-03-25 21:14:26 +0100194 pushl %eax; \
195 pushl %ecx; \
196 pushl %edx; \
197 call lockdep_sys_exit; \
198 popl %edx; \
199 popl %ecx; \
200 popl %eax;
Denys Vlasenko7dc7cc02015-03-25 21:14:27 +0100201# define LOCKDEP_SYS_EXIT_IRQ
Denys Vlasenko40e2ec62015-03-25 21:14:26 +0100202# endif
Denys Vlasenko7dc7cc02015-03-25 21:14:27 +0100203#else
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +0100204# define LOCKDEP_SYS_EXIT
205# define LOCKDEP_SYS_EXIT_IRQ
Denys Vlasenko7dc7cc02015-03-25 21:14:27 +0100206#endif
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +0100207#endif /* __ASSEMBLY__ */
Denys Vlasenko40e2ec62015-03-25 21:14:26 +0100208
Thomas Gleixner96a388d2007-10-11 11:20:03 +0200209#endif