xref: /linux/arch/x86/include/asm/smap.h (revision 5e21a3ecad1500e35b46701e7f3f232e15d78e69)
1b886d83cSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
251ae4a2dSH. Peter Anvin /*
351ae4a2dSH. Peter Anvin  * Supervisor Mode Access Prevention support
451ae4a2dSH. Peter Anvin  *
551ae4a2dSH. Peter Anvin  * Copyright (C) 2012 Intel Corporation
651ae4a2dSH. Peter Anvin  * Author: H. Peter Anvin <hpa@linux.intel.com>
751ae4a2dSH. Peter Anvin  */
851ae4a2dSH. Peter Anvin 
951ae4a2dSH. Peter Anvin #ifndef _ASM_X86_SMAP_H
1051ae4a2dSH. Peter Anvin #define _ASM_X86_SMAP_H
1151ae4a2dSH. Peter Anvin 
1251ae4a2dSH. Peter Anvin #include <asm/nops.h>
13cd4d09ecSBorislav Petkov #include <asm/cpufeatures.h>
14*5e21a3ecSJuergen Gross #include <asm/alternative.h>
1551ae4a2dSH. Peter Anvin 
1651ae4a2dSH. Peter Anvin /* "Raw" instruction opcodes */
17a936af8eSPeter Zijlstra #define __ASM_CLAC	".byte 0x0f,0x01,0xca"
18a936af8eSPeter Zijlstra #define __ASM_STAC	".byte 0x0f,0x01,0xcb"
1951ae4a2dSH. Peter Anvin 
2051ae4a2dSH. Peter Anvin #ifdef __ASSEMBLY__
2151ae4a2dSH. Peter Anvin 
2251ae4a2dSH. Peter Anvin #ifdef CONFIG_X86_SMAP
2351ae4a2dSH. Peter Anvin 
2451ae4a2dSH. Peter Anvin #define ASM_CLAC \
25a936af8eSPeter Zijlstra 	ALTERNATIVE "", __ASM_CLAC, X86_FEATURE_SMAP
2651ae4a2dSH. Peter Anvin 
2751ae4a2dSH. Peter Anvin #define ASM_STAC \
28a936af8eSPeter Zijlstra 	ALTERNATIVE "", __ASM_STAC, X86_FEATURE_SMAP
2951ae4a2dSH. Peter Anvin 
3051ae4a2dSH. Peter Anvin #else /* CONFIG_X86_SMAP */
3151ae4a2dSH. Peter Anvin 
3251ae4a2dSH. Peter Anvin #define ASM_CLAC
3351ae4a2dSH. Peter Anvin #define ASM_STAC
3451ae4a2dSH. Peter Anvin 
3551ae4a2dSH. Peter Anvin #endif /* CONFIG_X86_SMAP */
3651ae4a2dSH. Peter Anvin 
3751ae4a2dSH. Peter Anvin #else /* __ASSEMBLY__ */
3851ae4a2dSH. Peter Anvin 
3951ae4a2dSH. Peter Anvin #ifdef CONFIG_X86_SMAP
4051ae4a2dSH. Peter Anvin 
4163bcff2aSH. Peter Anvin static __always_inline void clac(void)
4251ae4a2dSH. Peter Anvin {
4351ae4a2dSH. Peter Anvin 	/* Note: a barrier is implicit in alternative() */
44a936af8eSPeter Zijlstra 	alternative("", __ASM_CLAC, X86_FEATURE_SMAP);
4551ae4a2dSH. Peter Anvin }
4651ae4a2dSH. Peter Anvin 
4763bcff2aSH. Peter Anvin static __always_inline void stac(void)
4851ae4a2dSH. Peter Anvin {
4951ae4a2dSH. Peter Anvin 	/* Note: a barrier is implicit in alternative() */
50a936af8eSPeter Zijlstra 	alternative("", __ASM_STAC, X86_FEATURE_SMAP);
5151ae4a2dSH. Peter Anvin }
5251ae4a2dSH. Peter Anvin 
53e74deb11SPeter Zijlstra static __always_inline unsigned long smap_save(void)
54e74deb11SPeter Zijlstra {
55e74deb11SPeter Zijlstra 	unsigned long flags;
56e74deb11SPeter Zijlstra 
571ff865e3SPeter Zijlstra 	asm volatile ("# smap_save\n\t"
581ff865e3SPeter Zijlstra 		      ALTERNATIVE("jmp 1f", "", X86_FEATURE_SMAP)
591ff865e3SPeter Zijlstra 		      "pushf; pop %0; " __ASM_CLAC "\n\t"
601ff865e3SPeter Zijlstra 		      "1:"
61e74deb11SPeter Zijlstra 		      : "=rm" (flags) : : "memory", "cc");
62e74deb11SPeter Zijlstra 
63e74deb11SPeter Zijlstra 	return flags;
64e74deb11SPeter Zijlstra }
65e74deb11SPeter Zijlstra 
66e74deb11SPeter Zijlstra static __always_inline void smap_restore(unsigned long flags)
67e74deb11SPeter Zijlstra {
681ff865e3SPeter Zijlstra 	asm volatile ("# smap_restore\n\t"
691ff865e3SPeter Zijlstra 		      ALTERNATIVE("jmp 1f", "", X86_FEATURE_SMAP)
701ff865e3SPeter Zijlstra 		      "push %0; popf\n\t"
711ff865e3SPeter Zijlstra 		      "1:"
72e74deb11SPeter Zijlstra 		      : : "g" (flags) : "memory", "cc");
73e74deb11SPeter Zijlstra }
74e74deb11SPeter Zijlstra 
7551ae4a2dSH. Peter Anvin /* These macros can be used in asm() statements */
7651ae4a2dSH. Peter Anvin #define ASM_CLAC \
77a936af8eSPeter Zijlstra 	ALTERNATIVE("", __ASM_CLAC, X86_FEATURE_SMAP)
7851ae4a2dSH. Peter Anvin #define ASM_STAC \
79a936af8eSPeter Zijlstra 	ALTERNATIVE("", __ASM_STAC, X86_FEATURE_SMAP)
8051ae4a2dSH. Peter Anvin 
8151ae4a2dSH. Peter Anvin #else /* CONFIG_X86_SMAP */
8251ae4a2dSH. Peter Anvin 
8351ae4a2dSH. Peter Anvin static inline void clac(void) { }
8451ae4a2dSH. Peter Anvin static inline void stac(void) { }
8551ae4a2dSH. Peter Anvin 
86e74deb11SPeter Zijlstra static inline unsigned long smap_save(void) { return 0; }
87e74deb11SPeter Zijlstra static inline void smap_restore(unsigned long flags) { }
88e74deb11SPeter Zijlstra 
8951ae4a2dSH. Peter Anvin #define ASM_CLAC
9051ae4a2dSH. Peter Anvin #define ASM_STAC
9151ae4a2dSH. Peter Anvin 
9251ae4a2dSH. Peter Anvin #endif /* CONFIG_X86_SMAP */
9351ae4a2dSH. Peter Anvin 
9451ae4a2dSH. Peter Anvin #endif /* __ASSEMBLY__ */
9551ae4a2dSH. Peter Anvin 
9651ae4a2dSH. Peter Anvin #endif /* _ASM_X86_SMAP_H */
97