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
46 extern void xen_cli(void);
47 extern void xen_sti(void);
48 extern u_int xen_rcr2(void);
49 extern void xen_load_cr3(u_int data);
50 extern void xen_tlb_flush(void);
51 extern void xen_invlpg(u_int addr);
52 extern void write_eflags(u_int eflags);
53 extern u_int read_eflags(void);
56 struct region_descriptor;
58 #define readb(va) (*(volatile uint8_t *) (va))
59 #define readw(va) (*(volatile uint16_t *) (va))
60 #define readl(va) (*(volatile uint32_t *) (va))
62 #define writeb(va, d) (*(volatile uint8_t *) (va) = (d))
63 #define writew(va, d) (*(volatile uint16_t *) (va) = (d))
64 #define writel(va, d) (*(volatile uint32_t *) (va) = (d))
66 #if defined(__GNUCLIKE_ASM) && defined(__CC_SUPPORTS___INLINE)
71 __asm __volatile("int $3");
79 __asm("bsfl %1,%0" : "=r" (result) : "rm" (mask) : "cc");
88 __asm("bsrl %1,%0" : "=r" (result) : "rm" (mask) : "cc");
96 __asm __volatile("clflush %0" : : "m" (*(char *)addr));
103 __asm __volatile("clts");
112 __asm __volatile("cli" : : : "memory");
117 do_cpuid(u_int ax, u_int *p)
119 __asm __volatile("cpuid"
120 : "=a" (p[0]), "=b" (p[1]), "=c" (p[2]), "=d" (p[3])
125 cpuid_count(u_int ax, u_int cx, u_int *p)
127 __asm __volatile("cpuid"
128 : "=a" (p[0]), "=b" (p[1]), "=c" (p[2]), "=d" (p[3])
129 : "0" (ax), "c" (cx));
138 __asm __volatile("sti");
143 cpu_monitor(const void *addr, u_long extensions, u_int hints)
146 __asm __volatile("monitor"
147 : : "a" (addr), "c" (extensions), "d" (hints));
151 cpu_mwait(u_long extensions, u_int hints)
154 __asm __volatile("mwait" : : "a" (hints), "c" (extensions));
161 __asm __volatile("lfence" : : : "memory");
168 __asm __volatile("mfence" : : : "memory");
173 #define HAVE_INLINE_FFS
179 * Note that gcc-2's builtin ffs would be used if we didn't declare
180 * this inline or turn off the builtin. The builtin is faster but
181 * broken in gcc-2.4.5 and slower but working in gcc-2.5 and later
184 return (mask == 0 ? mask : (int)bsfl((u_int)mask) + 1);
187 #define HAVE_INLINE_FLS
192 return (mask == 0 ? mask : (int)bsrl((u_int)mask) + 1);
200 __asm __volatile("hlt");
203 static __inline u_char
208 __asm __volatile("inb %w1, %0" : "=a" (data) : "Nd" (port));
212 static __inline u_int
217 __asm __volatile("inl %w1, %0" : "=a" (data) : "Nd" (port));
222 insb(u_int port, void *addr, size_t count)
224 __asm __volatile("cld; rep; insb"
225 : "+D" (addr), "+c" (count)
231 insw(u_int port, void *addr, size_t count)
233 __asm __volatile("cld; rep; insw"
234 : "+D" (addr), "+c" (count)
240 insl(u_int port, void *addr, size_t count)
242 __asm __volatile("cld; rep; insl"
243 : "+D" (addr), "+c" (count)
251 __asm __volatile("invd");
254 static __inline u_short
259 __asm __volatile("inw %w1, %0" : "=a" (data) : "Nd" (port));
264 outb(u_int port, u_char data)
266 __asm __volatile("outb %0, %w1" : : "a" (data), "Nd" (port));
270 outl(u_int port, u_int data)
272 __asm __volatile("outl %0, %w1" : : "a" (data), "Nd" (port));
276 outsb(u_int port, const void *addr, size_t count)
278 __asm __volatile("cld; rep; outsb"
279 : "+S" (addr), "+c" (count)
284 outsw(u_int port, const void *addr, size_t count)
286 __asm __volatile("cld; rep; outsw"
287 : "+S" (addr), "+c" (count)
292 outsl(u_int port, const void *addr, size_t count)
294 __asm __volatile("cld; rep; outsl"
295 : "+S" (addr), "+c" (count)
300 outw(u_int port, u_short data)
302 __asm __volatile("outw %0, %w1" : : "a" (data), "Nd" (port));
308 __asm __volatile("pause");
311 static __inline u_int
320 __asm __volatile("pushfl; popl %0" : "=r" (ef));
324 static __inline uint64_t
329 __asm __volatile("rdmsr" : "=A" (rv) : "c" (msr));
333 static __inline uint64_t
338 __asm __volatile("rdpmc" : "=A" (rv) : "c" (pmc));
342 static __inline uint64_t
347 __asm __volatile("rdtsc" : "=A" (rv));
351 static __inline uint32_t
356 __asm __volatile("rdtsc" : "=a" (rv) : : "edx");
363 __asm __volatile("wbinvd");
368 _write_eflags(u_int ef)
370 write_eflags(u_int ef)
373 __asm __volatile("pushl %0; popfl" : : "r" (ef));
377 wrmsr(u_int msr, uint64_t newval)
379 __asm __volatile("wrmsr" : : "A" (newval), "c" (msr));
386 __asm __volatile("movl %0,%%cr0" : : "r" (data));
389 static __inline u_int
394 __asm __volatile("movl %%cr0,%0" : "=r" (data));
398 static __inline u_int
406 __asm __volatile("movl %%cr2,%0" : "=r" (data));
416 __asm __volatile("movl %0,%%cr3" : : "r" (data) : "memory");
420 static __inline u_int
425 __asm __volatile("movl %%cr3,%0" : "=r" (data));
432 __asm __volatile("movl %0,%%cr4" : : "r" (data));
435 static __inline u_int
440 __asm __volatile("movl %%cr4,%0" : "=r" (data));
445 * Global TLB flush (except for thise for pages marked PG_G)
458 * TLB flush for an individual page (even if it has PG_G).
459 * Only works on 486+ CPUs (i386 does not have PG_G).
468 __asm __volatile("invlpg %0" : : "m" (*(char *)addr) : "memory");
472 static __inline u_short
476 __asm __volatile("movw %%fs,%0" : "=rm" (sel));
480 static __inline uint64_t
484 __asm __volatile("sgdt %0" : "=m" (gdtr));
488 static __inline u_short
492 __asm __volatile("movw %%gs,%0" : "=rm" (sel));
496 static __inline uint64_t
500 __asm __volatile("sidt %0" : "=m" (idtr));
504 static __inline u_short
508 __asm __volatile("sldt %0" : "=g" (ldtr));
512 static __inline u_short
516 __asm __volatile("movw %%ss,%0" : "=rm" (sel));
520 static __inline u_short
524 __asm __volatile("str %0" : "=g" (tr));
531 __asm __volatile("movw %0,%%fs" : : "rm" (sel));
537 __asm __volatile("movw %0,%%gs" : : "rm" (sel));
541 lidt(struct region_descriptor *addr)
543 __asm __volatile("lidt (%0)" : : "r" (addr));
549 __asm __volatile("lldt %0" : : "r" (sel));
555 __asm __volatile("ltr %0" : : "r" (sel));
558 static __inline u_int
562 __asm __volatile("movl %%dr0,%0" : "=r" (data));
569 __asm __volatile("movl %0,%%dr0" : : "r" (dr0));
572 static __inline u_int
576 __asm __volatile("movl %%dr1,%0" : "=r" (data));
583 __asm __volatile("movl %0,%%dr1" : : "r" (dr1));
586 static __inline u_int
590 __asm __volatile("movl %%dr2,%0" : "=r" (data));
597 __asm __volatile("movl %0,%%dr2" : : "r" (dr2));
600 static __inline u_int
604 __asm __volatile("movl %%dr3,%0" : "=r" (data));
611 __asm __volatile("movl %0,%%dr3" : : "r" (dr3));
614 static __inline u_int
618 __asm __volatile("movl %%dr4,%0" : "=r" (data));
625 __asm __volatile("movl %0,%%dr4" : : "r" (dr4));
628 static __inline u_int
632 __asm __volatile("movl %%dr5,%0" : "=r" (data));
639 __asm __volatile("movl %0,%%dr5" : : "r" (dr5));
642 static __inline u_int
646 __asm __volatile("movl %%dr6,%0" : "=r" (data));
653 __asm __volatile("movl %0,%%dr6" : : "r" (dr6));
656 static __inline u_int
660 __asm __volatile("movl %%dr7,%0" : "=r" (data));
667 __asm __volatile("movl %0,%%dr7" : : "r" (dr7));
670 static __inline u_char
671 read_cyrix_reg(u_char reg)
678 write_cyrix_reg(u_char reg, u_char data)
684 static __inline register_t
689 eflags = read_eflags();
695 intr_restore(register_t eflags)
697 write_eflags(eflags);
700 #else /* !(__GNUCLIKE_ASM && __CC_SUPPORTS___INLINE) */
702 int breakpoint(void);
703 u_int bsfl(u_int mask);
704 u_int bsrl(u_int mask);
705 void clflush(u_long addr);
707 void cpuid_count(u_int ax, u_int cx, u_int *p);
708 void disable_intr(void);
709 void do_cpuid(u_int ax, u_int *p);
710 void enable_intr(void);
712 void ia32_pause(void);
713 u_char inb(u_int port);
714 u_int inl(u_int port);
715 void insb(u_int port, void *addr, size_t count);
716 void insl(u_int port, void *addr, size_t count);
717 void insw(u_int port, void *addr, size_t count);
718 register_t intr_disable(void);
719 void intr_restore(register_t ef);
721 void invlpg(u_int addr);
723 u_short inw(u_int port);
724 void lidt(struct region_descriptor *addr);
725 void lldt(u_short sel);
726 void load_cr0(u_int cr0);
727 void load_cr3(u_int cr3);
728 void load_cr4(u_int cr4);
729 void load_dr0(u_int dr0);
730 void load_dr1(u_int dr1);
731 void load_dr2(u_int dr2);
732 void load_dr3(u_int dr3);
733 void load_dr4(u_int dr4);
734 void load_dr5(u_int dr5);
735 void load_dr6(u_int dr6);
736 void load_dr7(u_int dr7);
737 void load_fs(u_short sel);
738 void load_gs(u_short sel);
739 void ltr(u_short sel);
740 void outb(u_int port, u_char data);
741 void outl(u_int port, u_int data);
742 void outsb(u_int port, const void *addr, size_t count);
743 void outsl(u_int port, const void *addr, size_t count);
744 void outsw(u_int port, const void *addr, size_t count);
745 void outw(u_int port, u_short data);
750 uint64_t rdmsr(u_int msr);
751 uint64_t rdpmc(u_int pmc);
760 uint64_t rdtsc(void);
761 u_char read_cyrix_reg(u_char reg);
762 u_int read_eflags(void);
770 void write_cyrix_reg(u_char reg, u_char data);
771 void write_eflags(u_int ef);
772 void wrmsr(u_int msr, uint64_t newval);
774 #endif /* __GNUCLIKE_ASM && __CC_SUPPORTS___INLINE */
776 void reset_dbregs(void);
779 int rdmsr_safe(u_int msr, uint64_t *val);
780 int wrmsr_safe(u_int msr, uint64_t newval);
783 #endif /* !_MACHINE_CPUFUNC_H_ */