1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2003 Peter Wemm.
5  * Copyright (c) 1993 The Regents of the University of California.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * $FreeBSD: stable/12/sys/amd64/include/cpufunc.h 370919 2021-10-17 18:50:31Z kib $
33  */
34 
35 /*
36  * Functions to provide access to special i386 instructions.
37  * This in included in sys/systm.h, and that file should be
38  * used in preference to this.
39  */
40 
41 #ifndef _MACHINE_CPUFUNC_H_
42 #define	_MACHINE_CPUFUNC_H_
43 
44 #ifndef _SYS_CDEFS_H_
45 #error this file needs sys/cdefs.h as a prerequisite
46 #endif
47 
48 struct region_descriptor;
49 
50 #define readb(va)	(*(volatile uint8_t *) (va))
51 #define readw(va)	(*(volatile uint16_t *) (va))
52 #define readl(va)	(*(volatile uint32_t *) (va))
53 #define readq(va)	(*(volatile uint64_t *) (va))
54 
55 #define writeb(va, d)	(*(volatile uint8_t *) (va) = (d))
56 #define writew(va, d)	(*(volatile uint16_t *) (va) = (d))
57 #define writel(va, d)	(*(volatile uint32_t *) (va) = (d))
58 #define writeq(va, d)	(*(volatile uint64_t *) (va) = (d))
59 
60 #if defined(__GNUCLIKE_ASM) && defined(__CC_SUPPORTS___INLINE)
61 
62 static __inline void
breakpoint(void)63 breakpoint(void)
64 {
65 	__asm __volatile("int $3");
66 }
67 
68 static __inline __pure2 u_int
bsfl(u_int mask)69 bsfl(u_int mask)
70 {
71 	u_int	result;
72 
73 	__asm __volatile("bsfl %1,%0" : "=r" (result) : "rm" (mask));
74 	return (result);
75 }
76 
77 static __inline __pure2 u_long
bsfq(u_long mask)78 bsfq(u_long mask)
79 {
80 	u_long	result;
81 
82 	__asm __volatile("bsfq %1,%0" : "=r" (result) : "rm" (mask));
83 	return (result);
84 }
85 
86 static __inline __pure2 u_int
bsrl(u_int mask)87 bsrl(u_int mask)
88 {
89 	u_int	result;
90 
91 	__asm __volatile("bsrl %1,%0" : "=r" (result) : "rm" (mask));
92 	return (result);
93 }
94 
95 static __inline __pure2 u_long
bsrq(u_long mask)96 bsrq(u_long mask)
97 {
98 	u_long	result;
99 
100 	__asm __volatile("bsrq %1,%0" : "=r" (result) : "rm" (mask));
101 	return (result);
102 }
103 
104 static __inline void
clflush(u_long addr)105 clflush(u_long addr)
106 {
107 
108 	__asm __volatile("clflush %0" : : "m" (*(char *)addr));
109 }
110 
111 static __inline void
clflushopt(u_long addr)112 clflushopt(u_long addr)
113 {
114 
115 	__asm __volatile(".byte 0x66;clflush %0" : : "m" (*(char *)addr));
116 }
117 
118 static __inline void
clwb(u_long addr)119 clwb(u_long addr)
120 {
121 
122 	__asm __volatile("clwb %0" : : "m" (*(char *)addr));
123 }
124 
125 static __inline void
clts(void)126 clts(void)
127 {
128 
129 	__asm __volatile("clts");
130 }
131 
132 static __inline void
disable_intr(void)133 disable_intr(void)
134 {
135 	__asm __volatile("cli" : : : "memory");
136 }
137 
138 static __inline void
do_cpuid(u_int ax,u_int * p)139 do_cpuid(u_int ax, u_int *p)
140 {
141 	__asm __volatile("cpuid"
142 			 : "=a" (p[0]), "=b" (p[1]), "=c" (p[2]), "=d" (p[3])
143 			 :  "0" (ax));
144 }
145 
146 static __inline void
cpuid_count(u_int ax,u_int cx,u_int * p)147 cpuid_count(u_int ax, u_int cx, u_int *p)
148 {
149 	__asm __volatile("cpuid"
150 			 : "=a" (p[0]), "=b" (p[1]), "=c" (p[2]), "=d" (p[3])
151 			 :  "0" (ax), "c" (cx));
152 }
153 
154 static __inline void
enable_intr(void)155 enable_intr(void)
156 {
157 	__asm __volatile("sti");
158 }
159 
160 #ifdef _KERNEL
161 
162 #define	HAVE_INLINE_FFS
163 #define        ffs(x)  __builtin_ffs(x)
164 
165 #define	HAVE_INLINE_FFSL
166 
167 static __inline __pure2 int
ffsl(long mask)168 ffsl(long mask)
169 {
170 
171 	return (__builtin_ffsl(mask));
172 }
173 
174 #define	HAVE_INLINE_FFSLL
175 
176 static __inline __pure2 int
ffsll(long long mask)177 ffsll(long long mask)
178 {
179 	return (ffsl((long)mask));
180 }
181 
182 #define	HAVE_INLINE_FLS
183 
184 static __inline __pure2 int
fls(int mask)185 fls(int mask)
186 {
187 	return (mask == 0 ? mask : (int)bsrl((u_int)mask) + 1);
188 }
189 
190 #define	HAVE_INLINE_FLSL
191 
192 static __inline __pure2 int
flsl(long mask)193 flsl(long mask)
194 {
195 	return (mask == 0 ? mask : (int)bsrq((u_long)mask) + 1);
196 }
197 
198 #define	HAVE_INLINE_FLSLL
199 
200 static __inline __pure2 int
flsll(long long mask)201 flsll(long long mask)
202 {
203 	return (flsl((long)mask));
204 }
205 
206 #endif /* _KERNEL */
207 
208 static __inline void
halt(void)209 halt(void)
210 {
211 	__asm __volatile("hlt");
212 }
213 
214 static __inline u_char
inb(u_int port)215 inb(u_int port)
216 {
217 	u_char	data;
218 
219 	__asm __volatile("inb %w1, %0" : "=a" (data) : "Nd" (port));
220 	return (data);
221 }
222 
223 static __inline u_int
inl(u_int port)224 inl(u_int port)
225 {
226 	u_int	data;
227 
228 	__asm __volatile("inl %w1, %0" : "=a" (data) : "Nd" (port));
229 	return (data);
230 }
231 
232 static __inline void
insb(u_int port,void * addr,size_t count)233 insb(u_int port, void *addr, size_t count)
234 {
235 	__asm __volatile("cld; rep; insb"
236 			 : "+D" (addr), "+c" (count)
237 			 : "d" (port)
238 			 : "memory");
239 }
240 
241 static __inline void
insw(u_int port,void * addr,size_t count)242 insw(u_int port, void *addr, size_t count)
243 {
244 	__asm __volatile("cld; rep; insw"
245 			 : "+D" (addr), "+c" (count)
246 			 : "d" (port)
247 			 : "memory");
248 }
249 
250 static __inline void
insl(u_int port,void * addr,size_t count)251 insl(u_int port, void *addr, size_t count)
252 {
253 	__asm __volatile("cld; rep; insl"
254 			 : "+D" (addr), "+c" (count)
255 			 : "d" (port)
256 			 : "memory");
257 }
258 
259 static __inline void
invd(void)260 invd(void)
261 {
262 	__asm __volatile("invd");
263 }
264 
265 static __inline u_short
inw(u_int port)266 inw(u_int port)
267 {
268 	u_short	data;
269 
270 	__asm __volatile("inw %w1, %0" : "=a" (data) : "Nd" (port));
271 	return (data);
272 }
273 
274 static __inline void
outb(u_int port,u_char data)275 outb(u_int port, u_char data)
276 {
277 	__asm __volatile("outb %0, %w1" : : "a" (data), "Nd" (port));
278 }
279 
280 static __inline void
outl(u_int port,u_int data)281 outl(u_int port, u_int data)
282 {
283 	__asm __volatile("outl %0, %w1" : : "a" (data), "Nd" (port));
284 }
285 
286 static __inline void
outsb(u_int port,const void * addr,size_t count)287 outsb(u_int port, const void *addr, size_t count)
288 {
289 	__asm __volatile("cld; rep; outsb"
290 			 : "+S" (addr), "+c" (count)
291 			 : "d" (port));
292 }
293 
294 static __inline void
outsw(u_int port,const void * addr,size_t count)295 outsw(u_int port, const void *addr, size_t count)
296 {
297 	__asm __volatile("cld; rep; outsw"
298 			 : "+S" (addr), "+c" (count)
299 			 : "d" (port));
300 }
301 
302 static __inline void
outsl(u_int port,const void * addr,size_t count)303 outsl(u_int port, const void *addr, size_t count)
304 {
305 	__asm __volatile("cld; rep; outsl"
306 			 : "+S" (addr), "+c" (count)
307 			 : "d" (port));
308 }
309 
310 static __inline void
outw(u_int port,u_short data)311 outw(u_int port, u_short data)
312 {
313 	__asm __volatile("outw %0, %w1" : : "a" (data), "Nd" (port));
314 }
315 
316 static __inline u_long
popcntq(u_long mask)317 popcntq(u_long mask)
318 {
319 	u_long result;
320 
321 	__asm __volatile("popcntq %1,%0" : "=r" (result) : "rm" (mask));
322 	return (result);
323 }
324 
325 static __inline void
lfence(void)326 lfence(void)
327 {
328 
329 	__asm __volatile("lfence" : : : "memory");
330 }
331 
332 static __inline void
mfence(void)333 mfence(void)
334 {
335 
336 	__asm __volatile("mfence" : : : "memory");
337 }
338 
339 static __inline void
sfence(void)340 sfence(void)
341 {
342 
343 	__asm __volatile("sfence" : : : "memory");
344 }
345 
346 static __inline void
ia32_pause(void)347 ia32_pause(void)
348 {
349 	__asm __volatile("pause");
350 }
351 
352 static __inline u_long
read_rflags(void)353 read_rflags(void)
354 {
355 	u_long	rf;
356 
357 	__asm __volatile("pushfq; popq %0" : "=r" (rf));
358 	return (rf);
359 }
360 
361 static __inline uint64_t
rdmsr(u_int msr)362 rdmsr(u_int msr)
363 {
364 	uint32_t low, high;
365 
366 	__asm __volatile("rdmsr" : "=a" (low), "=d" (high) : "c" (msr));
367 	return (low | ((uint64_t)high << 32));
368 }
369 
370 static __inline uint32_t
rdmsr32(u_int msr)371 rdmsr32(u_int msr)
372 {
373 	uint32_t low;
374 
375 	__asm __volatile("rdmsr" : "=a" (low) : "c" (msr) : "rdx");
376 	return (low);
377 }
378 
379 static __inline uint64_t
rdpmc(u_int pmc)380 rdpmc(u_int pmc)
381 {
382 	uint32_t low, high;
383 
384 	__asm __volatile("rdpmc" : "=a" (low), "=d" (high) : "c" (pmc));
385 	return (low | ((uint64_t)high << 32));
386 }
387 
388 static __inline uint64_t
rdtsc(void)389 rdtsc(void)
390 {
391 	uint32_t low, high;
392 
393 	__asm __volatile("rdtsc" : "=a" (low), "=d" (high));
394 	return (low | ((uint64_t)high << 32));
395 }
396 
397 static __inline uint64_t
rdtsc_ordered_lfence(void)398 rdtsc_ordered_lfence(void)
399 {
400 	lfence();
401 	return (rdtsc());
402 }
403 
404 static __inline uint64_t
rdtsc_ordered_mfence(void)405 rdtsc_ordered_mfence(void)
406 {
407 	mfence();
408 	return (rdtsc());
409 }
410 
411 static __inline uint64_t
rdtscp(void)412 rdtscp(void)
413 {
414 	uint32_t low, high;
415 
416 	__asm __volatile("rdtscp" : "=a" (low), "=d" (high) : : "ecx");
417 	return (low | ((uint64_t)high << 32));
418 }
419 
420 static __inline uint64_t
rdtscp_aux(uint32_t * aux)421 rdtscp_aux(uint32_t *aux)
422 {
423 	uint32_t low, high;
424 
425 	__asm __volatile("rdtscp" : "=a" (low), "=d" (high), "=c" (*aux));
426 	return (low | ((uint64_t)high << 32));
427 }
428 
429 static __inline uint32_t
rdtsc32(void)430 rdtsc32(void)
431 {
432 	uint32_t rv;
433 
434 	__asm __volatile("rdtsc" : "=a" (rv) : : "edx");
435 	return (rv);
436 }
437 
438 static __inline uint32_t
rdtscp32(void)439 rdtscp32(void)
440 {
441 	uint32_t rv;
442 
443 	__asm __volatile("rdtscp" : "=a" (rv) : : "ecx", "edx");
444 	return (rv);
445 }
446 
447 static __inline void
wbinvd(void)448 wbinvd(void)
449 {
450 	__asm __volatile("wbinvd");
451 }
452 
453 static __inline void
write_rflags(u_long rf)454 write_rflags(u_long rf)
455 {
456 	__asm __volatile("pushq %0;  popfq" : : "r" (rf));
457 }
458 
459 static __inline void
wrmsr(u_int msr,uint64_t newval)460 wrmsr(u_int msr, uint64_t newval)
461 {
462 	uint32_t low, high;
463 
464 	low = newval;
465 	high = newval >> 32;
466 	__asm __volatile("wrmsr" : : "a" (low), "d" (high), "c" (msr));
467 }
468 
469 static __inline void
load_cr0(u_long data)470 load_cr0(u_long data)
471 {
472 
473 	__asm __volatile("movq %0,%%cr0" : : "r" (data));
474 }
475 
476 static __inline u_long
rcr0(void)477 rcr0(void)
478 {
479 	u_long	data;
480 
481 	__asm __volatile("movq %%cr0,%0" : "=r" (data));
482 	return (data);
483 }
484 
485 static __inline u_long
rcr2(void)486 rcr2(void)
487 {
488 	u_long	data;
489 
490 	__asm __volatile("movq %%cr2,%0" : "=r" (data));
491 	return (data);
492 }
493 
494 static __inline void
load_cr3(u_long data)495 load_cr3(u_long data)
496 {
497 
498 	__asm __volatile("movq %0,%%cr3" : : "r" (data) : "memory");
499 }
500 
501 static __inline u_long
rcr3(void)502 rcr3(void)
503 {
504 	u_long	data;
505 
506 	__asm __volatile("movq %%cr3,%0" : "=r" (data));
507 	return (data);
508 }
509 
510 static __inline void
load_cr4(u_long data)511 load_cr4(u_long data)
512 {
513 	__asm __volatile("movq %0,%%cr4" : : "r" (data));
514 }
515 
516 static __inline u_long
rcr4(void)517 rcr4(void)
518 {
519 	u_long	data;
520 
521 	__asm __volatile("movq %%cr4,%0" : "=r" (data));
522 	return (data);
523 }
524 
525 static __inline u_long
rxcr(u_int reg)526 rxcr(u_int reg)
527 {
528 	u_int low, high;
529 
530 	__asm __volatile("xgetbv" : "=a" (low), "=d" (high) : "c" (reg));
531 	return (low | ((uint64_t)high << 32));
532 }
533 
534 static __inline void
load_xcr(u_int reg,u_long val)535 load_xcr(u_int reg, u_long val)
536 {
537 	u_int low, high;
538 
539 	low = val;
540 	high = val >> 32;
541 	__asm __volatile("xsetbv" : : "c" (reg), "a" (low), "d" (high));
542 }
543 
544 /*
545  * Global TLB flush (except for thise for pages marked PG_G)
546  */
547 static __inline void
invltlb(void)548 invltlb(void)
549 {
550 
551 	load_cr3(rcr3());
552 }
553 
554 #ifndef CR4_PGE
555 #define	CR4_PGE	0x00000080	/* Page global enable */
556 #endif
557 
558 /*
559  * Perform the guaranteed invalidation of all TLB entries.  This
560  * includes the global entries, and entries in all PCIDs, not only the
561  * current context.  The function works both on non-PCID CPUs and CPUs
562  * with the PCID turned off or on.  See IA-32 SDM Vol. 3a 4.10.4.1
563  * Operations that Invalidate TLBs and Paging-Structure Caches.
564  */
565 static __inline void
invltlb_glob(void)566 invltlb_glob(void)
567 {
568 	uint64_t cr4;
569 
570 	cr4 = rcr4();
571 	load_cr4(cr4 & ~CR4_PGE);
572 	/*
573 	 * Although preemption at this point could be detrimental to
574 	 * performance, it would not lead to an error.  PG_G is simply
575 	 * ignored if CR4.PGE is clear.  Moreover, in case this block
576 	 * is re-entered, the load_cr4() either above or below will
577 	 * modify CR4.PGE flushing the TLB.
578 	 */
579 	load_cr4(cr4 | CR4_PGE);
580 }
581 
582 /*
583  * TLB flush for an individual page (even if it has PG_G).
584  * Only works on 486+ CPUs (i386 does not have PG_G).
585  */
586 static __inline void
invlpg(u_long addr)587 invlpg(u_long addr)
588 {
589 
590 	__asm __volatile("invlpg %0" : : "m" (*(char *)addr) : "memory");
591 }
592 
593 #define	INVPCID_ADDR	0
594 #define	INVPCID_CTX	1
595 #define	INVPCID_CTXGLOB	2
596 #define	INVPCID_ALLCTX	3
597 
598 struct invpcid_descr {
599 	uint64_t	pcid:12 __packed;
600 	uint64_t	pad:52 __packed;
601 	uint64_t	addr;
602 } __packed;
603 
604 static __inline void
invpcid(struct invpcid_descr * d,int type)605 invpcid(struct invpcid_descr *d, int type)
606 {
607 
608 	__asm __volatile("invpcid (%0),%1"
609 	    : : "r" (d), "r" ((u_long)type) : "memory");
610 }
611 
612 static __inline u_short
rfs(void)613 rfs(void)
614 {
615 	u_short sel;
616 	__asm __volatile("movw %%fs,%0" : "=rm" (sel));
617 	return (sel);
618 }
619 
620 static __inline u_short
rgs(void)621 rgs(void)
622 {
623 	u_short sel;
624 	__asm __volatile("movw %%gs,%0" : "=rm" (sel));
625 	return (sel);
626 }
627 
628 static __inline u_short
rss(void)629 rss(void)
630 {
631 	u_short sel;
632 	__asm __volatile("movw %%ss,%0" : "=rm" (sel));
633 	return (sel);
634 }
635 
636 static __inline void
load_ds(u_short sel)637 load_ds(u_short sel)
638 {
639 	__asm __volatile("movw %0,%%ds" : : "rm" (sel));
640 }
641 
642 static __inline void
load_es(u_short sel)643 load_es(u_short sel)
644 {
645 	__asm __volatile("movw %0,%%es" : : "rm" (sel));
646 }
647 
648 static __inline void
cpu_monitor(const void * addr,u_long extensions,u_int hints)649 cpu_monitor(const void *addr, u_long extensions, u_int hints)
650 {
651 
652 	__asm __volatile("monitor"
653 	    : : "a" (addr), "c" (extensions), "d" (hints));
654 }
655 
656 static __inline void
cpu_mwait(u_long extensions,u_int hints)657 cpu_mwait(u_long extensions, u_int hints)
658 {
659 
660 	__asm __volatile("mwait" : : "a" (hints), "c" (extensions));
661 }
662 
663 static __inline uint32_t
rdpkru(void)664 rdpkru(void)
665 {
666 	uint32_t res;
667 
668 	__asm __volatile("rdpkru" :  "=a" (res) : "c" (0) : "edx");
669 	return (res);
670 }
671 
672 static __inline void
wrpkru(uint32_t mask)673 wrpkru(uint32_t mask)
674 {
675 
676 	__asm __volatile("wrpkru" :  : "a" (mask),  "c" (0), "d" (0));
677 }
678 
679 #ifdef _KERNEL
680 /* This is defined in <machine/specialreg.h> but is too painful to get to */
681 #ifndef	MSR_FSBASE
682 #define	MSR_FSBASE	0xc0000100
683 #endif
684 static __inline void
load_fs(u_short sel)685 load_fs(u_short sel)
686 {
687 	/* Preserve the fsbase value across the selector load */
688 	__asm __volatile("rdmsr; movw %0,%%fs; wrmsr"
689 	    : : "rm" (sel), "c" (MSR_FSBASE) : "eax", "edx");
690 }
691 
692 #ifndef	MSR_GSBASE
693 #define	MSR_GSBASE	0xc0000101
694 #endif
695 static __inline void
load_gs(u_short sel)696 load_gs(u_short sel)
697 {
698 	/*
699 	 * Preserve the gsbase value across the selector load.
700 	 * Note that we have to disable interrupts because the gsbase
701 	 * being trashed happens to be the kernel gsbase at the time.
702 	 */
703 	__asm __volatile("pushfq; cli; rdmsr; movw %0,%%gs; wrmsr; popfq"
704 	    : : "rm" (sel), "c" (MSR_GSBASE) : "eax", "edx");
705 }
706 #else
707 /* Usable by userland */
708 static __inline void
load_fs(u_short sel)709 load_fs(u_short sel)
710 {
711 	__asm __volatile("movw %0,%%fs" : : "rm" (sel));
712 }
713 
714 static __inline void
load_gs(u_short sel)715 load_gs(u_short sel)
716 {
717 	__asm __volatile("movw %0,%%gs" : : "rm" (sel));
718 }
719 #endif
720 
721 static __inline uint64_t
rdfsbase(void)722 rdfsbase(void)
723 {
724 	uint64_t x;
725 
726 	__asm __volatile("rdfsbase %0" : "=r" (x));
727 	return (x);
728 }
729 
730 static __inline void
wrfsbase(uint64_t x)731 wrfsbase(uint64_t x)
732 {
733 
734 	__asm __volatile("wrfsbase %0" : : "r" (x));
735 }
736 
737 static __inline uint64_t
rdgsbase(void)738 rdgsbase(void)
739 {
740 	uint64_t x;
741 
742 	__asm __volatile("rdgsbase %0" : "=r" (x));
743 	return (x);
744 }
745 
746 static __inline void
wrgsbase(uint64_t x)747 wrgsbase(uint64_t x)
748 {
749 
750 	__asm __volatile("wrgsbase %0" : : "r" (x));
751 }
752 
753 static __inline void
bare_lgdt(struct region_descriptor * addr)754 bare_lgdt(struct region_descriptor *addr)
755 {
756 	__asm __volatile("lgdt (%0)" : : "r" (addr));
757 }
758 
759 static __inline void
sgdt(struct region_descriptor * addr)760 sgdt(struct region_descriptor *addr)
761 {
762 	char *loc;
763 
764 	loc = (char *)addr;
765 	__asm __volatile("sgdt %0" : "=m" (*loc) : : "memory");
766 }
767 
768 static __inline void
lidt(struct region_descriptor * addr)769 lidt(struct region_descriptor *addr)
770 {
771 	__asm __volatile("lidt (%0)" : : "r" (addr));
772 }
773 
774 static __inline void
sidt(struct region_descriptor * addr)775 sidt(struct region_descriptor *addr)
776 {
777 	char *loc;
778 
779 	loc = (char *)addr;
780 	__asm __volatile("sidt %0" : "=m" (*loc) : : "memory");
781 }
782 
783 static __inline void
lldt(u_short sel)784 lldt(u_short sel)
785 {
786 	__asm __volatile("lldt %0" : : "r" (sel));
787 }
788 
789 static __inline u_short
sldt(void)790 sldt(void)
791 {
792 	u_short sel;
793 
794 	__asm __volatile("sldt %0" : "=r" (sel));
795 	return (sel);
796 }
797 
798 static __inline void
ltr(u_short sel)799 ltr(u_short sel)
800 {
801 	__asm __volatile("ltr %0" : : "r" (sel));
802 }
803 
804 static __inline uint32_t
read_tr(void)805 read_tr(void)
806 {
807 	u_short sel;
808 
809 	__asm __volatile("str %0" : "=r" (sel));
810 	return (sel);
811 }
812 
813 static __inline uint64_t
rdr0(void)814 rdr0(void)
815 {
816 	uint64_t data;
817 	__asm __volatile("movq %%dr0,%0" : "=r" (data));
818 	return (data);
819 }
820 
821 static __inline void
load_dr0(uint64_t dr0)822 load_dr0(uint64_t dr0)
823 {
824 	__asm __volatile("movq %0,%%dr0" : : "r" (dr0));
825 }
826 
827 static __inline uint64_t
rdr1(void)828 rdr1(void)
829 {
830 	uint64_t data;
831 	__asm __volatile("movq %%dr1,%0" : "=r" (data));
832 	return (data);
833 }
834 
835 static __inline void
load_dr1(uint64_t dr1)836 load_dr1(uint64_t dr1)
837 {
838 	__asm __volatile("movq %0,%%dr1" : : "r" (dr1));
839 }
840 
841 static __inline uint64_t
rdr2(void)842 rdr2(void)
843 {
844 	uint64_t data;
845 	__asm __volatile("movq %%dr2,%0" : "=r" (data));
846 	return (data);
847 }
848 
849 static __inline void
load_dr2(uint64_t dr2)850 load_dr2(uint64_t dr2)
851 {
852 	__asm __volatile("movq %0,%%dr2" : : "r" (dr2));
853 }
854 
855 static __inline uint64_t
rdr3(void)856 rdr3(void)
857 {
858 	uint64_t data;
859 	__asm __volatile("movq %%dr3,%0" : "=r" (data));
860 	return (data);
861 }
862 
863 static __inline void
load_dr3(uint64_t dr3)864 load_dr3(uint64_t dr3)
865 {
866 	__asm __volatile("movq %0,%%dr3" : : "r" (dr3));
867 }
868 
869 static __inline uint64_t
rdr6(void)870 rdr6(void)
871 {
872 	uint64_t data;
873 	__asm __volatile("movq %%dr6,%0" : "=r" (data));
874 	return (data);
875 }
876 
877 static __inline void
load_dr6(uint64_t dr6)878 load_dr6(uint64_t dr6)
879 {
880 	__asm __volatile("movq %0,%%dr6" : : "r" (dr6));
881 }
882 
883 static __inline uint64_t
rdr7(void)884 rdr7(void)
885 {
886 	uint64_t data;
887 	__asm __volatile("movq %%dr7,%0" : "=r" (data));
888 	return (data);
889 }
890 
891 static __inline void
load_dr7(uint64_t dr7)892 load_dr7(uint64_t dr7)
893 {
894 	__asm __volatile("movq %0,%%dr7" : : "r" (dr7));
895 }
896 
897 static __inline register_t
intr_disable(void)898 intr_disable(void)
899 {
900 	register_t rflags;
901 
902 	rflags = read_rflags();
903 	disable_intr();
904 	return (rflags);
905 }
906 
907 static __inline void
intr_restore(register_t rflags)908 intr_restore(register_t rflags)
909 {
910 	write_rflags(rflags);
911 }
912 
913 static __inline void
stac(void)914 stac(void)
915 {
916 
917 	__asm __volatile("stac" : : : "cc");
918 }
919 
920 static __inline void
clac(void)921 clac(void)
922 {
923 
924 	__asm __volatile("clac" : : : "cc");
925 }
926 
927 enum {
928 	SGX_ECREATE	= 0x0,
929 	SGX_EADD	= 0x1,
930 	SGX_EINIT	= 0x2,
931 	SGX_EREMOVE	= 0x3,
932 	SGX_EDGBRD	= 0x4,
933 	SGX_EDGBWR	= 0x5,
934 	SGX_EEXTEND	= 0x6,
935 	SGX_ELDU	= 0x8,
936 	SGX_EBLOCK	= 0x9,
937 	SGX_EPA		= 0xA,
938 	SGX_EWB		= 0xB,
939 	SGX_ETRACK	= 0xC,
940 };
941 
942 enum {
943 	SGX_PT_SECS = 0x00,
944 	SGX_PT_TCS  = 0x01,
945 	SGX_PT_REG  = 0x02,
946 	SGX_PT_VA   = 0x03,
947 	SGX_PT_TRIM = 0x04,
948 };
949 
950 int sgx_encls(uint32_t eax, uint64_t rbx, uint64_t rcx, uint64_t rdx);
951 
952 static __inline int
sgx_ecreate(void * pginfo,void * secs)953 sgx_ecreate(void *pginfo, void *secs)
954 {
955 
956 	return (sgx_encls(SGX_ECREATE, (uint64_t)pginfo,
957 	    (uint64_t)secs, 0));
958 }
959 
960 static __inline int
sgx_eadd(void * pginfo,void * epc)961 sgx_eadd(void *pginfo, void *epc)
962 {
963 
964 	return (sgx_encls(SGX_EADD, (uint64_t)pginfo,
965 	    (uint64_t)epc, 0));
966 }
967 
968 static __inline int
sgx_einit(void * sigstruct,void * secs,void * einittoken)969 sgx_einit(void *sigstruct, void *secs, void *einittoken)
970 {
971 
972 	return (sgx_encls(SGX_EINIT, (uint64_t)sigstruct,
973 	    (uint64_t)secs, (uint64_t)einittoken));
974 }
975 
976 static __inline int
sgx_eextend(void * secs,void * epc)977 sgx_eextend(void *secs, void *epc)
978 {
979 
980 	return (sgx_encls(SGX_EEXTEND, (uint64_t)secs,
981 	    (uint64_t)epc, 0));
982 }
983 
984 static __inline int
sgx_epa(void * epc)985 sgx_epa(void *epc)
986 {
987 
988 	return (sgx_encls(SGX_EPA, SGX_PT_VA, (uint64_t)epc, 0));
989 }
990 
991 static __inline int
sgx_eldu(uint64_t rbx,uint64_t rcx,uint64_t rdx)992 sgx_eldu(uint64_t rbx, uint64_t rcx,
993     uint64_t rdx)
994 {
995 
996 	return (sgx_encls(SGX_ELDU, rbx, rcx, rdx));
997 }
998 
999 static __inline int
sgx_eremove(void * epc)1000 sgx_eremove(void *epc)
1001 {
1002 
1003 	return (sgx_encls(SGX_EREMOVE, 0, (uint64_t)epc, 0));
1004 }
1005 
1006 #else /* !(__GNUCLIKE_ASM && __CC_SUPPORTS___INLINE) */
1007 
1008 int	breakpoint(void);
1009 u_int	bsfl(u_int mask);
1010 u_int	bsrl(u_int mask);
1011 void	clflush(u_long addr);
1012 void	clts(void);
1013 void	cpuid_count(u_int ax, u_int cx, u_int *p);
1014 void	disable_intr(void);
1015 void	do_cpuid(u_int ax, u_int *p);
1016 void	enable_intr(void);
1017 void	halt(void);
1018 void	ia32_pause(void);
1019 u_char	inb(u_int port);
1020 u_int	inl(u_int port);
1021 void	insb(u_int port, void *addr, size_t count);
1022 void	insl(u_int port, void *addr, size_t count);
1023 void	insw(u_int port, void *addr, size_t count);
1024 register_t	intr_disable(void);
1025 void	intr_restore(register_t rf);
1026 void	invd(void);
1027 void	invlpg(u_int addr);
1028 void	invltlb(void);
1029 u_short	inw(u_int port);
1030 void	lidt(struct region_descriptor *addr);
1031 void	lldt(u_short sel);
1032 void	load_cr0(u_long cr0);
1033 void	load_cr3(u_long cr3);
1034 void	load_cr4(u_long cr4);
1035 void	load_dr0(uint64_t dr0);
1036 void	load_dr1(uint64_t dr1);
1037 void	load_dr2(uint64_t dr2);
1038 void	load_dr3(uint64_t dr3);
1039 void	load_dr6(uint64_t dr6);
1040 void	load_dr7(uint64_t dr7);
1041 void	load_fs(u_short sel);
1042 void	load_gs(u_short sel);
1043 void	ltr(u_short sel);
1044 void	outb(u_int port, u_char data);
1045 void	outl(u_int port, u_int data);
1046 void	outsb(u_int port, const void *addr, size_t count);
1047 void	outsl(u_int port, const void *addr, size_t count);
1048 void	outsw(u_int port, const void *addr, size_t count);
1049 void	outw(u_int port, u_short data);
1050 u_long	rcr0(void);
1051 u_long	rcr2(void);
1052 u_long	rcr3(void);
1053 u_long	rcr4(void);
1054 uint64_t rdmsr(u_int msr);
1055 uint32_t rdmsr32(u_int msr);
1056 uint64_t rdpmc(u_int pmc);
1057 uint64_t rdr0(void);
1058 uint64_t rdr1(void);
1059 uint64_t rdr2(void);
1060 uint64_t rdr3(void);
1061 uint64_t rdr6(void);
1062 uint64_t rdr7(void);
1063 uint64_t rdtsc(void);
1064 u_long	read_rflags(void);
1065 u_int	rfs(void);
1066 u_int	rgs(void);
1067 void	wbinvd(void);
1068 void	write_rflags(u_int rf);
1069 void	wrmsr(u_int msr, uint64_t newval);
1070 
1071 #endif	/* __GNUCLIKE_ASM && __CC_SUPPORTS___INLINE */
1072 
1073 void	reset_dbregs(void);
1074 
1075 #ifdef _KERNEL
1076 int	rdmsr_safe(u_int msr, uint64_t *val);
1077 int	wrmsr_safe(u_int msr, uint64_t newval);
1078 #endif
1079 
1080 #endif /* !_MACHINE_CPUFUNC_H_ */
1081