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 cpu_monitor(const void *addr
, int extensions
, int hints
)
520 __asm
__volatile("monitor;"
521 : :"a" (addr
), "c" (extensions
), "d"(hints
));
525 cpu_mwait(int extensions
, int hints
)
527 __asm
__volatile("mwait;" : :"a" (hints
), "c" (extensions
));
531 /* This is defined in <machine/specialreg.h> but is too painful to get to */
533 #define MSR_FSBASE 0xc0000100
538 register u_int32_t fsbase
__asm("ecx");
540 /* Preserve the fsbase value across the selector load */
542 __asm
__volatile("rdmsr; movl %0,%%fs; wrmsr"
543 : : "rm" (sel
), "c" (fsbase
) : "eax", "edx");
547 #define MSR_GSBASE 0xc0000101
552 register u_int32_t gsbase
__asm("ecx");
555 * Preserve the gsbase value across the selector load.
556 * Note that we have to disable interrupts because the gsbase
557 * being trashed happens to be the kernel gsbase at the time.
560 __asm
__volatile("pushfq; cli; rdmsr; movl %0,%%gs; wrmsr; popfq"
561 : : "rm" (sel
), "c" (gsbase
) : "eax", "edx");
564 /* Usable by userland */
568 __asm
__volatile("movl %0,%%fs" : : "rm" (sel
));
574 __asm
__volatile("movl %0,%%gs" : : "rm" (sel
));
579 lidt(struct region_descriptor
*addr
)
581 __asm
__volatile("lidt (%0)" : : "r" (addr
));
584 static __inline u_short
588 __asm
__volatile("sldt %0" : "=g" (ldt
));
595 __asm
__volatile("lldt %0" : : "r" (sel
));
601 __asm
__volatile("ltr %0" : : "r" (sel
));
604 static __inline u_int64_t
608 __asm
__volatile("movq %%dr0,%0" : "=r" (data
));
613 load_dr0(u_int64_t dr0
)
615 __asm
__volatile("movq %0,%%dr0" : : "r" (dr0
));
618 static __inline u_int64_t
622 __asm
__volatile("movq %%dr1,%0" : "=r" (data
));
627 load_dr1(u_int64_t dr1
)
629 __asm
__volatile("movq %0,%%dr1" : : "r" (dr1
));
632 static __inline u_int64_t
636 __asm
__volatile("movq %%dr2,%0" : "=r" (data
));
641 load_dr2(u_int64_t dr2
)
643 __asm
__volatile("movq %0,%%dr2" : : "r" (dr2
));
646 static __inline u_int64_t
650 __asm
__volatile("movq %%dr3,%0" : "=r" (data
));
655 load_dr3(u_int64_t dr3
)
657 __asm
__volatile("movq %0,%%dr3" : : "r" (dr3
));
660 static __inline u_int64_t
664 __asm
__volatile("movq %%dr4,%0" : "=r" (data
));
669 load_dr4(u_int64_t dr4
)
671 __asm
__volatile("movq %0,%%dr4" : : "r" (dr4
));
674 static __inline u_int64_t
678 __asm
__volatile("movq %%dr5,%0" : "=r" (data
));
683 load_dr5(u_int64_t dr5
)
685 __asm
__volatile("movq %0,%%dr5" : : "r" (dr5
));
688 static __inline u_int64_t
692 __asm
__volatile("movq %%dr6,%0" : "=r" (data
));
697 load_dr6(u_int64_t dr6
)
699 __asm
__volatile("movq %0,%%dr6" : : "r" (dr6
));
702 static __inline u_int64_t
706 __asm
__volatile("movq %%dr7,%0" : "=r" (data
));
711 load_dr7(u_int64_t dr7
)
713 __asm
__volatile("movq %0,%%dr7" : : "r" (dr7
));
716 static __inline register_t
721 rflags
= read_rflags();
727 intr_restore(register_t rflags
)
729 write_rflags(rflags
);
732 #else /* !(__GNUCLIKE_ASM && __CC_SUPPORTS___INLINE) */
734 int breakpoint(void);
735 u_int
bsfl(u_int mask
);
736 u_int
bsrl(u_int mask
);
737 void disable_intr(void);
738 void do_cpuid(u_int ax
, u_int
*p
);
739 void enable_intr(void);
741 void ia32_pause(void);
742 u_char
inb(u_int port
);
743 u_int
inl(u_int port
);
744 void insb(u_int port
, void *addr
, size_t cnt
);
745 void insl(u_int port
, void *addr
, size_t cnt
);
746 void insw(u_int port
, void *addr
, size_t cnt
);
747 register_t
intr_disable(void);
748 void intr_restore(register_t rf
);
750 void invlpg(u_int addr
);
752 u_short
inw(u_int port
);
753 void lidt(struct region_descriptor
*addr
);
754 void lldt(u_short sel
);
755 void load_cr0(u_long cr0
);
756 void load_cr3(u_long cr3
);
757 void load_cr4(u_long cr4
);
758 void load_dr0(u_int64_t dr0
);
759 void load_dr1(u_int64_t dr1
);
760 void load_dr2(u_int64_t dr2
);
761 void load_dr3(u_int64_t dr3
);
762 void load_dr4(u_int64_t dr4
);
763 void load_dr5(u_int64_t dr5
);
764 void load_dr6(u_int64_t dr6
);
765 void load_dr7(u_int64_t dr7
);
766 void load_fs(u_int sel
);
767 void load_gs(u_int sel
);
768 void ltr(u_short sel
);
769 void outb(u_int port
, u_char data
);
770 void outl(u_int port
, u_int data
);
771 void outsb(u_int port
, const void *addr
, size_t cnt
);
772 void outsl(u_int port
, const void *addr
, size_t cnt
);
773 void outsw(u_int port
, const void *addr
, size_t cnt
);
774 void outw(u_int port
, u_short data
);
779 u_int64_t
rdmsr(u_int msr
);
780 u_int64_t
rdpmc(u_int pmc
);
781 u_int64_t
rdr0(void);
782 u_int64_t
rdr1(void);
783 u_int64_t
rdr2(void);
784 u_int64_t
rdr3(void);
785 u_int64_t
rdr4(void);
786 u_int64_t
rdr5(void);
787 u_int64_t
rdr6(void);
788 u_int64_t
rdr7(void);
789 u_int64_t
rdtsc(void);
790 u_int
read_rflags(void);
794 void write_rflags(u_int rf
);
795 void wrmsr(u_int msr
, u_int64_t newval
);
797 #endif /* __GNUCLIKE_ASM && __CC_SUPPORTS___INLINE */
799 void reset_dbregs(void);
802 int rdmsr_safe(u_int msr
, uint64_t *val
);
803 int wrmsr_safe(u_int msr
, uint64_t newval
);
806 #endif /* !_MACHINE_CPUFUNC_H_ */