blob: 8b58d6975d5d4b4fa3a77e25529caf464b23bbfe [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001/* SPDX-License-Identifier: GPL-2.0-only */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * Supervisor Mode Access Prevention support
4 *
5 * Copyright (C) 2012 Intel Corporation
6 * Author: H. Peter Anvin <hpa@linux.intel.com>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007 */
8
9#ifndef _ASM_X86_SMAP_H
10#define _ASM_X86_SMAP_H
11
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000012#include <asm/nops.h>
13#include <asm/cpufeatures.h>
14
15/* "Raw" instruction opcodes */
David Brazdil0f672f62019-12-10 10:32:29 +000016#define __ASM_CLAC ".byte 0x0f,0x01,0xca"
17#define __ASM_STAC ".byte 0x0f,0x01,0xcb"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000018
19#ifdef __ASSEMBLY__
20
21#include <asm/alternative-asm.h>
22
23#ifdef CONFIG_X86_SMAP
24
25#define ASM_CLAC \
David Brazdil0f672f62019-12-10 10:32:29 +000026 ALTERNATIVE "", __ASM_CLAC, X86_FEATURE_SMAP
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000027
28#define ASM_STAC \
David Brazdil0f672f62019-12-10 10:32:29 +000029 ALTERNATIVE "", __ASM_STAC, X86_FEATURE_SMAP
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000030
31#else /* CONFIG_X86_SMAP */
32
33#define ASM_CLAC
34#define ASM_STAC
35
36#endif /* CONFIG_X86_SMAP */
37
38#else /* __ASSEMBLY__ */
39
40#include <asm/alternative.h>
41
42#ifdef CONFIG_X86_SMAP
43
44static __always_inline void clac(void)
45{
46 /* Note: a barrier is implicit in alternative() */
David Brazdil0f672f62019-12-10 10:32:29 +000047 alternative("", __ASM_CLAC, X86_FEATURE_SMAP);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000048}
49
50static __always_inline void stac(void)
51{
52 /* Note: a barrier is implicit in alternative() */
David Brazdil0f672f62019-12-10 10:32:29 +000053 alternative("", __ASM_STAC, X86_FEATURE_SMAP);
54}
55
56static __always_inline unsigned long smap_save(void)
57{
58 unsigned long flags;
59
Olivier Deprez0e641232021-09-23 10:07:05 +020060 asm volatile ("# smap_save\n\t"
61 ALTERNATIVE("jmp 1f", "", X86_FEATURE_SMAP)
62 "pushf; pop %0; " __ASM_CLAC "\n\t"
63 "1:"
David Brazdil0f672f62019-12-10 10:32:29 +000064 : "=rm" (flags) : : "memory", "cc");
65
66 return flags;
67}
68
69static __always_inline void smap_restore(unsigned long flags)
70{
Olivier Deprez0e641232021-09-23 10:07:05 +020071 asm volatile ("# smap_restore\n\t"
72 ALTERNATIVE("jmp 1f", "", X86_FEATURE_SMAP)
73 "push %0; popf\n\t"
74 "1:"
David Brazdil0f672f62019-12-10 10:32:29 +000075 : : "g" (flags) : "memory", "cc");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000076}
77
78/* These macros can be used in asm() statements */
79#define ASM_CLAC \
David Brazdil0f672f62019-12-10 10:32:29 +000080 ALTERNATIVE("", __ASM_CLAC, X86_FEATURE_SMAP)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000081#define ASM_STAC \
David Brazdil0f672f62019-12-10 10:32:29 +000082 ALTERNATIVE("", __ASM_STAC, X86_FEATURE_SMAP)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000083
84#else /* CONFIG_X86_SMAP */
85
86static inline void clac(void) { }
87static inline void stac(void) { }
88
David Brazdil0f672f62019-12-10 10:32:29 +000089static inline unsigned long smap_save(void) { return 0; }
90static inline void smap_restore(unsigned long flags) { }
91
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000092#define ASM_CLAC
93#define ASM_STAC
94
95#endif /* CONFIG_X86_SMAP */
96
97#endif /* __ASSEMBLY__ */
98
99#endif /* _ASM_X86_SMAP_H */