2 * Copyright (c) 2003 Peter Wemm.
3 * Copyright (c) 1993 The Regents of the University of California.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 4. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Functions to provide access to special i386 instructions.
35 * This in included in sys/systm.h, and that file should be
36 * used in preference to this.
39 #ifndef _MACHINE_CPUFUNC_H_
40 #define _MACHINE_CPUFUNC_H_
43 #error this file needs sys/cdefs.h as a prerequisite
46 struct region_descriptor;
48 #define readb(va) (*(volatile uint8_t *) (va))
49 #define readw(va) (*(volatile uint16_t *) (va))
50 #define readl(va) (*(volatile uint32_t *) (va))
51 #define readq(va) (*(volatile uint64_t *) (va))
53 #define writeb(va, d) (*(volatile uint8_t *) (va) = (d))
54 #define writew(va, d) (*(volatile uint16_t *) (va) = (d))
55 #define writel(va, d) (*(volatile uint32_t *) (va) = (d))
56 #define writeq(va, d) (*(volatile uint64_t *) (va) = (d))
58 #if defined(__GNUCLIKE_ASM) && defined(__CC_SUPPORTS___INLINE)
63 __asm __volatile("int $3");
71 __asm __volatile("bsfl %1,%0" : "=r" (result) : "rm" (mask));
75 static __inline u_long
80 __asm __volatile("bsfq %1,%0" : "=r" (result) : "rm" (mask));
89 __asm __volatile("bsrl %1,%0" : "=r" (result) : "rm" (mask));
93 static __inline u_long
98 __asm __volatile("bsrq %1,%0" : "=r" (result) : "rm" (mask));
106 __asm __volatile("clflush %0" : : "m" (*(char *)addr));
113 __asm __volatile("clts");
119 __asm __volatile("cli" : : : "memory");
123 do_cpuid(u_int ax, u_int *p)
125 __asm __volatile("cpuid"
126 : "=a" (p[0]), "=b" (p[1]), "=c" (p[2]), "=d" (p[3])
131 cpuid_count(u_int ax, u_int cx, u_int *p)
133 __asm __volatile("cpuid"
134 : "=a" (p[0]), "=b" (p[1]), "=c" (p[2]), "=d" (p[3])
135 : "0" (ax), "c" (cx));
141 __asm __volatile("sti");
146 #define HAVE_INLINE_FFS
147 #define ffs(x) __builtin_ffs(x)
149 #define HAVE_INLINE_FFSL
154 return (mask == 0 ? mask : (int)bsfq((u_long)mask) + 1);
157 #define HAVE_INLINE_FLS
162 return (mask == 0 ? mask : (int)bsrl((u_int)mask) + 1);
165 #define HAVE_INLINE_FLSL
170 return (mask == 0 ? mask : (int)bsrq((u_long)mask) + 1);
178 __asm __volatile("hlt");
181 static __inline u_char
186 __asm __volatile("inb %w1, %0" : "=a" (data) : "Nd" (port));
190 static __inline u_int
195 __asm __volatile("inl %w1, %0" : "=a" (data) : "Nd" (port));
200 insb(u_int port, void *addr, size_t count)
202 __asm __volatile("cld; rep; insb"
203 : "+D" (addr), "+c" (count)
209 insw(u_int port, void *addr, size_t count)
211 __asm __volatile("cld; rep; insw"
212 : "+D" (addr), "+c" (count)
218 insl(u_int port, void *addr, size_t count)
220 __asm __volatile("cld; rep; insl"
221 : "+D" (addr), "+c" (count)
229 __asm __volatile("invd");
232 static __inline u_short
237 __asm __volatile("inw %w1, %0" : "=a" (data) : "Nd" (port));
242 outb(u_int port, u_char data)
244 __asm __volatile("outb %0, %w1" : : "a" (data), "Nd" (port));
248 outl(u_int port, u_int data)
250 __asm __volatile("outl %0, %w1" : : "a" (data), "Nd" (port));
254 outsb(u_int port, const void *addr, size_t count)
256 __asm __volatile("cld; rep; outsb"
257 : "+S" (addr), "+c" (count)
262 outsw(u_int port, const void *addr, size_t count)
264 __asm __volatile("cld; rep; outsw"
265 : "+S" (addr), "+c" (count)
270 outsl(u_int port, const void *addr, size_t count)
272 __asm __volatile("cld; rep; outsl"
273 : "+S" (addr), "+c" (count)
278 outw(u_int port, u_short data)
280 __asm __volatile("outw %0, %w1" : : "a" (data), "Nd" (port));
283 static __inline u_long
288 __asm __volatile("popcntq %1,%0" : "=r" (result) : "rm" (mask));
296 __asm __volatile("lfence" : : : "memory");
303 __asm __volatile("mfence" : : : "memory");
309 __asm __volatile("pause");
312 static __inline u_long
317 __asm __volatile("pushfq; popq %0" : "=r" (rf));
321 static __inline uint64_t
326 __asm __volatile("rdmsr" : "=a" (low), "=d" (high) : "c" (msr));
327 return (low | ((uint64_t)high << 32));
330 static __inline uint64_t
335 __asm __volatile("rdpmc" : "=a" (low), "=d" (high) : "c" (pmc));
336 return (low | ((uint64_t)high << 32));
339 static __inline uint64_t
344 __asm __volatile("rdtsc" : "=a" (low), "=d" (high));
345 return (low | ((uint64_t)high << 32));
348 static __inline uint32_t
353 __asm __volatile("rdtsc" : "=a" (rv) : : "edx");
360 __asm __volatile("wbinvd");
364 write_rflags(u_long rf)
366 __asm __volatile("pushq %0; popfq" : : "r" (rf));
370 wrmsr(u_int msr, uint64_t newval)
376 __asm __volatile("wrmsr" : : "a" (low), "d" (high), "c" (msr));
380 load_cr0(u_long data)
383 __asm __volatile("movq %0,%%cr0" : : "r" (data));
386 static __inline u_long
391 __asm __volatile("movq %%cr0,%0" : "=r" (data));
395 static __inline u_long
400 __asm __volatile("movq %%cr2,%0" : "=r" (data));
405 load_cr3(u_long data)
408 __asm __volatile("movq %0,%%cr3" : : "r" (data) : "memory");
411 static __inline u_long
416 __asm __volatile("movq %%cr3,%0" : "=r" (data));
421 load_cr4(u_long data)
423 __asm __volatile("movq %0,%%cr4" : : "r" (data));
426 static __inline u_long
431 __asm __volatile("movq %%cr4,%0" : "=r" (data));
435 static __inline u_long
440 __asm __volatile("xgetbv" : "=a" (low), "=d" (high) : "c" (reg));
441 return (low | ((uint64_t)high << 32));
445 load_xcr(u_int reg, u_long val)
451 __asm __volatile("xsetbv" : : "c" (reg), "a" (low), "d" (high));
455 * Global TLB flush (except for thise for pages marked PG_G)
465 * TLB flush for an individual page (even if it has PG_G).
466 * Only works on 486+ CPUs (i386 does not have PG_G).
472 __asm __volatile("invlpg %0" : : "m" (*(char *)addr) : "memory");
475 static __inline u_short
479 __asm __volatile("movw %%fs,%0" : "=rm" (sel));
483 static __inline u_short
487 __asm __volatile("movw %%gs,%0" : "=rm" (sel));
491 static __inline u_short
495 __asm __volatile("movw %%ss,%0" : "=rm" (sel));
502 __asm __volatile("movw %0,%%ds" : : "rm" (sel));
508 __asm __volatile("movw %0,%%es" : : "rm" (sel));
512 cpu_monitor(const void *addr, u_long extensions, u_int hints)
515 __asm __volatile("monitor"
516 : : "a" (addr), "c" (extensions), "d" (hints));
520 cpu_mwait(u_long extensions, u_int hints)
523 __asm __volatile("mwait" : : "a" (hints), "c" (extensions));
527 /* This is defined in <machine/specialreg.h> but is too painful to get to */
529 #define MSR_FSBASE 0xc0000100
534 /* Preserve the fsbase value across the selector load */
535 __asm __volatile("rdmsr; movw %0,%%fs; wrmsr"
536 : : "rm" (sel), "c" (MSR_FSBASE) : "eax", "edx");
540 #define MSR_GSBASE 0xc0000101
546 * Preserve the gsbase value across the selector load.
547 * Note that we have to disable interrupts because the gsbase
548 * being trashed happens to be the kernel gsbase at the time.
550 __asm __volatile("pushfq; cli; rdmsr; movw %0,%%gs; wrmsr; popfq"
551 : : "rm" (sel), "c" (MSR_GSBASE) : "eax", "edx");
554 /* Usable by userland */
558 __asm __volatile("movw %0,%%fs" : : "rm" (sel));
564 __asm __volatile("movw %0,%%gs" : : "rm" (sel));
569 lidt(struct region_descriptor *addr)
571 __asm __volatile("lidt (%0)" : : "r" (addr));
577 __asm __volatile("lldt %0" : : "r" (sel));
583 __asm __volatile("ltr %0" : : "r" (sel));
586 static __inline uint64_t
590 __asm __volatile("movq %%dr0,%0" : "=r" (data));
595 load_dr0(uint64_t dr0)
597 __asm __volatile("movq %0,%%dr0" : : "r" (dr0));
600 static __inline uint64_t
604 __asm __volatile("movq %%dr1,%0" : "=r" (data));
609 load_dr1(uint64_t dr1)
611 __asm __volatile("movq %0,%%dr1" : : "r" (dr1));
614 static __inline uint64_t
618 __asm __volatile("movq %%dr2,%0" : "=r" (data));
623 load_dr2(uint64_t dr2)
625 __asm __volatile("movq %0,%%dr2" : : "r" (dr2));
628 static __inline uint64_t
632 __asm __volatile("movq %%dr3,%0" : "=r" (data));
637 load_dr3(uint64_t dr3)
639 __asm __volatile("movq %0,%%dr3" : : "r" (dr3));
642 static __inline uint64_t
646 __asm __volatile("movq %%dr4,%0" : "=r" (data));
651 load_dr4(uint64_t dr4)
653 __asm __volatile("movq %0,%%dr4" : : "r" (dr4));
656 static __inline uint64_t
660 __asm __volatile("movq %%dr5,%0" : "=r" (data));
665 load_dr5(uint64_t dr5)
667 __asm __volatile("movq %0,%%dr5" : : "r" (dr5));
670 static __inline uint64_t
674 __asm __volatile("movq %%dr6,%0" : "=r" (data));
679 load_dr6(uint64_t dr6)
681 __asm __volatile("movq %0,%%dr6" : : "r" (dr6));
684 static __inline uint64_t
688 __asm __volatile("movq %%dr7,%0" : "=r" (data));
693 load_dr7(uint64_t dr7)
695 __asm __volatile("movq %0,%%dr7" : : "r" (dr7));
698 static __inline register_t
703 rflags = read_rflags();
709 intr_restore(register_t rflags)
711 write_rflags(rflags);
714 #else /* !(__GNUCLIKE_ASM && __CC_SUPPORTS___INLINE) */
716 int breakpoint(void);
717 u_int bsfl(u_int mask);
718 u_int bsrl(u_int mask);
719 void clflush(u_long addr);
721 void cpuid_count(u_int ax, u_int cx, u_int *p);
722 void disable_intr(void);
723 void do_cpuid(u_int ax, u_int *p);
724 void enable_intr(void);
726 void ia32_pause(void);
727 u_char inb(u_int port);
728 u_int inl(u_int port);
729 void insb(u_int port, void *addr, size_t count);
730 void insl(u_int port, void *addr, size_t count);
731 void insw(u_int port, void *addr, size_t count);
732 register_t intr_disable(void);
733 void intr_restore(register_t rf);
735 void invlpg(u_int addr);
737 u_short inw(u_int port);
738 void lidt(struct region_descriptor *addr);
739 void lldt(u_short sel);
740 void load_cr0(u_long cr0);
741 void load_cr3(u_long cr3);
742 void load_cr4(u_long cr4);
743 void load_dr0(uint64_t dr0);
744 void load_dr1(uint64_t dr1);
745 void load_dr2(uint64_t dr2);
746 void load_dr3(uint64_t dr3);
747 void load_dr4(uint64_t dr4);
748 void load_dr5(uint64_t dr5);
749 void load_dr6(uint64_t dr6);
750 void load_dr7(uint64_t dr7);
751 void load_fs(u_short sel);
752 void load_gs(u_short sel);
753 void ltr(u_short sel);
754 void outb(u_int port, u_char data);
755 void outl(u_int port, u_int data);
756 void outsb(u_int port, const void *addr, size_t count);
757 void outsl(u_int port, const void *addr, size_t count);
758 void outsw(u_int port, const void *addr, size_t count);
759 void outw(u_int port, u_short data);
764 uint64_t rdmsr(u_int msr);
765 uint64_t rdpmc(u_int pmc);
774 uint64_t rdtsc(void);
775 u_int read_rflags(void);
779 void write_rflags(u_int rf);
780 void wrmsr(u_int msr, uint64_t newval);
782 #endif /* __GNUCLIKE_ASM && __CC_SUPPORTS___INLINE */
784 void reset_dbregs(void);
787 int rdmsr_safe(u_int msr, uint64_t *val);
788 int wrmsr_safe(u_int msr, uint64_t newval);
791 #endif /* !_MACHINE_CPUFUNC_H_ */