New rldt() to go with lldt()
[freebsd-src/fkvm-freebsd.git] / sys / amd64 / include / cpufunc.h
blob619fbdf80a87172235a6ffe1f5c1d2afaf787306
1 /*-
2 * Copyright (c) 2003 Peter Wemm.
3 * Copyright (c) 1993 The Regents of the University of California.
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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
28 * SUCH DAMAGE.
30 * $FreeBSD$
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_
42 #ifndef _SYS_CDEFS_H_
43 #error this file needs sys/cdefs.h as a prerequisite
44 #endif
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)
60 static __inline void
61 breakpoint(void)
63 __asm __volatile("int $3");
66 static __inline u_int
67 bsfl(u_int mask)
69 u_int result;
71 __asm __volatile("bsfl %1,%0" : "=r" (result) : "rm" (mask));
72 return (result);
75 static __inline u_long
76 bsfq(u_long mask)
78 u_long result;
80 __asm __volatile("bsfq %1,%0" : "=r" (result) : "rm" (mask));
81 return (result);
84 static __inline u_int
85 bsrl(u_int mask)
87 u_int result;
89 __asm __volatile("bsrl %1,%0" : "=r" (result) : "rm" (mask));
90 return (result);
93 static __inline u_long
94 bsrq(u_long mask)
96 u_long result;
98 __asm __volatile("bsrq %1,%0" : "=r" (result) : "rm" (mask));
99 return (result);
102 static __inline void
103 disable_intr(void)
105 __asm __volatile("cli" : : : "memory");
108 static __inline void
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])
113 : "0" (ax));
116 static __inline void
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));
124 static __inline void
125 enable_intr(void)
127 __asm __volatile("sti");
130 #ifdef _KERNEL
132 #define HAVE_INLINE_FFS
133 #define ffs(x) __builtin_ffs(x)
135 #define HAVE_INLINE_FFSL
137 static __inline int
138 ffsl(long mask)
140 return (mask == 0 ? mask : (int)bsfq((u_long)mask) + 1);
143 #define HAVE_INLINE_FLS
145 static __inline int
146 fls(int mask)
148 return (mask == 0 ? mask : (int)bsrl((u_int)mask) + 1);
151 #define HAVE_INLINE_FLSL
153 static __inline int
154 flsl(long mask)
156 return (mask == 0 ? mask : (int)bsrq((u_long)mask) + 1);
159 #endif /* _KERNEL */
161 static __inline void
162 halt(void)
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__ ({ \
191 u_char _data; \
192 if (__builtin_constant_p(port) && ((port) & 0xffff) < 0x100 \
193 && (port) < 0x10000) \
194 _data = inbc(port); \
195 else \
196 _data = inbv(port); \
197 _data; })
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
205 inbc(u_int port)
207 u_char data;
209 __asm __volatile("inb %1,%0" : "=a" (data) : "id" ((u_short)(port)));
210 return (data);
213 static __inline void
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
222 inbv(u_int port)
224 u_char data;
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));
231 return (data);
234 static __inline u_int
235 inl(u_int port)
237 u_int data;
239 __asm __volatile("inl %%dx,%0" : "=a" (data) : "d" (port));
240 return (data);
243 static __inline void
244 insb(u_int port, void *addr, size_t cnt)
246 __asm __volatile("cld; rep; insb"
247 : "+D" (addr), "+c" (cnt)
248 : "d" (port)
249 : "memory");
252 static __inline void
253 insw(u_int port, void *addr, size_t cnt)
255 __asm __volatile("cld; rep; insw"
256 : "+D" (addr), "+c" (cnt)
257 : "d" (port)
258 : "memory");
261 static __inline void
262 insl(u_int port, void *addr, size_t cnt)
264 __asm __volatile("cld; rep; insl"
265 : "+D" (addr), "+c" (cnt)
266 : "d" (port)
267 : "memory");
270 static __inline void
271 invd(void)
273 __asm __volatile("invd");
276 static __inline u_short
277 inw(u_int port)
279 u_short data;
281 __asm __volatile("inw %%dx,%0" : "=a" (data) : "d" (port));
282 return (data);
285 static __inline void
286 outbv(u_int port, u_char data)
288 u_char al;
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.
295 al = data;
296 __asm __volatile("outb %0,%%dx" : : "a" (al), "d" (port));
299 static __inline void
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));
310 static __inline void
311 outsb(u_int port, const void *addr, size_t cnt)
313 __asm __volatile("cld; rep; outsb"
314 : "+S" (addr), "+c" (cnt)
315 : "d" (port));
318 static __inline void
319 outsw(u_int port, const void *addr, size_t cnt)
321 __asm __volatile("cld; rep; outsw"
322 : "+S" (addr), "+c" (cnt)
323 : "d" (port));
326 static __inline void
327 outsl(u_int port, const void *addr, size_t cnt)
329 __asm __volatile("cld; rep; outsl"
330 : "+S" (addr), "+c" (cnt)
331 : "d" (port));
334 static __inline void
335 outw(u_int port, u_short data)
337 __asm __volatile("outw %0,%%dx" : : "a" (data), "d" (port));
340 static __inline void
341 ia32_pause(void)
343 __asm __volatile("pause");
346 static __inline u_long
347 read_rflags(void)
349 u_long rf;
351 __asm __volatile("pushfq; popq %0" : "=r" (rf));
352 return (rf);
355 static __inline u_int64_t
356 rdmsr(u_int msr)
358 u_int32_t low, high;
360 __asm __volatile("rdmsr" : "=a" (low), "=d" (high) : "c" (msr));
361 return (low | ((u_int64_t)high << 32));
364 static __inline u_int64_t
365 rdpmc(u_int pmc)
367 u_int32_t low, high;
369 __asm __volatile("rdpmc" : "=a" (low), "=d" (high) : "c" (pmc));
370 return (low | ((u_int64_t)high << 32));
373 static __inline u_int64_t
374 rdtsc(void)
376 u_int32_t low, high;
378 __asm __volatile("rdtsc" : "=a" (low), "=d" (high));
379 return (low | ((u_int64_t)high << 32));
382 static __inline void
383 wbinvd(void)
385 __asm __volatile("wbinvd");
388 static __inline void
389 write_rflags(u_long rf)
391 __asm __volatile("pushq %0; popfq" : : "r" (rf));
394 static __inline void
395 wrmsr(u_int msr, u_int64_t newval)
397 u_int32_t low, high;
399 low = newval;
400 high = newval >> 32;
401 __asm __volatile("wrmsr" : : "a" (low), "d" (high), "c" (msr));
404 static __inline void
405 load_cr0(u_long data)
408 __asm __volatile("movq %0,%%cr0" : : "r" (data));
411 static __inline u_long
412 rcr0(void)
414 u_long data;
416 __asm __volatile("movq %%cr0,%0" : "=r" (data));
417 return (data);
420 static __inline u_long
421 rcr2(void)
423 u_long data;
425 __asm __volatile("movq %%cr2,%0" : "=r" (data));
426 return (data);
429 static __inline void
430 load_cr3(u_long data)
433 __asm __volatile("movq %0,%%cr3" : : "r" (data) : "memory");
436 static __inline u_long
437 rcr3(void)
439 u_long data;
441 __asm __volatile("movq %%cr3,%0" : "=r" (data));
442 return (data);
445 static __inline void
446 load_cr4(u_long data)
448 __asm __volatile("movq %0,%%cr4" : : "r" (data));
451 static __inline u_long
452 rcr4(void)
454 u_long data;
456 __asm __volatile("movq %%cr4,%0" : "=r" (data));
457 return (data);
461 * Global TLB flush (except for thise for pages marked PG_G)
463 static __inline void
464 invltlb(void)
467 load_cr3(rcr3());
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).
474 static __inline void
475 invlpg(u_long addr)
478 __asm __volatile("invlpg %0" : : "m" (*(char *)addr) : "memory");
481 static __inline u_int
482 rfs(void)
484 u_int sel;
485 __asm __volatile("movl %%fs,%0" : "=rm" (sel));
486 return (sel);
489 static __inline u_int
490 rgs(void)
492 u_int sel;
493 __asm __volatile("movl %%gs,%0" : "=rm" (sel));
494 return (sel);
497 static __inline u_int
498 rss(void)
500 u_int sel;
501 __asm __volatile("movl %%ss,%0" : "=rm" (sel));
502 return (sel);
505 static __inline void
506 load_ds(u_int sel)
508 __asm __volatile("movl %0,%%ds" : : "rm" (sel));
511 static __inline void
512 load_es(u_int sel)
514 __asm __volatile("movl %0,%%es" : : "rm" (sel));
517 static inline void
518 cpu_monitor(const void *addr, int extensions, int hints)
520 __asm __volatile("monitor;"
521 : :"a" (addr), "c" (extensions), "d"(hints));
524 static inline void
525 cpu_mwait(int extensions, int hints)
527 __asm __volatile("mwait;" : :"a" (hints), "c" (extensions));
530 #ifdef _KERNEL
531 /* This is defined in <machine/specialreg.h> but is too painful to get to */
532 #ifndef MSR_FSBASE
533 #define MSR_FSBASE 0xc0000100
534 #endif
535 static __inline void
536 load_fs(u_int sel)
538 register u_int32_t fsbase __asm("ecx");
540 /* Preserve the fsbase value across the selector load */
541 fsbase = MSR_FSBASE;
542 __asm __volatile("rdmsr; movl %0,%%fs; wrmsr"
543 : : "rm" (sel), "c" (fsbase) : "eax", "edx");
546 #ifndef MSR_GSBASE
547 #define MSR_GSBASE 0xc0000101
548 #endif
549 static __inline void
550 load_gs(u_int sel)
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.
559 gsbase = MSR_GSBASE;
560 __asm __volatile("pushfq; cli; rdmsr; movl %0,%%gs; wrmsr; popfq"
561 : : "rm" (sel), "c" (gsbase) : "eax", "edx");
563 #else
564 /* Usable by userland */
565 static __inline void
566 load_fs(u_int sel)
568 __asm __volatile("movl %0,%%fs" : : "rm" (sel));
571 static __inline void
572 load_gs(u_int sel)
574 __asm __volatile("movl %0,%%gs" : : "rm" (sel));
576 #endif
578 static __inline void
579 lidt(struct region_descriptor *addr)
581 __asm __volatile("lidt (%0)" : : "r" (addr));
584 static __inline u_short
585 rldt(void)
587 u_short ldt;
588 __asm __volatile("sldt %0" : "=g" (ldt));
589 return (ldt);
592 static __inline void
593 lldt(u_short sel)
595 __asm __volatile("lldt %0" : : "r" (sel));
598 static __inline void
599 ltr(u_short sel)
601 __asm __volatile("ltr %0" : : "r" (sel));
604 static __inline u_int64_t
605 rdr0(void)
607 u_int64_t data;
608 __asm __volatile("movq %%dr0,%0" : "=r" (data));
609 return (data);
612 static __inline void
613 load_dr0(u_int64_t dr0)
615 __asm __volatile("movq %0,%%dr0" : : "r" (dr0));
618 static __inline u_int64_t
619 rdr1(void)
621 u_int64_t data;
622 __asm __volatile("movq %%dr1,%0" : "=r" (data));
623 return (data);
626 static __inline void
627 load_dr1(u_int64_t dr1)
629 __asm __volatile("movq %0,%%dr1" : : "r" (dr1));
632 static __inline u_int64_t
633 rdr2(void)
635 u_int64_t data;
636 __asm __volatile("movq %%dr2,%0" : "=r" (data));
637 return (data);
640 static __inline void
641 load_dr2(u_int64_t dr2)
643 __asm __volatile("movq %0,%%dr2" : : "r" (dr2));
646 static __inline u_int64_t
647 rdr3(void)
649 u_int64_t data;
650 __asm __volatile("movq %%dr3,%0" : "=r" (data));
651 return (data);
654 static __inline void
655 load_dr3(u_int64_t dr3)
657 __asm __volatile("movq %0,%%dr3" : : "r" (dr3));
660 static __inline u_int64_t
661 rdr4(void)
663 u_int64_t data;
664 __asm __volatile("movq %%dr4,%0" : "=r" (data));
665 return (data);
668 static __inline void
669 load_dr4(u_int64_t dr4)
671 __asm __volatile("movq %0,%%dr4" : : "r" (dr4));
674 static __inline u_int64_t
675 rdr5(void)
677 u_int64_t data;
678 __asm __volatile("movq %%dr5,%0" : "=r" (data));
679 return (data);
682 static __inline void
683 load_dr5(u_int64_t dr5)
685 __asm __volatile("movq %0,%%dr5" : : "r" (dr5));
688 static __inline u_int64_t
689 rdr6(void)
691 u_int64_t data;
692 __asm __volatile("movq %%dr6,%0" : "=r" (data));
693 return (data);
696 static __inline void
697 load_dr6(u_int64_t dr6)
699 __asm __volatile("movq %0,%%dr6" : : "r" (dr6));
702 static __inline u_int64_t
703 rdr7(void)
705 u_int64_t data;
706 __asm __volatile("movq %%dr7,%0" : "=r" (data));
707 return (data);
710 static __inline void
711 load_dr7(u_int64_t dr7)
713 __asm __volatile("movq %0,%%dr7" : : "r" (dr7));
716 static __inline register_t
717 intr_disable(void)
719 register_t rflags;
721 rflags = read_rflags();
722 disable_intr();
723 return (rflags);
726 static __inline void
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);
740 void halt(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);
749 void invd(void);
750 void invlpg(u_int addr);
751 void invltlb(void);
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);
775 u_long rcr0(void);
776 u_long rcr2(void);
777 u_long rcr3(void);
778 u_long rcr4(void);
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);
791 u_int rfs(void);
792 u_int rgs(void);
793 void wbinvd(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);
801 #ifdef _KERNEL
802 int rdmsr_safe(u_int msr, uint64_t *val);
803 int wrmsr_safe(u_int msr, uint64_t newval);
804 #endif
806 #endif /* !_MACHINE_CPUFUNC_H_ */