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 907f07f55SThomas Gleixner #include <asm/nospec-branch.h> 1007f07f55SThomas 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); 20*7a745be1SPeter Zijlstra extern __always_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 47*7a745be1SPeter Zijlstra static __always_inline void native_irq_disable(void) 48bb898558SAl Viro { 49bb898558SAl Viro asm volatile("cli": : :"memory"); 50bb898558SAl Viro } 51bb898558SAl Viro 52*7a745be1SPeter Zijlstra static __always_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 { 5907f07f55SThomas 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 { 6507f07f55SThomas 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 77*7a745be1SPeter Zijlstra static __always_inline unsigned long arch_local_save_flags(void) 78bb898558SAl Viro { 79bb898558SAl Viro return native_save_fl(); 80bb898558SAl Viro } 81bb898558SAl Viro 82*7a745be1SPeter Zijlstra static __always_inline void arch_local_irq_restore(unsigned long flags) 83bb898558SAl Viro { 84bb898558SAl Viro native_restore_fl(flags); 85bb898558SAl Viro } 86bb898558SAl Viro 87*7a745be1SPeter Zijlstra static __always_inline void arch_local_irq_disable(void) 88bb898558SAl Viro { 89bb898558SAl Viro native_irq_disable(); 90bb898558SAl Viro } 91bb898558SAl Viro 92*7a745be1SPeter Zijlstra static __always_inline 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 */ 118*7a745be1SPeter Zijlstra static __always_inline 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__ 162*7a745be1SPeter Zijlstra static __always_inline int arch_irqs_disabled_flags(unsigned long flags) 163bb898558SAl Viro { 164bb898558SAl Viro return !(flags & X86_EFLAGS_IF); 165bb898558SAl Viro } 166bb898558SAl Viro 167*7a745be1SPeter Zijlstra static __always_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 175bb898558SAl Viro #endif 176