cpufunc.h revision 1.16 1 /* $NetBSD: cpufunc.h,v 1.16 2007/05/11 14:01:46 fvdl 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 _AMD64_CPUFUNC_H_
40 #define _AMD64_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 #ifdef _KERNEL
53 void x86_pause(void);
54 #else
55 static __inline void
56 x86_pause(void)
57 {
58 __asm volatile("pause");
59 }
60 #endif
61
62 /*
63 * XXX if lfence isn't available...
64 *
65 * memory clobber to avoid compiler reordering.
66 */
67 static __inline void
68 x86_lfence(void)
69 {
70
71 __asm volatile("lfence" : : : "memory");
72 }
73
74 static __inline void
75 x86_sfence(void)
76 {
77
78 __asm volatile("sfence" : : : "memory");
79 }
80
81 static __inline void
82 x86_mfence(void)
83 {
84
85 __asm volatile("mfence" : : : "memory");
86 }
87
88 #ifdef _KERNEL
89
90 void x86_flush(void);
91 void x86_patch(void);
92
93 extern int cpu_feature;
94
95 static __inline void
96 invlpg(u_int64_t addr)
97 {
98 __asm volatile("invlpg (%0)" : : "r" (addr) : "memory");
99 }
100
101 static __inline void
102 lgs(u_short sel)
103 {
104 __asm volatile("cli");
105 __asm volatile("swapgs");
106 __asm volatile("movw %0, %%gs" : : "r" (sel));
107 __asm volatile("mfence");
108 __asm volatile("swapgs");
109 __asm volatile("sti");
110 }
111
112 static __inline void
113 lidt(struct region_descriptor *region)
114 {
115 __asm volatile("lidt %0" : : "m" (*region));
116 }
117
118 static __inline void
119 lldt(u_short sel)
120 {
121 __asm volatile("lldt %0" : : "r" (sel));
122 }
123
124 static __inline void
125 ltr(u_short sel)
126 {
127 __asm volatile("ltr %0" : : "r" (sel));
128 }
129
130 static __inline void
131 lcr8(u_int val)
132 {
133 u_int64_t val64 = val;
134 __asm volatile("movq %0,%%cr8" : : "r" (val64));
135 }
136
137 /*
138 * Upper 32 bits are reserved anyway, so just keep this 32bits.
139 */
140 static __inline void
141 lcr0(u_int val)
142 {
143 u_int64_t val64 = val;
144 __asm volatile("movq %0,%%cr0" : : "r" (val64));
145 }
146
147 static __inline u_int
148 rcr0(void)
149 {
150 u_int64_t val64;
151 u_int val;
152 __asm volatile("movq %%cr0,%0" : "=r" (val64));
153 val = val64;
154 return val;
155 }
156
157 static __inline u_int64_t
158 rcr2(void)
159 {
160 u_int64_t val;
161 __asm volatile("movq %%cr2,%0" : "=r" (val));
162 return val;
163 }
164
165 static __inline void
166 lcr3(u_int64_t val)
167 {
168 __asm volatile("movq %0,%%cr3" : : "r" (val));
169 }
170
171 static __inline u_int64_t
172 rcr3(void)
173 {
174 u_int64_t val;
175 __asm volatile("movq %%cr3,%0" : "=r" (val));
176 return val;
177 }
178
179 /*
180 * Same as for cr0. Don't touch upper 32 bits.
181 */
182 static __inline void
183 lcr4(u_int val)
184 {
185 u_int64_t val64 = val;
186
187 __asm volatile("movq %0,%%cr4" : : "r" (val64));
188 }
189
190 static __inline u_int
191 rcr4(void)
192 {
193 u_int val;
194 u_int64_t val64;
195 __asm volatile("movq %%cr4,%0" : "=r" (val64));
196 val = val64;
197 return val;
198 }
199
200 static __inline void
201 tlbflush(void)
202 {
203 u_int64_t val;
204 __asm volatile("movq %%cr3,%0" : "=r" (val));
205 __asm volatile("movq %0,%%cr3" : : "r" (val));
206 }
207
208 static __inline void
209 tlbflushg(void)
210 {
211 /*
212 * Big hammer: flush all TLB entries, including ones from PTE's
213 * with the G bit set. This should only be necessary if TLB
214 * shootdown falls far behind.
215 *
216 * Intel Architecture Software Developer's Manual, Volume 3,
217 * System Programming, section 9.10, "Invalidating the
218 * Translation Lookaside Buffers (TLBS)":
219 * "The following operations invalidate all TLB entries, irrespective
220 * of the setting of the G flag:
221 * ...
222 * "(P6 family processors only): Writing to control register CR4 to
223 * modify the PSE, PGE, or PAE flag."
224 *
225 * (the alternatives not quoted above are not an option here.)
226 *
227 * If PGE is not in use, we reload CR3 for the benefit of
228 * pre-P6-family processors.
229 */
230
231 if (cpu_feature & CPUID_PGE) {
232 u_int cr4 = rcr4();
233 lcr4(cr4 & ~CR4_PGE);
234 lcr4(cr4);
235 } else
236 tlbflush();
237 }
238
239 #ifdef notyet
240 void setidt __P((int idx, /*XXX*/void *func, int typ, int dpl));
241 #endif
242
243
244 /* XXXX ought to be in psl.h with spl() functions */
245
246 static __inline void
247 disable_intr(void)
248 {
249 __asm volatile("cli");
250 }
251
252 static __inline void
253 enable_intr(void)
254 {
255 __asm volatile("sti");
256 }
257
258 static __inline u_long
259 read_rflags(void)
260 {
261 u_long ef;
262
263 __asm volatile("pushfq; popq %0" : "=r" (ef));
264 return (ef);
265 }
266
267 static __inline void
268 write_rflags(u_long ef)
269 {
270 __asm volatile("pushq %0; popfq" : : "r" (ef));
271 }
272
273
274 static __inline u_int64_t
275 rdmsr(u_int msr)
276 {
277 uint32_t hi, lo;
278 __asm volatile("rdmsr" : "=d" (hi), "=a" (lo) : "c" (msr));
279 return (((uint64_t)hi << 32) | (uint64_t) lo);
280 }
281
282 static __inline void
283 wrmsr(u_int msr, u_int64_t newval)
284 {
285 __asm volatile("wrmsr" :
286 : "a" (newval & 0xffffffff), "d" (newval >> 32), "c" (msr));
287 }
288
289 /*
290 * Some of the undocumented AMD64 MSRs need a 'passcode' to access.
291 *
292 * See LinuxBIOSv2: src/cpu/amd/model_fxx/model_fxx_init.c
293 */
294
295 #define OPTERON_MSR_PASSCODE 0x9c5a203a
296
297 static __inline u_int64_t
298 rdmsr_locked(u_int msr, u_int code)
299 {
300 uint32_t hi, lo;
301 __asm volatile("rdmsr"
302 : "=d" (hi), "=a" (lo)
303 : "c" (msr), "D" (code));
304 return (((uint64_t)hi << 32) | (uint64_t) lo);
305 }
306
307 static __inline void
308 wrmsr_locked(u_int msr, u_int code, u_int64_t newval)
309 {
310 __asm volatile("wrmsr"
311 :
312 : "a" (newval & 0xffffffff), "d" (newval >> 32), "c" (msr),
313 "D" (code));
314 }
315
316 static __inline void
317 wbinvd(void)
318 {
319 __asm volatile("wbinvd");
320 }
321
322 static __inline u_int64_t
323 rdtsc(void)
324 {
325 uint32_t hi, lo;
326
327 __asm volatile("rdtsc" : "=d" (hi), "=a" (lo));
328 return (((uint64_t)hi << 32) | (uint64_t) lo);
329 }
330
331 static __inline u_int64_t
332 rdpmc(u_int pmc)
333 {
334 uint32_t hi, lo;
335
336 __asm volatile("rdpmc" : "=d" (hi), "=a" (lo) : "c" (pmc));
337 return (((uint64_t)hi << 32) | (uint64_t) lo);
338 }
339
340 /* Break into DDB/KGDB. */
341 static __inline void
342 breakpoint(void)
343 {
344 __asm volatile("int $3");
345 }
346
347 #define read_psl() read_rflags()
348 #define write_psl(x) write_rflags(x)
349
350 #endif /* _KERNEL */
351
352 #endif /* !_AMD64_CPUFUNC_H_ */
353