xref: /freebsd/sys/amd64/include/cpufunc.h (revision 80275388cb23e91281f69a415e251eed01bb06c3)
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 
43e31fa854SDoug Rabson #define readb(va)	(*(volatile u_int8_t *) (va))
44e31fa854SDoug Rabson #define readw(va)	(*(volatile u_int16_t *) (va))
45e31fa854SDoug Rabson #define readl(va)	(*(volatile u_int32_t *) (va))
46e31fa854SDoug Rabson 
47e31fa854SDoug Rabson #define writeb(va, d)	(*(volatile u_int8_t *) (va) = (d))
48e31fa854SDoug Rabson #define writew(va, d)	(*(volatile u_int16_t *) (va) = (d))
49e31fa854SDoug Rabson #define writel(va, d)	(*(volatile u_int32_t *) (va) = (d))
50e31fa854SDoug Rabson 
515b81b6b3SRodney W. Grimes #ifdef	__GNUC__
525b81b6b3SRodney W. Grimes 
532a32c15fSBruce Evans #ifdef SMP
542a32c15fSBruce Evans #include <machine/lock.h>		/* XXX */
552a32c15fSBruce Evans #endif
562a32c15fSBruce Evans 
572a32c15fSBruce Evans #ifdef SWTCH_OPTIM_STATS
582a32c15fSBruce Evans extern	int	tlb_flush_count;	/* XXX */
592a32c15fSBruce Evans #endif
602a32c15fSBruce Evans 
615dbd168eSBruce Evans static __inline void
625dbd168eSBruce Evans breakpoint(void)
63004bedebSBruce Evans {
64004bedebSBruce Evans 	__asm __volatile("int $3");
655b81b6b3SRodney W. Grimes }
665b81b6b3SRodney W. Grimes 
67c83b1328SBruce Evans static __inline u_int
68c83b1328SBruce Evans bsfl(u_int mask)
69c83b1328SBruce Evans {
70c83b1328SBruce Evans 	u_int	result;
71c83b1328SBruce Evans 
72c83b1328SBruce Evans 	__asm __volatile("bsfl %0,%0" : "=r" (result) : "0" (mask));
73c83b1328SBruce Evans 	return (result);
74c83b1328SBruce Evans }
75c83b1328SBruce Evans 
76c83b1328SBruce Evans static __inline u_int
77c83b1328SBruce Evans bsrl(u_int mask)
78c83b1328SBruce Evans {
79c83b1328SBruce Evans 	u_int	result;
80c83b1328SBruce Evans 
81c83b1328SBruce Evans 	__asm __volatile("bsrl %0,%0" : "=r" (result) : "0" (mask));
82c83b1328SBruce Evans 	return (result);
83c83b1328SBruce Evans }
84c83b1328SBruce Evans 
85004bedebSBruce Evans static __inline void
865b81b6b3SRodney W. Grimes disable_intr(void)
875b81b6b3SRodney W. Grimes {
888966b85cSJohn Dyson 	__asm __volatile("cli" : : : "memory");
892a32c15fSBruce Evans #ifdef SMP
9020233f27SSteve Passe 	MPINTR_LOCK();
912a32c15fSBruce Evans #endif
925b81b6b3SRodney W. Grimes }
935b81b6b3SRodney W. Grimes 
94004bedebSBruce Evans static __inline void
955b81b6b3SRodney W. Grimes enable_intr(void)
965b81b6b3SRodney W. Grimes {
972a32c15fSBruce Evans #ifdef SMP
9820233f27SSteve Passe 	MPINTR_UNLOCK();
992a32c15fSBruce Evans #endif
100fadc51bdSBruce Evans 	__asm __volatile("sti");
1015b81b6b3SRodney W. Grimes }
1025b81b6b3SRodney W. Grimes 
103264c3d87SPeter Wemm #define	HAVE_INLINE_FFS
104264c3d87SPeter Wemm 
105264c3d87SPeter Wemm static __inline int
106264c3d87SPeter Wemm ffs(int mask)
107264c3d87SPeter Wemm {
108264c3d87SPeter Wemm 	/*
109004bedebSBruce Evans 	 * Note that gcc-2's builtin ffs would be used if we didn't declare
110004bedebSBruce Evans 	 * this inline or turn off the builtin.  The builtin is faster but
111c83b1328SBruce Evans 	 * broken in gcc-2.4.5 and slower but working in gcc-2.5 and later
112c83b1328SBruce Evans 	 * versions.
113004bedebSBruce Evans 	 */
114c83b1328SBruce Evans 	 return (mask == 0 ? mask : bsfl((u_int)mask) + 1);
115004bedebSBruce Evans }
116004bedebSBruce Evans 
11713f588f8SGarrett Wollman #define	HAVE_INLINE_FLS
11813f588f8SGarrett Wollman 
11913f588f8SGarrett Wollman static __inline int
12013f588f8SGarrett Wollman fls(int mask)
12113f588f8SGarrett Wollman {
122c83b1328SBruce Evans 	return (mask == 0 ? mask : bsrl((u_int)mask) + 1);
12313f588f8SGarrett Wollman }
12413f588f8SGarrett Wollman 
125004bedebSBruce Evans #if __GNUC__ < 2
126004bedebSBruce Evans 
127004bedebSBruce Evans #define	inb(port)		inbv(port)
128004bedebSBruce Evans #define	outb(port, data)	outbv(port, data)
129004bedebSBruce Evans 
130004bedebSBruce Evans #else /* __GNUC >= 2 */
131004bedebSBruce Evans 
132004bedebSBruce Evans /*
1338089a043SBruce Evans  * The following complications are to get around gcc not having a
1348089a043SBruce Evans  * constraint letter for the range 0..255.  We still put "d" in the
1358089a043SBruce Evans  * constraint because "i" isn't a valid constraint when the port
1368089a043SBruce Evans  * isn't constant.  This only matters for -O0 because otherwise
1378089a043SBruce Evans  * the non-working version gets optimized away.
1388089a043SBruce Evans  *
139004bedebSBruce Evans  * Use an expression-statement instead of a conditional expression
140004bedebSBruce Evans  * because gcc-2.6.0 would promote the operands of the conditional
141004bedebSBruce Evans  * and produce poor code for "if ((inb(var) & const1) == const2)".
142388dfa71SBruce Evans  *
143388dfa71SBruce Evans  * The unnecessary test `(port) < 0x10000' is to generate a warning if
144388dfa71SBruce Evans  * the `port' has type u_short or smaller.  Such types are pessimal.
145388dfa71SBruce Evans  * This actually only works for signed types.  The range check is
146388dfa71SBruce Evans  * careful to avoid generating warnings.
147004bedebSBruce Evans  */
148388dfa71SBruce Evans #define	inb(port) __extension__ ({					\
149004bedebSBruce Evans 	u_char	_data;							\
150388dfa71SBruce Evans 	if (__builtin_constant_p(port) && ((port) & 0xffff) < 0x100	\
151388dfa71SBruce Evans 	    && (port) < 0x10000)					\
152004bedebSBruce Evans 		_data = inbc(port);					\
153004bedebSBruce Evans 	else								\
154004bedebSBruce Evans 		_data = inbv(port);					\
155004bedebSBruce Evans 	_data; })
156004bedebSBruce Evans 
157388dfa71SBruce Evans #define	outb(port, data) (						\
158388dfa71SBruce Evans 	__builtin_constant_p(port) && ((port) & 0xffff) < 0x100		\
159388dfa71SBruce Evans 	&& (port) < 0x10000						\
160004bedebSBruce Evans 	? outbc(port, data) : outbv(port, data))
161004bedebSBruce Evans 
162004bedebSBruce Evans static __inline u_char
163004bedebSBruce Evans inbc(u_int port)
164004bedebSBruce Evans {
165004bedebSBruce Evans 	u_char	data;
166004bedebSBruce Evans 
1678089a043SBruce Evans 	__asm __volatile("inb %1,%0" : "=a" (data) : "id" ((u_short)(port)));
168004bedebSBruce Evans 	return (data);
169004bedebSBruce Evans }
170004bedebSBruce Evans 
171004bedebSBruce Evans static __inline void
172004bedebSBruce Evans outbc(u_int port, u_char data)
173004bedebSBruce Evans {
1748089a043SBruce Evans 	__asm __volatile("outb %0,%1" : : "a" (data), "id" ((u_short)(port)));
175004bedebSBruce Evans }
176004bedebSBruce Evans 
177004bedebSBruce Evans #endif /* __GNUC <= 2 */
178004bedebSBruce Evans 
179004bedebSBruce Evans static __inline u_char
180004bedebSBruce Evans inbv(u_int port)
181004bedebSBruce Evans {
182004bedebSBruce Evans 	u_char	data;
183004bedebSBruce Evans 	/*
184004bedebSBruce Evans 	 * We use %%dx and not %1 here because i/o is done at %dx and not at
185004bedebSBruce Evans 	 * %edx, while gcc generates inferior code (movw instead of movl)
186004bedebSBruce Evans 	 * if we tell it to load (u_short) port.
187004bedebSBruce Evans 	 */
188004bedebSBruce Evans 	__asm __volatile("inb %%dx,%0" : "=a" (data) : "d" (port));
189004bedebSBruce Evans 	return (data);
190004bedebSBruce Evans }
191004bedebSBruce Evans 
19200be8601SBruce Evans static __inline u_int
193004bedebSBruce Evans inl(u_int port)
194004bedebSBruce Evans {
19500be8601SBruce Evans 	u_int	data;
196004bedebSBruce Evans 
197004bedebSBruce Evans 	__asm __volatile("inl %%dx,%0" : "=a" (data) : "d" (port));
198004bedebSBruce Evans 	return (data);
199004bedebSBruce Evans }
200004bedebSBruce Evans 
201004bedebSBruce Evans static __inline void
202004bedebSBruce Evans insb(u_int port, void *addr, size_t cnt)
203004bedebSBruce Evans {
204004bedebSBruce Evans 	__asm __volatile("cld; rep; insb"
205896763faSBruce Evans 			 : "=D" (addr), "=c" (cnt)
206896763faSBruce Evans 			 :  "0" (addr),  "1" (cnt), "d" (port)
207896763faSBruce Evans 			 : "memory");
208004bedebSBruce Evans }
209004bedebSBruce Evans 
210004bedebSBruce Evans static __inline void
211004bedebSBruce Evans insw(u_int port, void *addr, size_t cnt)
212004bedebSBruce Evans {
213004bedebSBruce Evans 	__asm __volatile("cld; rep; insw"
214896763faSBruce Evans 			 : "=D" (addr), "=c" (cnt)
215896763faSBruce Evans 			 :  "0" (addr),  "1" (cnt), "d" (port)
216896763faSBruce Evans 			 : "memory");
217004bedebSBruce Evans }
218004bedebSBruce Evans 
219004bedebSBruce Evans static __inline void
220004bedebSBruce Evans insl(u_int port, void *addr, size_t cnt)
221004bedebSBruce Evans {
222004bedebSBruce Evans 	__asm __volatile("cld; rep; insl"
223896763faSBruce Evans 			 : "=D" (addr), "=c" (cnt)
224896763faSBruce Evans 			 :  "0" (addr),  "1" (cnt), "d" (port)
225896763faSBruce Evans 			 : "memory");
226004bedebSBruce Evans }
227004bedebSBruce Evans 
228ece15d78SBruce Evans static __inline void
2294c024bbdSKATO Takenori invd(void)
2304c024bbdSKATO Takenori {
2314c024bbdSKATO Takenori 	__asm __volatile("invd");
2324c024bbdSKATO Takenori }
2334c024bbdSKATO Takenori 
234664a31e4SPeter Wemm #if defined(SMP) && defined(_KERNEL)
235477a642cSPeter Wemm 
236477a642cSPeter Wemm /*
237f48bbd5fSBruce Evans  * When using APIC IPI's, invlpg() is not simply the invlpg instruction
238f48bbd5fSBruce Evans  * (this is a bug) and the inlining cost is prohibitive since the call
239f40e6078SPeter Wemm  * executes into the IPI transmission system.
240477a642cSPeter Wemm  */
241477a642cSPeter Wemm void	invlpg		__P((u_int addr));
242477a642cSPeter Wemm void	invltlb		__P((void));
243477a642cSPeter Wemm 
2445498a452SJohn Dyson static __inline void
2455498a452SJohn Dyson cpu_invlpg(void *addr)
2465498a452SJohn Dyson {
2475498a452SJohn Dyson 	__asm __volatile("invlpg %0" : : "m" (*(char *)addr) : "memory");
2485498a452SJohn Dyson }
2495498a452SJohn Dyson 
2505931a9c2STor Egge static __inline void
2515931a9c2STor Egge cpu_invltlb(void)
2525931a9c2STor Egge {
25300be8601SBruce Evans 	u_int	temp;
2545931a9c2STor Egge 	/*
2555931a9c2STor Egge 	 * This should be implemented as load_cr3(rcr3()) when load_cr3()
2565931a9c2STor Egge 	 * is inlined.
2575931a9c2STor Egge 	 */
2585931a9c2STor Egge 	__asm __volatile("movl %%cr3, %0; movl %0, %%cr3" : "=r" (temp)
2595931a9c2STor Egge 			 : : "memory");
2605931a9c2STor Egge #if defined(SWTCH_OPTIM_STATS)
2615931a9c2STor Egge 	++tlb_flush_count;
2625931a9c2STor Egge #endif
2635931a9c2STor Egge }
264f48bbd5fSBruce Evans 
265664a31e4SPeter Wemm #else /* !(SMP && _KERNEL) */
266477a642cSPeter Wemm 
2674c024bbdSKATO Takenori static __inline void
268471176aaSJohn Dyson invlpg(u_int addr)
269ece15d78SBruce Evans {
2705498a452SJohn Dyson 	__asm __volatile("invlpg %0" : : "m" (*(char *)addr) : "memory");
271ece15d78SBruce Evans }
272ece15d78SBruce Evans 
273ece15d78SBruce Evans static __inline void
274ece15d78SBruce Evans invltlb(void)
275ece15d78SBruce Evans {
27600be8601SBruce Evans 	u_int	temp;
277ece15d78SBruce Evans 	/*
278ece15d78SBruce Evans 	 * This should be implemented as load_cr3(rcr3()) when load_cr3()
279ece15d78SBruce Evans 	 * is inlined.
280ece15d78SBruce Evans 	 */
281ece15d78SBruce Evans 	__asm __volatile("movl %%cr3, %0; movl %0, %%cr3" : "=r" (temp)
282ece15d78SBruce Evans 			 : : "memory");
283f48bbd5fSBruce Evans #ifdef SWTCH_OPTIM_STATS
28482566551SJohn Dyson 	++tlb_flush_count;
28582566551SJohn Dyson #endif
286ece15d78SBruce Evans }
2872c5d02ffSSteve Passe 
288664a31e4SPeter Wemm #endif /* SMP && _KERNEL */
289ece15d78SBruce Evans 
290004bedebSBruce Evans static __inline u_short
291004bedebSBruce Evans inw(u_int port)
292004bedebSBruce Evans {
293004bedebSBruce Evans 	u_short	data;
294004bedebSBruce Evans 
295004bedebSBruce Evans 	__asm __volatile("inw %%dx,%0" : "=a" (data) : "d" (port));
296004bedebSBruce Evans 	return (data);
297004bedebSBruce Evans }
298004bedebSBruce Evans 
2995dbd168eSBruce Evans static __inline u_int
3000264a0ebSPeter Wemm loadandclear(volatile u_int *addr)
3010ee893ebSBruce Evans {
3020ee893ebSBruce Evans 	u_int	result;
3030ee893ebSBruce Evans 
3040ee893ebSBruce Evans 	__asm __volatile("xorl %0,%0; xchgl %1,%0"
30530fd0561SDavid Greenman 			 : "=&r" (result) : "m" (*addr));
3060ee893ebSBruce Evans 	return (result);
3070ee893ebSBruce Evans }
3080ee893ebSBruce Evans 
309004bedebSBruce Evans static __inline void
310004bedebSBruce Evans outbv(u_int port, u_char data)
311004bedebSBruce Evans {
312004bedebSBruce Evans 	u_char	al;
313004bedebSBruce Evans 	/*
314004bedebSBruce Evans 	 * Use an unnecessary assignment to help gcc's register allocator.
315004bedebSBruce Evans 	 * This make a large difference for gcc-1.40 and a tiny difference
316004bedebSBruce Evans 	 * for gcc-2.6.0.  For gcc-1.40, al had to be ``asm("ax")'' for
317004bedebSBruce Evans 	 * best results.  gcc-2.6.0 can't handle this.
318004bedebSBruce Evans 	 */
319004bedebSBruce Evans 	al = data;
320004bedebSBruce Evans 	__asm __volatile("outb %0,%%dx" : : "a" (al), "d" (port));
321004bedebSBruce Evans }
322004bedebSBruce Evans 
323004bedebSBruce Evans static __inline void
32400be8601SBruce Evans outl(u_int port, u_int data)
325004bedebSBruce Evans {
326004bedebSBruce Evans 	/*
327004bedebSBruce Evans 	 * outl() and outw() aren't used much so we haven't looked at
328004bedebSBruce Evans 	 * possible micro-optimizations such as the unnecessary
329004bedebSBruce Evans 	 * assignment for them.
330004bedebSBruce Evans 	 */
331004bedebSBruce Evans 	__asm __volatile("outl %0,%%dx" : : "a" (data), "d" (port));
332004bedebSBruce Evans }
333004bedebSBruce Evans 
334004bedebSBruce Evans static __inline void
335e1a1bba4SJustin T. Gibbs outsb(u_int port, const void *addr, size_t cnt)
336004bedebSBruce Evans {
337004bedebSBruce Evans 	__asm __volatile("cld; rep; outsb"
338896763faSBruce Evans 			 : "=S" (addr), "=c" (cnt)
339896763faSBruce Evans 			 :  "0" (addr),  "1" (cnt), "d" (port));
340004bedebSBruce Evans }
341004bedebSBruce Evans 
342004bedebSBruce Evans static __inline void
343e1a1bba4SJustin T. Gibbs outsw(u_int port, const void *addr, size_t cnt)
344004bedebSBruce Evans {
345004bedebSBruce Evans 	__asm __volatile("cld; rep; outsw"
346896763faSBruce Evans 			 : "=S" (addr), "=c" (cnt)
347896763faSBruce Evans 			 :  "0" (addr),  "1" (cnt), "d" (port));
348004bedebSBruce Evans }
349004bedebSBruce Evans 
350004bedebSBruce Evans static __inline void
351e1a1bba4SJustin T. Gibbs outsl(u_int port, const void *addr, size_t cnt)
352004bedebSBruce Evans {
353004bedebSBruce Evans 	__asm __volatile("cld; rep; outsl"
354896763faSBruce Evans 			 : "=S" (addr), "=c" (cnt)
355896763faSBruce Evans 			 :  "0" (addr),  "1" (cnt), "d" (port));
356004bedebSBruce Evans }
357004bedebSBruce Evans 
358004bedebSBruce Evans static __inline void
359004bedebSBruce Evans outw(u_int port, u_short data)
360004bedebSBruce Evans {
361004bedebSBruce Evans 	__asm __volatile("outw %0,%%dx" : : "a" (data), "d" (port));
362004bedebSBruce Evans }
363004bedebSBruce Evans 
36400be8601SBruce Evans static __inline u_int
365004bedebSBruce Evans rcr2(void)
366004bedebSBruce Evans {
36700be8601SBruce Evans 	u_int	data;
368004bedebSBruce Evans 
369004bedebSBruce Evans 	__asm __volatile("movl %%cr2,%0" : "=r" (data));
370004bedebSBruce Evans 	return (data);
371004bedebSBruce Evans }
372004bedebSBruce Evans 
37300be8601SBruce Evans static __inline u_int
374004bedebSBruce Evans read_eflags(void)
3755b81b6b3SRodney W. Grimes {
37600be8601SBruce Evans 	u_int	ef;
377004bedebSBruce Evans 
378004bedebSBruce Evans 	__asm __volatile("pushfl; popl %0" : "=r" (ef));
3798db02de8SPaul Richards 	return (ef);
3805b81b6b3SRodney W. Grimes }
3815b81b6b3SRodney W. Grimes 
38200be8601SBruce Evans static __inline u_int64_t
3835dbd168eSBruce Evans rdmsr(u_int msr)
3845dbd168eSBruce Evans {
38500be8601SBruce Evans 	u_int64_t rv;
3865dbd168eSBruce Evans 
3875dbd168eSBruce Evans 	__asm __volatile(".byte 0x0f, 0x32" : "=A" (rv) : "c" (msr));
3885dbd168eSBruce Evans 	return (rv);
3895dbd168eSBruce Evans }
3905dbd168eSBruce Evans 
39100be8601SBruce Evans static __inline u_int64_t
3925dbd168eSBruce Evans rdpmc(u_int pmc)
3935dbd168eSBruce Evans {
39400be8601SBruce Evans 	u_int64_t rv;
3955dbd168eSBruce Evans 
3965dbd168eSBruce Evans 	__asm __volatile(".byte 0x0f, 0x33" : "=A" (rv) : "c" (pmc));
3975dbd168eSBruce Evans 	return (rv);
3985dbd168eSBruce Evans }
3995dbd168eSBruce Evans 
40000be8601SBruce Evans static __inline u_int64_t
4015dbd168eSBruce Evans rdtsc(void)
4025dbd168eSBruce Evans {
40300be8601SBruce Evans 	u_int64_t rv;
4045dbd168eSBruce Evans 
4055dbd168eSBruce Evans 	__asm __volatile(".byte 0x0f, 0x31" : "=A" (rv));
4065dbd168eSBruce Evans 	return (rv);
4075dbd168eSBruce Evans }
4085dbd168eSBruce Evans 
409004bedebSBruce Evans static __inline void
4104c024bbdSKATO Takenori wbinvd(void)
4114c024bbdSKATO Takenori {
4124c024bbdSKATO Takenori 	__asm __volatile("wbinvd");
4134c024bbdSKATO Takenori }
4144c024bbdSKATO Takenori 
4154c024bbdSKATO Takenori static __inline void
41600be8601SBruce Evans write_eflags(u_int ef)
4175b81b6b3SRodney W. Grimes {
418004bedebSBruce Evans 	__asm __volatile("pushl %0; popfl" : : "r" (ef));
4195b81b6b3SRodney W. Grimes }
4205b81b6b3SRodney W. Grimes 
421d69e8502SGarrett Wollman static __inline void
42200be8601SBruce Evans wrmsr(u_int msr, u_int64_t newval)
423d69e8502SGarrett Wollman {
42428dc3d27SGarrett Wollman 	__asm __volatile(".byte 0x0f, 0x30" : : "A" (newval), "c" (msr));
425d69e8502SGarrett Wollman }
426d69e8502SGarrett Wollman 
4275206bca1SLuoqi Chen static __inline u_int
4285206bca1SLuoqi Chen rfs(void)
4295206bca1SLuoqi Chen {
4305206bca1SLuoqi Chen 	u_int sel;
431e870e9b2SLuoqi Chen 	__asm __volatile("movl %%fs,%0" : "=rm" (sel));
4325206bca1SLuoqi Chen 	return (sel);
4335206bca1SLuoqi Chen }
4345206bca1SLuoqi Chen 
4355206bca1SLuoqi Chen static __inline u_int
4365206bca1SLuoqi Chen rgs(void)
4375206bca1SLuoqi Chen {
4385206bca1SLuoqi Chen 	u_int sel;
439e870e9b2SLuoqi Chen 	__asm __volatile("movl %%gs,%0" : "=rm" (sel));
4405206bca1SLuoqi Chen 	return (sel);
4415206bca1SLuoqi Chen }
4425206bca1SLuoqi Chen 
4435206bca1SLuoqi Chen static __inline void
4445206bca1SLuoqi Chen load_fs(u_int sel)
4455206bca1SLuoqi Chen {
446e870e9b2SLuoqi Chen 	__asm __volatile("movl %0,%%fs" : : "rm" (sel));
4475206bca1SLuoqi Chen }
4485206bca1SLuoqi Chen 
4495206bca1SLuoqi Chen static __inline void
4505206bca1SLuoqi Chen load_gs(u_int sel)
4515206bca1SLuoqi Chen {
452e870e9b2SLuoqi Chen 	__asm __volatile("movl %0,%%gs" : : "rm" (sel));
4535206bca1SLuoqi Chen }
4545206bca1SLuoqi Chen 
455de8050f9SBrian S. Dean static __inline u_int
456de8050f9SBrian S. Dean rdr0(void)
457de8050f9SBrian S. Dean {
458de8050f9SBrian S. Dean 	u_int	data;
45980275388SBrian S. Dean 	__asm __volatile("movl %%dr0,%0" : "=r" (data));
460de8050f9SBrian S. Dean 	return (data);
461de8050f9SBrian S. Dean }
462de8050f9SBrian S. Dean 
463de8050f9SBrian S. Dean static __inline u_int
464de8050f9SBrian S. Dean rdr1(void)
465de8050f9SBrian S. Dean {
466de8050f9SBrian S. Dean 	u_int	data;
46780275388SBrian S. Dean 	__asm __volatile("movl %%dr1,%0" : "=r" (data));
468de8050f9SBrian S. Dean 	return (data);
469de8050f9SBrian S. Dean }
470de8050f9SBrian S. Dean 
471de8050f9SBrian S. Dean static __inline u_int
472de8050f9SBrian S. Dean rdr2(void)
473de8050f9SBrian S. Dean {
474de8050f9SBrian S. Dean 	u_int	data;
47580275388SBrian S. Dean 	__asm __volatile("movl %%dr2,%0" : "=r" (data));
476de8050f9SBrian S. Dean 	return (data);
477de8050f9SBrian S. Dean }
478de8050f9SBrian S. Dean 
479de8050f9SBrian S. Dean static __inline u_int
480de8050f9SBrian S. Dean rdr3(void)
481de8050f9SBrian S. Dean {
482de8050f9SBrian S. Dean 	u_int	data;
48380275388SBrian S. Dean 	__asm __volatile("movl %%dr3,%0" : "=r" (data));
484de8050f9SBrian S. Dean 	return (data);
485de8050f9SBrian S. Dean }
486de8050f9SBrian S. Dean 
487de8050f9SBrian S. Dean static __inline u_int
488de8050f9SBrian S. Dean rdr6(void)
489de8050f9SBrian S. Dean {
490de8050f9SBrian S. Dean 	u_int	data;
49180275388SBrian S. Dean 	__asm __volatile("movl %%dr6,%0" : "=r" (data));
492de8050f9SBrian S. Dean 	return (data);
493de8050f9SBrian S. Dean }
494de8050f9SBrian S. Dean 
495de8050f9SBrian S. Dean static __inline u_int
496de8050f9SBrian S. Dean rdr7(void)
497de8050f9SBrian S. Dean {
498de8050f9SBrian S. Dean 	u_int	data;
49980275388SBrian S. Dean 	__asm __volatile("movl %%dr7,%0" : "=r" (data));
500de8050f9SBrian S. Dean 	return (data);
501de8050f9SBrian S. Dean }
502de8050f9SBrian S. Dean 
503004bedebSBruce Evans #else /* !__GNUC__ */
5045b81b6b3SRodney W. Grimes 
5055dbd168eSBruce Evans int	breakpoint	__P((void));
506c83b1328SBruce Evans u_int	bsfl		__P((u_int mask));
507c83b1328SBruce Evans u_int	bsrl		__P((u_int mask));
5085b81b6b3SRodney W. Grimes void	disable_intr	__P((void));
5095b81b6b3SRodney W. Grimes void	enable_intr	__P((void));
5105b81b6b3SRodney W. Grimes u_char	inb		__P((u_int port));
51100be8601SBruce Evans u_int	inl		__P((u_int port));
512004bedebSBruce Evans void	insb		__P((u_int port, void *addr, size_t cnt));
513004bedebSBruce Evans void	insl		__P((u_int port, void *addr, size_t cnt));
514004bedebSBruce Evans void	insw		__P((u_int port, void *addr, size_t cnt));
5154c024bbdSKATO Takenori void	invd		__P((void));
516ece15d78SBruce Evans void	invlpg		__P((u_int addr));
517ece15d78SBruce Evans void	invltlb		__P((void));
518004bedebSBruce Evans u_short	inw		__P((u_int port));
5190ee893ebSBruce Evans u_int	loadandclear	__P((u_int *addr));
520004bedebSBruce Evans void	outb		__P((u_int port, u_char data));
52100be8601SBruce Evans void	outl		__P((u_int port, u_int data));
522004bedebSBruce Evans void	outsb		__P((u_int port, void *addr, size_t cnt));
523004bedebSBruce Evans void	outsl		__P((u_int port, void *addr, size_t cnt));
524004bedebSBruce Evans void	outsw		__P((u_int port, void *addr, size_t cnt));
525004bedebSBruce Evans void	outw		__P((u_int port, u_short data));
52600be8601SBruce Evans u_int	rcr2		__P((void));
52700be8601SBruce Evans u_int64_t rdmsr		__P((u_int msr));
52800be8601SBruce Evans u_int64_t rdpmc		__P((u_int pmc));
52900be8601SBruce Evans u_int64_t rdtsc		__P((void));
53000be8601SBruce Evans u_int	read_eflags	__P((void));
5314c024bbdSKATO Takenori void	wbinvd		__P((void));
53200be8601SBruce Evans void	write_eflags	__P((u_int ef));
53300be8601SBruce Evans void	wrmsr		__P((u_int msr, u_int64_t newval));
5345206bca1SLuoqi Chen u_int	rfs		__P((void));
5355206bca1SLuoqi Chen u_int	rgs		__P((void));
5365206bca1SLuoqi Chen void	load_fs		__P((u_int sel));
5375206bca1SLuoqi Chen void	load_gs		__P((u_int sel));
538004bedebSBruce Evans 
5395b81b6b3SRodney W. Grimes #endif	/* __GNUC__ */
5405b81b6b3SRodney W. Grimes 
54100be8601SBruce Evans void	load_cr0	__P((u_int cr0));
54200be8601SBruce Evans void	load_cr3	__P((u_int cr3));
54300be8601SBruce Evans void	load_cr4	__P((u_int cr4));
544004bedebSBruce Evans void	ltr		__P((u_short sel));
545aaf08d94SGarrett Wollman u_int	rcr0		__P((void));
54600be8601SBruce Evans u_int	rcr3		__P((void));
54700be8601SBruce Evans u_int	rcr4		__P((void));
548de8050f9SBrian S. Dean void    load_dr6        __P((u_int dr6));
549de8050f9SBrian S. Dean void    reset_dbregs    __P((void));
5505b81b6b3SRodney W. Grimes 
551004bedebSBruce Evans #endif /* !_MACHINE_CPUFUNC_H_ */
552