1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3 * Supervisor Mode Access Prevention support
4 *
5 * Copyright (C) 2012 Intel Corporation
6 * Author: H. Peter Anvin <hpa@linux.intel.com>
7 */
8
9 #ifndef _ASM_X86_SMAP_H
10 #define _ASM_X86_SMAP_H
11
12 #include <asm/nops.h>
13 #include <asm/cpufeatures.h>
14 #include <asm/alternative.h>
15
16 #ifdef __ASSEMBLY__
17
18 #define ASM_CLAC \
19 ALTERNATIVE "", "clac", X86_FEATURE_SMAP
20
21 #define ASM_STAC \
22 ALTERNATIVE "", "stac", X86_FEATURE_SMAP
23
24 #else /* __ASSEMBLY__ */
25
clac(void)26 static __always_inline void clac(void)
27 {
28 /* Note: a barrier is implicit in alternative() */
29 alternative("", "clac", X86_FEATURE_SMAP);
30 }
31
stac(void)32 static __always_inline void stac(void)
33 {
34 /* Note: a barrier is implicit in alternative() */
35 alternative("", "stac", X86_FEATURE_SMAP);
36 }
37
smap_save(void)38 static __always_inline unsigned long smap_save(void)
39 {
40 unsigned long flags;
41
42 asm volatile ("# smap_save\n\t"
43 ALTERNATIVE("", "pushf; pop %0; " "clac" "\n\t",
44 X86_FEATURE_SMAP)
45 : "=rm" (flags) : : "memory", "cc");
46
47 return flags;
48 }
49
smap_restore(unsigned long flags)50 static __always_inline void smap_restore(unsigned long flags)
51 {
52 asm volatile ("# smap_restore\n\t"
53 ALTERNATIVE("", "push %0; popf\n\t",
54 X86_FEATURE_SMAP)
55 : : "g" (flags) : "memory", "cc");
56 }
57
58 /* These macros can be used in asm() statements */
59 #define ASM_CLAC \
60 ALTERNATIVE("", "clac", X86_FEATURE_SMAP)
61 #define ASM_STAC \
62 ALTERNATIVE("", "stac", X86_FEATURE_SMAP)
63
64 #endif /* __ASSEMBLY__ */
65
66 #endif /* _ASM_X86_SMAP_H */
67