xref: /freebsd/sys/amd64/include/cpufunc.h (revision 6eda157eaa3f7dc35ff109a4981eb4110e530b5d)
13c4dd356SDavid Greenman /*-
23c4dd356SDavid Greenman  * Copyright (c) 1993 The Regents of the University of California.
33c4dd356SDavid Greenman  * All rights reserved.
43c4dd356SDavid Greenman  *
53c4dd356SDavid Greenman  * Redistribution and use in source and binary forms, with or without
63c4dd356SDavid Greenman  * modification, are permitted provided that the following conditions
73c4dd356SDavid Greenman  * are met:
83c4dd356SDavid Greenman  * 1. Redistributions of source code must retain the above copyright
93c4dd356SDavid Greenman  *    notice, this list of conditions and the following disclaimer.
103c4dd356SDavid Greenman  * 2. Redistributions in binary form must reproduce the above copyright
113c4dd356SDavid Greenman  *    notice, this list of conditions and the following disclaimer in the
123c4dd356SDavid Greenman  *    documentation and/or other materials provided with the distribution.
133c4dd356SDavid Greenman  * 3. All advertising materials mentioning features or use of this software
143c4dd356SDavid Greenman  *    must display the following acknowledgement:
153c4dd356SDavid Greenman  *	This product includes software developed by the University of
163c4dd356SDavid Greenman  *	California, Berkeley and its contributors.
173c4dd356SDavid Greenman  * 4. Neither the name of the University nor the names of its contributors
183c4dd356SDavid Greenman  *    may be used to endorse or promote products derived from this software
193c4dd356SDavid Greenman  *    without specific prior written permission.
203c4dd356SDavid Greenman  *
213c4dd356SDavid Greenman  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
223c4dd356SDavid Greenman  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
233c4dd356SDavid Greenman  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
243c4dd356SDavid Greenman  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
253c4dd356SDavid Greenman  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
263c4dd356SDavid Greenman  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
273c4dd356SDavid Greenman  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
283c4dd356SDavid Greenman  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
293c4dd356SDavid Greenman  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
303c4dd356SDavid Greenman  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
313c4dd356SDavid Greenman  * SUCH DAMAGE.
323c4dd356SDavid Greenman  *
33c3aac50fSPeter Wemm  * $FreeBSD$
343c4dd356SDavid Greenman  */
353c4dd356SDavid Greenman 
365b81b6b3SRodney W. Grimes /*
375b81b6b3SRodney W. Grimes  * Functions to provide access to special i386 instructions.
385b81b6b3SRodney W. Grimes  */
395b81b6b3SRodney W. Grimes 
406e393973SGarrett Wollman #ifndef _MACHINE_CPUFUNC_H_
41004bedebSBruce Evans #define	_MACHINE_CPUFUNC_H_
426e393973SGarrett Wollman 
4329d5de8aSWarner Losh #include <sys/cdefs.h>
4429d5de8aSWarner Losh 
4529d5de8aSWarner Losh __BEGIN_DECLS
46e31fa854SDoug Rabson #define readb(va)	(*(volatile u_int8_t *) (va))
47e31fa854SDoug Rabson #define readw(va)	(*(volatile u_int16_t *) (va))
48e31fa854SDoug Rabson #define readl(va)	(*(volatile u_int32_t *) (va))
49e31fa854SDoug Rabson 
50e31fa854SDoug Rabson #define writeb(va, d)	(*(volatile u_int8_t *) (va) = (d))
51e31fa854SDoug Rabson #define writew(va, d)	(*(volatile u_int16_t *) (va) = (d))
52e31fa854SDoug Rabson #define writel(va, d)	(*(volatile u_int32_t *) (va) = (d))
53e31fa854SDoug Rabson 
545b81b6b3SRodney W. Grimes #ifdef	__GNUC__
555b81b6b3SRodney W. Grimes 
562a32c15fSBruce Evans #ifdef SWTCH_OPTIM_STATS
572a32c15fSBruce Evans extern	int	tlb_flush_count;	/* XXX */
582a32c15fSBruce Evans #endif
592a32c15fSBruce Evans 
605dbd168eSBruce Evans static __inline void
615dbd168eSBruce Evans breakpoint(void)
62004bedebSBruce Evans {
63004bedebSBruce Evans 	__asm __volatile("int $3");
645b81b6b3SRodney W. Grimes }
655b81b6b3SRodney W. Grimes 
66c83b1328SBruce Evans static __inline u_int
67c83b1328SBruce Evans bsfl(u_int mask)
68c83b1328SBruce Evans {
69c83b1328SBruce Evans 	u_int	result;
70c83b1328SBruce Evans 
71c83b1328SBruce Evans 	__asm __volatile("bsfl %0,%0" : "=r" (result) : "0" (mask));
72c83b1328SBruce Evans 	return (result);
73c83b1328SBruce Evans }
74c83b1328SBruce Evans 
75c83b1328SBruce Evans static __inline u_int
76c83b1328SBruce Evans bsrl(u_int mask)
77c83b1328SBruce Evans {
78c83b1328SBruce Evans 	u_int	result;
79c83b1328SBruce Evans 
80c83b1328SBruce Evans 	__asm __volatile("bsrl %0,%0" : "=r" (result) : "0" (mask));
81c83b1328SBruce Evans 	return (result);
82c83b1328SBruce Evans }
83c83b1328SBruce Evans 
84004bedebSBruce Evans static __inline void
855b81b6b3SRodney W. Grimes disable_intr(void)
865b81b6b3SRodney W. Grimes {
878966b85cSJohn Dyson 	__asm __volatile("cli" : : : "memory");
885b81b6b3SRodney W. Grimes }
895b81b6b3SRodney W. Grimes 
90004bedebSBruce Evans static __inline void
915b81b6b3SRodney W. Grimes enable_intr(void)
925b81b6b3SRodney W. Grimes {
93fadc51bdSBruce Evans 	__asm __volatile("sti");
945b81b6b3SRodney W. Grimes }
955b81b6b3SRodney W. Grimes 
96264c3d87SPeter Wemm #define	HAVE_INLINE_FFS
97264c3d87SPeter Wemm 
98264c3d87SPeter Wemm static __inline int
99264c3d87SPeter Wemm ffs(int mask)
100264c3d87SPeter Wemm {
101264c3d87SPeter Wemm 	/*
102004bedebSBruce Evans 	 * Note that gcc-2's builtin ffs would be used if we didn't declare
103004bedebSBruce Evans 	 * this inline or turn off the builtin.  The builtin is faster but
104c83b1328SBruce Evans 	 * broken in gcc-2.4.5 and slower but working in gcc-2.5 and later
105c83b1328SBruce Evans 	 * versions.
106004bedebSBruce Evans 	 */
107c83b1328SBruce Evans 	 return (mask == 0 ? mask : bsfl((u_int)mask) + 1);
108004bedebSBruce Evans }
109004bedebSBruce Evans 
11013f588f8SGarrett Wollman #define	HAVE_INLINE_FLS
11113f588f8SGarrett Wollman 
11213f588f8SGarrett Wollman static __inline int
11313f588f8SGarrett Wollman fls(int mask)
11413f588f8SGarrett Wollman {
115c83b1328SBruce Evans 	return (mask == 0 ? mask : bsrl((u_int)mask) + 1);
11613f588f8SGarrett Wollman }
11713f588f8SGarrett Wollman 
118004bedebSBruce Evans #if __GNUC__ < 2
119004bedebSBruce Evans 
120004bedebSBruce Evans #define	inb(port)		inbv(port)
121004bedebSBruce Evans #define	outb(port, data)	outbv(port, data)
122004bedebSBruce Evans 
123004bedebSBruce Evans #else /* __GNUC >= 2 */
124004bedebSBruce Evans 
125004bedebSBruce Evans /*
1268089a043SBruce Evans  * The following complications are to get around gcc not having a
1278089a043SBruce Evans  * constraint letter for the range 0..255.  We still put "d" in the
1288089a043SBruce Evans  * constraint because "i" isn't a valid constraint when the port
1298089a043SBruce Evans  * isn't constant.  This only matters for -O0 because otherwise
1308089a043SBruce Evans  * the non-working version gets optimized away.
1318089a043SBruce Evans  *
132004bedebSBruce Evans  * Use an expression-statement instead of a conditional expression
133004bedebSBruce Evans  * because gcc-2.6.0 would promote the operands of the conditional
134004bedebSBruce Evans  * and produce poor code for "if ((inb(var) & const1) == const2)".
135388dfa71SBruce Evans  *
136388dfa71SBruce Evans  * The unnecessary test `(port) < 0x10000' is to generate a warning if
137388dfa71SBruce Evans  * the `port' has type u_short or smaller.  Such types are pessimal.
138388dfa71SBruce Evans  * This actually only works for signed types.  The range check is
139388dfa71SBruce Evans  * careful to avoid generating warnings.
140004bedebSBruce Evans  */
141388dfa71SBruce Evans #define	inb(port) __extension__ ({					\
142004bedebSBruce Evans 	u_char	_data;							\
143388dfa71SBruce Evans 	if (__builtin_constant_p(port) && ((port) & 0xffff) < 0x100	\
144388dfa71SBruce Evans 	    && (port) < 0x10000)					\
145004bedebSBruce Evans 		_data = inbc(port);					\
146004bedebSBruce Evans 	else								\
147004bedebSBruce Evans 		_data = inbv(port);					\
148004bedebSBruce Evans 	_data; })
149004bedebSBruce Evans 
150388dfa71SBruce Evans #define	outb(port, data) (						\
151388dfa71SBruce Evans 	__builtin_constant_p(port) && ((port) & 0xffff) < 0x100		\
152388dfa71SBruce Evans 	&& (port) < 0x10000						\
153004bedebSBruce Evans 	? outbc(port, data) : outbv(port, data))
154004bedebSBruce Evans 
155004bedebSBruce Evans static __inline u_char
156004bedebSBruce Evans inbc(u_int port)
157004bedebSBruce Evans {
158004bedebSBruce Evans 	u_char	data;
159004bedebSBruce Evans 
1608089a043SBruce Evans 	__asm __volatile("inb %1,%0" : "=a" (data) : "id" ((u_short)(port)));
161004bedebSBruce Evans 	return (data);
162004bedebSBruce Evans }
163004bedebSBruce Evans 
164004bedebSBruce Evans static __inline void
165004bedebSBruce Evans outbc(u_int port, u_char data)
166004bedebSBruce Evans {
1678089a043SBruce Evans 	__asm __volatile("outb %0,%1" : : "a" (data), "id" ((u_short)(port)));
168004bedebSBruce Evans }
169004bedebSBruce Evans 
170004bedebSBruce Evans #endif /* __GNUC <= 2 */
171004bedebSBruce Evans 
172004bedebSBruce Evans static __inline u_char
173004bedebSBruce Evans inbv(u_int port)
174004bedebSBruce Evans {
175004bedebSBruce Evans 	u_char	data;
176004bedebSBruce Evans 	/*
177004bedebSBruce Evans 	 * We use %%dx and not %1 here because i/o is done at %dx and not at
178004bedebSBruce Evans 	 * %edx, while gcc generates inferior code (movw instead of movl)
179004bedebSBruce Evans 	 * if we tell it to load (u_short) port.
180004bedebSBruce Evans 	 */
181004bedebSBruce Evans 	__asm __volatile("inb %%dx,%0" : "=a" (data) : "d" (port));
182004bedebSBruce Evans 	return (data);
183004bedebSBruce Evans }
184004bedebSBruce Evans 
18500be8601SBruce Evans static __inline u_int
186004bedebSBruce Evans inl(u_int port)
187004bedebSBruce Evans {
18800be8601SBruce Evans 	u_int	data;
189004bedebSBruce Evans 
190004bedebSBruce Evans 	__asm __volatile("inl %%dx,%0" : "=a" (data) : "d" (port));
191004bedebSBruce Evans 	return (data);
192004bedebSBruce Evans }
193004bedebSBruce Evans 
194004bedebSBruce Evans static __inline void
195004bedebSBruce Evans insb(u_int port, void *addr, size_t cnt)
196004bedebSBruce Evans {
197004bedebSBruce Evans 	__asm __volatile("cld; rep; insb"
198896763faSBruce Evans 			 : "=D" (addr), "=c" (cnt)
199896763faSBruce Evans 			 :  "0" (addr),  "1" (cnt), "d" (port)
200896763faSBruce Evans 			 : "memory");
201004bedebSBruce Evans }
202004bedebSBruce Evans 
203004bedebSBruce Evans static __inline void
204004bedebSBruce Evans insw(u_int port, void *addr, size_t cnt)
205004bedebSBruce Evans {
206004bedebSBruce Evans 	__asm __volatile("cld; rep; insw"
207896763faSBruce Evans 			 : "=D" (addr), "=c" (cnt)
208896763faSBruce Evans 			 :  "0" (addr),  "1" (cnt), "d" (port)
209896763faSBruce Evans 			 : "memory");
210004bedebSBruce Evans }
211004bedebSBruce Evans 
212004bedebSBruce Evans static __inline void
213004bedebSBruce Evans insl(u_int port, void *addr, size_t cnt)
214004bedebSBruce Evans {
215004bedebSBruce Evans 	__asm __volatile("cld; rep; insl"
216896763faSBruce Evans 			 : "=D" (addr), "=c" (cnt)
217896763faSBruce Evans 			 :  "0" (addr),  "1" (cnt), "d" (port)
218896763faSBruce Evans 			 : "memory");
219004bedebSBruce Evans }
220004bedebSBruce Evans 
221ece15d78SBruce Evans static __inline void
2224c024bbdSKATO Takenori invd(void)
2234c024bbdSKATO Takenori {
2244c024bbdSKATO Takenori 	__asm __volatile("invd");
2254c024bbdSKATO Takenori }
2264c024bbdSKATO Takenori 
227664a31e4SPeter Wemm #if defined(SMP) && defined(_KERNEL)
228477a642cSPeter Wemm 
229477a642cSPeter Wemm /*
230f48bbd5fSBruce Evans  * When using APIC IPI's, invlpg() is not simply the invlpg instruction
231f48bbd5fSBruce Evans  * (this is a bug) and the inlining cost is prohibitive since the call
232f40e6078SPeter Wemm  * executes into the IPI transmission system.
233477a642cSPeter Wemm  */
234a5e25da4SWarner Losh void	invlpg		__P((u_int addr));
235a5e25da4SWarner Losh void	invltlb		__P((void));
236477a642cSPeter Wemm 
2375498a452SJohn Dyson static __inline void
2385498a452SJohn Dyson cpu_invlpg(void *addr)
2395498a452SJohn Dyson {
2405498a452SJohn Dyson 	__asm __volatile("invlpg %0" : : "m" (*(char *)addr) : "memory");
2415498a452SJohn Dyson }
2425498a452SJohn Dyson 
2435931a9c2STor Egge static __inline void
2445931a9c2STor Egge cpu_invltlb(void)
2455931a9c2STor Egge {
24600be8601SBruce Evans 	u_int	temp;
2475931a9c2STor Egge 	/*
2485931a9c2STor Egge 	 * This should be implemented as load_cr3(rcr3()) when load_cr3()
2495931a9c2STor Egge 	 * is inlined.
2505931a9c2STor Egge 	 */
2515931a9c2STor Egge 	__asm __volatile("movl %%cr3, %0; movl %0, %%cr3" : "=r" (temp)
2525931a9c2STor Egge 			 : : "memory");
2535931a9c2STor Egge #if defined(SWTCH_OPTIM_STATS)
2545931a9c2STor Egge 	++tlb_flush_count;
2555931a9c2STor Egge #endif
2565931a9c2STor Egge }
257f48bbd5fSBruce Evans 
258664a31e4SPeter Wemm #else /* !(SMP && _KERNEL) */
259477a642cSPeter Wemm 
2604c024bbdSKATO Takenori static __inline void
261471176aaSJohn Dyson invlpg(u_int addr)
262ece15d78SBruce Evans {
2635498a452SJohn Dyson 	__asm __volatile("invlpg %0" : : "m" (*(char *)addr) : "memory");
264ece15d78SBruce Evans }
265ece15d78SBruce Evans 
266ece15d78SBruce Evans static __inline void
267ece15d78SBruce Evans invltlb(void)
268ece15d78SBruce Evans {
26900be8601SBruce Evans 	u_int	temp;
270ece15d78SBruce Evans 	/*
271ece15d78SBruce Evans 	 * This should be implemented as load_cr3(rcr3()) when load_cr3()
272ece15d78SBruce Evans 	 * is inlined.
273ece15d78SBruce Evans 	 */
274ece15d78SBruce Evans 	__asm __volatile("movl %%cr3, %0; movl %0, %%cr3" : "=r" (temp)
275ece15d78SBruce Evans 			 : : "memory");
276f48bbd5fSBruce Evans #ifdef SWTCH_OPTIM_STATS
27782566551SJohn Dyson 	++tlb_flush_count;
27882566551SJohn Dyson #endif
279ece15d78SBruce Evans }
2802c5d02ffSSteve Passe 
281664a31e4SPeter Wemm #endif /* SMP && _KERNEL */
282ece15d78SBruce Evans 
283004bedebSBruce Evans static __inline u_short
284004bedebSBruce Evans inw(u_int port)
285004bedebSBruce Evans {
286004bedebSBruce Evans 	u_short	data;
287004bedebSBruce Evans 
288004bedebSBruce Evans 	__asm __volatile("inw %%dx,%0" : "=a" (data) : "d" (port));
289004bedebSBruce Evans 	return (data);
290004bedebSBruce Evans }
291004bedebSBruce Evans 
292004bedebSBruce Evans static __inline void
293004bedebSBruce Evans outbv(u_int port, u_char data)
294004bedebSBruce Evans {
295004bedebSBruce Evans 	u_char	al;
296004bedebSBruce Evans 	/*
297004bedebSBruce Evans 	 * Use an unnecessary assignment to help gcc's register allocator.
298004bedebSBruce Evans 	 * This make a large difference for gcc-1.40 and a tiny difference
299004bedebSBruce Evans 	 * for gcc-2.6.0.  For gcc-1.40, al had to be ``asm("ax")'' for
300004bedebSBruce Evans 	 * best results.  gcc-2.6.0 can't handle this.
301004bedebSBruce Evans 	 */
302004bedebSBruce Evans 	al = data;
303004bedebSBruce Evans 	__asm __volatile("outb %0,%%dx" : : "a" (al), "d" (port));
304004bedebSBruce Evans }
305004bedebSBruce Evans 
306004bedebSBruce Evans static __inline void
30700be8601SBruce Evans outl(u_int port, u_int data)
308004bedebSBruce Evans {
309004bedebSBruce Evans 	/*
310004bedebSBruce Evans 	 * outl() and outw() aren't used much so we haven't looked at
311004bedebSBruce Evans 	 * possible micro-optimizations such as the unnecessary
312004bedebSBruce Evans 	 * assignment for them.
313004bedebSBruce Evans 	 */
314004bedebSBruce Evans 	__asm __volatile("outl %0,%%dx" : : "a" (data), "d" (port));
315004bedebSBruce Evans }
316004bedebSBruce Evans 
317004bedebSBruce Evans static __inline void
318e1a1bba4SJustin T. Gibbs outsb(u_int port, const void *addr, size_t cnt)
319004bedebSBruce Evans {
320004bedebSBruce Evans 	__asm __volatile("cld; rep; outsb"
321896763faSBruce Evans 			 : "=S" (addr), "=c" (cnt)
322896763faSBruce Evans 			 :  "0" (addr),  "1" (cnt), "d" (port));
323004bedebSBruce Evans }
324004bedebSBruce Evans 
325004bedebSBruce Evans static __inline void
326e1a1bba4SJustin T. Gibbs outsw(u_int port, const void *addr, size_t cnt)
327004bedebSBruce Evans {
328004bedebSBruce Evans 	__asm __volatile("cld; rep; outsw"
329896763faSBruce Evans 			 : "=S" (addr), "=c" (cnt)
330896763faSBruce Evans 			 :  "0" (addr),  "1" (cnt), "d" (port));
331004bedebSBruce Evans }
332004bedebSBruce Evans 
333004bedebSBruce Evans static __inline void
334e1a1bba4SJustin T. Gibbs outsl(u_int port, const void *addr, size_t cnt)
335004bedebSBruce Evans {
336004bedebSBruce Evans 	__asm __volatile("cld; rep; outsl"
337896763faSBruce Evans 			 : "=S" (addr), "=c" (cnt)
338896763faSBruce Evans 			 :  "0" (addr),  "1" (cnt), "d" (port));
339004bedebSBruce Evans }
340004bedebSBruce Evans 
341004bedebSBruce Evans static __inline void
342004bedebSBruce Evans outw(u_int port, u_short data)
343004bedebSBruce Evans {
344004bedebSBruce Evans 	__asm __volatile("outw %0,%%dx" : : "a" (data), "d" (port));
345004bedebSBruce Evans }
346004bedebSBruce Evans 
34700be8601SBruce Evans static __inline u_int
348004bedebSBruce Evans rcr2(void)
349004bedebSBruce Evans {
35000be8601SBruce Evans 	u_int	data;
351004bedebSBruce Evans 
352004bedebSBruce Evans 	__asm __volatile("movl %%cr2,%0" : "=r" (data));
353004bedebSBruce Evans 	return (data);
354004bedebSBruce Evans }
355004bedebSBruce Evans 
35600be8601SBruce Evans static __inline u_int
357004bedebSBruce Evans read_eflags(void)
3585b81b6b3SRodney W. Grimes {
35900be8601SBruce Evans 	u_int	ef;
360004bedebSBruce Evans 
361004bedebSBruce Evans 	__asm __volatile("pushfl; popl %0" : "=r" (ef));
3628db02de8SPaul Richards 	return (ef);
3635b81b6b3SRodney W. Grimes }
3645b81b6b3SRodney W. Grimes 
36500be8601SBruce Evans static __inline u_int64_t
3665dbd168eSBruce Evans rdmsr(u_int msr)
3675dbd168eSBruce Evans {
36800be8601SBruce Evans 	u_int64_t rv;
3695dbd168eSBruce Evans 
37039413503SMark Murray 	__asm __volatile("rdmsr" : "=A" (rv) : "c" (msr));
3715dbd168eSBruce Evans 	return (rv);
3725dbd168eSBruce Evans }
3735dbd168eSBruce Evans 
37400be8601SBruce Evans static __inline u_int64_t
3755dbd168eSBruce Evans rdpmc(u_int pmc)
3765dbd168eSBruce Evans {
37700be8601SBruce Evans 	u_int64_t rv;
3785dbd168eSBruce Evans 
37939413503SMark Murray 	__asm __volatile("rdpmc" : "=A" (rv) : "c" (pmc));
3805dbd168eSBruce Evans 	return (rv);
3815dbd168eSBruce Evans }
3825dbd168eSBruce Evans 
38300be8601SBruce Evans static __inline u_int64_t
3845dbd168eSBruce Evans rdtsc(void)
3855dbd168eSBruce Evans {
38600be8601SBruce Evans 	u_int64_t rv;
3875dbd168eSBruce Evans 
38839413503SMark Murray 	__asm __volatile("rdtsc" : "=A" (rv));
3895dbd168eSBruce Evans 	return (rv);
3905dbd168eSBruce Evans }
3915dbd168eSBruce Evans 
392004bedebSBruce Evans static __inline void
3934c024bbdSKATO Takenori wbinvd(void)
3944c024bbdSKATO Takenori {
3954c024bbdSKATO Takenori 	__asm __volatile("wbinvd");
3964c024bbdSKATO Takenori }
3974c024bbdSKATO Takenori 
3984c024bbdSKATO Takenori static __inline void
39900be8601SBruce Evans write_eflags(u_int ef)
4005b81b6b3SRodney W. Grimes {
401004bedebSBruce Evans 	__asm __volatile("pushl %0; popfl" : : "r" (ef));
4025b81b6b3SRodney W. Grimes }
4035b81b6b3SRodney W. Grimes 
404d69e8502SGarrett Wollman static __inline void
40500be8601SBruce Evans wrmsr(u_int msr, u_int64_t newval)
406d69e8502SGarrett Wollman {
40739413503SMark Murray 	__asm __volatile("wrmsr" : : "A" (newval), "c" (msr));
408d69e8502SGarrett Wollman }
409d69e8502SGarrett Wollman 
4105206bca1SLuoqi Chen static __inline u_int
4115206bca1SLuoqi Chen rfs(void)
4125206bca1SLuoqi Chen {
4135206bca1SLuoqi Chen 	u_int sel;
414e870e9b2SLuoqi Chen 	__asm __volatile("movl %%fs,%0" : "=rm" (sel));
4155206bca1SLuoqi Chen 	return (sel);
4165206bca1SLuoqi Chen }
4175206bca1SLuoqi Chen 
4185206bca1SLuoqi Chen static __inline u_int
4195206bca1SLuoqi Chen rgs(void)
4205206bca1SLuoqi Chen {
4215206bca1SLuoqi Chen 	u_int sel;
422e870e9b2SLuoqi Chen 	__asm __volatile("movl %%gs,%0" : "=rm" (sel));
4235206bca1SLuoqi Chen 	return (sel);
4245206bca1SLuoqi Chen }
4255206bca1SLuoqi Chen 
4265206bca1SLuoqi Chen static __inline void
4275206bca1SLuoqi Chen load_fs(u_int sel)
4285206bca1SLuoqi Chen {
429e870e9b2SLuoqi Chen 	__asm __volatile("movl %0,%%fs" : : "rm" (sel));
4305206bca1SLuoqi Chen }
4315206bca1SLuoqi Chen 
4325206bca1SLuoqi Chen static __inline void
4335206bca1SLuoqi Chen load_gs(u_int sel)
4345206bca1SLuoqi Chen {
435e870e9b2SLuoqi Chen 	__asm __volatile("movl %0,%%gs" : : "rm" (sel));
4365206bca1SLuoqi Chen }
4375206bca1SLuoqi Chen 
438de8050f9SBrian S. Dean static __inline u_int
439de8050f9SBrian S. Dean rdr0(void)
440de8050f9SBrian S. Dean {
441de8050f9SBrian S. Dean 	u_int	data;
44280275388SBrian S. Dean 	__asm __volatile("movl %%dr0,%0" : "=r" (data));
443de8050f9SBrian S. Dean 	return (data);
444de8050f9SBrian S. Dean }
445de8050f9SBrian S. Dean 
4466eda157eSBrian S. Dean static __inline void
4476eda157eSBrian S. Dean load_dr0(u_int sel)
4486eda157eSBrian S. Dean {
4496eda157eSBrian S. Dean 	__asm __volatile("movl %0,%%dr0" : : "r" (sel));
4506eda157eSBrian S. Dean }
4516eda157eSBrian S. Dean 
452de8050f9SBrian S. Dean static __inline u_int
453de8050f9SBrian S. Dean rdr1(void)
454de8050f9SBrian S. Dean {
455de8050f9SBrian S. Dean 	u_int	data;
45680275388SBrian S. Dean 	__asm __volatile("movl %%dr1,%0" : "=r" (data));
457de8050f9SBrian S. Dean 	return (data);
458de8050f9SBrian S. Dean }
459de8050f9SBrian S. Dean 
4606eda157eSBrian S. Dean static __inline void
4616eda157eSBrian S. Dean load_dr1(u_int sel)
4626eda157eSBrian S. Dean {
4636eda157eSBrian S. Dean 	__asm __volatile("movl %0,%%dr1" : : "r" (sel));
4646eda157eSBrian S. Dean }
4656eda157eSBrian S. Dean 
466de8050f9SBrian S. Dean static __inline u_int
467de8050f9SBrian S. Dean rdr2(void)
468de8050f9SBrian S. Dean {
469de8050f9SBrian S. Dean 	u_int	data;
47080275388SBrian S. Dean 	__asm __volatile("movl %%dr2,%0" : "=r" (data));
471de8050f9SBrian S. Dean 	return (data);
472de8050f9SBrian S. Dean }
473de8050f9SBrian S. Dean 
4746eda157eSBrian S. Dean static __inline void
4756eda157eSBrian S. Dean load_dr2(u_int sel)
4766eda157eSBrian S. Dean {
4776eda157eSBrian S. Dean 	__asm __volatile("movl %0,%%dr2" : : "r" (sel));
4786eda157eSBrian S. Dean }
4796eda157eSBrian S. Dean 
480de8050f9SBrian S. Dean static __inline u_int
481de8050f9SBrian S. Dean rdr3(void)
482de8050f9SBrian S. Dean {
483de8050f9SBrian S. Dean 	u_int	data;
48480275388SBrian S. Dean 	__asm __volatile("movl %%dr3,%0" : "=r" (data));
485de8050f9SBrian S. Dean 	return (data);
486de8050f9SBrian S. Dean }
487de8050f9SBrian S. Dean 
4886eda157eSBrian S. Dean static __inline void
4896eda157eSBrian S. Dean load_dr3(u_int sel)
4906eda157eSBrian S. Dean {
4916eda157eSBrian S. Dean 	__asm __volatile("movl %0,%%dr3" : : "r" (sel));
4926eda157eSBrian S. Dean }
4936eda157eSBrian S. Dean 
4946eda157eSBrian S. Dean static __inline u_int
4956eda157eSBrian S. Dean rdr4(void)
4966eda157eSBrian S. Dean {
4976eda157eSBrian S. Dean 	u_int	data;
4986eda157eSBrian S. Dean 	__asm __volatile("movl %%dr4,%0" : "=r" (data));
4996eda157eSBrian S. Dean 	return (data);
5006eda157eSBrian S. Dean }
5016eda157eSBrian S. Dean 
5026eda157eSBrian S. Dean static __inline void
5036eda157eSBrian S. Dean load_dr4(u_int sel)
5046eda157eSBrian S. Dean {
5056eda157eSBrian S. Dean 	__asm __volatile("movl %0,%%dr4" : : "r" (sel));
5066eda157eSBrian S. Dean }
5076eda157eSBrian S. Dean 
5086eda157eSBrian S. Dean static __inline u_int
5096eda157eSBrian S. Dean rdr5(void)
5106eda157eSBrian S. Dean {
5116eda157eSBrian S. Dean 	u_int	data;
5126eda157eSBrian S. Dean 	__asm __volatile("movl %%dr5,%0" : "=r" (data));
5136eda157eSBrian S. Dean 	return (data);
5146eda157eSBrian S. Dean }
5156eda157eSBrian S. Dean 
5166eda157eSBrian S. Dean static __inline void
5176eda157eSBrian S. Dean load_dr5(u_int sel)
5186eda157eSBrian S. Dean {
5196eda157eSBrian S. Dean 	__asm __volatile("movl %0,%%dr5" : : "r" (sel));
5206eda157eSBrian S. Dean }
5216eda157eSBrian S. Dean 
522de8050f9SBrian S. Dean static __inline u_int
523de8050f9SBrian S. Dean rdr6(void)
524de8050f9SBrian S. Dean {
525de8050f9SBrian S. Dean 	u_int	data;
52680275388SBrian S. Dean 	__asm __volatile("movl %%dr6,%0" : "=r" (data));
527de8050f9SBrian S. Dean 	return (data);
528de8050f9SBrian S. Dean }
529de8050f9SBrian S. Dean 
5306eda157eSBrian S. Dean static __inline void
5316eda157eSBrian S. Dean load_dr6(u_int sel)
5326eda157eSBrian S. Dean {
5336eda157eSBrian S. Dean 	__asm __volatile("movl %0,%%dr6" : : "r" (sel));
5346eda157eSBrian S. Dean }
5356eda157eSBrian S. Dean 
536de8050f9SBrian S. Dean static __inline u_int
537de8050f9SBrian S. Dean rdr7(void)
538de8050f9SBrian S. Dean {
539de8050f9SBrian S. Dean 	u_int	data;
54080275388SBrian S. Dean 	__asm __volatile("movl %%dr7,%0" : "=r" (data));
541de8050f9SBrian S. Dean 	return (data);
542de8050f9SBrian S. Dean }
543de8050f9SBrian S. Dean 
5446eda157eSBrian S. Dean static __inline void
5456eda157eSBrian S. Dean load_dr7(u_int sel)
5466eda157eSBrian S. Dean {
5476eda157eSBrian S. Dean 	__asm __volatile("movl %0,%%dr7" : : "r" (sel));
5486eda157eSBrian S. Dean }
5496eda157eSBrian S. Dean 
550034dc442SJohn Baldwin static __inline critical_t
551034dc442SJohn Baldwin critical_enter(void)
552034dc442SJohn Baldwin {
553034dc442SJohn Baldwin 	critical_t eflags;
554034dc442SJohn Baldwin 
555034dc442SJohn Baldwin 	eflags = read_eflags();
556034dc442SJohn Baldwin 	disable_intr();
557034dc442SJohn Baldwin 	return (eflags);
558034dc442SJohn Baldwin }
559034dc442SJohn Baldwin 
560034dc442SJohn Baldwin static __inline void
561034dc442SJohn Baldwin critical_exit(critical_t eflags)
562034dc442SJohn Baldwin {
563034dc442SJohn Baldwin 	write_eflags(eflags);
564034dc442SJohn Baldwin }
565034dc442SJohn Baldwin 
566004bedebSBruce Evans #else /* !__GNUC__ */
5675b81b6b3SRodney W. Grimes 
568a5e25da4SWarner Losh int	breakpoint	__P((void));
569a5e25da4SWarner Losh u_int	bsfl		__P((u_int mask));
570a5e25da4SWarner Losh u_int	bsrl		__P((u_int mask));
571a5e25da4SWarner Losh void	disable_intr	__P((void));
572a5e25da4SWarner Losh void	enable_intr	__P((void));
573a5e25da4SWarner Losh u_char	inb		__P((u_int port));
574a5e25da4SWarner Losh u_int	inl		__P((u_int port));
575a5e25da4SWarner Losh void	insb		__P((u_int port, void *addr, size_t cnt));
576a5e25da4SWarner Losh void	insl		__P((u_int port, void *addr, size_t cnt));
577a5e25da4SWarner Losh void	insw		__P((u_int port, void *addr, size_t cnt));
578a5e25da4SWarner Losh void	invd		__P((void));
579a5e25da4SWarner Losh void	invlpg		__P((u_int addr));
580a5e25da4SWarner Losh void	invltlb		__P((void));
581a5e25da4SWarner Losh u_short	inw		__P((u_int port));
582a5e25da4SWarner Losh void	outb		__P((u_int port, u_char data));
583a5e25da4SWarner Losh void	outl		__P((u_int port, u_int data));
584a5e25da4SWarner Losh void	outsb		__P((u_int port, void *addr, size_t cnt));
585a5e25da4SWarner Losh void	outsl		__P((u_int port, void *addr, size_t cnt));
586a5e25da4SWarner Losh void	outsw		__P((u_int port, void *addr, size_t cnt));
587a5e25da4SWarner Losh void	outw		__P((u_int port, u_short data));
588a5e25da4SWarner Losh u_int	rcr2		__P((void));
589a5e25da4SWarner Losh u_int64_t rdmsr		__P((u_int msr));
590a5e25da4SWarner Losh u_int64_t rdpmc		__P((u_int pmc));
591a5e25da4SWarner Losh u_int64_t rdtsc		__P((void));
592a5e25da4SWarner Losh u_int	read_eflags	__P((void));
593a5e25da4SWarner Losh void	wbinvd		__P((void));
594a5e25da4SWarner Losh void	write_eflags	__P((u_int ef));
595a5e25da4SWarner Losh void	wrmsr		__P((u_int msr, u_int64_t newval));
596a5e25da4SWarner Losh u_int	rfs		__P((void));
597a5e25da4SWarner Losh u_int	rgs		__P((void));
598a5e25da4SWarner Losh void	load_fs		__P((u_int sel));
599a5e25da4SWarner Losh void	load_gs		__P((u_int sel));
600a5e25da4SWarner Losh critical_t critical_enter __P((void));
601a5e25da4SWarner Losh void	critical_exit	__P((critical_t eflags));
602004bedebSBruce Evans 
6035b81b6b3SRodney W. Grimes #endif	/* __GNUC__ */
6045b81b6b3SRodney W. Grimes 
605a5e25da4SWarner Losh void	load_cr0	__P((u_int cr0));
606a5e25da4SWarner Losh void	load_cr3	__P((u_int cr3));
607a5e25da4SWarner Losh void	load_cr4	__P((u_int cr4));
608a5e25da4SWarner Losh void	ltr		__P((u_short sel));
609a5e25da4SWarner Losh u_int	rcr0		__P((void));
610a5e25da4SWarner Losh u_int	rcr3		__P((void));
611a5e25da4SWarner Losh u_int	rcr4		__P((void));
612a5e25da4SWarner Losh void    reset_dbregs    __P((void));
61329d5de8aSWarner Losh __END_DECLS
6145b81b6b3SRodney W. Grimes 
615004bedebSBruce Evans #endif /* !_MACHINE_CPUFUNC_H_ */
616