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