2 * Copyright (c) 2003 Peter Wemm.
3 * Copyright (c) 1993 The Regents of the University of California.
4 * Copyright (c) 2008 The DragonFly Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * $FreeBSD: src/sys/amd64/include/cpufunc.h,v 1.139 2004/01/28 23:53:04 peter Exp $
36 * $DragonFly: src/sys/cpu/amd64/include/cpufunc.h,v 1.3 2008/08/29 17:07:06 dillon Exp $
40 * Functions to provide access to special i386 instructions.
41 * This in included in sys/systm.h, and that file should be
42 * used in preference to this.
45 #ifndef _CPU_CPUFUNC_H_
46 #define _CPU_CPUFUNC_H_
48 #include <sys/cdefs.h>
49 #include <machine/psl.h>
52 struct region_descriptor;
55 #define readb(va) (*(volatile u_int8_t *) (va))
56 #define readw(va) (*(volatile u_int16_t *) (va))
57 #define readl(va) (*(volatile u_int32_t *) (va))
58 #define readq(va) (*(volatile u_int64_t *) (va))
60 #define writeb(va, d) (*(volatile u_int8_t *) (va) = (d))
61 #define writew(va, d) (*(volatile u_int16_t *) (va) = (d))
62 #define writel(va, d) (*(volatile u_int32_t *) (va) = (d))
63 #define writeq(va, d) (*(volatile u_int64_t *) (va) = (d))
68 #include <machine/lock.h> /* XXX */
74 __asm __volatile("int $3");
80 __asm __volatile("pause");
88 __asm __volatile("bsfl %1,%0" : "=r" (result) : "rm" (mask));
92 static __inline u_long
97 __asm __volatile("bsfq %1,%0" : "=r" (result) : "rm" (mask));
101 static __inline u_int
106 __asm __volatile("bsrl %1,%0" : "=r" (result) : "rm" (mask));
110 static __inline u_long
115 __asm __volatile("bsrq %1,%0" : "=r" (result) : "rm" (mask));
120 cpu_disable_intr(void)
122 __asm __volatile("cli" : : : "memory");
126 do_cpuid(u_int ax, u_int *p)
128 __asm __volatile("cpuid"
129 : "=a" (p[0]), "=b" (p[1]), "=c" (p[2]), "=d" (p[3])
134 cpuid_count(u_int ax, u_int cx, u_int *p)
136 __asm __volatile("cpuid"
137 : "=a" (p[0]), "=b" (p[1]), "=c" (p[2]), "=d" (p[3])
138 : "0" (ax), "c" (cx));
142 cpu_enable_intr(void)
144 __asm __volatile("sti");
148 * Cpu and compiler memory ordering fence. mfence ensures strong read and
151 * A serializing or fence instruction is required here. A locked bus
152 * cycle on data for which we already own cache mastership is the most
159 __asm __volatile("mfence" : : : "memory");
161 __asm __volatile("" : : : "memory");
166 * cpu_lfence() ensures strong read ordering for reads issued prior
167 * to the instruction verses reads issued afterwords.
169 * A serializing or fence instruction is required here. A locked bus
170 * cycle on data for which we already own cache mastership is the most
177 __asm __volatile("lfence" : : : "memory");
179 __asm __volatile("" : : : "memory");
184 * cpu_sfence() ensures strong write ordering for writes issued prior
185 * to the instruction verses writes issued afterwords. Writes are
186 * ordered on intel cpus so we do not actually have to do anything.
192 __asm __volatile("sfence" : : : "memory");
194 __asm __volatile("" : : : "memory");
199 * cpu_ccfence() prevents the compiler from reordering instructions, in
200 * particular stores, relative to the current cpu. Use cpu_sfence() if
201 * you need to guarentee ordering by both the compiler and by the cpu.
203 * This also prevents the compiler from caching memory loads into local
204 * variables across the routine.
209 __asm __volatile("" : : : "memory");
214 #define HAVE_INLINE_FFS
221 * Note that gcc-2's builtin ffs would be used if we didn't declare
222 * this inline or turn off the builtin. The builtin is faster but
223 * broken in gcc-2.4.5 and slower but working in gcc-2.5 and later
226 return (mask == 0 ? mask : (int)bsfl((u_int)mask) + 1);
228 /* Actually, the above is way out of date. The builtins use cmov etc */
229 return (__builtin_ffs(mask));
233 #define HAVE_INLINE_FFSL
238 return (mask == 0 ? mask : (int)bsfq((u_long)mask) + 1);
241 #define HAVE_INLINE_FLS
246 return (mask == 0 ? mask : (int)bsrl((u_int)mask) + 1);
249 #define HAVE_INLINE_FLSL
254 return (mask == 0 ? mask : (int)bsrq((u_long)mask) + 1);
262 __asm __volatile("hlt");
266 * The following complications are to get around gcc not having a
267 * constraint letter for the range 0..255. We still put "d" in the
268 * constraint because "i" isn't a valid constraint when the port
269 * isn't constant. This only matters for -O0 because otherwise
270 * the non-working version gets optimized away.
272 * Use an expression-statement instead of a conditional expression
273 * because gcc-2.6.0 would promote the operands of the conditional
274 * and produce poor code for "if ((inb(var) & const1) == const2)".
276 * The unnecessary test `(port) < 0x10000' is to generate a warning if
277 * the `port' has type u_short or smaller. Such types are pessimal.
278 * This actually only works for signed types. The range check is
279 * careful to avoid generating warnings.
281 #define inb(port) __extension__ ({ \
283 if (__builtin_constant_p(port) && ((port) & 0xffff) < 0x100 \
284 && (port) < 0x10000) \
285 _data = inbc(port); \
287 _data = inbv(port); \
290 #define outb(port, data) ( \
291 __builtin_constant_p(port) && ((port) & 0xffff) < 0x100 \
292 && (port) < 0x10000 \
293 ? outbc(port, data) : outbv(port, data))
295 static __inline u_char
300 __asm __volatile("inb %1,%0" : "=a" (data) : "id" ((u_short)(port)));
305 outbc(u_int port, u_char data)
307 __asm __volatile("outb %0,%1" : : "a" (data), "id" ((u_short)(port)));
310 static __inline u_char
315 * We use %%dx and not %1 here because i/o is done at %dx and not at
316 * %edx, while gcc generates inferior code (movw instead of movl)
317 * if we tell it to load (u_short) port.
319 __asm __volatile("inb %%dx,%0" : "=a" (data) : "d" (port));
323 static __inline u_int
328 __asm __volatile("inl %%dx,%0" : "=a" (data) : "d" (port));
333 insb(u_int port, void *addr, size_t cnt)
335 __asm __volatile("cld; rep; insb"
336 : "+D" (addr), "+c" (cnt)
342 insw(u_int port, void *addr, size_t cnt)
344 __asm __volatile("cld; rep; insw"
345 : "+D" (addr), "+c" (cnt)
351 insl(u_int port, void *addr, size_t cnt)
353 __asm __volatile("cld; rep; insl"
354 : "+D" (addr), "+c" (cnt)
362 __asm __volatile("invd");
368 * If we are not a true-SMP box then smp_invltlb() is a NOP. Note that this
369 * will cause the invl*() functions to be equivalent to the cpu_invl*()
373 void smp_invltlb(void);
375 #define smp_invltlb()
378 #ifndef _CPU_INVLPG_DEFINED
381 * Invalidate a patricular VA on this cpu only
384 cpu_invlpg(void *addr)
386 __asm __volatile("invlpg %0" : : "m" (*(char *)addr) : "memory");
394 __asm __volatile("rep; nop");
399 static __inline u_short
404 __asm __volatile("inw %%dx,%0" : "=a" (data) : "d" (port));
408 static __inline u_int
409 loadandclear(volatile u_int *addr)
413 __asm __volatile("xorl %0,%0; xchgl %1,%0"
414 : "=&r" (result) : "m" (*addr));
419 outbv(u_int port, u_char data)
423 * Use an unnecessary assignment to help gcc's register allocator.
424 * This make a large difference for gcc-1.40 and a tiny difference
425 * for gcc-2.6.0. For gcc-1.40, al had to be ``asm("ax")'' for
426 * best results. gcc-2.6.0 can't handle this.
429 __asm __volatile("outb %0,%%dx" : : "a" (al), "d" (port));
433 outl(u_int port, u_int data)
436 * outl() and outw() aren't used much so we haven't looked at
437 * possible micro-optimizations such as the unnecessary
438 * assignment for them.
440 __asm __volatile("outl %0,%%dx" : : "a" (data), "d" (port));
444 outsb(u_int port, const void *addr, size_t cnt)
446 __asm __volatile("cld; rep; outsb"
447 : "+S" (addr), "+c" (cnt)
452 outsw(u_int port, const void *addr, size_t cnt)
454 __asm __volatile("cld; rep; outsw"
455 : "+S" (addr), "+c" (cnt)
460 outsl(u_int port, const void *addr, size_t cnt)
462 __asm __volatile("cld; rep; outsl"
463 : "+S" (addr), "+c" (cnt)
468 outw(u_int port, u_short data)
470 __asm __volatile("outw %0,%%dx" : : "a" (data), "d" (port));
476 __asm __volatile("pause");
479 static __inline u_long
484 __asm __volatile("pushfq; popq %0" : "=r" (rf));
488 static __inline u_int64_t
493 __asm __volatile("rdmsr" : "=a" (low), "=d" (high) : "c" (msr));
494 return (low | ((u_int64_t)high << 32));
497 static __inline u_int64_t
502 __asm __volatile("rdpmc" : "=a" (low), "=d" (high) : "c" (pmc));
503 return (low | ((u_int64_t)high << 32));
506 #define _RDTSC_SUPPORTED_
508 static __inline u_int64_t
513 __asm __volatile("rdtsc" : "=a" (low), "=d" (high));
514 return (low | ((u_int64_t)high << 32));
520 __asm __volatile("wbinvd");
524 write_rflags(u_long rf)
526 __asm __volatile("pushq %0; popfq" : : "r" (rf));
530 wrmsr(u_int msr, u_int64_t newval)
536 __asm __volatile("wrmsr" : : "a" (low), "d" (high), "c" (msr));
540 load_cr0(u_long data)
543 __asm __volatile("movq %0,%%cr0" : : "r" (data));
546 static __inline u_long
551 __asm __volatile("movq %%cr0,%0" : "=r" (data));
555 static __inline u_long
560 __asm __volatile("movq %%cr2,%0" : "=r" (data));
565 load_cr3(u_long data)
568 __asm __volatile("movq %0,%%cr3" : : "r" (data) : "memory");
571 static __inline u_long
576 __asm __volatile("movq %%cr3,%0" : "=r" (data));
581 load_cr4(u_long data)
583 __asm __volatile("movq %0,%%cr4" : : "r" (data));
586 static __inline u_long
591 __asm __volatile("movq %%cr4,%0" : "=r" (data));
596 * Global TLB flush (except for thise for pages marked PG_G)
606 * TLB flush for an individual page (even if it has PG_G).
607 * Only works on 486+ CPUs (i386 does not have PG_G).
613 __asm __volatile("invlpg %0" : : "m" (*(char *)addr) : "memory");
616 static __inline u_int
620 __asm __volatile("movl %%fs,%0" : "=rm" (sel));
624 static __inline u_int
628 __asm __volatile("movl %%gs,%0" : "=rm" (sel));
635 __asm __volatile("movl %0,%%ds" : : "rm" (sel));
641 __asm __volatile("movl %0,%%es" : : "rm" (sel));
645 /* This is defined in <machine/specialreg.h> but is too painful to get to */
647 #define MSR_FSBASE 0xc0000100
652 /* Preserve the fsbase value across the selector load */
653 __asm __volatile("rdmsr; movl %0,%%fs; wrmsr"
654 : : "rm" (sel), "c" (MSR_FSBASE) : "eax", "edx");
658 #define MSR_GSBASE 0xc0000101
664 * Preserve the gsbase value across the selector load.
665 * Note that we have to disable interrupts because the gsbase
666 * being trashed happens to be the kernel gsbase at the time.
668 __asm __volatile("pushfq; cli; rdmsr; movw %0,%%gs; wrmsr; popfq"
669 : : "rm" (sel), "c" (MSR_GSBASE) : "eax", "edx");
672 /* Usable by userland */
676 __asm __volatile("movl %0,%%fs" : : "rm" (sel));
682 __asm __volatile("movl %0,%%gs" : : "rm" (sel));
686 /* void lidt(struct region_descriptor *addr); */
688 lidt(struct region_descriptor *addr)
690 __asm __volatile("lidt (%0)" : : "r" (addr));
693 /* void lldt(u_short sel); */
697 __asm __volatile("lldt %0" : : "r" (sel));
700 /* void ltr(u_short sel); */
704 __asm __volatile("ltr %0" : : "r" (sel));
707 static __inline u_int64_t
711 __asm __volatile("movq %%dr0,%0" : "=r" (data));
716 load_dr0(u_int64_t dr0)
718 __asm __volatile("movq %0,%%dr0" : : "r" (dr0));
721 static __inline u_int64_t
725 __asm __volatile("movq %%dr1,%0" : "=r" (data));
730 load_dr1(u_int64_t dr1)
732 __asm __volatile("movq %0,%%dr1" : : "r" (dr1));
735 static __inline u_int64_t
739 __asm __volatile("movq %%dr2,%0" : "=r" (data));
744 load_dr2(u_int64_t dr2)
746 __asm __volatile("movq %0,%%dr2" : : "r" (dr2));
749 static __inline u_int64_t
753 __asm __volatile("movq %%dr3,%0" : "=r" (data));
758 load_dr3(u_int64_t dr3)
760 __asm __volatile("movq %0,%%dr3" : : "r" (dr3));
763 static __inline u_int64_t
767 __asm __volatile("movq %%dr4,%0" : "=r" (data));
772 load_dr4(u_int64_t dr4)
774 __asm __volatile("movq %0,%%dr4" : : "r" (dr4));
777 static __inline u_int64_t
781 __asm __volatile("movq %%dr5,%0" : "=r" (data));
786 load_dr5(u_int64_t dr5)
788 __asm __volatile("movq %0,%%dr5" : : "r" (dr5));
791 static __inline u_int64_t
795 __asm __volatile("movq %%dr6,%0" : "=r" (data));
800 load_dr6(u_int64_t dr6)
802 __asm __volatile("movq %0,%%dr6" : : "r" (dr6));
805 static __inline u_int64_t
809 __asm __volatile("movq %%dr7,%0" : "=r" (data));
814 load_dr7(u_int64_t dr7)
816 __asm __volatile("movq %0,%%dr7" : : "r" (dr7));
819 static __inline register_t
824 rflags = read_rflags();
830 intr_restore(register_t rflags)
832 write_rflags(rflags);
835 #else /* !__GNUC__ */
837 int breakpoint(void);
838 void cpu_pause(void);
839 u_int bsfl(u_int mask);
840 u_int bsrl(u_int mask);
841 void cpu_disable_intr(void);
842 void cpu_enable_intr(void);
843 void cpu_invlpg(u_long addr);
844 void cpu_invlpg_range(u_long start, u_long end);
845 void do_cpuid(u_int ax, u_int *p);
847 u_char inb(u_int port);
848 u_int inl(u_int port);
849 void insb(u_int port, void *addr, size_t cnt);
850 void insl(u_int port, void *addr, size_t cnt);
851 void insw(u_int port, void *addr, size_t cnt);
853 void invlpg(u_int addr);
854 void invlpg_range(u_int start, u_int end);
855 void cpu_invltlb(void);
856 u_short inw(u_int port);
857 void load_cr0(u_int cr0);
858 void load_cr3(u_int cr3);
859 void load_cr4(u_int cr4);
860 void load_fs(u_int sel);
861 void load_gs(u_int sel);
862 struct region_descriptor;
863 void lidt(struct region_descriptor *addr);
864 void lldt(u_short sel);
865 void ltr(u_short sel);
866 void outb(u_int port, u_char data);
867 void outl(u_int port, u_int data);
868 void outsb(u_int port, void *addr, size_t cnt);
869 void outsl(u_int port, void *addr, size_t cnt);
870 void outsw(u_int port, void *addr, size_t cnt);
871 void outw(u_int port, u_short data);
872 void ia32_pause(void);
879 u_int64_t rdmsr(u_int msr);
880 u_int64_t rdpmc(u_int pmc);
881 u_int64_t rdtsc(void);
882 u_int read_rflags(void);
884 void write_rflags(u_int rf);
885 void wrmsr(u_int msr, u_int64_t newval);
886 u_int64_t rdr0(void);
887 void load_dr0(u_int64_t dr0);
888 u_int64_t rdr1(void);
889 void load_dr1(u_int64_t dr1);
890 u_int64_t rdr2(void);
891 void load_dr2(u_int64_t dr2);
892 u_int64_t rdr3(void);
893 void load_dr3(u_int64_t dr3);
894 u_int64_t rdr4(void);
895 void load_dr4(u_int64_t dr4);
896 u_int64_t rdr5(void);
897 void load_dr5(u_int64_t dr5);
898 u_int64_t rdr6(void);
899 void load_dr6(u_int64_t dr6);
900 u_int64_t rdr7(void);
901 void load_dr7(u_int64_t dr7);
902 register_t intr_disable(void);
903 void intr_restore(register_t rf);
905 #endif /* __GNUC__ */
907 void reset_dbregs(void);
911 #endif /* !_CPU_CPUFUNC_H_ */