1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Supervisor Mode Access Prevention support
5 * Copyright (C) 2012 Intel Corporation
6 * Author: H. Peter Anvin <hpa@linux.intel.com>
9 #ifndef _ASM_X86_SMAP_H
10 #define _ASM_X86_SMAP_H
13 #include <asm/cpufeatures.h>
14 #include <asm/alternative.h>
16 /* "Raw" instruction opcodes */
17 #define __ASM_CLAC ".byte 0x0f,0x01,0xca"
18 #define __ASM_STAC ".byte 0x0f,0x01,0xcb"
22 #ifdef CONFIG_X86_SMAP
25 ALTERNATIVE "", __ASM_CLAC, X86_FEATURE_SMAP
28 ALTERNATIVE "", __ASM_STAC, X86_FEATURE_SMAP
30 #else /* CONFIG_X86_SMAP */
35 #endif /* CONFIG_X86_SMAP */
37 #else /* __ASSEMBLY__ */
39 #ifdef CONFIG_X86_SMAP
41 static __always_inline void clac(void)
43 /* Note: a barrier is implicit in alternative() */
44 alternative("", __ASM_CLAC, X86_FEATURE_SMAP);
47 static __always_inline void stac(void)
49 /* Note: a barrier is implicit in alternative() */
50 alternative("", __ASM_STAC, X86_FEATURE_SMAP);
53 static __always_inline unsigned long smap_save(void)
57 asm volatile ("# smap_save\n\t"
58 ALTERNATIVE("", "pushf; pop %0; " __ASM_CLAC "\n\t",
60 : "=rm" (flags) : : "memory", "cc");
65 static __always_inline void smap_restore(unsigned long flags)
67 asm volatile ("# smap_restore\n\t"
68 ALTERNATIVE("", "push %0; popf\n\t",
70 : : "g" (flags) : "memory", "cc");
73 /* These macros can be used in asm() statements */
75 ALTERNATIVE("", __ASM_CLAC, X86_FEATURE_SMAP)
77 ALTERNATIVE("", __ASM_STAC, X86_FEATURE_SMAP)
79 #else /* CONFIG_X86_SMAP */
81 static inline void clac(void) { }
82 static inline void stac(void) { }
84 static inline unsigned long smap_save(void) { return 0; }
85 static inline void smap_restore(unsigned long flags) { }
90 #endif /* CONFIG_X86_SMAP */
92 #endif /* __ASSEMBLY__ */
94 #endif /* _ASM_X86_SMAP_H */