blob: 5999b0b3dd4a8733885269447dcbf202c1ab65f6 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
David Howellsf05e7982012-03-28 18:11:12 +01002#ifndef _ASM_X86_SPECIAL_INSNS_H
3#define _ASM_X86_SPECIAL_INSNS_H
4
5
6#ifdef __KERNEL__
7
Ross Zwisler719d3592015-02-19 10:37:28 -07008#include <asm/nops.h>
Kees Cook873d50d2019-06-17 21:55:02 -07009#include <asm/processor-flags.h>
Thomas Gleixner410367e2020-03-04 23:32:15 +010010#include <linux/irqflags.h>
Kees Cook873d50d2019-06-17 21:55:02 -070011#include <linux/jump_label.h>
Ross Zwisler719d3592015-02-19 10:37:28 -070012
David Howellsf05e7982012-03-28 18:11:12 +010013/*
14 * Volatile isn't enough to prevent the compiler from reordering the
15 * read/write functions for the control registers and messing everything up.
16 * A memory clobber would solve the problem, but would prevent reordering of
17 * all loads stores around it, which can hurt performance. Solution is to
18 * use a variable and mimic reads and writes to it to enforce serialization
19 */
Jan Beulich1d10f6e2013-05-29 13:29:12 +010020extern unsigned long __force_order;
David Howellsf05e7982012-03-28 18:11:12 +010021
Thomas Gleixner7652ac92019-07-10 21:42:46 +020022void native_write_cr0(unsigned long val);
Kees Cook873d50d2019-06-17 21:55:02 -070023
David Howellsf05e7982012-03-28 18:11:12 +010024static inline unsigned long native_read_cr0(void)
25{
26 unsigned long val;
27 asm volatile("mov %%cr0,%0\n\t" : "=r" (val), "=m" (__force_order));
28 return val;
29}
30
Peter Zijlstra2823e832020-06-03 13:40:22 +020031static __always_inline unsigned long native_read_cr2(void)
David Howellsf05e7982012-03-28 18:11:12 +010032{
33 unsigned long val;
34 asm volatile("mov %%cr2,%0\n\t" : "=r" (val), "=m" (__force_order));
35 return val;
36}
37
Peter Zijlstra2823e832020-06-03 13:40:22 +020038static __always_inline void native_write_cr2(unsigned long val)
David Howellsf05e7982012-03-28 18:11:12 +010039{
40 asm volatile("mov %0,%%cr2": : "r" (val), "m" (__force_order));
41}
42
Andy Lutomirski6c690ee2017-06-12 10:26:14 -070043static inline unsigned long __native_read_cr3(void)
David Howellsf05e7982012-03-28 18:11:12 +010044{
45 unsigned long val;
46 asm volatile("mov %%cr3,%0\n\t" : "=r" (val), "=m" (__force_order));
47 return val;
48}
49
50static inline void native_write_cr3(unsigned long val)
51{
52 asm volatile("mov %0,%%cr3": : "r" (val), "m" (__force_order));
53}
54
55static inline unsigned long native_read_cr4(void)
56{
57 unsigned long val;
David Howellsf05e7982012-03-28 18:11:12 +010058#ifdef CONFIG_X86_32
Andy Lutomirski1ef55be12016-09-29 12:48:12 -070059 /*
60 * This could fault if CR4 does not exist. Non-existent CR4
61 * is functionally equivalent to CR4 == 0. Keep it simple and pretend
62 * that CR4 == 0 on CPUs that don't have CR4.
63 */
David Howellsf05e7982012-03-28 18:11:12 +010064 asm volatile("1: mov %%cr4, %0\n"
65 "2:\n"
66 _ASM_EXTABLE(1b, 2b)
67 : "=r" (val), "=m" (__force_order) : "0" (0));
68#else
Andy Lutomirski1ef55be12016-09-29 12:48:12 -070069 /* CR4 always exists on x86_64. */
70 asm volatile("mov %%cr4,%0\n\t" : "=r" (val), "=m" (__force_order));
David Howellsf05e7982012-03-28 18:11:12 +010071#endif
72 return val;
73}
74
Thomas Gleixner7652ac92019-07-10 21:42:46 +020075void native_write_cr4(unsigned long val);
David Howellsf05e7982012-03-28 18:11:12 +010076
Dave Hansena927cb82016-02-12 13:02:15 -080077#ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
Sebastian Andrzej Siewiorc806e8872019-04-03 18:41:41 +020078static inline u32 rdpkru(void)
Dave Hansena927cb82016-02-12 13:02:15 -080079{
80 u32 ecx = 0;
81 u32 edx, pkru;
82
83 /*
84 * "rdpkru" instruction. Places PKRU contents in to EAX,
85 * clears EDX and requires that ecx=0.
86 */
87 asm volatile(".byte 0x0f,0x01,0xee\n\t"
88 : "=a" (pkru), "=d" (edx)
89 : "c" (ecx));
90 return pkru;
91}
Xiao Guangrong9e901992016-03-22 16:51:17 +080092
Sebastian Andrzej Siewiorc806e8872019-04-03 18:41:41 +020093static inline void wrpkru(u32 pkru)
Xiao Guangrong9e901992016-03-22 16:51:17 +080094{
95 u32 ecx = 0, edx = 0;
96
97 /*
98 * "wrpkru" instruction. Loads contents in EAX to PKRU,
99 * requires that ecx = edx = 0.
100 */
101 asm volatile(".byte 0x0f,0x01,0xef\n\t"
102 : : "a" (pkru), "c"(ecx), "d"(edx));
103}
Sebastian Andrzej Siewiorc806e8872019-04-03 18:41:41 +0200104
105static inline void __write_pkru(u32 pkru)
106{
Sebastian Andrzej Siewior577ff462019-04-03 18:41:42 +0200107 /*
108 * WRPKRU is relatively expensive compared to RDPKRU.
109 * Avoid WRPKRU when it would not change the value.
110 */
111 if (pkru == rdpkru())
112 return;
113
Sebastian Andrzej Siewiorc806e8872019-04-03 18:41:41 +0200114 wrpkru(pkru);
115}
116
Dave Hansena927cb82016-02-12 13:02:15 -0800117#else
Sebastian Andrzej Siewiorc806e8872019-04-03 18:41:41 +0200118static inline u32 rdpkru(void)
Dave Hansena927cb82016-02-12 13:02:15 -0800119{
120 return 0;
121}
Xiao Guangrong9e901992016-03-22 16:51:17 +0800122
123static inline void __write_pkru(u32 pkru)
124{
125}
Dave Hansena927cb82016-02-12 13:02:15 -0800126#endif
127
David Howellsf05e7982012-03-28 18:11:12 +0100128static inline void native_wbinvd(void)
129{
130 asm volatile("wbinvd": : :"memory");
131}
132
Thomas Gleixner410367e2020-03-04 23:32:15 +0100133extern asmlinkage void asm_load_gs_index(unsigned int selector);
134
135static inline void native_load_gs_index(unsigned int selector)
136{
137 unsigned long flags;
138
139 local_irq_save(flags);
140 asm_load_gs_index(selector);
141 local_irq_restore(flags);
142}
David Howellsf05e7982012-03-28 18:11:12 +0100143
Juergen Gross87930012017-09-04 12:25:27 +0200144static inline unsigned long __read_cr4(void)
145{
146 return native_read_cr4();
147}
148
Juergen Grossfdc02692018-08-28 09:40:25 +0200149#ifdef CONFIG_PARAVIRT_XXL
David Howellsf05e7982012-03-28 18:11:12 +0100150#include <asm/paravirt.h>
Juergen Grossfdc02692018-08-28 09:40:25 +0200151#else
David Howellsf05e7982012-03-28 18:11:12 +0100152
153static inline unsigned long read_cr0(void)
154{
155 return native_read_cr0();
156}
157
158static inline void write_cr0(unsigned long x)
159{
160 native_write_cr0(x);
161}
162
Peter Zijlstra2823e832020-06-03 13:40:22 +0200163static __always_inline unsigned long read_cr2(void)
David Howellsf05e7982012-03-28 18:11:12 +0100164{
165 return native_read_cr2();
166}
167
Peter Zijlstra2823e832020-06-03 13:40:22 +0200168static __always_inline void write_cr2(unsigned long x)
David Howellsf05e7982012-03-28 18:11:12 +0100169{
170 native_write_cr2(x);
171}
172
Andy Lutomirski6c690ee2017-06-12 10:26:14 -0700173/*
174 * Careful! CR3 contains more than just an address. You probably want
175 * read_cr3_pa() instead.
176 */
177static inline unsigned long __read_cr3(void)
David Howellsf05e7982012-03-28 18:11:12 +0100178{
Andy Lutomirski6c690ee2017-06-12 10:26:14 -0700179 return __native_read_cr3();
David Howellsf05e7982012-03-28 18:11:12 +0100180}
181
182static inline void write_cr3(unsigned long x)
183{
184 native_write_cr3(x);
185}
186
Andy Lutomirski1e02ce42014-10-24 15:58:08 -0700187static inline void __write_cr4(unsigned long x)
David Howellsf05e7982012-03-28 18:11:12 +0100188{
189 native_write_cr4(x);
190}
191
192static inline void wbinvd(void)
193{
194 native_wbinvd();
195}
196
197#ifdef CONFIG_X86_64
198
Thomas Gleixner410367e2020-03-04 23:32:15 +0100199static inline void load_gs_index(unsigned int selector)
David Howellsf05e7982012-03-28 18:11:12 +0100200{
201 native_load_gs_index(selector);
202}
203
204#endif
205
Juergen Grossfdc02692018-08-28 09:40:25 +0200206#endif /* CONFIG_PARAVIRT_XXL */
David Howellsf05e7982012-03-28 18:11:12 +0100207
David Howellsf05e7982012-03-28 18:11:12 +0100208static inline void clflush(volatile void *__p)
209{
210 asm volatile("clflush %0" : "+m" (*(volatile char __force *)__p));
211}
212
Ross Zwisler171699f2014-02-26 12:06:49 -0700213static inline void clflushopt(volatile void *__p)
214{
215 alternative_io(".byte " __stringify(NOP_DS_PREFIX) "; clflush %P0",
216 ".byte 0x66; clflush %P0",
217 X86_FEATURE_CLFLUSHOPT,
218 "+m" (*(volatile char __force *)__p));
219}
220
Ross Zwislerd9dc64f2015-01-27 09:53:51 -0700221static inline void clwb(volatile void *__p)
222{
223 volatile struct { char x[64]; } *p = __p;
224
225 asm volatile(ALTERNATIVE_2(
226 ".byte " __stringify(NOP_DS_PREFIX) "; clflush (%[pax])",
227 ".byte 0x66; clflush (%[pax])", /* clflushopt (%%rax) */
228 X86_FEATURE_CLFLUSHOPT,
229 ".byte 0x66, 0x0f, 0xae, 0x30", /* clwb (%%rax) */
230 X86_FEATURE_CLWB)
231 : [p] "+m" (*p)
232 : [pax] "a" (p));
233}
234
David Howellsf05e7982012-03-28 18:11:12 +0100235#define nop() asm volatile ("nop")
236
Ricardo Neribf9c9122020-08-06 20:28:33 -0700237static inline void serialize(void)
238{
239 /* Instruction opcode for SERIALIZE; supported in binutils >= 2.35. */
240 asm volatile(".byte 0xf, 0x1, 0xe8" ::: "memory");
241}
242
David Howellsf05e7982012-03-28 18:11:12 +0100243#endif /* __KERNEL__ */
244
245#endif /* _ASM_X86_SPECIAL_INSNS_H */