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