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 u_int8_t *) (va))
49 #define readw(va) (*(volatile u_int16_t *) (va))
50 #define readl(va) (*(volatile u_int32_t *) (va))
51 #define readq(va) (*(volatile u_int64_t *) (va))
53 #define writeb(va, d) (*(volatile u_int8_t *) (va) = (d))
54 #define writew(va, d) (*(volatile u_int16_t *) (va) = (d))
55 #define writel(va, d) (*(volatile u_int32_t *) (va) = (d))
56 #define writeq(va, d) (*(volatile u_int64_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));
105 __asm __volatile("cli" : : : "memory");
109 do_cpuid(u_int ax, u_int *p)
111 __asm __volatile("cpuid"
112 : "=a" (p[0]), "=b" (p[1]), "=c" (p[2]), "=d" (p[3])
117 cpuid_count(u_int ax, u_int cx, u_int *p)
119 __asm __volatile("cpuid"
120 : "=a" (p[0]), "=b" (p[1]), "=c" (p[2]), "=d" (p[3])
121 : "0" (ax), "c" (cx));
127 __asm __volatile("sti");
132 #define HAVE_INLINE_FFS
133 #define ffs(x) __builtin_ffs(x)
135 #define HAVE_INLINE_FFSL
140 return (mask == 0 ? mask : (int)bsfq((u_long)mask) + 1);
143 #define HAVE_INLINE_FLS
148 return (mask == 0 ? mask : (int)bsrl((u_int)mask) + 1);
151 #define HAVE_INLINE_FLSL
156 return (mask == 0 ? mask : (int)bsrq((u_long)mask) + 1);
164 __asm __volatile("hlt");
167 #if !defined(__GNUCLIKE_BUILTIN_CONSTANT_P) || __GNUCLIKE_ASM < 3
169 #define inb(port) inbv(port)
170 #define outb(port, data) outbv(port, data)
172 #else /* __GNUCLIKE_BUILTIN_CONSTANT_P && __GNUCLIKE_ASM >= 3 */
175 * The following complications are to get around gcc not having a
176 * constraint letter for the range 0..255. We still put "d" in the
177 * constraint because "i" isn't a valid constraint when the port
178 * isn't constant. This only matters for -O0 because otherwise
179 * the non-working version gets optimized away.
181 * Use an expression-statement instead of a conditional expression
182 * because gcc-2.6.0 would promote the operands of the conditional
183 * and produce poor code for "if ((inb(var) & const1) == const2)".
185 * The unnecessary test `(port) < 0x10000' is to generate a warning if
186 * the `port' has type u_short or smaller. Such types are pessimal.
187 * This actually only works for signed types. The range check is
188 * careful to avoid generating warnings.
190 #define inb(port) __extension__ ({ \
192 if (__builtin_constant_p(port) && ((port) & 0xffff) < 0x100 \
193 && (port) < 0x10000) \
194 _data = inbc(port); \
196 _data = inbv(port); \
199 #define outb(port, data) ( \
200 __builtin_constant_p(port) && ((port) & 0xffff) < 0x100 \
201 && (port) < 0x10000 \
202 ? outbc(port, data) : outbv(port, data))
204 static __inline u_char
209 __asm __volatile("inb %1,%0" : "=a" (data) : "id" ((u_short)(port)));
214 outbc(u_int port, u_char data)
216 __asm __volatile("outb %0,%1" : : "a" (data), "id" ((u_short)(port)));
219 #endif /* __GNUCLIKE_BUILTIN_CONSTANT_P && __GNUCLIKE_ASM >= 3*/
221 static __inline u_char
226 * We use %%dx and not %1 here because i/o is done at %dx and not at
227 * %edx, while gcc generates inferior code (movw instead of movl)
228 * if we tell it to load (u_short) port.
230 __asm __volatile("inb %%dx,%0" : "=a" (data) : "d" (port));
234 static __inline u_int
239 __asm __volatile("inl %%dx,%0" : "=a" (data) : "d" (port));
244 insb(u_int port, void *addr, size_t cnt)
246 __asm __volatile("cld; rep; insb"
247 : "+D" (addr), "+c" (cnt)
253 insw(u_int port, void *addr, size_t cnt)
255 __asm __volatile("cld; rep; insw"
256 : "+D" (addr), "+c" (cnt)
262 insl(u_int port, void *addr, size_t cnt)
264 __asm __volatile("cld; rep; insl"
265 : "+D" (addr), "+c" (cnt)
273 __asm __volatile("invd");
276 static __inline u_short
281 __asm __volatile("inw %%dx,%0" : "=a" (data) : "d" (port));
286 outbv(u_int port, u_char data)
290 * Use an unnecessary assignment to help gcc's register allocator.
291 * This make a large difference for gcc-1.40 and a tiny difference
292 * for gcc-2.6.0. For gcc-1.40, al had to be ``asm("ax")'' for
293 * best results. gcc-2.6.0 can't handle this.
296 __asm __volatile("outb %0,%%dx" : : "a" (al), "d" (port));
300 outl(u_int port, u_int data)
303 * outl() and outw() aren't used much so we haven't looked at
304 * possible micro-optimizations such as the unnecessary
305 * assignment for them.
307 __asm __volatile("outl %0,%%dx" : : "a" (data), "d" (port));
311 outsb(u_int port, const void *addr, size_t cnt)
313 __asm __volatile("cld; rep; outsb"
314 : "+S" (addr), "+c" (cnt)
319 outsw(u_int port, const void *addr, size_t cnt)
321 __asm __volatile("cld; rep; outsw"
322 : "+S" (addr), "+c" (cnt)
327 outsl(u_int port, const void *addr, size_t cnt)
329 __asm __volatile("cld; rep; outsl"
330 : "+S" (addr), "+c" (cnt)
335 outw(u_int port, u_short data)
337 __asm __volatile("outw %0,%%dx" : : "a" (data), "d" (port));
343 __asm __volatile("pause");
346 static __inline u_long
351 __asm __volatile("pushfq; popq %0" : "=r" (rf));
355 static __inline u_int64_t
360 __asm __volatile("rdmsr" : "=a" (low), "=d" (high) : "c" (msr));
361 return (low | ((u_int64_t)high << 32));
364 static __inline u_int64_t
369 __asm __volatile("rdpmc" : "=a" (low), "=d" (high) : "c" (pmc));
370 return (low | ((u_int64_t)high << 32));
373 static __inline u_int64_t
378 __asm __volatile("rdtsc" : "=a" (low), "=d" (high));
379 return (low | ((u_int64_t)high << 32));
385 __asm __volatile("wbinvd");
389 write_rflags(u_long rf)
391 __asm __volatile("pushq %0; popfq" : : "r" (rf));
395 wrmsr(u_int msr, u_int64_t newval)
401 __asm __volatile("wrmsr" : : "a" (low), "d" (high), "c" (msr));
405 load_cr0(u_long data)
408 __asm __volatile("movq %0,%%cr0" : : "r" (data));
411 static __inline u_long
416 __asm __volatile("movq %%cr0,%0" : "=r" (data));
420 static __inline u_long
425 __asm __volatile("movq %%cr2,%0" : "=r" (data));
430 load_cr3(u_long data)
433 __asm __volatile("movq %0,%%cr3" : : "r" (data) : "memory");
436 static __inline u_long
441 __asm __volatile("movq %%cr3,%0" : "=r" (data));
446 load_cr4(u_long data)
448 __asm __volatile("movq %0,%%cr4" : : "r" (data));
451 static __inline u_long
456 __asm __volatile("movq %%cr4,%0" : "=r" (data));
461 * Global TLB flush (except for thise for pages marked PG_G)
471 * TLB flush for an individual page (even if it has PG_G).
472 * Only works on 486+ CPUs (i386 does not have PG_G).
478 __asm __volatile("invlpg %0" : : "m" (*(char *)addr) : "memory");
481 static __inline u_int
485 __asm __volatile("movl %%fs,%0" : "=rm" (sel));
489 static __inline u_int
493 __asm __volatile("movl %%gs,%0" : "=rm" (sel));
497 static __inline u_int
501 __asm __volatile("movl %%ss,%0" : "=rm" (sel));
508 __asm __volatile("movl %0,%%ds" : : "rm" (sel));
514 __asm __volatile("movl %0,%%es" : : "rm" (sel));
518 /* This is defined in <machine/specialreg.h> but is too painful to get to */
520 #define MSR_FSBASE 0xc0000100
525 register u_int32_t fsbase __asm("ecx");
527 /* Preserve the fsbase value across the selector load */
529 __asm __volatile("rdmsr; movl %0,%%fs; wrmsr"
530 : : "rm" (sel), "c" (fsbase) : "eax", "edx");
534 #define MSR_GSBASE 0xc0000101
539 register u_int32_t gsbase __asm("ecx");
542 * Preserve the gsbase value across the selector load.
543 * Note that we have to disable interrupts because the gsbase
544 * being trashed happens to be the kernel gsbase at the time.
547 __asm __volatile("pushfq; cli; rdmsr; movl %0,%%gs; wrmsr; popfq"
548 : : "rm" (sel), "c" (gsbase) : "eax", "edx");
551 /* Usable by userland */
555 __asm __volatile("movl %0,%%fs" : : "rm" (sel));
561 __asm __volatile("movl %0,%%gs" : : "rm" (sel));
566 lidt(struct region_descriptor *addr)
568 __asm __volatile("lidt (%0)" : : "r" (addr));
574 __asm __volatile("lldt %0" : : "r" (sel));
580 __asm __volatile("ltr %0" : : "r" (sel));
583 static __inline u_int64_t
587 __asm __volatile("movq %%dr0,%0" : "=r" (data));
592 load_dr0(u_int64_t dr0)
594 __asm __volatile("movq %0,%%dr0" : : "r" (dr0));
597 static __inline u_int64_t
601 __asm __volatile("movq %%dr1,%0" : "=r" (data));
606 load_dr1(u_int64_t dr1)
608 __asm __volatile("movq %0,%%dr1" : : "r" (dr1));
611 static __inline u_int64_t
615 __asm __volatile("movq %%dr2,%0" : "=r" (data));
620 load_dr2(u_int64_t dr2)
622 __asm __volatile("movq %0,%%dr2" : : "r" (dr2));
625 static __inline u_int64_t
629 __asm __volatile("movq %%dr3,%0" : "=r" (data));
634 load_dr3(u_int64_t dr3)
636 __asm __volatile("movq %0,%%dr3" : : "r" (dr3));
639 static __inline u_int64_t
643 __asm __volatile("movq %%dr4,%0" : "=r" (data));
648 load_dr4(u_int64_t dr4)
650 __asm __volatile("movq %0,%%dr4" : : "r" (dr4));
653 static __inline u_int64_t
657 __asm __volatile("movq %%dr5,%0" : "=r" (data));
662 load_dr5(u_int64_t dr5)
664 __asm __volatile("movq %0,%%dr5" : : "r" (dr5));
667 static __inline u_int64_t
671 __asm __volatile("movq %%dr6,%0" : "=r" (data));
676 load_dr6(u_int64_t dr6)
678 __asm __volatile("movq %0,%%dr6" : : "r" (dr6));
681 static __inline u_int64_t
685 __asm __volatile("movq %%dr7,%0" : "=r" (data));
690 load_dr7(u_int64_t dr7)
692 __asm __volatile("movq %0,%%dr7" : : "r" (dr7));
695 static __inline register_t
700 rflags = read_rflags();
706 intr_restore(register_t rflags)
708 write_rflags(rflags);
711 #else /* !(__GNUCLIKE_ASM && __CC_SUPPORTS___INLINE) */
713 int breakpoint(void);
714 u_int bsfl(u_int mask);
715 u_int bsrl(u_int mask);
716 void disable_intr(void);
717 void do_cpuid(u_int ax, u_int *p);
718 void enable_intr(void);
720 void ia32_pause(void);
721 u_char inb(u_int port);
722 u_int inl(u_int port);
723 void insb(u_int port, void *addr, size_t cnt);
724 void insl(u_int port, void *addr, size_t cnt);
725 void insw(u_int port, void *addr, size_t cnt);
726 register_t intr_disable(void);
727 void intr_restore(register_t rf);
729 void invlpg(u_int addr);
731 u_short inw(u_int port);
732 void lidt(struct region_descriptor *addr);
733 void lldt(u_short sel);
734 void load_cr0(u_long cr0);
735 void load_cr3(u_long cr3);
736 void load_cr4(u_long cr4);
737 void load_dr0(u_int64_t dr0);
738 void load_dr1(u_int64_t dr1);
739 void load_dr2(u_int64_t dr2);
740 void load_dr3(u_int64_t dr3);
741 void load_dr4(u_int64_t dr4);
742 void load_dr5(u_int64_t dr5);
743 void load_dr6(u_int64_t dr6);
744 void load_dr7(u_int64_t dr7);
745 void load_fs(u_int sel);
746 void load_gs(u_int sel);
747 void ltr(u_short sel);
748 void outb(u_int port, u_char data);
749 void outl(u_int port, u_int data);
750 void outsb(u_int port, const void *addr, size_t cnt);
751 void outsl(u_int port, const void *addr, size_t cnt);
752 void outsw(u_int port, const void *addr, size_t cnt);
753 void outw(u_int port, u_short data);
758 u_int64_t rdmsr(u_int msr);
759 u_int64_t rdpmc(u_int pmc);
760 u_int64_t rdr0(void);
761 u_int64_t rdr1(void);
762 u_int64_t rdr2(void);
763 u_int64_t rdr3(void);
764 u_int64_t rdr4(void);
765 u_int64_t rdr5(void);
766 u_int64_t rdr6(void);
767 u_int64_t rdr7(void);
768 u_int64_t rdtsc(void);
769 u_int read_rflags(void);
773 void write_rflags(u_int rf);
774 void wrmsr(u_int msr, u_int64_t newval);
776 #endif /* __GNUCLIKE_ASM && __CC_SUPPORTS___INLINE */
778 void reset_dbregs(void);
781 int rdmsr_safe(u_int msr, uint64_t *val);
782 int wrmsr_safe(u_int msr, uint64_t newval);
785 #endif /* !_MACHINE_CPUFUNC_H_ */