x86/mm/pkeys: Add functions to fetch PKRU
[linux] / arch / x86 / include / asm / special_insns.h
1 #ifndef _ASM_X86_SPECIAL_INSNS_H
2 #define _ASM_X86_SPECIAL_INSNS_H
3
4
5 #ifdef __KERNEL__
6
7 #include <asm/nops.h>
8
9 static inline void native_clts(void)
10 {
11         asm volatile("clts");
12 }
13
14 /*
15  * Volatile isn't enough to prevent the compiler from reordering the
16  * read/write functions for the control registers and messing everything up.
17  * A memory clobber would solve the problem, but would prevent reordering of
18  * all loads stores around it, which can hurt performance. Solution is to
19  * use a variable and mimic reads and writes to it to enforce serialization
20  */
21 extern unsigned long __force_order;
22
23 static inline unsigned long native_read_cr0(void)
24 {
25         unsigned long val;
26         asm volatile("mov %%cr0,%0\n\t" : "=r" (val), "=m" (__force_order));
27         return val;
28 }
29
30 static inline void native_write_cr0(unsigned long val)
31 {
32         asm volatile("mov %0,%%cr0": : "r" (val), "m" (__force_order));
33 }
34
35 static inline unsigned long native_read_cr2(void)
36 {
37         unsigned long val;
38         asm volatile("mov %%cr2,%0\n\t" : "=r" (val), "=m" (__force_order));
39         return val;
40 }
41
42 static inline void native_write_cr2(unsigned long val)
43 {
44         asm volatile("mov %0,%%cr2": : "r" (val), "m" (__force_order));
45 }
46
47 static inline unsigned long native_read_cr3(void)
48 {
49         unsigned long val;
50         asm volatile("mov %%cr3,%0\n\t" : "=r" (val), "=m" (__force_order));
51         return val;
52 }
53
54 static inline void native_write_cr3(unsigned long val)
55 {
56         asm volatile("mov %0,%%cr3": : "r" (val), "m" (__force_order));
57 }
58
59 static inline unsigned long native_read_cr4(void)
60 {
61         unsigned long val;
62         asm volatile("mov %%cr4,%0\n\t" : "=r" (val), "=m" (__force_order));
63         return val;
64 }
65
66 static inline unsigned long native_read_cr4_safe(void)
67 {
68         unsigned long val;
69         /* This could fault if %cr4 does not exist. In x86_64, a cr4 always
70          * exists, so it will never fail. */
71 #ifdef CONFIG_X86_32
72         asm volatile("1: mov %%cr4, %0\n"
73                      "2:\n"
74                      _ASM_EXTABLE(1b, 2b)
75                      : "=r" (val), "=m" (__force_order) : "0" (0));
76 #else
77         val = native_read_cr4();
78 #endif
79         return val;
80 }
81
82 static inline void native_write_cr4(unsigned long val)
83 {
84         asm volatile("mov %0,%%cr4": : "r" (val), "m" (__force_order));
85 }
86
87 #ifdef CONFIG_X86_64
88 static inline unsigned long native_read_cr8(void)
89 {
90         unsigned long cr8;
91         asm volatile("movq %%cr8,%0" : "=r" (cr8));
92         return cr8;
93 }
94
95 static inline void native_write_cr8(unsigned long val)
96 {
97         asm volatile("movq %0,%%cr8" :: "r" (val) : "memory");
98 }
99 #endif
100
101 #ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
102 static inline u32 __read_pkru(void)
103 {
104         u32 ecx = 0;
105         u32 edx, pkru;
106
107         /*
108          * "rdpkru" instruction.  Places PKRU contents in to EAX,
109          * clears EDX and requires that ecx=0.
110          */
111         asm volatile(".byte 0x0f,0x01,0xee\n\t"
112                      : "=a" (pkru), "=d" (edx)
113                      : "c" (ecx));
114         return pkru;
115 }
116 #else
117 static inline u32 __read_pkru(void)
118 {
119         return 0;
120 }
121 #endif
122
123 static inline void native_wbinvd(void)
124 {
125         asm volatile("wbinvd": : :"memory");
126 }
127
128 extern asmlinkage void native_load_gs_index(unsigned);
129
130 #ifdef CONFIG_PARAVIRT
131 #include <asm/paravirt.h>
132 #else
133
134 static inline unsigned long read_cr0(void)
135 {
136         return native_read_cr0();
137 }
138
139 static inline void write_cr0(unsigned long x)
140 {
141         native_write_cr0(x);
142 }
143
144 static inline unsigned long read_cr2(void)
145 {
146         return native_read_cr2();
147 }
148
149 static inline void write_cr2(unsigned long x)
150 {
151         native_write_cr2(x);
152 }
153
154 static inline unsigned long read_cr3(void)
155 {
156         return native_read_cr3();
157 }
158
159 static inline void write_cr3(unsigned long x)
160 {
161         native_write_cr3(x);
162 }
163
164 static inline unsigned long __read_cr4(void)
165 {
166         return native_read_cr4();
167 }
168
169 static inline unsigned long __read_cr4_safe(void)
170 {
171         return native_read_cr4_safe();
172 }
173
174 static inline void __write_cr4(unsigned long x)
175 {
176         native_write_cr4(x);
177 }
178
179 static inline void wbinvd(void)
180 {
181         native_wbinvd();
182 }
183
184 #ifdef CONFIG_X86_64
185
186 static inline unsigned long read_cr8(void)
187 {
188         return native_read_cr8();
189 }
190
191 static inline void write_cr8(unsigned long x)
192 {
193         native_write_cr8(x);
194 }
195
196 static inline void load_gs_index(unsigned selector)
197 {
198         native_load_gs_index(selector);
199 }
200
201 #endif
202
203 /* Clear the 'TS' bit */
204 static inline void clts(void)
205 {
206         native_clts();
207 }
208
209 #endif/* CONFIG_PARAVIRT */
210
211 #define stts() write_cr0(read_cr0() | X86_CR0_TS)
212
213 static inline void clflush(volatile void *__p)
214 {
215         asm volatile("clflush %0" : "+m" (*(volatile char __force *)__p));
216 }
217
218 static inline void clflushopt(volatile void *__p)
219 {
220         alternative_io(".byte " __stringify(NOP_DS_PREFIX) "; clflush %P0",
221                        ".byte 0x66; clflush %P0",
222                        X86_FEATURE_CLFLUSHOPT,
223                        "+m" (*(volatile char __force *)__p));
224 }
225
226 static inline void clwb(volatile void *__p)
227 {
228         volatile struct { char x[64]; } *p = __p;
229
230         asm volatile(ALTERNATIVE_2(
231                 ".byte " __stringify(NOP_DS_PREFIX) "; clflush (%[pax])",
232                 ".byte 0x66; clflush (%[pax])", /* clflushopt (%%rax) */
233                 X86_FEATURE_CLFLUSHOPT,
234                 ".byte 0x66, 0x0f, 0xae, 0x30",  /* clwb (%%rax) */
235                 X86_FEATURE_CLWB)
236                 : [p] "+m" (*p)
237                 : [pax] "a" (p));
238 }
239
240 /**
241  * pcommit_sfence() - persistent commit and fence
242  *
243  * The PCOMMIT instruction ensures that data that has been flushed from the
244  * processor's cache hierarchy with CLWB, CLFLUSHOPT or CLFLUSH is accepted to
245  * memory and is durable on the DIMM.  The primary use case for this is
246  * persistent memory.
247  *
248  * This function shows how to properly use CLWB/CLFLUSHOPT/CLFLUSH and PCOMMIT
249  * with appropriate fencing.
250  *
251  * Example:
252  * void flush_and_commit_buffer(void *vaddr, unsigned int size)
253  * {
254  *         unsigned long clflush_mask = boot_cpu_data.x86_clflush_size - 1;
255  *         void *vend = vaddr + size;
256  *         void *p;
257  *
258  *         for (p = (void *)((unsigned long)vaddr & ~clflush_mask);
259  *              p < vend; p += boot_cpu_data.x86_clflush_size)
260  *                 clwb(p);
261  *
262  *         // SFENCE to order CLWB/CLFLUSHOPT/CLFLUSH cache flushes
263  *         // MFENCE via mb() also works
264  *         wmb();
265  *
266  *         // PCOMMIT and the required SFENCE for ordering
267  *         pcommit_sfence();
268  * }
269  *
270  * After this function completes the data pointed to by 'vaddr' has been
271  * accepted to memory and will be durable if the 'vaddr' points to persistent
272  * memory.
273  *
274  * PCOMMIT must always be ordered by an MFENCE or SFENCE, so to help simplify
275  * things we include both the PCOMMIT and the required SFENCE in the
276  * alternatives generated by pcommit_sfence().
277  */
278 static inline void pcommit_sfence(void)
279 {
280         alternative(ASM_NOP7,
281                     ".byte 0x66, 0x0f, 0xae, 0xf8\n\t" /* pcommit */
282                     "sfence",
283                     X86_FEATURE_PCOMMIT);
284 }
285
286 #define nop() asm volatile ("nop")
287
288
289 #endif /* __KERNEL__ */
290
291 #endif /* _ASM_X86_SPECIAL_INSNS_H */