2 * Copyright (c) 1993 The Regents of the University of California.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * Functions to provide access to special i386 instructions.
34 * This in included in sys/systm.h, and that file should be
35 * used in preference to this.
38 #ifndef _MACHINE_CPUFUNC_H_
39 #define _MACHINE_CPUFUNC_H_
42 #error this file needs sys/cdefs.h as a prerequisite
45 struct region_descriptor;
47 #define readb(va) (*(volatile uint8_t *) (va))
48 #define readw(va) (*(volatile uint16_t *) (va))
49 #define readl(va) (*(volatile uint32_t *) (va))
51 #define writeb(va, d) (*(volatile uint8_t *) (va) = (d))
52 #define writew(va, d) (*(volatile uint16_t *) (va) = (d))
53 #define writel(va, d) (*(volatile uint32_t *) (va) = (d))
55 #if defined(__GNUCLIKE_ASM) && defined(__CC_SUPPORTS___INLINE)
60 __asm __volatile("int $3");
68 __asm("bsfl %1,%0" : "=r" (result) : "rm" (mask) : "cc");
77 __asm("bsrl %1,%0" : "=r" (result) : "rm" (mask) : "cc");
85 __asm __volatile("clflush %0" : : "m" (*(char *)addr));
89 clflushopt(u_long addr)
92 __asm __volatile(".byte 0x66;clflush %0" : : "m" (*(char *)addr));
99 __asm __volatile("clts");
106 __asm __volatile("cli" : : : "memory");
110 do_cpuid(u_int ax, u_int *p)
112 __asm __volatile("cpuid"
113 : "=a" (p[0]), "=b" (p[1]), "=c" (p[2]), "=d" (p[3])
118 cpuid_count(u_int ax, u_int cx, u_int *p)
120 __asm __volatile("cpuid"
121 : "=a" (p[0]), "=b" (p[1]), "=c" (p[2]), "=d" (p[3])
122 : "0" (ax), "c" (cx));
129 __asm __volatile("sti");
133 cpu_monitor(const void *addr, u_long extensions, u_int hints)
136 __asm __volatile("monitor"
137 : : "a" (addr), "c" (extensions), "d" (hints));
141 cpu_mwait(u_long extensions, u_int hints)
144 __asm __volatile("mwait" : : "a" (hints), "c" (extensions));
151 __asm __volatile("lfence" : : : "memory");
158 __asm __volatile("mfence" : : : "memory");
165 __asm __volatile("sfence" : : : "memory");
170 #define HAVE_INLINE_FFS
176 * Note that gcc-2's builtin ffs would be used if we didn't declare
177 * this inline or turn off the builtin. The builtin is faster but
178 * broken in gcc-2.4.5 and slower but working in gcc-2.5 and later
181 return (mask == 0 ? mask : (int)bsfl((u_int)mask) + 1);
184 #define HAVE_INLINE_FFSL
189 return (ffs((int)mask));
192 #define HAVE_INLINE_FLS
197 return (mask == 0 ? mask : (int)bsrl((u_int)mask) + 1);
200 #define HAVE_INLINE_FLSL
205 return (fls((int)mask));
213 __asm __volatile("hlt");
216 static __inline u_char
221 __asm __volatile("inb %w1, %0" : "=a" (data) : "Nd" (port));
225 static __inline u_int
230 __asm __volatile("inl %w1, %0" : "=a" (data) : "Nd" (port));
235 insb(u_int port, void *addr, size_t count)
237 __asm __volatile("cld; rep; insb"
238 : "+D" (addr), "+c" (count)
244 insw(u_int port, void *addr, size_t count)
246 __asm __volatile("cld; rep; insw"
247 : "+D" (addr), "+c" (count)
253 insl(u_int port, void *addr, size_t count)
255 __asm __volatile("cld; rep; insl"
256 : "+D" (addr), "+c" (count)
264 __asm __volatile("invd");
267 static __inline u_short
272 __asm __volatile("inw %w1, %0" : "=a" (data) : "Nd" (port));
277 outb(u_int port, u_char data)
279 __asm __volatile("outb %0, %w1" : : "a" (data), "Nd" (port));
283 outl(u_int port, u_int data)
285 __asm __volatile("outl %0, %w1" : : "a" (data), "Nd" (port));
289 outsb(u_int port, const void *addr, size_t count)
291 __asm __volatile("cld; rep; outsb"
292 : "+S" (addr), "+c" (count)
297 outsw(u_int port, const void *addr, size_t count)
299 __asm __volatile("cld; rep; outsw"
300 : "+S" (addr), "+c" (count)
305 outsl(u_int port, const void *addr, size_t count)
307 __asm __volatile("cld; rep; outsl"
308 : "+S" (addr), "+c" (count)
313 outw(u_int port, u_short data)
315 __asm __volatile("outw %0, %w1" : : "a" (data), "Nd" (port));
321 __asm __volatile("pause");
324 static __inline u_int
329 __asm __volatile("pushfl; popl %0" : "=r" (ef));
333 static __inline uint64_t
338 __asm __volatile("rdmsr" : "=A" (rv) : "c" (msr));
342 static __inline uint32_t
347 __asm __volatile("rdmsr" : "=a" (low) : "c" (msr) : "edx");
351 static __inline uint64_t
356 __asm __volatile("rdpmc" : "=A" (rv) : "c" (pmc));
360 static __inline uint64_t
365 __asm __volatile("rdtsc" : "=A" (rv));
369 static __inline uint32_t
374 __asm __volatile("rdtsc" : "=a" (rv) : : "edx");
381 __asm __volatile("wbinvd");
385 write_eflags(u_int ef)
387 __asm __volatile("pushl %0; popfl" : : "r" (ef));
391 wrmsr(u_int msr, uint64_t newval)
393 __asm __volatile("wrmsr" : : "A" (newval), "c" (msr));
400 __asm __volatile("movl %0,%%cr0" : : "r" (data));
403 static __inline u_int
408 __asm __volatile("movl %%cr0,%0" : "=r" (data));
412 static __inline u_int
417 __asm __volatile("movl %%cr2,%0" : "=r" (data));
425 __asm __volatile("movl %0,%%cr3" : : "r" (data) : "memory");
428 static __inline u_int
433 __asm __volatile("movl %%cr3,%0" : "=r" (data));
440 __asm __volatile("movl %0,%%cr4" : : "r" (data));
443 static __inline u_int
448 __asm __volatile("movl %%cr4,%0" : "=r" (data));
452 static __inline uint64_t
457 __asm __volatile("xgetbv" : "=a" (low), "=d" (high) : "c" (reg));
458 return (low | ((uint64_t)high << 32));
462 load_xcr(u_int reg, uint64_t val)
468 __asm __volatile("xsetbv" : : "c" (reg), "a" (low), "d" (high));
472 * Global TLB flush (except for thise for pages marked PG_G)
482 * TLB flush for an individual page (even if it has PG_G).
483 * Only works on 486+ CPUs (i386 does not have PG_G).
489 __asm __volatile("invlpg %0" : : "m" (*(char *)addr) : "memory");
492 static __inline u_short
496 __asm __volatile("movw %%fs,%0" : "=rm" (sel));
500 static __inline uint64_t
504 __asm __volatile("sgdt %0" : "=m" (gdtr));
508 static __inline u_short
512 __asm __volatile("movw %%gs,%0" : "=rm" (sel));
516 static __inline uint64_t
520 __asm __volatile("sidt %0" : "=m" (idtr));
524 static __inline u_short
528 __asm __volatile("sldt %0" : "=g" (ldtr));
532 static __inline u_short
536 __asm __volatile("movw %%ss,%0" : "=rm" (sel));
540 static __inline u_short
544 __asm __volatile("str %0" : "=g" (tr));
551 __asm __volatile("movw %0,%%fs" : : "rm" (sel));
557 __asm __volatile("movw %0,%%gs" : : "rm" (sel));
561 lidt(struct region_descriptor *addr)
563 __asm __volatile("lidt (%0)" : : "r" (addr));
569 __asm __volatile("lldt %0" : : "r" (sel));
575 __asm __volatile("ltr %0" : : "r" (sel));
578 static __inline u_int
582 __asm __volatile("movl %%dr0,%0" : "=r" (data));
589 __asm __volatile("movl %0,%%dr0" : : "r" (dr0));
592 static __inline u_int
596 __asm __volatile("movl %%dr1,%0" : "=r" (data));
603 __asm __volatile("movl %0,%%dr1" : : "r" (dr1));
606 static __inline u_int
610 __asm __volatile("movl %%dr2,%0" : "=r" (data));
617 __asm __volatile("movl %0,%%dr2" : : "r" (dr2));
620 static __inline u_int
624 __asm __volatile("movl %%dr3,%0" : "=r" (data));
631 __asm __volatile("movl %0,%%dr3" : : "r" (dr3));
634 static __inline u_int
638 __asm __volatile("movl %%dr4,%0" : "=r" (data));
645 __asm __volatile("movl %0,%%dr4" : : "r" (dr4));
648 static __inline u_int
652 __asm __volatile("movl %%dr5,%0" : "=r" (data));
659 __asm __volatile("movl %0,%%dr5" : : "r" (dr5));
662 static __inline u_int
666 __asm __volatile("movl %%dr6,%0" : "=r" (data));
673 __asm __volatile("movl %0,%%dr6" : : "r" (dr6));
676 static __inline u_int
680 __asm __volatile("movl %%dr7,%0" : "=r" (data));
687 __asm __volatile("movl %0,%%dr7" : : "r" (dr7));
690 static __inline u_char
691 read_cyrix_reg(u_char reg)
698 write_cyrix_reg(u_char reg, u_char data)
704 static __inline register_t
709 eflags = read_eflags();
715 intr_restore(register_t eflags)
717 write_eflags(eflags);
720 #else /* !(__GNUCLIKE_ASM && __CC_SUPPORTS___INLINE) */
722 int breakpoint(void);
723 u_int bsfl(u_int mask);
724 u_int bsrl(u_int mask);
725 void clflush(u_long addr);
727 void cpuid_count(u_int ax, u_int cx, u_int *p);
728 void disable_intr(void);
729 void do_cpuid(u_int ax, u_int *p);
730 void enable_intr(void);
732 void ia32_pause(void);
733 u_char inb(u_int port);
734 u_int inl(u_int port);
735 void insb(u_int port, void *addr, size_t count);
736 void insl(u_int port, void *addr, size_t count);
737 void insw(u_int port, void *addr, size_t count);
738 register_t intr_disable(void);
739 void intr_restore(register_t ef);
741 void invlpg(u_int addr);
743 u_short inw(u_int port);
744 void lidt(struct region_descriptor *addr);
745 void lldt(u_short sel);
746 void load_cr0(u_int cr0);
747 void load_cr3(u_int cr3);
748 void load_cr4(u_int cr4);
749 void load_dr0(u_int dr0);
750 void load_dr1(u_int dr1);
751 void load_dr2(u_int dr2);
752 void load_dr3(u_int dr3);
753 void load_dr4(u_int dr4);
754 void load_dr5(u_int dr5);
755 void load_dr6(u_int dr6);
756 void load_dr7(u_int dr7);
757 void load_fs(u_short sel);
758 void load_gs(u_short sel);
759 void ltr(u_short sel);
760 void outb(u_int port, u_char data);
761 void outl(u_int port, u_int data);
762 void outsb(u_int port, const void *addr, size_t count);
763 void outsl(u_int port, const void *addr, size_t count);
764 void outsw(u_int port, const void *addr, size_t count);
765 void outw(u_int port, u_short data);
770 uint64_t rdmsr(u_int msr);
771 uint64_t rdpmc(u_int pmc);
780 uint64_t rdtsc(void);
781 u_char read_cyrix_reg(u_char reg);
782 u_int read_eflags(void);
790 void write_cyrix_reg(u_char reg, u_char data);
791 void write_eflags(u_int ef);
792 void wrmsr(u_int msr, uint64_t newval);
794 #endif /* __GNUCLIKE_ASM && __CC_SUPPORTS___INLINE */
796 void reset_dbregs(void);
799 int rdmsr_safe(u_int msr, uint64_t *val);
800 int wrmsr_safe(u_int msr, uint64_t newval);
803 #endif /* !_MACHINE_CPUFUNC_H_ */