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 u_int8_t *) (va))
59 #define readw(va) (*(volatile u_int16_t *) (va))
60 #define readl(va) (*(volatile u_int32_t *) (va))
62 #define writeb(va, d) (*(volatile u_int8_t *) (va) = (d))
63 #define writew(va, d) (*(volatile u_int16_t *) (va) = (d))
64 #define writel(va, d) (*(volatile u_int32_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));
105 __asm __volatile("cli" : : : "memory");
110 do_cpuid(u_int ax, u_int *p)
112 __asm __volatile("cpuid"
113 : "=a" (p[0]), "=b" (p[1]), "=c" (p[2]), "=d" (p[3])
118 cpuid_count(u_int ax, u_int cx, u_int *p)
120 __asm __volatile("cpuid"
121 : "=a" (p[0]), "=b" (p[1]), "=c" (p[2]), "=d" (p[3])
122 : "0" (ax), "c" (cx));
131 __asm __volatile("sti");
136 cpu_monitor(const void *addr, int extensions, int hints)
138 __asm __volatile("monitor;"
139 : :"a" (addr), "c" (extensions), "d"(hints));
143 cpu_mwait(int extensions, int hints)
145 __asm __volatile("mwait;" : :"a" (hints), "c" (extensions));
152 __asm __volatile("lfence" : : : "memory");
159 __asm __volatile("mfence" : : : "memory");
164 #define HAVE_INLINE_FFS
170 * Note that gcc-2's builtin ffs would be used if we didn't declare
171 * this inline or turn off the builtin. The builtin is faster but
172 * broken in gcc-2.4.5 and slower but working in gcc-2.5 and later
175 return (mask == 0 ? mask : (int)bsfl((u_int)mask) + 1);
178 #define HAVE_INLINE_FLS
183 return (mask == 0 ? mask : (int)bsrl((u_int)mask) + 1);
191 __asm __volatile("hlt");
194 static __inline u_char
199 __asm volatile("inb %w1, %0" : "=a" (data) : "Nd" (port));
203 static __inline u_int
208 __asm volatile("inl %w1, %0" : "=a" (data) : "Nd" (port));
213 insb(u_int port, void *addr, size_t cnt)
215 __asm __volatile("cld; rep; insb"
216 : "+D" (addr), "+c" (cnt)
222 insw(u_int port, void *addr, size_t cnt)
224 __asm __volatile("cld; rep; insw"
225 : "+D" (addr), "+c" (cnt)
231 insl(u_int port, void *addr, size_t cnt)
233 __asm __volatile("cld; rep; insl"
234 : "+D" (addr), "+c" (cnt)
242 __asm __volatile("invd");
245 static __inline u_short
250 __asm volatile("inw %w1, %0" : "=a" (data) : "Nd" (port));
255 outb(u_int port, u_char data)
257 __asm __volatile("outb %0, %w1" : : "a" (data), "Nd" (port));
261 outl(u_int port, u_int data)
263 __asm volatile("outl %0, %w1" : : "a" (data), "Nd" (port));
267 outsb(u_int port, const void *addr, size_t cnt)
269 __asm __volatile("cld; rep; outsb"
270 : "+S" (addr), "+c" (cnt)
275 outsw(u_int port, const void *addr, size_t cnt)
277 __asm __volatile("cld; rep; outsw"
278 : "+S" (addr), "+c" (cnt)
283 outsl(u_int port, const void *addr, size_t cnt)
285 __asm __volatile("cld; rep; outsl"
286 : "+S" (addr), "+c" (cnt)
291 outw(u_int port, u_short data)
293 __asm volatile("outw %0, %w1" : : "a" (data), "Nd" (port));
299 __asm __volatile("pause");
302 static __inline u_int
311 __asm __volatile("pushfl; popl %0" : "=r" (ef));
315 static __inline uint64_t
320 __asm __volatile("rdmsr" : "=A" (rv) : "c" (msr));
324 static __inline uint64_t
329 __asm __volatile("rdpmc" : "=A" (rv) : "c" (pmc));
333 static __inline uint64_t
338 __asm __volatile("rdtsc" : "=A" (rv));
345 __asm __volatile("wbinvd");
350 _write_eflags(u_int ef)
352 write_eflags(u_int ef)
355 __asm __volatile("pushl %0; popfl" : : "r" (ef));
359 wrmsr(u_int msr, uint64_t newval)
361 __asm __volatile("wrmsr" : : "A" (newval), "c" (msr));
368 __asm __volatile("movl %0,%%cr0" : : "r" (data));
371 static __inline u_int
376 __asm __volatile("movl %%cr0,%0" : "=r" (data));
380 static __inline u_int
388 __asm __volatile("movl %%cr2,%0" : "=r" (data));
398 __asm __volatile("movl %0,%%cr3" : : "r" (data) : "memory");
402 static __inline u_int
407 __asm __volatile("movl %%cr3,%0" : "=r" (data));
414 __asm __volatile("movl %0,%%cr4" : : "r" (data));
417 static __inline u_int
422 __asm __volatile("movl %%cr4,%0" : "=r" (data));
427 * Global TLB flush (except for thise for pages marked PG_G)
440 * TLB flush for an individual page (even if it has PG_G).
441 * Only works on 486+ CPUs (i386 does not have PG_G).
450 __asm __volatile("invlpg %0" : : "m" (*(char *)addr) : "memory");
454 static __inline u_int
458 __asm __volatile("mov %%fs,%0" : "=rm" (sel));
462 static __inline uint64_t
466 __asm __volatile("sgdt %0" : "=m" (gdtr));
470 static __inline u_int
474 __asm __volatile("mov %%gs,%0" : "=rm" (sel));
478 static __inline uint64_t
482 __asm __volatile("sidt %0" : "=m" (idtr));
486 static __inline u_short
490 __asm __volatile("sldt %0" : "=g" (ldtr));
494 static __inline u_int
498 __asm __volatile("mov %%ss,%0" : "=rm" (sel));
502 static __inline u_short
506 __asm __volatile("str %0" : "=g" (tr));
513 __asm __volatile("mov %0,%%fs" : : "rm" (sel));
519 __asm __volatile("mov %0,%%gs" : : "rm" (sel));
523 lidt(struct region_descriptor *addr)
525 __asm __volatile("lidt (%0)" : : "r" (addr));
531 __asm __volatile("lldt %0" : : "r" (sel));
537 __asm __volatile("ltr %0" : : "r" (sel));
540 static __inline u_int
544 __asm __volatile("movl %%dr0,%0" : "=r" (data));
551 __asm __volatile("movl %0,%%dr0" : : "r" (dr0));
554 static __inline u_int
558 __asm __volatile("movl %%dr1,%0" : "=r" (data));
565 __asm __volatile("movl %0,%%dr1" : : "r" (dr1));
568 static __inline u_int
572 __asm __volatile("movl %%dr2,%0" : "=r" (data));
579 __asm __volatile("movl %0,%%dr2" : : "r" (dr2));
582 static __inline u_int
586 __asm __volatile("movl %%dr3,%0" : "=r" (data));
593 __asm __volatile("movl %0,%%dr3" : : "r" (dr3));
596 static __inline u_int
600 __asm __volatile("movl %%dr4,%0" : "=r" (data));
607 __asm __volatile("movl %0,%%dr4" : : "r" (dr4));
610 static __inline u_int
614 __asm __volatile("movl %%dr5,%0" : "=r" (data));
621 __asm __volatile("movl %0,%%dr5" : : "r" (dr5));
624 static __inline u_int
628 __asm __volatile("movl %%dr6,%0" : "=r" (data));
635 __asm __volatile("movl %0,%%dr6" : : "r" (dr6));
638 static __inline u_int
642 __asm __volatile("movl %%dr7,%0" : "=r" (data));
649 __asm __volatile("movl %0,%%dr7" : : "r" (dr7));
652 static __inline u_char
653 read_cyrix_reg(u_char reg)
660 write_cyrix_reg(u_char reg, u_char data)
666 static __inline register_t
671 eflags = read_eflags();
677 intr_restore(register_t eflags)
679 write_eflags(eflags);
682 #else /* !(__GNUCLIKE_ASM && __CC_SUPPORTS___INLINE) */
684 int breakpoint(void);
685 u_int bsfl(u_int mask);
686 u_int bsrl(u_int mask);
687 void disable_intr(void);
688 void do_cpuid(u_int ax, u_int *p);
689 void enable_intr(void);
691 void ia32_pause(void);
692 u_char inb(u_int port);
693 u_int inl(u_int port);
694 void insb(u_int port, void *addr, size_t cnt);
695 void insl(u_int port, void *addr, size_t cnt);
696 void insw(u_int port, void *addr, size_t cnt);
697 register_t intr_disable(void);
698 void intr_restore(register_t ef);
700 void invlpg(u_int addr);
702 u_short inw(u_int port);
703 void lidt(struct region_descriptor *addr);
704 void lldt(u_short sel);
705 void load_cr0(u_int cr0);
706 void load_cr3(u_int cr3);
707 void load_cr4(u_int cr4);
708 void load_dr0(u_int dr0);
709 void load_dr1(u_int dr1);
710 void load_dr2(u_int dr2);
711 void load_dr3(u_int dr3);
712 void load_dr4(u_int dr4);
713 void load_dr5(u_int dr5);
714 void load_dr6(u_int dr6);
715 void load_dr7(u_int dr7);
716 void load_fs(u_int sel);
717 void load_gs(u_int sel);
718 void ltr(u_short sel);
719 void outb(u_int port, u_char data);
720 void outl(u_int port, u_int data);
721 void outsb(u_int port, const void *addr, size_t cnt);
722 void outsl(u_int port, const void *addr, size_t cnt);
723 void outsw(u_int port, const void *addr, size_t cnt);
724 void outw(u_int port, u_short data);
729 uint64_t rdmsr(u_int msr);
730 uint64_t rdpmc(u_int pmc);
739 uint64_t rdtsc(void);
740 u_char read_cyrix_reg(u_char reg);
741 u_int read_eflags(void);
749 void write_cyrix_reg(u_char reg, u_char data);
750 void write_eflags(u_int ef);
751 void wrmsr(u_int msr, uint64_t newval);
753 #endif /* __GNUCLIKE_ASM && __CC_SUPPORTS___INLINE */
755 void reset_dbregs(void);
758 int rdmsr_safe(u_int msr, uint64_t *val);
759 int wrmsr_safe(u_int msr, uint64_t newval);
762 #endif /* !_MACHINE_CPUFUNC_H_ */