xref: /openbmc/linux/arch/x86/include/asm/irqflags.h (revision 07f07f55a29cb705e221eda7894dd67ab81ef343)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2bb898558SAl Viro #ifndef _X86_IRQFLAGS_H_
3bb898558SAl Viro #define _X86_IRQFLAGS_H_
4bb898558SAl Viro 
5bb898558SAl Viro #include <asm/processor-flags.h>
6bb898558SAl Viro 
7bb898558SAl Viro #ifndef __ASSEMBLY__
86727ad9eSChris Metcalf 
9*07f07f55SThomas Gleixner #include <asm/nospec-branch.h>
10*07f07f55SThomas Gleixner 
116727ad9eSChris Metcalf /* Provide __cpuidle; we can't safely include <linux/cpu.h> */
126727ad9eSChris Metcalf #define __cpuidle __attribute__((__section__(".cpuidle.text")))
136727ad9eSChris Metcalf 
14bb898558SAl Viro /*
15bb898558SAl Viro  * Interrupt control:
16bb898558SAl Viro  */
17bb898558SAl Viro 
18208cbb32SNick Desaulniers /* Declaration required for gcc < 4.9 to prevent -Werror=missing-prototypes */
19208cbb32SNick Desaulniers extern inline unsigned long native_save_fl(void);
20d0a8d937SNick Desaulniers extern inline unsigned long native_save_fl(void)
21bb898558SAl Viro {
22bb898558SAl Viro 	unsigned long flags;
23bb898558SAl Viro 
24f1f029c7SH. Peter Anvin 	/*
25ab94fcf5SH. Peter Anvin 	 * "=rm" is safe here, because "pop" adjusts the stack before
26ab94fcf5SH. Peter Anvin 	 * it evaluates its effective address -- this is part of the
27ab94fcf5SH. Peter Anvin 	 * documented behavior of the "pop" instruction.
28f1f029c7SH. Peter Anvin 	 */
29bb898558SAl Viro 	asm volatile("# __raw_save_flags\n\t"
30bb898558SAl Viro 		     "pushf ; pop %0"
31ab94fcf5SH. Peter Anvin 		     : "=rm" (flags)
32bb898558SAl Viro 		     : /* no input */
33bb898558SAl Viro 		     : "memory");
34bb898558SAl Viro 
35bb898558SAl Viro 	return flags;
36bb898558SAl Viro }
37bb898558SAl Viro 
381f59a458SNick Desaulniers extern inline void native_restore_fl(unsigned long flags);
391f59a458SNick Desaulniers extern inline void native_restore_fl(unsigned long flags)
40bb898558SAl Viro {
41bb898558SAl Viro 	asm volatile("push %0 ; popf"
42bb898558SAl Viro 		     : /* no output */
43bb898558SAl Viro 		     :"g" (flags)
44bb898558SAl Viro 		     :"memory", "cc");
45bb898558SAl Viro }
46bb898558SAl Viro 
47bb898558SAl Viro static inline void native_irq_disable(void)
48bb898558SAl Viro {
49bb898558SAl Viro 	asm volatile("cli": : :"memory");
50bb898558SAl Viro }
51bb898558SAl Viro 
52bb898558SAl Viro static inline void native_irq_enable(void)
53bb898558SAl Viro {
54bb898558SAl Viro 	asm volatile("sti": : :"memory");
55bb898558SAl Viro }
56bb898558SAl Viro 
576727ad9eSChris Metcalf static inline __cpuidle void native_safe_halt(void)
58bb898558SAl Viro {
59*07f07f55SThomas Gleixner 	mds_idle_clear_cpu_buffers();
60bb898558SAl Viro 	asm volatile("sti; hlt": : :"memory");
61bb898558SAl Viro }
62bb898558SAl Viro 
636727ad9eSChris Metcalf static inline __cpuidle void native_halt(void)
64bb898558SAl Viro {
65*07f07f55SThomas Gleixner 	mds_idle_clear_cpu_buffers();
66bb898558SAl Viro 	asm volatile("hlt": : :"memory");
67bb898558SAl Viro }
68bb898558SAl Viro 
69bb898558SAl Viro #endif
70bb898558SAl Viro 
716da63eb2SJuergen Gross #ifdef CONFIG_PARAVIRT_XXL
72bb898558SAl Viro #include <asm/paravirt.h>
73bb898558SAl Viro #else
74bb898558SAl Viro #ifndef __ASSEMBLY__
75e08fbb78SSteven Rostedt #include <linux/types.h>
76bb898558SAl Viro 
77e08fbb78SSteven Rostedt static inline notrace unsigned long arch_local_save_flags(void)
78bb898558SAl Viro {
79bb898558SAl Viro 	return native_save_fl();
80bb898558SAl Viro }
81bb898558SAl Viro 
82e08fbb78SSteven Rostedt static inline notrace void arch_local_irq_restore(unsigned long flags)
83bb898558SAl Viro {
84bb898558SAl Viro 	native_restore_fl(flags);
85bb898558SAl Viro }
86bb898558SAl Viro 
87e08fbb78SSteven Rostedt static inline notrace void arch_local_irq_disable(void)
88bb898558SAl Viro {
89bb898558SAl Viro 	native_irq_disable();
90bb898558SAl Viro }
91bb898558SAl Viro 
92e08fbb78SSteven Rostedt static inline notrace void arch_local_irq_enable(void)
93bb898558SAl Viro {
94bb898558SAl Viro 	native_irq_enable();
95bb898558SAl Viro }
96bb898558SAl Viro 
97bb898558SAl Viro /*
98bb898558SAl Viro  * Used in the idle loop; sti takes one instruction cycle
99bb898558SAl Viro  * to complete:
100bb898558SAl Viro  */
1016727ad9eSChris Metcalf static inline __cpuidle void arch_safe_halt(void)
102bb898558SAl Viro {
103bb898558SAl Viro 	native_safe_halt();
104bb898558SAl Viro }
105bb898558SAl Viro 
106bb898558SAl Viro /*
107bb898558SAl Viro  * Used when interrupts are already enabled or to
108bb898558SAl Viro  * shutdown the processor:
109bb898558SAl Viro  */
1106727ad9eSChris Metcalf static inline __cpuidle void halt(void)
111bb898558SAl Viro {
112bb898558SAl Viro 	native_halt();
113bb898558SAl Viro }
114bb898558SAl Viro 
115bb898558SAl Viro /*
116bb898558SAl Viro  * For spinlocks, etc:
117bb898558SAl Viro  */
118e08fbb78SSteven Rostedt static inline notrace unsigned long arch_local_irq_save(void)
119bb898558SAl Viro {
120df9ee292SDavid Howells 	unsigned long flags = arch_local_save_flags();
121df9ee292SDavid Howells 	arch_local_irq_disable();
122bb898558SAl Viro 	return flags;
123bb898558SAl Viro }
124bb898558SAl Viro #else
125bb898558SAl Viro 
126bb898558SAl Viro #define ENABLE_INTERRUPTS(x)	sti
127bb898558SAl Viro #define DISABLE_INTERRUPTS(x)	cli
128bb898558SAl Viro 
129bb898558SAl Viro #ifdef CONFIG_X86_64
1309bad5658SJuergen Gross #ifdef CONFIG_DEBUG_ENTRY
1319bad5658SJuergen Gross #define SAVE_FLAGS(x)		pushfq; popq %rax
1329bad5658SJuergen Gross #endif
1339bad5658SJuergen Gross 
134bb898558SAl Viro #define SWAPGS	swapgs
135bb898558SAl Viro /*
136bb898558SAl Viro  * Currently paravirt can't handle swapgs nicely when we
137bb898558SAl Viro  * don't have a stack we can rely on (such as a user space
138bb898558SAl Viro  * stack).  So we either find a way around these or just fault
139bb898558SAl Viro  * and emulate if a guest tries to call swapgs directly.
140bb898558SAl Viro  *
141bb898558SAl Viro  * Either way, this is a good way to document that we don't
142bb898558SAl Viro  * have a reliable stack. x86_64 only.
143bb898558SAl Viro  */
144bb898558SAl Viro #define SWAPGS_UNSAFE_STACK	swapgs
145bb898558SAl Viro 
1467209a75dSAndy Lutomirski #define INTERRUPT_RETURN	jmp native_iret
147bb898558SAl Viro #define USERGS_SYSRET64				\
148bb898558SAl Viro 	swapgs;					\
149bb898558SAl Viro 	sysretq;
150bb898558SAl Viro #define USERGS_SYSRET32				\
151bb898558SAl Viro 	swapgs;					\
152bb898558SAl Viro 	sysretl
153bb898558SAl Viro 
154bb898558SAl Viro #else
155bb898558SAl Viro #define INTERRUPT_RETURN		iret
156bb898558SAl Viro #endif
157bb898558SAl Viro 
158bb898558SAl Viro #endif /* __ASSEMBLY__ */
1599bad5658SJuergen Gross #endif /* CONFIG_PARAVIRT_XXL */
160bb898558SAl Viro 
161bb898558SAl Viro #ifndef __ASSEMBLY__
162df9ee292SDavid Howells static inline int arch_irqs_disabled_flags(unsigned long flags)
163bb898558SAl Viro {
164bb898558SAl Viro 	return !(flags & X86_EFLAGS_IF);
165bb898558SAl Viro }
166bb898558SAl Viro 
167df9ee292SDavid Howells static inline int arch_irqs_disabled(void)
168bb898558SAl Viro {
169df9ee292SDavid Howells 	unsigned long flags = arch_local_save_flags();
170bb898558SAl Viro 
171df9ee292SDavid Howells 	return arch_irqs_disabled_flags(flags);
172bb898558SAl Viro }
17340e2ec65SDenys Vlasenko #endif /* !__ASSEMBLY__ */
174bb898558SAl Viro 
17540e2ec65SDenys Vlasenko #ifdef __ASSEMBLY__
17640e2ec65SDenys Vlasenko #ifdef CONFIG_TRACE_IRQFLAGS
17740e2ec65SDenys Vlasenko #  define TRACE_IRQS_ON		call trace_hardirqs_on_thunk;
17840e2ec65SDenys Vlasenko #  define TRACE_IRQS_OFF	call trace_hardirqs_off_thunk;
179bb898558SAl Viro #else
18040e2ec65SDenys Vlasenko #  define TRACE_IRQS_ON
18140e2ec65SDenys Vlasenko #  define TRACE_IRQS_OFF
18240e2ec65SDenys Vlasenko #endif
18340e2ec65SDenys Vlasenko #ifdef CONFIG_DEBUG_LOCK_ALLOC
184bb898558SAl Viro #  ifdef CONFIG_X86_64
1857dc7cc07SDenys Vlasenko #    define LOCKDEP_SYS_EXIT		call lockdep_sys_exit_thunk
1867dc7cc07SDenys Vlasenko #    define LOCKDEP_SYS_EXIT_IRQ \
187bb898558SAl Viro 	TRACE_IRQS_ON; \
188bb898558SAl Viro 	sti; \
1897dc7cc07SDenys Vlasenko 	call lockdep_sys_exit_thunk; \
190bb898558SAl Viro 	cli; \
191bb898558SAl Viro 	TRACE_IRQS_OFF;
192bb898558SAl Viro #  else
1937dc7cc07SDenys Vlasenko #    define LOCKDEP_SYS_EXIT \
194bb898558SAl Viro 	pushl %eax;				\
195bb898558SAl Viro 	pushl %ecx;				\
196bb898558SAl Viro 	pushl %edx;				\
197bb898558SAl Viro 	call lockdep_sys_exit;			\
198bb898558SAl Viro 	popl %edx;				\
199bb898558SAl Viro 	popl %ecx;				\
200bb898558SAl Viro 	popl %eax;
2017dc7cc07SDenys Vlasenko #    define LOCKDEP_SYS_EXIT_IRQ
202bb898558SAl Viro #  endif
203bb898558SAl Viro #else
204bb898558SAl Viro #  define LOCKDEP_SYS_EXIT
205bb898558SAl Viro #  define LOCKDEP_SYS_EXIT_IRQ
206bb898558SAl Viro #endif
207bb898558SAl Viro #endif /* __ASSEMBLY__ */
20840e2ec65SDenys Vlasenko 
209bb898558SAl Viro #endif
210