xref: /kvm-unit-tests/lib/x86/processor.h (revision 4143fbfd524b140b81122286c3984bd1aaac82b7)
1 #ifndef _X86_PROCESSOR_H_
2 #define _X86_PROCESSOR_H_
3 
4 #include "libcflat.h"
5 #include "desc.h"
6 #include "msr.h"
7 #include <bitops.h>
8 #include <stdint.h>
9 
10 #define NONCANONICAL	0xaaaaaaaaaaaaaaaaull
11 
12 #ifdef __x86_64__
13 #  define R "r"
14 #  define W "q"
15 #  define S "8"
16 #else
17 #  define R "e"
18 #  define W "l"
19 #  define S "4"
20 #endif
21 
22 #define DB_VECTOR 1
23 #define BP_VECTOR 3
24 #define UD_VECTOR 6
25 #define DF_VECTOR 8
26 #define TS_VECTOR 10
27 #define NP_VECTOR 11
28 #define SS_VECTOR 12
29 #define GP_VECTOR 13
30 #define PF_VECTOR 14
31 #define AC_VECTOR 17
32 #define CP_VECTOR 21
33 
34 #define X86_CR0_PE	BIT(0)
35 #define X86_CR0_MP	BIT(1)
36 #define X86_CR0_EM	BIT(2)
37 #define X86_CR0_TS	BIT(3)
38 #define X86_CR0_ET	BIT(4)
39 #define X86_CR0_NE	BIT(5)
40 #define X86_CR0_WP	BIT(16)
41 #define X86_CR0_AM	BIT(18)
42 #define X86_CR0_NW	BIT(29)
43 #define X86_CR0_CD	BIT(30)
44 #define X86_CR0_PG	BIT(31)
45 
46 #define X86_CR3_PCID_MASK	GENMASK(11, 0)
47 
48 #define X86_CR4_VME		BIT(0)
49 #define X86_CR4_PVI		BIT(1)
50 #define X86_CR4_TSD		BIT(2)
51 #define X86_CR4_DE		BIT(3)
52 #define X86_CR4_PSE		BIT(4)
53 #define X86_CR4_PAE		BIT(5)
54 #define X86_CR4_MCE		BIT(6)
55 #define X86_CR4_PGE		BIT(7)
56 #define X86_CR4_PCE		BIT(8)
57 #define X86_CR4_OSFXSR		BIT(9)
58 #define X86_CR4_OSXMMEXCPT	BIT(10)
59 #define X86_CR4_UMIP		BIT(11)
60 #define X86_CR4_LA57		BIT(12)
61 #define X86_CR4_VMXE		BIT(13)
62 #define X86_CR4_SMXE		BIT(14)
63 /* UNUSED			BIT(15) */
64 #define X86_CR4_FSGSBASE	BIT(16)
65 #define X86_CR4_PCIDE		BIT(17)
66 #define X86_CR4_OSXSAVE		BIT(18)
67 #define X86_CR4_KL		BIT(19)
68 #define X86_CR4_SMEP		BIT(20)
69 #define X86_CR4_SMAP		BIT(21)
70 #define X86_CR4_PKE		BIT(22)
71 #define X86_CR4_CET		BIT(23)
72 #define X86_CR4_PKS		BIT(24)
73 
74 #define X86_EFLAGS_CF		BIT(0)
75 #define X86_EFLAGS_FIXED	BIT(1)
76 #define X86_EFLAGS_PF		BIT(2)
77 /* RESERVED 0			BIT(3) */
78 #define X86_EFLAGS_AF		BIT(4)
79 /* RESERVED 0			BIT(5) */
80 #define X86_EFLAGS_ZF		BIT(6)
81 #define X86_EFLAGS_SF		BIT(7)
82 #define X86_EFLAGS_TF		BIT(8)
83 #define X86_EFLAGS_IF		BIT(9)
84 #define X86_EFLAGS_DF		BIT(10)
85 #define X86_EFLAGS_OF		BIT(11)
86 #define X86_EFLAGS_IOPL		GENMASK(13, 12)
87 #define X86_EFLAGS_NT		BIT(14)
88 /* RESERVED 0			BIT(15) */
89 #define X86_EFLAGS_RF		BIT(16)
90 #define X86_EFLAGS_VM		BIT(17)
91 #define X86_EFLAGS_AC		BIT(18)
92 #define X86_EFLAGS_VIF		BIT(19)
93 #define X86_EFLAGS_VIP		BIT(20)
94 #define X86_EFLAGS_ID		BIT(21)
95 
96 #define X86_EFLAGS_ALU (X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | \
97 			X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF)
98 
99 
100 /*
101  * CPU features
102  */
103 
104 enum cpuid_output_regs {
105 	EAX,
106 	EBX,
107 	ECX,
108 	EDX
109 };
110 
111 struct cpuid { u32 a, b, c, d; };
112 
113 static inline struct cpuid raw_cpuid(u32 function, u32 index)
114 {
115 	struct cpuid r;
116 	asm volatile ("cpuid"
117 		      : "=a"(r.a), "=b"(r.b), "=c"(r.c), "=d"(r.d)
118 		      : "0"(function), "2"(index));
119 	return r;
120 }
121 
122 static inline struct cpuid cpuid_indexed(u32 function, u32 index)
123 {
124 	u32 level = raw_cpuid(function & 0xf0000000, 0).a;
125 	if (level < function)
126 	return (struct cpuid) { 0, 0, 0, 0 };
127 	return raw_cpuid(function, index);
128 }
129 
130 static inline struct cpuid cpuid(u32 function)
131 {
132 	return cpuid_indexed(function, 0);
133 }
134 
135 static inline u8 cpuid_maxphyaddr(void)
136 {
137 	if (raw_cpuid(0x80000000, 0).a < 0x80000008)
138 	return 36;
139 	return raw_cpuid(0x80000008, 0).a & 0xff;
140 }
141 
142 static inline bool is_intel(void)
143 {
144 	struct cpuid c = cpuid(0);
145 	u32 name[4] = {c.b, c.d, c.c };
146 
147 	return strcmp((char *)name, "GenuineIntel") == 0;
148 }
149 
150 #define	CPUID(a, b, c, d) ((((unsigned long long) a) << 32) | (b << 16) | \
151 			  (c << 8) | d)
152 
153 /*
154  * Each X86_FEATURE_XXX definition is 64-bit and contains the following
155  * CPUID meta-data:
156  *
157  * 	[63:32] :  input value for EAX
158  * 	[31:16] :  input value for ECX
159  * 	[15:8]  :  output register
160  * 	[7:0]   :  bit position in output register
161  */
162 
163 /*
164  * Basic Leafs, a.k.a. Intel defined
165  */
166 #define	X86_FEATURE_MWAIT		(CPUID(0x1, 0, ECX, 3))
167 #define	X86_FEATURE_VMX			(CPUID(0x1, 0, ECX, 5))
168 #define	X86_FEATURE_PCID		(CPUID(0x1, 0, ECX, 17))
169 #define	X86_FEATURE_MOVBE		(CPUID(0x1, 0, ECX, 22))
170 #define	X86_FEATURE_TSC_DEADLINE_TIMER	(CPUID(0x1, 0, ECX, 24))
171 #define	X86_FEATURE_XSAVE		(CPUID(0x1, 0, ECX, 26))
172 #define	X86_FEATURE_OSXSAVE		(CPUID(0x1, 0, ECX, 27))
173 #define	X86_FEATURE_RDRAND		(CPUID(0x1, 0, ECX, 30))
174 #define	X86_FEATURE_MCE			(CPUID(0x1, 0, EDX, 7))
175 #define	X86_FEATURE_APIC		(CPUID(0x1, 0, EDX, 9))
176 #define	X86_FEATURE_CLFLUSH		(CPUID(0x1, 0, EDX, 19))
177 #define	X86_FEATURE_XMM			(CPUID(0x1, 0, EDX, 25))
178 #define	X86_FEATURE_XMM2		(CPUID(0x1, 0, EDX, 26))
179 #define	X86_FEATURE_TSC_ADJUST		(CPUID(0x7, 0, EBX, 1))
180 #define	X86_FEATURE_HLE			(CPUID(0x7, 0, EBX, 4))
181 #define	X86_FEATURE_SMEP		(CPUID(0x7, 0, EBX, 7))
182 #define	X86_FEATURE_INVPCID		(CPUID(0x7, 0, EBX, 10))
183 #define	X86_FEATURE_RTM			(CPUID(0x7, 0, EBX, 11))
184 #define	X86_FEATURE_SMAP		(CPUID(0x7, 0, EBX, 20))
185 #define	X86_FEATURE_PCOMMIT		(CPUID(0x7, 0, EBX, 22))
186 #define	X86_FEATURE_CLFLUSHOPT		(CPUID(0x7, 0, EBX, 23))
187 #define	X86_FEATURE_CLWB		(CPUID(0x7, 0, EBX, 24))
188 #define	X86_FEATURE_UMIP		(CPUID(0x7, 0, ECX, 2))
189 #define	X86_FEATURE_PKU			(CPUID(0x7, 0, ECX, 3))
190 #define	X86_FEATURE_LA57		(CPUID(0x7, 0, ECX, 16))
191 #define	X86_FEATURE_RDPID		(CPUID(0x7, 0, ECX, 22))
192 #define	X86_FEATURE_SHSTK		(CPUID(0x7, 0, ECX, 7))
193 #define	X86_FEATURE_IBT			(CPUID(0x7, 0, EDX, 20))
194 #define	X86_FEATURE_SPEC_CTRL		(CPUID(0x7, 0, EDX, 26))
195 #define	X86_FEATURE_ARCH_CAPABILITIES	(CPUID(0x7, 0, EDX, 29))
196 #define	X86_FEATURE_PKS			(CPUID(0x7, 0, ECX, 31))
197 
198 /*
199  * Extended Leafs, a.k.a. AMD defined
200  */
201 #define	X86_FEATURE_SVM			(CPUID(0x80000001, 0, ECX, 2))
202 #define	X86_FEATURE_NX			(CPUID(0x80000001, 0, EDX, 20))
203 #define	X86_FEATURE_GBPAGES		(CPUID(0x80000001, 0, EDX, 26))
204 #define	X86_FEATURE_RDTSCP		(CPUID(0x80000001, 0, EDX, 27))
205 #define	X86_FEATURE_LM			(CPUID(0x80000001, 0, EDX, 29))
206 #define	X86_FEATURE_RDPRU		(CPUID(0x80000008, 0, EBX, 4))
207 #define	X86_FEATURE_AMD_IBPB		(CPUID(0x80000008, 0, EBX, 12))
208 #define	X86_FEATURE_NPT			(CPUID(0x8000000A, 0, EDX, 0))
209 #define	X86_FEATURE_LBRV		(CPUID(0x8000000A, 0, EDX, 1))
210 #define	X86_FEATURE_NRIPS		(CPUID(0x8000000A, 0, EDX, 3))
211 #define X86_FEATURE_TSCRATEMSR		(CPUID(0x8000000A, 0, EDX, 4))
212 #define X86_FEATURE_PAUSEFILTER		(CPUID(0x8000000A, 0, EDX, 10))
213 #define X86_FEATURE_PFTHRESHOLD		(CPUID(0x8000000A, 0, EDX, 12))
214 #define	X86_FEATURE_VGIF		(CPUID(0x8000000A, 0, EDX, 16))
215 
216 
217 static inline bool this_cpu_has(u64 feature)
218 {
219 	u32 input_eax = feature >> 32;
220 	u32 input_ecx = (feature >> 16) & 0xffff;
221 	u32 output_reg = (feature >> 8) & 0xff;
222 	u8 bit = feature & 0xff;
223 	struct cpuid c;
224 	u32 *tmp;
225 
226 	c = cpuid_indexed(input_eax, input_ecx);
227 	tmp = (u32 *)&c;
228 
229 	return ((*(tmp + (output_reg % 32))) & (1 << bit));
230 }
231 
232 struct far_pointer32 {
233 	u32 offset;
234 	u16 selector;
235 } __attribute__((packed));
236 
237 struct descriptor_table_ptr {
238 	u16 limit;
239 	ulong base;
240 } __attribute__((packed));
241 
242 static inline void clac(void)
243 {
244 	asm volatile (".byte 0x0f, 0x01, 0xca" : : : "memory");
245 }
246 
247 static inline void stac(void)
248 {
249 	asm volatile (".byte 0x0f, 0x01, 0xcb" : : : "memory");
250 }
251 
252 static inline u16 read_cs(void)
253 {
254 	unsigned val;
255 
256 	asm volatile ("mov %%cs, %0" : "=mr"(val));
257 	return val;
258 }
259 
260 static inline u16 read_ds(void)
261 {
262 	unsigned val;
263 
264 	asm volatile ("mov %%ds, %0" : "=mr"(val));
265 	return val;
266 }
267 
268 static inline u16 read_es(void)
269 {
270 	unsigned val;
271 
272 	asm volatile ("mov %%es, %0" : "=mr"(val));
273 	return val;
274 }
275 
276 static inline u16 read_ss(void)
277 {
278 	unsigned val;
279 
280 	asm volatile ("mov %%ss, %0" : "=mr"(val));
281 	return val;
282 }
283 
284 static inline u16 read_fs(void)
285 {
286 	unsigned val;
287 
288 	asm volatile ("mov %%fs, %0" : "=mr"(val));
289 	return val;
290 }
291 
292 static inline u16 read_gs(void)
293 {
294 	unsigned val;
295 
296 	asm volatile ("mov %%gs, %0" : "=mr"(val));
297 	return val;
298 }
299 
300 static inline unsigned long read_rflags(void)
301 {
302 	unsigned long f;
303 	asm volatile ("pushf; pop %0\n\t" : "=rm"(f));
304 	return f;
305 }
306 
307 static inline void write_ds(unsigned val)
308 {
309 	asm volatile ("mov %0, %%ds" : : "rm"(val) : "memory");
310 }
311 
312 static inline void write_es(unsigned val)
313 {
314 	asm volatile ("mov %0, %%es" : : "rm"(val) : "memory");
315 }
316 
317 static inline void write_ss(unsigned val)
318 {
319 	asm volatile ("mov %0, %%ss" : : "rm"(val) : "memory");
320 }
321 
322 static inline void write_fs(unsigned val)
323 {
324 	asm volatile ("mov %0, %%fs" : : "rm"(val) : "memory");
325 }
326 
327 static inline void write_gs(unsigned val)
328 {
329 	asm volatile ("mov %0, %%gs" : : "rm"(val) : "memory");
330 }
331 
332 static inline void write_rflags(unsigned long f)
333 {
334 	asm volatile ("push %0; popf\n\t" : : "rm"(f));
335 }
336 
337 static inline void set_iopl(int iopl)
338 {
339 	unsigned long flags = read_rflags() & ~X86_EFLAGS_IOPL;
340 	flags |= iopl * (X86_EFLAGS_IOPL / 3);
341 	write_rflags(flags);
342 }
343 
344 static inline u64 rdmsr(u32 index)
345 {
346 	u32 a, d;
347 	asm volatile ("rdmsr" : "=a"(a), "=d"(d) : "c"(index) : "memory");
348 	return a | ((u64)d << 32);
349 }
350 
351 static inline void wrmsr(u32 index, u64 val)
352 {
353 	u32 a = val, d = val >> 32;
354 	asm volatile ("wrmsr" : : "a"(a), "d"(d), "c"(index) : "memory");
355 }
356 
357 static inline int rdmsr_safe(u32 index)
358 {
359 	asm volatile (ASM_TRY("1f")
360 		      "rdmsr\n\t"
361 		      "1:"
362 		      : : "c"(index) : "memory", "eax", "edx");
363 	return exception_vector();
364 }
365 
366 static inline int wrmsr_safe(u32 index, u64 val)
367 {
368 	u32 a = val, d = val >> 32;
369 
370 	asm volatile (ASM_TRY("1f")
371 		      "wrmsr\n\t"
372 		      "1:"
373 		      : : "a"(a), "d"(d), "c"(index) : "memory");
374 	return exception_vector();
375 }
376 
377 static inline uint64_t rdpmc(uint32_t index)
378 {
379 	uint32_t a, d;
380 	asm volatile ("rdpmc" : "=a"(a), "=d"(d) : "c"(index));
381 	return a | ((uint64_t)d << 32);
382 }
383 
384 static inline void write_cr0(ulong val)
385 {
386 	asm volatile ("mov %0, %%cr0" : : "r"(val) : "memory");
387 }
388 
389 static inline ulong read_cr0(void)
390 {
391 	ulong val;
392 	asm volatile ("mov %%cr0, %0" : "=r"(val) : : "memory");
393 	return val;
394 }
395 
396 static inline void write_cr2(ulong val)
397 {
398 	asm volatile ("mov %0, %%cr2" : : "r"(val) : "memory");
399 }
400 
401 static inline ulong read_cr2(void)
402 {
403 	ulong val;
404 	asm volatile ("mov %%cr2, %0" : "=r"(val) : : "memory");
405 	return val;
406 }
407 
408 static inline void write_cr3(ulong val)
409 {
410 	asm volatile ("mov %0, %%cr3" : : "r"(val) : "memory");
411 }
412 
413 static inline ulong read_cr3(void)
414 {
415 	ulong val;
416 	asm volatile ("mov %%cr3, %0" : "=r"(val) : : "memory");
417 	return val;
418 }
419 
420 static inline void update_cr3(void *cr3)
421 {
422 	write_cr3((ulong)cr3);
423 }
424 
425 static inline void write_cr4(ulong val)
426 {
427 	asm volatile ("mov %0, %%cr4" : : "r"(val) : "memory");
428 }
429 
430 static inline ulong read_cr4(void)
431 {
432 	ulong val;
433 	asm volatile ("mov %%cr4, %0" : "=r"(val) : : "memory");
434 	return val;
435 }
436 
437 static inline void write_cr8(ulong val)
438 {
439 	asm volatile ("mov %0, %%cr8" : : "r"(val) : "memory");
440 }
441 
442 static inline ulong read_cr8(void)
443 {
444 	ulong val;
445 	asm volatile ("mov %%cr8, %0" : "=r"(val) : : "memory");
446 	return val;
447 }
448 
449 static inline void lgdt(const struct descriptor_table_ptr *ptr)
450 {
451 	asm volatile ("lgdt %0" : : "m"(*ptr));
452 }
453 
454 static inline void sgdt(struct descriptor_table_ptr *ptr)
455 {
456 	asm volatile ("sgdt %0" : "=m"(*ptr));
457 }
458 
459 static inline void lidt(const struct descriptor_table_ptr *ptr)
460 {
461 	asm volatile ("lidt %0" : : "m"(*ptr));
462 }
463 
464 static inline void sidt(struct descriptor_table_ptr *ptr)
465 {
466 	asm volatile ("sidt %0" : "=m"(*ptr));
467 }
468 
469 static inline void lldt(u16 val)
470 {
471 	asm volatile ("lldt %0" : : "rm"(val));
472 }
473 
474 static inline u16 sldt(void)
475 {
476 	u16 val;
477 	asm volatile ("sldt %0" : "=rm"(val));
478 	return val;
479 }
480 
481 static inline void ltr(u16 val)
482 {
483 	asm volatile ("ltr %0" : : "rm"(val));
484 }
485 
486 static inline u16 str(void)
487 {
488 	u16 val;
489 	asm volatile ("str %0" : "=rm"(val));
490 	return val;
491 }
492 
493 static inline void write_dr0(void *val)
494 {
495 	asm volatile ("mov %0, %%dr0" : : "r"(val) : "memory");
496 }
497 
498 static inline void write_dr1(void *val)
499 {
500 	asm volatile ("mov %0, %%dr1" : : "r"(val) : "memory");
501 }
502 
503 static inline void write_dr2(void *val)
504 {
505 	asm volatile ("mov %0, %%dr2" : : "r"(val) : "memory");
506 }
507 
508 static inline void write_dr3(void *val)
509 {
510 	asm volatile ("mov %0, %%dr3" : : "r"(val) : "memory");
511 }
512 
513 static inline void write_dr6(ulong val)
514 {
515 	asm volatile ("mov %0, %%dr6" : : "r"(val) : "memory");
516 }
517 
518 static inline ulong read_dr6(void)
519 {
520 	ulong val;
521 	asm volatile ("mov %%dr6, %0" : "=r"(val));
522 	return val;
523 }
524 
525 static inline void write_dr7(ulong val)
526 {
527 	asm volatile ("mov %0, %%dr7" : : "r"(val) : "memory");
528 }
529 
530 static inline ulong read_dr7(void)
531 {
532 	ulong val;
533 	asm volatile ("mov %%dr7, %0" : "=r"(val));
534 	return val;
535 }
536 
537 static inline void pause(void)
538 {
539 	asm volatile ("pause");
540 }
541 
542 static inline void cli(void)
543 {
544 	asm volatile ("cli");
545 }
546 
547 static inline void sti(void)
548 {
549 	asm volatile ("sti");
550 }
551 
552 static inline unsigned long long rdrand(void)
553 {
554 	long long r;
555 
556 	asm volatile("rdrand %0\n\t"
557 		     "jc 1f\n\t"
558 		     "mov $0, %0\n\t"
559 		     "1:\n\t" : "=r" (r));
560 	return r;
561 }
562 
563 static inline unsigned long long rdtsc(void)
564 {
565 	long long r;
566 
567 #ifdef __x86_64__
568 	unsigned a, d;
569 
570 	asm volatile ("rdtsc" : "=a"(a), "=d"(d));
571 	r = a | ((long long)d << 32);
572 #else
573 	asm volatile ("rdtsc" : "=A"(r));
574 #endif
575 	return r;
576 }
577 
578 /*
579  * Per the advice in the SDM, volume 2, the sequence "mfence; lfence"
580  * executed immediately before rdtsc ensures that rdtsc will be
581  * executed only after all previous instructions have executed and all
582  * previous loads and stores are globally visible. In addition, the
583  * lfence immediately after rdtsc ensures that rdtsc will be executed
584  * prior to the execution of any subsequent instruction.
585  */
586 static inline unsigned long long fenced_rdtsc(void)
587 {
588 	unsigned long long tsc;
589 
590 #ifdef __x86_64__
591 	unsigned int eax, edx;
592 
593 	asm volatile ("mfence; lfence; rdtsc; lfence" : "=a"(eax), "=d"(edx));
594 	tsc = eax | ((unsigned long long)edx << 32);
595 #else
596 	asm volatile ("mfence; lfence; rdtsc; lfence" : "=A"(tsc));
597 #endif
598 	return tsc;
599 }
600 
601 static inline unsigned long long rdtscp(u32 *aux)
602 {
603 	long long r;
604 
605 #ifdef __x86_64__
606 	unsigned a, d;
607 
608 	asm volatile ("rdtscp" : "=a"(a), "=d"(d), "=c"(*aux));
609 	r = a | ((long long)d << 32);
610 #else
611 	asm volatile ("rdtscp" : "=A"(r), "=c"(*aux));
612 #endif
613 	return r;
614 }
615 
616 static inline void wrtsc(u64 tsc)
617 {
618 	wrmsr(MSR_IA32_TSC, tsc);
619 }
620 
621 static inline void irq_disable(void)
622 {
623 	asm volatile("cli");
624 }
625 
626 /* Note that irq_enable() does not ensure an interrupt shadow due
627  * to the vagaries of compiler optimizations.  If you need the
628  * shadow, use a single asm with "sti" and the instruction after it.
629  */
630 static inline void irq_enable(void)
631 {
632 	asm volatile("sti");
633 }
634 
635 static inline void invlpg(volatile void *va)
636 {
637 	asm volatile("invlpg (%0)" ::"r" (va) : "memory");
638 }
639 
640 static inline void safe_halt(void)
641 {
642 	asm volatile("sti; hlt");
643 }
644 
645 static inline u32 read_pkru(void)
646 {
647 	unsigned int eax, edx;
648 	unsigned int ecx = 0;
649 	unsigned int pkru;
650 
651 	asm volatile(".byte 0x0f,0x01,0xee\n\t"
652 		     : "=a" (eax), "=d" (edx)
653 		     : "c" (ecx));
654 	pkru = eax;
655 	return pkru;
656 }
657 
658 static inline void write_pkru(u32 pkru)
659 {
660 	unsigned int eax = pkru;
661 	unsigned int ecx = 0;
662 	unsigned int edx = 0;
663 
664 	asm volatile(".byte 0x0f,0x01,0xef\n\t"
665 		     : : "a" (eax), "c" (ecx), "d" (edx));
666 }
667 
668 static inline bool is_canonical(u64 addr)
669 {
670 	int va_width = (raw_cpuid(0x80000008, 0).a & 0xff00) >> 8;
671 	int shift_amt = 64 - va_width;
672 
673 	return (s64)(addr << shift_amt) >> shift_amt == addr;
674 }
675 
676 static inline void clear_bit(int bit, u8 *addr)
677 {
678 	__asm__ __volatile__("btr %1, %0"
679 			     : "+m" (*addr) : "Ir" (bit) : "cc", "memory");
680 }
681 
682 static inline void set_bit(int bit, u8 *addr)
683 {
684 	__asm__ __volatile__("bts %1, %0"
685 			     : "+m" (*addr) : "Ir" (bit) : "cc", "memory");
686 }
687 
688 static inline void flush_tlb(void)
689 {
690 	ulong cr4;
691 
692 	cr4 = read_cr4();
693 	write_cr4(cr4 ^ X86_CR4_PGE);
694 	write_cr4(cr4);
695 }
696 
697 static inline int has_spec_ctrl(void)
698 {
699 	return !!(this_cpu_has(X86_FEATURE_SPEC_CTRL));
700 }
701 
702 static inline int cpu_has_efer_nx(void)
703 {
704 	return !!(this_cpu_has(X86_FEATURE_NX));
705 }
706 
707 static inline bool cpuid_osxsave(void)
708 {
709 	return cpuid(1).c & (1 << (X86_FEATURE_OSXSAVE % 32));
710 }
711 
712 #endif
713