xref: /freebsd/sys/amd64/include/cpufunc.h (revision e65a471221b62e8e81ab62e06c532b883c45d7b7)
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  *
33e65a4712SBruce Evans  *	$Id: cpufunc.h,v 1.44 1995/12/28 23:34:17 davidg Exp $
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 
435b81b6b3SRodney W. Grimes #include <sys/cdefs.h>
445b81b6b3SRodney W. Grimes #include <sys/types.h>
455b81b6b3SRodney W. Grimes 
46004bedebSBruce Evans #include <machine/spl.h>	/* XXX belongs elsewhere */
47d2306226SDavid Greenman 
485b81b6b3SRodney W. Grimes #ifdef	__GNUC__
495b81b6b3SRodney W. Grimes 
50004bedebSBruce Evans #ifdef BDE_DEBUGGER
515b81b6b3SRodney W. Grimes extern int	bdb_exists;
525b81b6b3SRodney W. Grimes 
53004bedebSBruce Evans static __inline int
54004bedebSBruce Evans bdb(void)
55004bedebSBruce Evans {
565b81b6b3SRodney W. Grimes 	if (!bdb_exists)
575b81b6b3SRodney W. Grimes 		return (0);
58004bedebSBruce Evans 	__asm __volatile("int $3");
595b81b6b3SRodney W. Grimes 	return (1);
605b81b6b3SRodney W. Grimes }
61004bedebSBruce Evans #endif /* BDE_DEBUGGER */
625b81b6b3SRodney W. Grimes 
63004bedebSBruce Evans static __inline void
645b81b6b3SRodney W. Grimes disable_intr(void)
655b81b6b3SRodney W. Grimes {
668966b85cSJohn Dyson 	__asm __volatile("cli" : : : "memory");
675b81b6b3SRodney W. Grimes }
685b81b6b3SRodney W. Grimes 
69004bedebSBruce Evans static __inline void
705b81b6b3SRodney W. Grimes enable_intr(void)
715b81b6b3SRodney W. Grimes {
72fadc51bdSBruce Evans 	__asm __volatile("sti");
735b81b6b3SRodney W. Grimes }
745b81b6b3SRodney W. Grimes 
75004bedebSBruce Evans #define	HAVE_INLINE_FFS
76004bedebSBruce Evans 
77004bedebSBruce Evans static __inline int
78004bedebSBruce Evans ffs(int mask)
79004bedebSBruce Evans {
80004bedebSBruce Evans 	int	result;
81004bedebSBruce Evans 	/*
82004bedebSBruce Evans 	 * bsfl turns out to be not all that slow on 486's.  It can beaten
83004bedebSBruce Evans 	 * using a binary search to reduce to 4 bits and then a table lookup,
84004bedebSBruce Evans 	 * but only if the code is inlined and in the cache, and the code
85004bedebSBruce Evans 	 * is quite large so inlining it probably busts the cache.
86004bedebSBruce Evans 	 *
87004bedebSBruce Evans 	 * Note that gcc-2's builtin ffs would be used if we didn't declare
88004bedebSBruce Evans 	 * this inline or turn off the builtin.  The builtin is faster but
89004bedebSBruce Evans 	 * broken in gcc-2.4.5 and slower but working in gcc-2.5 and 2.6.
90004bedebSBruce Evans 	 */
91004bedebSBruce Evans 	__asm __volatile("testl %0,%0; je 1f; bsfl %0,%0; incl %0; 1:"
92004bedebSBruce Evans 			 : "=r" (result) : "0" (mask));
93004bedebSBruce Evans 	return (result);
94004bedebSBruce Evans }
95004bedebSBruce Evans 
96004bedebSBruce Evans #if __GNUC__ < 2
97004bedebSBruce Evans 
98004bedebSBruce Evans #define	inb(port)		inbv(port)
99004bedebSBruce Evans #define	outb(port, data)	outbv(port, data)
100004bedebSBruce Evans 
101004bedebSBruce Evans #else /* __GNUC >= 2 */
102004bedebSBruce Evans 
103004bedebSBruce Evans /*
1048089a043SBruce Evans  * The following complications are to get around gcc not having a
1058089a043SBruce Evans  * constraint letter for the range 0..255.  We still put "d" in the
1068089a043SBruce Evans  * constraint because "i" isn't a valid constraint when the port
1078089a043SBruce Evans  * isn't constant.  This only matters for -O0 because otherwise
1088089a043SBruce Evans  * the non-working version gets optimized away.
1098089a043SBruce Evans  *
110004bedebSBruce Evans  * Use an expression-statement instead of a conditional expression
111004bedebSBruce Evans  * because gcc-2.6.0 would promote the operands of the conditional
112004bedebSBruce Evans  * and produce poor code for "if ((inb(var) & const1) == const2)".
113004bedebSBruce Evans  */
114004bedebSBruce Evans #define	inb(port)	({						\
115004bedebSBruce Evans 	u_char	_data;							\
116004bedebSBruce Evans 	if (__builtin_constant_p((int) (port)) && (port) < 256ul)	\
117004bedebSBruce Evans 		_data = inbc(port);					\
118004bedebSBruce Evans 	else								\
119004bedebSBruce Evans 		_data = inbv(port);					\
120004bedebSBruce Evans 	_data; })
121004bedebSBruce Evans 
122004bedebSBruce Evans #define	outb(port, data) \
123004bedebSBruce Evans 	(__builtin_constant_p((int) (port)) && (port) < 256ul \
124004bedebSBruce Evans 	 ? outbc(port, data) : outbv(port, data))
125004bedebSBruce Evans 
126004bedebSBruce Evans static __inline u_char
127004bedebSBruce Evans inbc(u_int port)
128004bedebSBruce Evans {
129004bedebSBruce Evans 	u_char	data;
130004bedebSBruce Evans 
1318089a043SBruce Evans 	__asm __volatile("inb %1,%0" : "=a" (data) : "id" ((u_short)(port)));
132004bedebSBruce Evans 	return (data);
133004bedebSBruce Evans }
134004bedebSBruce Evans 
135004bedebSBruce Evans static __inline void
136004bedebSBruce Evans outbc(u_int port, u_char data)
137004bedebSBruce Evans {
1388089a043SBruce Evans 	__asm __volatile("outb %0,%1" : : "a" (data), "id" ((u_short)(port)));
139004bedebSBruce Evans }
140004bedebSBruce Evans 
141004bedebSBruce Evans #endif /* __GNUC <= 2 */
142004bedebSBruce Evans 
143004bedebSBruce Evans static __inline u_char
144004bedebSBruce Evans inbv(u_int port)
145004bedebSBruce Evans {
146004bedebSBruce Evans 	u_char	data;
147004bedebSBruce Evans 	/*
148004bedebSBruce Evans 	 * We use %%dx and not %1 here because i/o is done at %dx and not at
149004bedebSBruce Evans 	 * %edx, while gcc generates inferior code (movw instead of movl)
150004bedebSBruce Evans 	 * if we tell it to load (u_short) port.
151004bedebSBruce Evans 	 */
152004bedebSBruce Evans 	__asm __volatile("inb %%dx,%0" : "=a" (data) : "d" (port));
153004bedebSBruce Evans 	return (data);
154004bedebSBruce Evans }
155004bedebSBruce Evans 
156004bedebSBruce Evans static __inline u_long
157004bedebSBruce Evans inl(u_int port)
158004bedebSBruce Evans {
159004bedebSBruce Evans 	u_long	data;
160004bedebSBruce Evans 
161004bedebSBruce Evans 	__asm __volatile("inl %%dx,%0" : "=a" (data) : "d" (port));
162004bedebSBruce Evans 	return (data);
163004bedebSBruce Evans }
164004bedebSBruce Evans 
165004bedebSBruce Evans static __inline void
166004bedebSBruce Evans insb(u_int port, void *addr, size_t cnt)
167004bedebSBruce Evans {
168004bedebSBruce Evans 	__asm __volatile("cld; rep; insb"
169004bedebSBruce Evans 			 : : "d" (port), "D" (addr), "c" (cnt)
170004bedebSBruce Evans 			 : "di", "cx", "memory");
171004bedebSBruce Evans }
172004bedebSBruce Evans 
173004bedebSBruce Evans static __inline void
174004bedebSBruce Evans insw(u_int port, void *addr, size_t cnt)
175004bedebSBruce Evans {
176004bedebSBruce Evans 	__asm __volatile("cld; rep; insw"
177004bedebSBruce Evans 			 : : "d" (port), "D" (addr), "c" (cnt)
178004bedebSBruce Evans 			 : "di", "cx", "memory");
179004bedebSBruce Evans }
180004bedebSBruce Evans 
181004bedebSBruce Evans static __inline void
182004bedebSBruce Evans insl(u_int port, void *addr, size_t cnt)
183004bedebSBruce Evans {
184004bedebSBruce Evans 	__asm __volatile("cld; rep; insl"
185004bedebSBruce Evans 			 : : "d" (port), "D" (addr), "c" (cnt)
186004bedebSBruce Evans 			 : "di", "cx", "memory");
187004bedebSBruce Evans }
188004bedebSBruce Evans 
189004bedebSBruce Evans static __inline u_short
190004bedebSBruce Evans inw(u_int port)
191004bedebSBruce Evans {
192004bedebSBruce Evans 	u_short	data;
193004bedebSBruce Evans 
194004bedebSBruce Evans 	__asm __volatile("inw %%dx,%0" : "=a" (data) : "d" (port));
195004bedebSBruce Evans 	return (data);
196004bedebSBruce Evans }
197004bedebSBruce Evans 
1980ee893ebSBruce Evans static __inline unsigned
1990ee893ebSBruce Evans loadandclear(u_int *addr)
2000ee893ebSBruce Evans {
2010ee893ebSBruce Evans 	u_int	result;
2020ee893ebSBruce Evans 
2030ee893ebSBruce Evans 	__asm __volatile("xorl %0,%0; xchgl %1,%0"
20430fd0561SDavid Greenman 			 : "=&r" (result) : "m" (*addr));
2050ee893ebSBruce Evans 	return (result);
2060ee893ebSBruce Evans }
2070ee893ebSBruce Evans 
208004bedebSBruce Evans static __inline void
209004bedebSBruce Evans outbv(u_int port, u_char data)
210004bedebSBruce Evans {
211004bedebSBruce Evans 	u_char	al;
212004bedebSBruce Evans 	/*
213004bedebSBruce Evans 	 * Use an unnecessary assignment to help gcc's register allocator.
214004bedebSBruce Evans 	 * This make a large difference for gcc-1.40 and a tiny difference
215004bedebSBruce Evans 	 * for gcc-2.6.0.  For gcc-1.40, al had to be ``asm("ax")'' for
216004bedebSBruce Evans 	 * best results.  gcc-2.6.0 can't handle this.
217004bedebSBruce Evans 	 */
218004bedebSBruce Evans 	al = data;
219004bedebSBruce Evans 	__asm __volatile("outb %0,%%dx" : : "a" (al), "d" (port));
220004bedebSBruce Evans }
221004bedebSBruce Evans 
222004bedebSBruce Evans static __inline void
223004bedebSBruce Evans outl(u_int port, u_long data)
224004bedebSBruce Evans {
225004bedebSBruce Evans 	/*
226004bedebSBruce Evans 	 * outl() and outw() aren't used much so we haven't looked at
227004bedebSBruce Evans 	 * possible micro-optimizations such as the unnecessary
228004bedebSBruce Evans 	 * assignment for them.
229004bedebSBruce Evans 	 */
230004bedebSBruce Evans 	__asm __volatile("outl %0,%%dx" : : "a" (data), "d" (port));
231004bedebSBruce Evans }
232004bedebSBruce Evans 
233004bedebSBruce Evans static __inline void
234004bedebSBruce Evans outsb(u_int port, void *addr, size_t cnt)
235004bedebSBruce Evans {
236004bedebSBruce Evans 	__asm __volatile("cld; rep; outsb"
237004bedebSBruce Evans 			 : : "d" (port), "S" (addr), "c" (cnt)
238b5ba45f6SDavid Greenman 			 : "si", "cx");
239004bedebSBruce Evans }
240004bedebSBruce Evans 
241004bedebSBruce Evans static __inline void
242004bedebSBruce Evans outsw(u_int port, void *addr, size_t cnt)
243004bedebSBruce Evans {
244004bedebSBruce Evans 	__asm __volatile("cld; rep; outsw"
245004bedebSBruce Evans 			 : : "d" (port), "S" (addr), "c" (cnt)
246b5ba45f6SDavid Greenman 			 : "si", "cx");
247004bedebSBruce Evans }
248004bedebSBruce Evans 
249004bedebSBruce Evans static __inline void
250004bedebSBruce Evans outsl(u_int port, void *addr, size_t cnt)
251004bedebSBruce Evans {
252004bedebSBruce Evans 	__asm __volatile("cld; rep; outsl"
253004bedebSBruce Evans 			 : : "d" (port), "S" (addr), "c" (cnt)
254b5ba45f6SDavid Greenman 			 : "si", "cx");
255004bedebSBruce Evans }
256004bedebSBruce Evans 
257004bedebSBruce Evans static __inline void
258004bedebSBruce Evans outw(u_int port, u_short data)
259004bedebSBruce Evans {
260004bedebSBruce Evans 	__asm __volatile("outw %0,%%dx" : : "a" (data), "d" (port));
261004bedebSBruce Evans }
262004bedebSBruce Evans 
263004bedebSBruce Evans static __inline void
264004bedebSBruce Evans pmap_update(void)
265004bedebSBruce Evans {
266004bedebSBruce Evans 	u_long	temp;
267004bedebSBruce Evans 	/*
268004bedebSBruce Evans 	 * This should be implemented as load_cr3(rcr3()) when load_cr3()
269004bedebSBruce Evans 	 * is inlined.
270004bedebSBruce Evans 	 */
271fadc51bdSBruce Evans 	__asm __volatile("movl %%cr3, %0; movl %0, %%cr3" : "=r" (temp)
272fadc51bdSBruce Evans 			 : : "memory");
273004bedebSBruce Evans }
274004bedebSBruce Evans 
275004bedebSBruce Evans static __inline u_long
276004bedebSBruce Evans rcr2(void)
277004bedebSBruce Evans {
278004bedebSBruce Evans 	u_long	data;
279004bedebSBruce Evans 
280004bedebSBruce Evans 	__asm __volatile("movl %%cr2,%0" : "=r" (data));
281004bedebSBruce Evans 	return (data);
282004bedebSBruce Evans }
283004bedebSBruce Evans 
284004bedebSBruce Evans static __inline u_long
285004bedebSBruce Evans read_eflags(void)
2865b81b6b3SRodney W. Grimes {
2878db02de8SPaul Richards 	u_long	ef;
288004bedebSBruce Evans 
289004bedebSBruce Evans 	__asm __volatile("pushfl; popl %0" : "=r" (ef));
2908db02de8SPaul Richards 	return (ef);
2915b81b6b3SRodney W. Grimes }
2925b81b6b3SRodney W. Grimes 
293004bedebSBruce Evans static __inline void
2948db02de8SPaul Richards write_eflags(u_long ef)
2955b81b6b3SRodney W. Grimes {
296004bedebSBruce Evans 	__asm __volatile("pushl %0; popfl" : : "r" (ef));
2975b81b6b3SRodney W. Grimes }
2985b81b6b3SRodney W. Grimes 
299004bedebSBruce Evans #else /* !__GNUC__ */
3005b81b6b3SRodney W. Grimes 
3015b81b6b3SRodney W. Grimes int	bdb		__P((void));
3025b81b6b3SRodney W. Grimes void	disable_intr	__P((void));
3035b81b6b3SRodney W. Grimes void	enable_intr	__P((void));
3045b81b6b3SRodney W. Grimes u_char	inb		__P((u_int port));
305004bedebSBruce Evans u_long	inl		__P((u_int port));
306004bedebSBruce Evans void	insb		__P((u_int port, void *addr, size_t cnt));
307004bedebSBruce Evans void	insl		__P((u_int port, void *addr, size_t cnt));
308004bedebSBruce Evans void	insw		__P((u_int port, void *addr, size_t cnt));
309004bedebSBruce Evans u_short	inw		__P((u_int port));
3100ee893ebSBruce Evans u_int	loadandclear	__P((u_int *addr));
311004bedebSBruce Evans void	outb		__P((u_int port, u_char data));
312004bedebSBruce Evans void	outl		__P((u_int port, u_long data));
313004bedebSBruce Evans void	outsb		__P((u_int port, void *addr, size_t cnt));
314004bedebSBruce Evans void	outsl		__P((u_int port, void *addr, size_t cnt));
315004bedebSBruce Evans void	outsw		__P((u_int port, void *addr, size_t cnt));
316004bedebSBruce Evans void	outw		__P((u_int port, u_short data));
317ceb91b3eSBruce Evans void	pmap_update	__P((void));
318004bedebSBruce Evans u_long	read_eflags	__P((void));
319004bedebSBruce Evans u_long	rcr2		__P((void));
320004bedebSBruce Evans void	write_eflags	__P((u_long ef));
321004bedebSBruce Evans 
3225b81b6b3SRodney W. Grimes #endif	/* __GNUC__ */
3235b81b6b3SRodney W. Grimes 
324004bedebSBruce Evans /*
325004bedebSBruce Evans  * These variables and functions in support.s are used.
326004bedebSBruce Evans  */
327004bedebSBruce Evans extern u_int atdevbase;	/* offset in virtual memory of ISA io mem */
328004bedebSBruce Evans 
329e65a4712SBruce Evans void	bcopyb		__P((const void *from, void *to, size_t len));
330004bedebSBruce Evans void	fillw		__P((int /*u_short*/ pat, void *base, size_t cnt));
331004bedebSBruce Evans int	fusword		__P((void *base));
332004bedebSBruce Evans void	load_cr0	__P((u_long cr0));
333004bedebSBruce Evans void	load_cr3	__P((u_long cr3));
334004bedebSBruce Evans void	ltr		__P((u_short sel));
335aaf08d94SGarrett Wollman u_int	rcr0		__P((void));
336004bedebSBruce Evans u_long	rcr3		__P((void));
337004bedebSBruce Evans int	rtcin		__P((int val));
3385b81b6b3SRodney W. Grimes 
339004bedebSBruce Evans /*
340004bedebSBruce Evans  * These functions are NOT in support.s and should be declared elsewhere.
341004bedebSBruce Evans  */
342ff030ea1SBruce Evans void	Debugger	__P((const char *msg));
343004bedebSBruce Evans u_long	kvtop		__P((void *addr));
344004bedebSBruce Evans typedef void alias_for_inthand_t __P((u_int cs, u_int ef, u_int esp,
345004bedebSBruce Evans 				      u_int ss));
346004bedebSBruce Evans void	setidt		__P((int idx, alias_for_inthand_t *func, int typ,
3472838c968SDavid Greenman 			     int dpl, int selec));
34826931201SDavid Greenman 
349004bedebSBruce Evans #endif /* !_MACHINE_CPUFUNC_H_ */
350