cpufunc.h revision 1.22 1 1.22 fvdl /* $NetBSD: cpufunc.h,v 1.22 2002/10/01 12:57:04 fvdl Exp $ */
2 1.8 cgd
3 1.16 mycroft /*-
4 1.16 mycroft * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 1.4 mycroft * All rights reserved.
6 1.2 mycroft *
7 1.16 mycroft * This code is derived from software contributed to The NetBSD Foundation
8 1.16 mycroft * by Charles M. Hannum.
9 1.16 mycroft *
10 1.4 mycroft * Redistribution and use in source and binary forms, with or without
11 1.4 mycroft * modification, are permitted provided that the following conditions
12 1.4 mycroft * are met:
13 1.4 mycroft * 1. Redistributions of source code must retain the above copyright
14 1.4 mycroft * notice, this list of conditions and the following disclaimer.
15 1.4 mycroft * 2. Redistributions in binary form must reproduce the above copyright
16 1.4 mycroft * notice, this list of conditions and the following disclaimer in the
17 1.4 mycroft * documentation and/or other materials provided with the distribution.
18 1.4 mycroft * 3. All advertising materials mentioning features or use of this software
19 1.4 mycroft * must display the following acknowledgement:
20 1.16 mycroft * This product includes software developed by the NetBSD
21 1.16 mycroft * Foundation, Inc. and its contributors.
22 1.16 mycroft * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.16 mycroft * contributors may be used to endorse or promote products derived
24 1.16 mycroft * from this software without specific prior written permission.
25 1.4 mycroft *
26 1.16 mycroft * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.16 mycroft * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.16 mycroft * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.16 mycroft * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.16 mycroft * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.16 mycroft * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.16 mycroft * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.16 mycroft * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.16 mycroft * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.16 mycroft * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.16 mycroft * POSSIBILITY OF SUCH DAMAGE.
37 1.4 mycroft */
38 1.4 mycroft
39 1.7 mycroft #ifndef _I386_CPUFUNC_H_
40 1.7 mycroft #define _I386_CPUFUNC_H_
41 1.7 mycroft
42 1.4 mycroft /*
43 1.4 mycroft * Functions to provide access to i386-specific instructions.
44 1.1 cgd */
45 1.1 cgd
46 1.1 cgd #include <sys/cdefs.h>
47 1.1 cgd #include <sys/types.h>
48 1.13 perry
49 1.22 fvdl #include <machine/specialreg.h>
50 1.22 fvdl
51 1.14 perry #ifdef _KERNEL
52 1.1 cgd
53 1.10 chuck static __inline void
54 1.10 chuck invlpg(u_int addr)
55 1.10 chuck {
56 1.10 chuck __asm __volatile("invlpg (%0)" : : "r" (addr) : "memory");
57 1.10 chuck }
58 1.10 chuck
59 1.1 cgd static __inline void
60 1.4 mycroft lidt(void *p)
61 1.4 mycroft {
62 1.4 mycroft __asm __volatile("lidt (%0)" : : "r" (p));
63 1.4 mycroft }
64 1.4 mycroft
65 1.4 mycroft static __inline void
66 1.4 mycroft lldt(u_short sel)
67 1.4 mycroft {
68 1.4 mycroft __asm __volatile("lldt %0" : : "r" (sel));
69 1.4 mycroft }
70 1.4 mycroft
71 1.4 mycroft static __inline void
72 1.4 mycroft ltr(u_short sel)
73 1.4 mycroft {
74 1.4 mycroft __asm __volatile("ltr %0" : : "r" (sel));
75 1.4 mycroft }
76 1.4 mycroft
77 1.4 mycroft static __inline void
78 1.4 mycroft lcr0(u_int val)
79 1.4 mycroft {
80 1.4 mycroft __asm __volatile("movl %0,%%cr0" : : "r" (val));
81 1.4 mycroft }
82 1.4 mycroft
83 1.4 mycroft static __inline u_int
84 1.4 mycroft rcr0(void)
85 1.4 mycroft {
86 1.4 mycroft u_int val;
87 1.6 mycroft __asm __volatile("movl %%cr0,%0" : "=r" (val));
88 1.4 mycroft return val;
89 1.4 mycroft }
90 1.4 mycroft
91 1.4 mycroft static __inline u_int
92 1.4 mycroft rcr2(void)
93 1.4 mycroft {
94 1.4 mycroft u_int val;
95 1.6 mycroft __asm __volatile("movl %%cr2,%0" : "=r" (val));
96 1.4 mycroft return val;
97 1.4 mycroft }
98 1.4 mycroft
99 1.4 mycroft static __inline void
100 1.4 mycroft lcr3(u_int val)
101 1.4 mycroft {
102 1.4 mycroft __asm __volatile("movl %0,%%cr3" : : "r" (val));
103 1.4 mycroft }
104 1.4 mycroft
105 1.4 mycroft static __inline u_int
106 1.4 mycroft rcr3(void)
107 1.4 mycroft {
108 1.4 mycroft u_int val;
109 1.6 mycroft __asm __volatile("movl %%cr3,%0" : "=r" (val));
110 1.10 chuck return val;
111 1.10 chuck }
112 1.10 chuck
113 1.10 chuck static __inline void
114 1.10 chuck lcr4(u_int val)
115 1.10 chuck {
116 1.10 chuck __asm __volatile("movl %0,%%cr4" : : "r" (val));
117 1.10 chuck }
118 1.10 chuck
119 1.10 chuck static __inline u_int
120 1.10 chuck rcr4(void)
121 1.10 chuck {
122 1.10 chuck u_int val;
123 1.10 chuck __asm __volatile("movl %%cr4,%0" : "=r" (val));
124 1.4 mycroft return val;
125 1.6 mycroft }
126 1.6 mycroft
127 1.6 mycroft static __inline void
128 1.6 mycroft tlbflush(void)
129 1.6 mycroft {
130 1.6 mycroft u_int val;
131 1.22 fvdl val = rcr3();
132 1.22 fvdl lcr3(val);
133 1.22 fvdl }
134 1.22 fvdl
135 1.22 fvdl static __inline void
136 1.22 fvdl tlbflushg(void)
137 1.22 fvdl {
138 1.22 fvdl /*
139 1.22 fvdl * Big hammer: flush all TLB entries, including ones from PTE's
140 1.22 fvdl * with the G bit set. This should only be necessary if TLB
141 1.22 fvdl * shootdown falls far behind.
142 1.22 fvdl *
143 1.22 fvdl * Intel Architecture Software Developer's Manual, Volume 3,
144 1.22 fvdl * System Programming, section 9.10, "Invalidating the
145 1.22 fvdl * Translation Lookaside Buffers (TLBS)":
146 1.22 fvdl * "The following operations invalidate all TLB entries, irrespective
147 1.22 fvdl * of the setting of the G flag:
148 1.22 fvdl * ...
149 1.22 fvdl * "(P6 family processors only): Writing to control register CR4 to
150 1.22 fvdl * modify the PSE, PGE, or PAE flag."
151 1.22 fvdl *
152 1.22 fvdl * (the alternatives not quoted above are not an option here.)
153 1.22 fvdl *
154 1.22 fvdl * If PGE is not in use, we reload CR3 for the benefit of
155 1.22 fvdl * pre-P6-family processors.
156 1.22 fvdl */
157 1.22 fvdl
158 1.22 fvdl #if defined(I686_CPU)
159 1.22 fvdl if (cpu_feature & CPUID_PGE) {
160 1.22 fvdl u_int cr4 = rcr4();
161 1.22 fvdl lcr4(cr4 & ~CR4_PGE);
162 1.22 fvdl lcr4(cr4);
163 1.22 fvdl } else
164 1.22 fvdl #endif
165 1.22 fvdl tlbflush();
166 1.4 mycroft }
167 1.22 fvdl
168 1.4 mycroft
169 1.4 mycroft #ifdef notyet
170 1.4 mycroft void setidt __P((int idx, /*XXX*/caddr_t func, int typ, int dpl));
171 1.4 mycroft #endif
172 1.4 mycroft
173 1.21 yamt /* debug register */
174 1.21 yamt void dr0(caddr_t, u_int32_t, u_int32_t, u_int32_t);
175 1.21 yamt
176 1.21 yamt static __inline u_int
177 1.21 yamt rdr6(void)
178 1.21 yamt {
179 1.21 yamt u_int val;
180 1.21 yamt
181 1.21 yamt __asm __volatile("movl %%dr6,%0" : "=r" (val));
182 1.21 yamt return val;
183 1.21 yamt }
184 1.21 yamt
185 1.21 yamt static __inline void
186 1.21 yamt ldr6(u_int val)
187 1.21 yamt {
188 1.21 yamt
189 1.21 yamt __asm __volatile("movl %0,%%dr6" : : "r" (val));
190 1.21 yamt }
191 1.4 mycroft
192 1.4 mycroft /* XXXX ought to be in psl.h with spl() functions */
193 1.4 mycroft
194 1.4 mycroft static __inline void
195 1.1 cgd disable_intr(void)
196 1.1 cgd {
197 1.1 cgd __asm __volatile("cli");
198 1.1 cgd }
199 1.1 cgd
200 1.1 cgd static __inline void
201 1.1 cgd enable_intr(void)
202 1.1 cgd {
203 1.1 cgd __asm __volatile("sti");
204 1.12 drochner }
205 1.12 drochner
206 1.12 drochner static __inline u_long
207 1.12 drochner read_eflags(void)
208 1.12 drochner {
209 1.12 drochner u_long ef;
210 1.12 drochner
211 1.12 drochner __asm __volatile("pushfl; popl %0" : "=r" (ef));
212 1.12 drochner return (ef);
213 1.12 drochner }
214 1.12 drochner
215 1.12 drochner static __inline void
216 1.12 drochner write_eflags(u_long ef)
217 1.12 drochner {
218 1.12 drochner __asm __volatile("pushl %0; popfl" : : "r" (ef));
219 1.17 thorpej }
220 1.17 thorpej
221 1.17 thorpej static __inline u_int64_t
222 1.17 thorpej rdmsr(u_int msr)
223 1.17 thorpej {
224 1.17 thorpej u_int64_t rv;
225 1.17 thorpej
226 1.19 thorpej __asm __volatile("rdmsr" : "=A" (rv) : "c" (msr));
227 1.17 thorpej return (rv);
228 1.17 thorpej }
229 1.17 thorpej
230 1.17 thorpej static __inline void
231 1.17 thorpej wrmsr(u_int msr, u_int64_t newval)
232 1.17 thorpej {
233 1.19 thorpej __asm __volatile("wrmsr" : : "A" (newval), "c" (msr));
234 1.17 thorpej }
235 1.17 thorpej
236 1.17 thorpej static __inline void
237 1.17 thorpej wbinvd(void)
238 1.17 thorpej {
239 1.17 thorpej __asm __volatile("wbinvd");
240 1.18 thorpej }
241 1.18 thorpej
242 1.18 thorpej static __inline u_int64_t
243 1.18 thorpej rdtsc(void)
244 1.18 thorpej {
245 1.18 thorpej u_int64_t rv;
246 1.18 thorpej
247 1.19 thorpej __asm __volatile("rdtsc" : "=A" (rv));
248 1.18 thorpej return (rv);
249 1.18 thorpej }
250 1.18 thorpej
251 1.18 thorpej static __inline u_int64_t
252 1.18 thorpej rdpmc(u_int pmc)
253 1.18 thorpej {
254 1.18 thorpej u_int64_t rv;
255 1.18 thorpej
256 1.19 thorpej __asm __volatile("rdpmc" : "=A" (rv) : "c" (pmc));
257 1.18 thorpej return (rv);
258 1.9 thorpej }
259 1.9 thorpej
260 1.9 thorpej /* Break into DDB/KGDB. */
261 1.9 thorpej static __inline void
262 1.9 thorpej breakpoint(void)
263 1.9 thorpej {
264 1.9 thorpej __asm __volatile("int $3");
265 1.1 cgd }
266 1.20 thorpej
267 1.20 thorpej /*
268 1.20 thorpej * XXX Maybe these don't belong here...
269 1.20 thorpej */
270 1.20 thorpej
271 1.20 thorpej extern int (*copyout_func)(const void *, void *, size_t);
272 1.20 thorpej extern int (*copyin_func)(const void *, void *, size_t);
273 1.20 thorpej
274 1.20 thorpej int i386_copyout(const void *, void *, size_t);
275 1.20 thorpej int i486_copyout(const void *, void *, size_t);
276 1.20 thorpej
277 1.20 thorpej int i386_copyin(const void *, void *, size_t);
278 1.14 perry
279 1.14 perry #endif /* _KERNEL */
280 1.1 cgd
281 1.7 mycroft #endif /* !_I386_CPUFUNC_H_ */
282