blob: c8ef23f2c28f17c59308b9c41179c47f85e075ad [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +01002#ifndef _X86_IRQFLAGS_H_
3#define _X86_IRQFLAGS_H_
4
5#include <asm/processor-flags.h>
6
7#ifndef __ASSEMBLY__
Chris Metcalf6727ad92016-10-07 17:02:55 -07008
9/* Provide __cpuidle; we can't safely include <linux/cpu.h> */
10#define __cpuidle __attribute__((__section__(".cpuidle.text")))
11
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +010012/*
13 * Interrupt control:
14 */
15
16static inline unsigned long native_save_fl(void)
17{
18 unsigned long flags;
19
H. Peter Anvinf1f029c2009-08-03 16:33:40 -070020 /*
H. Peter Anvinab94fcf2009-08-25 16:47:16 -070021 * "=rm" is safe here, because "pop" adjusts the stack before
22 * it evaluates its effective address -- this is part of the
23 * documented behavior of the "pop" instruction.
H. Peter Anvinf1f029c2009-08-03 16:33:40 -070024 */
Joe Perchescf7f7192008-03-23 01:02:30 -070025 asm volatile("# __raw_save_flags\n\t"
26 "pushf ; pop %0"
H. Peter Anvinab94fcf2009-08-25 16:47:16 -070027 : "=rm" (flags)
Joe Perchescf7f7192008-03-23 01:02:30 -070028 : /* no input */
29 : "memory");
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +010030
31 return flags;
32}
33
34static inline void native_restore_fl(unsigned long flags)
35{
Joe Perchescf7f7192008-03-23 01:02:30 -070036 asm volatile("push %0 ; popf"
37 : /* no output */
38 :"g" (flags)
39 :"memory", "cc");
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +010040}
41
42static inline void native_irq_disable(void)
43{
44 asm volatile("cli": : :"memory");
45}
46
47static inline void native_irq_enable(void)
48{
49 asm volatile("sti": : :"memory");
50}
51
Chris Metcalf6727ad92016-10-07 17:02:55 -070052static inline __cpuidle void native_safe_halt(void)
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +010053{
54 asm volatile("sti; hlt": : :"memory");
55}
56
Chris Metcalf6727ad92016-10-07 17:02:55 -070057static inline __cpuidle void native_halt(void)
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +010058{
59 asm volatile("hlt": : :"memory");
60}
61
62#endif
63
64#ifdef CONFIG_PARAVIRT
65#include <asm/paravirt.h>
Thomas Gleixner96a388d2007-10-11 11:20:03 +020066#else
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +010067#ifndef __ASSEMBLY__
Steven Rostedte08fbb72011-07-01 23:04:36 -040068#include <linux/types.h>
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +010069
Steven Rostedte08fbb72011-07-01 23:04:36 -040070static inline notrace unsigned long arch_local_save_flags(void)
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +010071{
72 return native_save_fl();
73}
74
Steven Rostedte08fbb72011-07-01 23:04:36 -040075static inline notrace void arch_local_irq_restore(unsigned long flags)
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +010076{
77 native_restore_fl(flags);
78}
79
Steven Rostedte08fbb72011-07-01 23:04:36 -040080static inline notrace void arch_local_irq_disable(void)
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +010081{
82 native_irq_disable();
83}
84
Steven Rostedte08fbb72011-07-01 23:04:36 -040085static inline notrace void arch_local_irq_enable(void)
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +010086{
87 native_irq_enable();
88}
89
90/*
91 * Used in the idle loop; sti takes one instruction cycle
92 * to complete:
93 */
Chris Metcalf6727ad92016-10-07 17:02:55 -070094static inline __cpuidle void arch_safe_halt(void)
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +010095{
96 native_safe_halt();
97}
98
99/*
100 * Used when interrupts are already enabled or to
101 * shutdown the processor:
102 */
Chris Metcalf6727ad92016-10-07 17:02:55 -0700103static inline __cpuidle void halt(void)
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +0100104{
105 native_halt();
106}
107
108/*
109 * For spinlocks, etc:
110 */
Steven Rostedte08fbb72011-07-01 23:04:36 -0400111static inline notrace unsigned long arch_local_irq_save(void)
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +0100112{
David Howellsdf9ee292010-10-07 14:08:55 +0100113 unsigned long flags = arch_local_save_flags();
114 arch_local_irq_disable();
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +0100115 return flags;
116}
117#else
118
119#define ENABLE_INTERRUPTS(x) sti
120#define DISABLE_INTERRUPTS(x) cli
121
122#ifdef CONFIG_X86_64
Jeremy Fitzhardingedf366e92008-06-27 12:04:03 -0700123#define SWAPGS swapgs
124/*
125 * Currently paravirt can't handle swapgs nicely when we
126 * don't have a stack we can rely on (such as a user space
127 * stack). So we either find a way around these or just fault
128 * and emulate if a guest tries to call swapgs directly.
129 *
130 * Either way, this is a good way to document that we don't
131 * have a reliable stack. x86_64 only.
132 */
Jeremy Fitzhardingea00394f2008-06-25 00:19:30 -0400133#define SWAPGS_UNSAFE_STACK swapgs
Jeremy Fitzhardingedf366e92008-06-27 12:04:03 -0700134
135#define PARAVIRT_ADJUST_EXCEPTION_FRAME /* */
136
Andy Lutomirski7209a752014-07-23 08:34:11 -0700137#define INTERRUPT_RETURN jmp native_iret
Jeremy Fitzhardinge2be29982008-06-25 00:19:28 -0400138#define USERGS_SYSRET64 \
139 swapgs; \
140 sysretq;
141#define USERGS_SYSRET32 \
142 swapgs; \
143 sysretl
Jeremy Fitzhardinge2be29982008-06-25 00:19:28 -0400144
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +0100145#else
146#define INTERRUPT_RETURN iret
Jeremy Fitzhardinged75cd222008-06-25 00:19:26 -0400147#define ENABLE_INTERRUPTS_SYSEXIT sti; sysexit
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +0100148#define GET_CR0_INTO_EAX movl %cr0, %eax
149#endif
150
151
152#endif /* __ASSEMBLY__ */
153#endif /* CONFIG_PARAVIRT */
154
155#ifndef __ASSEMBLY__
David Howellsdf9ee292010-10-07 14:08:55 +0100156static inline int arch_irqs_disabled_flags(unsigned long flags)
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +0100157{
158 return !(flags & X86_EFLAGS_IF);
159}
160
David Howellsdf9ee292010-10-07 14:08:55 +0100161static inline int arch_irqs_disabled(void)
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +0100162{
David Howellsdf9ee292010-10-07 14:08:55 +0100163 unsigned long flags = arch_local_save_flags();
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +0100164
David Howellsdf9ee292010-10-07 14:08:55 +0100165 return arch_irqs_disabled_flags(flags);
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +0100166}
Denys Vlasenko40e2ec62015-03-25 21:14:26 +0100167#endif /* !__ASSEMBLY__ */
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +0100168
Denys Vlasenko40e2ec62015-03-25 21:14:26 +0100169#ifdef __ASSEMBLY__
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +0100170#ifdef CONFIG_TRACE_IRQFLAGS
Steven Rostedt81d68a92008-05-12 21:20:42 +0200171# define TRACE_IRQS_ON call trace_hardirqs_on_thunk;
172# define TRACE_IRQS_OFF call trace_hardirqs_off_thunk;
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +0100173#else
174# define TRACE_IRQS_ON
175# define TRACE_IRQS_OFF
176#endif
177#ifdef CONFIG_DEBUG_LOCK_ALLOC
Denys Vlasenko40e2ec62015-03-25 21:14:26 +0100178# ifdef CONFIG_X86_64
Denys Vlasenko7dc7cc02015-03-25 21:14:27 +0100179# define LOCKDEP_SYS_EXIT call lockdep_sys_exit_thunk
180# define LOCKDEP_SYS_EXIT_IRQ \
Denys Vlasenko40e2ec62015-03-25 21:14:26 +0100181 TRACE_IRQS_ON; \
182 sti; \
Denys Vlasenko7dc7cc02015-03-25 21:14:27 +0100183 call lockdep_sys_exit_thunk; \
Denys Vlasenko40e2ec62015-03-25 21:14:26 +0100184 cli; \
185 TRACE_IRQS_OFF;
186# else
Denys Vlasenko7dc7cc02015-03-25 21:14:27 +0100187# define LOCKDEP_SYS_EXIT \
Denys Vlasenko40e2ec62015-03-25 21:14:26 +0100188 pushl %eax; \
189 pushl %ecx; \
190 pushl %edx; \
191 call lockdep_sys_exit; \
192 popl %edx; \
193 popl %ecx; \
194 popl %eax;
Denys Vlasenko7dc7cc02015-03-25 21:14:27 +0100195# define LOCKDEP_SYS_EXIT_IRQ
Denys Vlasenko40e2ec62015-03-25 21:14:26 +0100196# endif
Denys Vlasenko7dc7cc02015-03-25 21:14:27 +0100197#else
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +0100198# define LOCKDEP_SYS_EXIT
199# define LOCKDEP_SYS_EXIT_IRQ
Denys Vlasenko7dc7cc02015-03-25 21:14:27 +0100200#endif
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +0100201#endif /* __ASSEMBLY__ */
Denys Vlasenko40e2ec62015-03-25 21:14:26 +0100202
Thomas Gleixner96a388d2007-10-11 11:20:03 +0200203#endif