cpufunc.h revision 35976
1193267Sjkim/*-
2193267Sjkim * Copyright (c) 1993 The Regents of the University of California.
3193267Sjkim * All rights reserved.
4193267Sjkim *
5193267Sjkim * Redistribution and use in source and binary forms, with or without
6193267Sjkim * modification, are permitted provided that the following conditions
7217355Sjkim * are met:
8298714Sjkim * 1. Redistributions of source code must retain the above copyright
9193267Sjkim *    notice, this list of conditions and the following disclaimer.
10193267Sjkim * 2. Redistributions in binary form must reproduce the above copyright
11217355Sjkim *    notice, this list of conditions and the following disclaimer in the
12217355Sjkim *    documentation and/or other materials provided with the distribution.
13217355Sjkim * 3. All advertising materials mentioning features or use of this software
14217355Sjkim *    must display the following acknowledgement:
15217355Sjkim *	This product includes software developed by the University of
16217355Sjkim *	California, Berkeley and its contributors.
17217355Sjkim * 4. Neither the name of the University nor the names of its contributors
18217355Sjkim *    may be used to endorse or promote products derived from this software
19217355Sjkim *    without specific prior written permission.
20217355Sjkim *
21217355Sjkim * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22217355Sjkim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23217355Sjkim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24217355Sjkim * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25193267Sjkim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26217355Sjkim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27217355Sjkim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28217355Sjkim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29193267Sjkim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30217355Sjkim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31217355Sjkim * SUCH DAMAGE.
32217355Sjkim *
33217355Sjkim *	$Id: cpufunc.h,v 1.77 1998/05/11 02:13:47 dyson Exp $
34217355Sjkim */
35217355Sjkim
36217355Sjkim/*
37217355Sjkim * Functions to provide access to special i386 instructions.
38217355Sjkim */
39217355Sjkim
40217355Sjkim#ifndef _MACHINE_CPUFUNC_H_
41217355Sjkim#define	_MACHINE_CPUFUNC_H_
42217355Sjkim
43193267Sjkim#include <sys/cdefs.h>
44193267Sjkim#include <sys/types.h>
45213800Sjkim
46213800Sjkim#include <machine/lock.h>
47193267Sjkim
48228110Sjkim#if defined(SWTCH_OPTIM_STATS)
49228110Sjkimextern int tlb_flush_count;
50228110Sjkim#endif
51228110Sjkim
52228110Sjkim#ifdef	__GNUC__
53223480Sjkim
54193267Sjkimstatic __inline void
55193267Sjkimbreakpoint(void)
56193267Sjkim{
57193267Sjkim	__asm __volatile("int $3");
58193267Sjkim}
59193267Sjkim
60193267Sjkimstatic __inline void
61209734Sjkimdisable_intr(void)
62193267Sjkim{
63193267Sjkim	__asm __volatile("cli" : : : "memory");
64193267Sjkim	MPINTR_LOCK();
65193267Sjkim}
66193267Sjkim
67272444Sjkimstatic __inline void
68193267Sjkimenable_intr(void)
69193267Sjkim{
70193267Sjkim	MPINTR_UNLOCK();
71193267Sjkim	__asm __volatile("sti");
72193267Sjkim}
73193267Sjkim
74193267Sjkim#define	HAVE_INLINE_FFS
75193267Sjkim
76193267Sjkimstatic __inline int
77193267Sjkimffs(int mask)
78193267Sjkim{
79245582Sjkim	int	result;
80245582Sjkim	/*
81272444Sjkim	 * bsfl turns out to be not all that slow on 486's.  It can beaten
82272444Sjkim	 * using a binary search to reduce to 4 bits and then a table lookup,
83245582Sjkim	 * but only if the code is inlined and in the cache, and the code
84272444Sjkim	 * is quite large so inlining it probably busts the cache.
85193267Sjkim	 *
86272444Sjkim	 * Note that gcc-2's builtin ffs would be used if we didn't declare
87272444Sjkim	 * this inline or turn off the builtin.  The builtin is faster but
88272444Sjkim	 * broken in gcc-2.4.5 and slower but working in gcc-2.5 and 2.6.
89272444Sjkim	 */
90272444Sjkim	__asm __volatile("testl %0,%0; je 1f; bsfl %0,%0; incl %0; 1:"
91272444Sjkim			 : "=r" (result) : "0" (mask));
92272444Sjkim	return (result);
93272444Sjkim}
94272444Sjkim
95272444Sjkim#define	HAVE_INLINE_FLS
96272444Sjkim
97272444Sjkimstatic __inline int
98272444Sjkimfls(int mask)
99272444Sjkim{
100272444Sjkim	int	result;
101272444Sjkim	__asm __volatile("testl %0,%0; je 1f; bsrl %0,%0; incl %0; 1:"
102272444Sjkim			 : "=r" (result) : "0" (mask));
103272444Sjkim	return (result);
104272444Sjkim}
105272444Sjkim
106272444Sjkim#if __GNUC__ < 2
107193267Sjkim
108193267Sjkim#define	inb(port)		inbv(port)
109272444Sjkim#define	outb(port, data)	outbv(port, data)
110272444Sjkim
111272444Sjkim#else /* __GNUC >= 2 */
112272444Sjkim
113272444Sjkim/*
114272444Sjkim * The following complications are to get around gcc not having a
115272444Sjkim * constraint letter for the range 0..255.  We still put "d" in the
116272444Sjkim * constraint because "i" isn't a valid constraint when the port
117272444Sjkim * isn't constant.  This only matters for -O0 because otherwise
118272444Sjkim * the non-working version gets optimized away.
119272444Sjkim *
120272444Sjkim * Use an expression-statement instead of a conditional expression
121272444Sjkim * because gcc-2.6.0 would promote the operands of the conditional
122272444Sjkim * and produce poor code for "if ((inb(var) & const1) == const2)".
123272444Sjkim *
124272444Sjkim * The unnecessary test `(port) < 0x10000' is to generate a warning if
125272444Sjkim * the `port' has type u_short or smaller.  Such types are pessimal.
126272444Sjkim * This actually only works for signed types.  The range check is
127272444Sjkim * careful to avoid generating warnings.
128272444Sjkim */
129272444Sjkim#define	inb(port) __extension__ ({					\
130272444Sjkim	u_char	_data;							\
131272444Sjkim	if (__builtin_constant_p(port) && ((port) & 0xffff) < 0x100	\
132272444Sjkim	    && (port) < 0x10000)					\
133272444Sjkim		_data = inbc(port);					\
134272444Sjkim	else								\
135272444Sjkim		_data = inbv(port);					\
136272444Sjkim	_data; })
137272444Sjkim
138272444Sjkim#define	outb(port, data) (						\
139272444Sjkim	__builtin_constant_p(port) && ((port) & 0xffff) < 0x100		\
140272444Sjkim	&& (port) < 0x10000						\
141272444Sjkim	? outbc(port, data) : outbv(port, data))
142272444Sjkim
143272444Sjkimstatic __inline u_char
144272444Sjkiminbc(u_int port)
145272444Sjkim{
146272444Sjkim	u_char	data;
147272444Sjkim
148272444Sjkim	__asm __volatile("inb %1,%0" : "=a" (data) : "id" ((u_short)(port)));
149272444Sjkim	return (data);
150272444Sjkim}
151272444Sjkim
152272444Sjkimstatic __inline void
153272444Sjkimoutbc(u_int port, u_char data)
154272444Sjkim{
155272444Sjkim	__asm __volatile("outb %0,%1" : : "a" (data), "id" ((u_short)(port)));
156272444Sjkim}
157272444Sjkim
158272444Sjkim#endif /* __GNUC <= 2 */
159272444Sjkim
160272444Sjkimstatic __inline u_char
161272444Sjkiminbv(u_int port)
162272444Sjkim{
163272444Sjkim	u_char	data;
164272444Sjkim	/*
165272444Sjkim	 * We use %%dx and not %1 here because i/o is done at %dx and not at
166272444Sjkim	 * %edx, while gcc generates inferior code (movw instead of movl)
167272444Sjkim	 * if we tell it to load (u_short) port.
168272444Sjkim	 */
169272444Sjkim	__asm __volatile("inb %%dx,%0" : "=a" (data) : "d" (port));
170272444Sjkim	return (data);
171272444Sjkim}
172272444Sjkim
173272444Sjkimstatic __inline u_long
174272444Sjkiminl(u_int port)
175272444Sjkim{
176272444Sjkim	u_long	data;
177272444Sjkim
178272444Sjkim	__asm __volatile("inl %%dx,%0" : "=a" (data) : "d" (port));
179272444Sjkim	return (data);
180272444Sjkim}
181272444Sjkim
182272444Sjkimstatic __inline void
183272444Sjkiminsb(u_int port, void *addr, size_t cnt)
184272444Sjkim{
185272444Sjkim	__asm __volatile("cld; rep; insb"
186272444Sjkim			 : : "d" (port), "D" (addr), "c" (cnt)
187272444Sjkim			 : "di", "cx", "memory");
188272444Sjkim}
189272444Sjkim
190272444Sjkimstatic __inline void
191272444Sjkiminsw(u_int port, void *addr, size_t cnt)
192272444Sjkim{
193272444Sjkim	__asm __volatile("cld; rep; insw"
194272444Sjkim			 : : "d" (port), "D" (addr), "c" (cnt)
195272444Sjkim			 : "di", "cx", "memory");
196272444Sjkim}
197272444Sjkim
198272444Sjkimstatic __inline void
199272444Sjkiminsl(u_int port, void *addr, size_t cnt)
200272444Sjkim{
201272444Sjkim	__asm __volatile("cld; rep; insl"
202272444Sjkim			 : : "d" (port), "D" (addr), "c" (cnt)
203272444Sjkim			 : "di", "cx", "memory");
204272444Sjkim}
205272444Sjkim
206193267Sjkimstatic __inline void
207193267Sjkiminvd(void)
208193267Sjkim{
209193267Sjkim	__asm __volatile("invd");
210193267Sjkim}
211193267Sjkim
212272444Sjkim#ifdef KERNEL
213193267Sjkim#ifdef SMP
214193267Sjkim
215193267Sjkim/*
216193267Sjkim * When using APIC IPI's, the inlining cost is prohibitive since the call
217213800Sjkim * executes into the IPI transmission system.
218213800Sjkim */
219193267Sjkimvoid	invlpg		__P((u_int addr));
220272444Sjkimvoid	invltlb		__P((void));
221193267Sjkim
222272444Sjkimstatic __inline void
223193267Sjkimcpu_invlpg(void *addr)
224272444Sjkim{
225272444Sjkim	__asm   __volatile("invlpg %0"::"m"(*(char *)addr):"memory");
226272444Sjkim}
227272444Sjkim
228272444Sjkim#else  /* !SMP */
229272444Sjkim
230272444Sjkimstatic __inline void
231272444Sjkiminvlpg(u_int addr)
232272444Sjkim{
233193267Sjkim	__asm   __volatile("invlpg %0"::"m"(*(char *)addr):"memory");
234193267Sjkim}
235193267Sjkim
236193267Sjkim
237213800Sjkimstatic __inline void
238213800Sjkiminvltlb(void)
239193267Sjkim{
240193267Sjkim	u_long	temp;
241272444Sjkim	/*
242193267Sjkim	 * This should be implemented as load_cr3(rcr3()) when load_cr3()
243193267Sjkim	 * is inlined.
244193267Sjkim	 */
245193267Sjkim	__asm __volatile("movl %%cr3, %0; movl %0, %%cr3" : "=r" (temp)
246272444Sjkim			 : : "memory");
247193267Sjkim#if defined(SWTCH_OPTIM_STATS)
248193267Sjkim	++tlb_flush_count;
249193267Sjkim#endif
250193267Sjkim}
251193267Sjkim
252193267Sjkim#endif	/* SMP */
253193267Sjkim#endif  /* KERNEL */
254193267Sjkim
255213800Sjkimstatic __inline u_short
256193267Sjkiminw(u_int port)
257193267Sjkim{
258193267Sjkim	u_short	data;
259193267Sjkim
260193267Sjkim	__asm __volatile("inw %%dx,%0" : "=a" (data) : "d" (port));
261193267Sjkim	return (data);
262193267Sjkim}
263193267Sjkim
264272444Sjkimstatic __inline u_int
265193267Sjkimloadandclear(u_int *addr)
266272444Sjkim{
267193267Sjkim	u_int	result;
268193267Sjkim
269193267Sjkim	__asm __volatile("xorl %0,%0; xchgl %1,%0"
270193267Sjkim			 : "=&r" (result) : "m" (*addr));
271193267Sjkim	return (result);
272193267Sjkim}
273213800Sjkim
274213800Sjkimstatic __inline void
275193267Sjkimoutbv(u_int port, u_char data)
276213800Sjkim{
277193267Sjkim	u_char	al;
278193267Sjkim	/*
279193267Sjkim	 * Use an unnecessary assignment to help gcc's register allocator.
280193267Sjkim	 * This make a large difference for gcc-1.40 and a tiny difference
281193267Sjkim	 * for gcc-2.6.0.  For gcc-1.40, al had to be ``asm("ax")'' for
282193267Sjkim	 * best results.  gcc-2.6.0 can't handle this.
283193267Sjkim	 */
284193267Sjkim	al = data;
285193267Sjkim	__asm __volatile("outb %0,%%dx" : : "a" (al), "d" (port));
286193267Sjkim}
287193267Sjkim
288193267Sjkimstatic __inline void
289193267Sjkimoutl(u_int port, u_long data)
290193267Sjkim{
291193267Sjkim	/*
292193267Sjkim	 * outl() and outw() aren't used much so we haven't looked at
293193267Sjkim	 * possible micro-optimizations such as the unnecessary
294193267Sjkim	 * assignment for them.
295193267Sjkim	 */
296193267Sjkim	__asm __volatile("outl %0,%%dx" : : "a" (data), "d" (port));
297193267Sjkim}
298193267Sjkim
299193267Sjkimstatic __inline void
300193267Sjkimoutsb(u_int port, const void *addr, size_t cnt)
301193267Sjkim{
302193267Sjkim	__asm __volatile("cld; rep; outsb"
303213800Sjkim			 : : "d" (port), "S" (addr), "c" (cnt)
304213800Sjkim			 : "si", "cx");
305213800Sjkim}
306193267Sjkim
307213800Sjkimstatic __inline void
308193267Sjkimoutsw(u_int port, const void *addr, size_t cnt)
309193267Sjkim{
310193267Sjkim	__asm __volatile("cld; rep; outsw"
311193267Sjkim			 : : "d" (port), "S" (addr), "c" (cnt)
312193267Sjkim			 : "si", "cx");
313193267Sjkim}
314193267Sjkim
315193267Sjkimstatic __inline void
316193267Sjkimoutsl(u_int port, const void *addr, size_t cnt)
317193267Sjkim{
318193267Sjkim	__asm __volatile("cld; rep; outsl"
319193267Sjkim			 : : "d" (port), "S" (addr), "c" (cnt)
320193267Sjkim			 : "si", "cx");
321193267Sjkim}
322193267Sjkim
323193267Sjkimstatic __inline void
324193267Sjkimoutw(u_int port, u_short data)
325193267Sjkim{
326193267Sjkim	__asm __volatile("outw %0,%%dx" : : "a" (data), "d" (port));
327193267Sjkim}
328193267Sjkim
329193267Sjkimstatic __inline u_long
330193267Sjkimrcr2(void)
331193267Sjkim{
332193267Sjkim	u_long	data;
333193267Sjkim
334193267Sjkim	__asm __volatile("movl %%cr2,%0" : "=r" (data));
335193267Sjkim	return (data);
336193267Sjkim}
337193267Sjkim
338193267Sjkimstatic __inline u_long
339193267Sjkimread_eflags(void)
340231844Sjkim{
341231844Sjkim	u_long	ef;
342231844Sjkim
343231844Sjkim	__asm __volatile("pushfl; popl %0" : "=r" (ef));
344231844Sjkim	return (ef);
345231844Sjkim}
346231844Sjkim
347231844Sjkimstatic __inline quad_t
348231844Sjkimrdmsr(u_int msr)
349231844Sjkim{
350231844Sjkim	quad_t rv;
351231844Sjkim
352231844Sjkim	__asm __volatile(".byte 0x0f, 0x32" : "=A" (rv) : "c" (msr));
353231844Sjkim	return (rv);
354231844Sjkim}
355231844Sjkim
356231844Sjkimstatic __inline quad_t
357231844Sjkimrdpmc(u_int pmc)
358231844Sjkim{
359231844Sjkim	quad_t rv;
360231844Sjkim
361231844Sjkim	__asm __volatile(".byte 0x0f, 0x33" : "=A" (rv) : "c" (pmc));
362231844Sjkim	return (rv);
363231844Sjkim}
364231844Sjkim
365231844Sjkimstatic __inline quad_t
366231844Sjkimrdtsc(void)
367193267Sjkim{
368193267Sjkim	quad_t rv;
369193267Sjkim
370193267Sjkim	__asm __volatile(".byte 0x0f, 0x31" : "=A" (rv));
371193267Sjkim	return (rv);
372193267Sjkim}
373193267Sjkim
374193267Sjkimstatic __inline void
375193267Sjkimsetbits(volatile unsigned *addr, u_int bits)
376193267Sjkim{
377193267Sjkim	__asm __volatile(
378193267Sjkim#ifdef SMP
379193267Sjkim			 "lock; "
380193267Sjkim#endif
381193267Sjkim			 "orl %1,%0" : "=m" (*addr) : "ir" (bits));
382193267Sjkim}
383193267Sjkim
384193267Sjkimstatic __inline void
385193267Sjkimwbinvd(void)
386193267Sjkim{
387193267Sjkim	__asm __volatile("wbinvd");
388193267Sjkim}
389193267Sjkim
390213800Sjkimstatic __inline void
391193267Sjkimwrite_eflags(u_long ef)
392193267Sjkim{
393193267Sjkim	__asm __volatile("pushl %0; popfl" : : "r" (ef));
394245582Sjkim}
395245582Sjkim
396193267Sjkimstatic __inline void
397193267Sjkimwrmsr(u_int msr, quad_t newval)
398193267Sjkim{
399193267Sjkim	__asm __volatile(".byte 0x0f, 0x30" : : "A" (newval), "c" (msr));
400193267Sjkim}
401193267Sjkim
402193267Sjkim#else /* !__GNUC__ */
403193267Sjkim
404193267Sjkimint	breakpoint	__P((void));
405245582Sjkimvoid	disable_intr	__P((void));
406193267Sjkimvoid	enable_intr	__P((void));
407193267Sjkimu_char	inb		__P((u_int port));
408245582Sjkimu_long	inl		__P((u_int port));
409245582Sjkimvoid	insb		__P((u_int port, void *addr, size_t cnt));
410245582Sjkimvoid	insl		__P((u_int port, void *addr, size_t cnt));
411245582Sjkimvoid	insw		__P((u_int port, void *addr, size_t cnt));
412245582Sjkimvoid	invd		__P((void));
413245582Sjkimvoid	invlpg		__P((u_int addr));
414245582Sjkimvoid	invltlb		__P((void));
415245582Sjkimu_short	inw		__P((u_int port));
416245582Sjkimu_int	loadandclear	__P((u_int *addr));
417245582Sjkimvoid	outb		__P((u_int port, u_char data));
418245582Sjkimvoid	outl		__P((u_int port, u_long data));
419245582Sjkimvoid	outsb		__P((u_int port, void *addr, size_t cnt));
420245582Sjkimvoid	outsl		__P((u_int port, void *addr, size_t cnt));
421245582Sjkimvoid	outsw		__P((u_int port, void *addr, size_t cnt));
422245582Sjkimvoid	outw		__P((u_int port, u_short data));
423245582Sjkimu_long	rcr2		__P((void));
424245582Sjkimquad_t	rdmsr		__P((u_int msr));
425245582Sjkimquad_t	rdpmc		__P((u_int pmc));
426245582Sjkimquad_t	rdtsc		__P((void));
427245582Sjkimu_long	read_eflags	__P((void));
428245582Sjkimvoid	setbits		__P((volatile unsigned *addr, u_int bits));
429245582Sjkimvoid	wbinvd		__P((void));
430245582Sjkimvoid	write_eflags	__P((u_long ef));
431245582Sjkimvoid	wrmsr		__P((u_int msr, quad_t newval));
432245582Sjkim
433245582Sjkim#endif	/* __GNUC__ */
434245582Sjkim
435193267Sjkimvoid	load_cr0	__P((u_long cr0));
436193267Sjkimvoid	load_cr3	__P((u_long cr3));
437193267Sjkimvoid	load_cr4	__P((u_long cr4));
438193267Sjkimvoid	ltr		__P((u_short sel));
439193267Sjkimu_int	rcr0		__P((void));
440193267Sjkimu_long	rcr3		__P((void));
441193267Sjkimu_long	rcr4		__P((void));
442213800Sjkimvoid	i686_pagezero	__P((void *addr));
443213800Sjkim
444193267Sjkim#endif /* !_MACHINE_CPUFUNC_H_ */
445193267Sjkim