2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1993 The Regents of the University of California.
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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Functions to provide access to special i386 instructions.
36 * This in included in sys/systm.h, and that file should be
37 * used in preference to this.
40 #ifndef _MACHINE_CPUFUNC_H_
41 #define _MACHINE_CPUFUNC_H_
44 #error this file needs sys/cdefs.h as a prerequisite
47 struct region_descriptor;
49 #define readb(va) (*(volatile uint8_t *) (va))
50 #define readw(va) (*(volatile uint16_t *) (va))
51 #define readl(va) (*(volatile uint32_t *) (va))
53 #define writeb(va, d) (*(volatile uint8_t *) (va) = (d))
54 #define writew(va, d) (*(volatile uint16_t *) (va) = (d))
55 #define writel(va, d) (*(volatile uint32_t *) (va) = (d))
57 #if defined(__GNUCLIKE_ASM) && defined(__CC_SUPPORTS___INLINE)
62 __asm __volatile("int $3");
65 static __inline __pure2 u_int
70 __asm("bsfl %1,%0" : "=r" (result) : "rm" (mask) : "cc");
74 static __inline __pure2 u_int
79 __asm("bsrl %1,%0" : "=r" (result) : "rm" (mask) : "cc");
87 __asm __volatile("clflush %0" : : "m" (*(char *)addr));
91 clflushopt(u_long addr)
94 __asm __volatile(".byte 0x66;clflush %0" : : "m" (*(char *)addr));
101 __asm __volatile("clts");
108 __asm __volatile("cli" : : : "memory");
113 do_cpuid(u_int ax, u_int *p)
115 __asm __volatile("cpuid"
116 : "=a" (p[0]), "=b" (p[1]), "=c" (p[2]), "=d" (p[3])
121 cpuid_count(u_int ax, u_int cx, u_int *p)
123 __asm __volatile("cpuid"
124 : "=a" (p[0]), "=b" (p[1]), "=c" (p[2]), "=d" (p[3])
125 : "0" (ax), "c" (cx));
129 do_cpuid(u_int ax, u_int *p)
136 : "=a" (p[0]), "=DS" (p[1]), "=c" (p[2]), "=d" (p[3])
141 cpuid_count(u_int ax, u_int cx, u_int *p)
148 : "=a" (p[0]), "=DS" (p[1]), "=c" (p[2]), "=d" (p[3])
149 : "0" (ax), "c" (cx));
157 __asm __volatile("sti");
161 cpu_monitor(const void *addr, u_long extensions, u_int hints)
164 __asm __volatile("monitor"
165 : : "a" (addr), "c" (extensions), "d" (hints));
169 cpu_mwait(u_long extensions, u_int hints)
172 __asm __volatile("mwait" : : "a" (hints), "c" (extensions));
179 __asm __volatile("lfence" : : : "memory");
186 __asm __volatile("mfence" : : : "memory");
193 __asm __volatile("sfence" : : : "memory");
198 #define HAVE_INLINE_FFS
200 static __inline __pure2 int
204 * Note that gcc-2's builtin ffs would be used if we didn't declare
205 * this inline or turn off the builtin. The builtin is faster but
206 * broken in gcc-2.4.5 and slower but working in gcc-2.5 and later
209 return (mask == 0 ? mask : (int)bsfl((u_int)mask) + 1);
212 #define HAVE_INLINE_FFSL
214 static __inline __pure2 int
217 return (ffs((int)mask));
220 #define HAVE_INLINE_FLS
222 static __inline __pure2 int
225 return (mask == 0 ? mask : (int)bsrl((u_int)mask) + 1);
228 #define HAVE_INLINE_FLSL
230 static __inline __pure2 int
233 return (fls((int)mask));
241 __asm __volatile("hlt");
244 static __inline u_char
249 __asm __volatile("inb %w1, %0" : "=a" (data) : "Nd" (port));
253 static __inline u_int
258 __asm __volatile("inl %w1, %0" : "=a" (data) : "Nd" (port));
263 insb(u_int port, void *addr, size_t count)
265 __asm __volatile("cld; rep; insb"
266 : "+D" (addr), "+c" (count)
272 insw(u_int port, void *addr, size_t count)
274 __asm __volatile("cld; rep; insw"
275 : "+D" (addr), "+c" (count)
281 insl(u_int port, void *addr, size_t count)
283 __asm __volatile("cld; rep; insl"
284 : "+D" (addr), "+c" (count)
292 __asm __volatile("invd");
295 static __inline u_short
300 __asm __volatile("inw %w1, %0" : "=a" (data) : "Nd" (port));
305 outb(u_int port, u_char data)
307 __asm __volatile("outb %0, %w1" : : "a" (data), "Nd" (port));
311 outl(u_int port, u_int data)
313 __asm __volatile("outl %0, %w1" : : "a" (data), "Nd" (port));
317 outsb(u_int port, const void *addr, size_t count)
319 __asm __volatile("cld; rep; outsb"
320 : "+S" (addr), "+c" (count)
325 outsw(u_int port, const void *addr, size_t count)
327 __asm __volatile("cld; rep; outsw"
328 : "+S" (addr), "+c" (count)
333 outsl(u_int port, const void *addr, size_t count)
335 __asm __volatile("cld; rep; outsl"
336 : "+S" (addr), "+c" (count)
341 outw(u_int port, u_short data)
343 __asm __volatile("outw %0, %w1" : : "a" (data), "Nd" (port));
349 __asm __volatile("pause");
352 static __inline u_int
357 __asm __volatile("pushfl; popl %0" : "=r" (ef));
361 static __inline uint64_t
366 __asm __volatile("rdmsr" : "=A" (rv) : "c" (msr));
370 static __inline uint32_t
375 __asm __volatile("rdmsr" : "=a" (low) : "c" (msr) : "edx");
379 static __inline uint64_t
384 __asm __volatile("rdpmc" : "=A" (rv) : "c" (pmc));
388 static __inline uint64_t
393 __asm __volatile("rdtsc" : "=A" (rv));
397 static __inline uint64_t
402 __asm __volatile("rdtscp" : "=A" (rv) : : "ecx");
406 static __inline uint32_t
411 __asm __volatile("rdtsc" : "=a" (rv) : : "edx");
418 __asm __volatile("wbinvd");
422 write_eflags(u_int ef)
424 __asm __volatile("pushl %0; popfl" : : "r" (ef));
428 wrmsr(u_int msr, uint64_t newval)
430 __asm __volatile("wrmsr" : : "A" (newval), "c" (msr));
437 __asm __volatile("movl %0,%%cr0" : : "r" (data));
440 static __inline u_int
445 __asm __volatile("movl %%cr0,%0" : "=r" (data));
449 static __inline u_int
454 __asm __volatile("movl %%cr2,%0" : "=r" (data));
462 __asm __volatile("movl %0,%%cr3" : : "r" (data) : "memory");
465 static __inline u_int
470 __asm __volatile("movl %%cr3,%0" : "=r" (data));
477 __asm __volatile("movl %0,%%cr4" : : "r" (data));
480 static __inline u_int
485 __asm __volatile("movl %%cr4,%0" : "=r" (data));
489 static __inline uint64_t
494 __asm __volatile("xgetbv" : "=a" (low), "=d" (high) : "c" (reg));
495 return (low | ((uint64_t)high << 32));
499 load_xcr(u_int reg, uint64_t val)
505 __asm __volatile("xsetbv" : : "c" (reg), "a" (low), "d" (high));
509 * Global TLB flush (except for thise for pages marked PG_G)
519 * TLB flush for an individual page (even if it has PG_G).
520 * Only works on 486+ CPUs (i386 does not have PG_G).
526 __asm __volatile("invlpg %0" : : "m" (*(char *)addr) : "memory");
529 static __inline u_short
533 __asm __volatile("movw %%fs,%0" : "=rm" (sel));
537 static __inline uint64_t
541 __asm __volatile("sgdt %0" : "=m" (gdtr));
545 static __inline u_short
549 __asm __volatile("movw %%gs,%0" : "=rm" (sel));
553 static __inline uint64_t
557 __asm __volatile("sidt %0" : "=m" (idtr));
561 static __inline u_short
565 __asm __volatile("sldt %0" : "=g" (ldtr));
569 static __inline u_short
573 __asm __volatile("movw %%ss,%0" : "=rm" (sel));
577 static __inline u_short
581 __asm __volatile("str %0" : "=g" (tr));
588 __asm __volatile("movw %0,%%fs" : : "rm" (sel));
594 __asm __volatile("movw %0,%%gs" : : "rm" (sel));
598 lidt(struct region_descriptor *addr)
600 __asm __volatile("lidt (%0)" : : "r" (addr));
606 __asm __volatile("lldt %0" : : "r" (sel));
612 __asm __volatile("ltr %0" : : "r" (sel));
615 static __inline u_int
619 __asm __volatile("movl %%dr0,%0" : "=r" (data));
626 __asm __volatile("movl %0,%%dr0" : : "r" (dr0));
629 static __inline u_int
633 __asm __volatile("movl %%dr1,%0" : "=r" (data));
640 __asm __volatile("movl %0,%%dr1" : : "r" (dr1));
643 static __inline u_int
647 __asm __volatile("movl %%dr2,%0" : "=r" (data));
654 __asm __volatile("movl %0,%%dr2" : : "r" (dr2));
657 static __inline u_int
661 __asm __volatile("movl %%dr3,%0" : "=r" (data));
668 __asm __volatile("movl %0,%%dr3" : : "r" (dr3));
671 static __inline u_int
675 __asm __volatile("movl %%dr6,%0" : "=r" (data));
682 __asm __volatile("movl %0,%%dr6" : : "r" (dr6));
685 static __inline u_int
689 __asm __volatile("movl %%dr7,%0" : "=r" (data));
696 __asm __volatile("movl %0,%%dr7" : : "r" (dr7));
699 static __inline u_char
700 read_cyrix_reg(u_char reg)
707 write_cyrix_reg(u_char reg, u_char data)
713 static __inline register_t
718 eflags = read_eflags();
724 intr_restore(register_t eflags)
726 write_eflags(eflags);
729 static __inline uint32_t
734 __asm __volatile("rdpkru" : "=a" (res) : "c" (0) : "edx");
739 wrpkru(uint32_t mask)
742 __asm __volatile("wrpkru" : : "a" (mask), "c" (0), "d" (0));
745 #else /* !(__GNUCLIKE_ASM && __CC_SUPPORTS___INLINE) */
747 int breakpoint(void);
748 u_int bsfl(u_int mask);
749 u_int bsrl(u_int mask);
750 void clflush(u_long addr);
752 void cpuid_count(u_int ax, u_int cx, u_int *p);
753 void disable_intr(void);
754 void do_cpuid(u_int ax, u_int *p);
755 void enable_intr(void);
757 void ia32_pause(void);
758 u_char inb(u_int port);
759 u_int inl(u_int port);
760 void insb(u_int port, void *addr, size_t count);
761 void insl(u_int port, void *addr, size_t count);
762 void insw(u_int port, void *addr, size_t count);
763 register_t intr_disable(void);
764 void intr_restore(register_t ef);
766 void invlpg(u_int addr);
768 u_short inw(u_int port);
769 void lidt(struct region_descriptor *addr);
770 void lldt(u_short sel);
771 void load_cr0(u_int cr0);
772 void load_cr3(u_int cr3);
773 void load_cr4(u_int cr4);
774 void load_dr0(u_int dr0);
775 void load_dr1(u_int dr1);
776 void load_dr2(u_int dr2);
777 void load_dr3(u_int dr3);
778 void load_dr6(u_int dr6);
779 void load_dr7(u_int dr7);
780 void load_fs(u_short sel);
781 void load_gs(u_short sel);
782 void ltr(u_short sel);
783 void outb(u_int port, u_char data);
784 void outl(u_int port, u_int data);
785 void outsb(u_int port, const void *addr, size_t count);
786 void outsl(u_int port, const void *addr, size_t count);
787 void outsw(u_int port, const void *addr, size_t count);
788 void outw(u_int port, u_short data);
793 uint64_t rdmsr(u_int msr);
794 uint64_t rdpmc(u_int pmc);
801 uint64_t rdtsc(void);
802 u_char read_cyrix_reg(u_char reg);
803 u_int read_eflags(void);
811 void write_cyrix_reg(u_char reg, u_char data);
812 void write_eflags(u_int ef);
813 void wrmsr(u_int msr, uint64_t newval);
815 #endif /* __GNUCLIKE_ASM && __CC_SUPPORTS___INLINE */
817 void reset_dbregs(void);
820 int rdmsr_safe(u_int msr, uint64_t *val);
821 int wrmsr_safe(u_int msr, uint64_t newval);
824 #endif /* !_MACHINE_CPUFUNC_H_ */